1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Filter.js
7 * @requires OpenLayers/Console.js
11 * Class: OpenLayers.Filter.Comparison
12 * This class represents a comparison filter.
15 * - <OpenLayers.Filter>
17 OpenLayers.Filter.Comparison = OpenLayers.Class(OpenLayers.Filter, {
21 * {String} type: type of the comparison. This is one of
22 * - OpenLayers.Filter.Comparison.EQUAL_TO = "==";
23 * - OpenLayers.Filter.Comparison.NOT_EQUAL_TO = "!=";
24 * - OpenLayers.Filter.Comparison.LESS_THAN = "<";
25 * - OpenLayers.Filter.Comparison.GREATER_THAN = ">";
26 * - OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO = "<=";
27 * - OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO = ">=";
28 * - OpenLayers.Filter.Comparison.BETWEEN = "..";
29 * - OpenLayers.Filter.Comparison.LIKE = "~";
34 * APIProperty: property
36 * name of the context property to compare
42 * {Number} or {String}
43 * comparison value for binary comparisons. In the case of a String, this
44 * can be a combination of text and propertyNames in the form
45 * "literal ${propertyName}"
51 * {Boolean} Force case sensitive searches for EQUAL_TO and NOT_EQUAL_TO
52 * comparisons. The Filter Encoding 1.1 specification added a matchCase
53 * attribute to ogc:PropertyIsEqualTo and ogc:PropertyIsNotEqualTo
54 * elements. This property will be serialized with those elements only
55 * if using the v1.1.0 filter format. However, when evaluating filters
56 * here, the matchCase property will always be respected (for EQUAL_TO
57 * and NOT_EQUAL_TO). Default is true.
62 * APIProperty: lowerBoundary
63 * {Number} or {String}
64 * lower boundary for between comparisons. In the case of a String, this
65 * can be a combination of text and propertyNames in the form
66 * "literal ${propertyName}"
71 * APIProperty: upperBoundary
72 * {Number} or {String}
73 * upper boundary for between comparisons. In the case of a String, this
74 * can be a combination of text and propertyNames in the form
75 * "literal ${propertyName}"
80 * Constructor: OpenLayers.Filter.Comparison
81 * Creates a comparison rule.
84 * options - {Object} An optional object with properties to set on the
88 * {<OpenLayers.Filter.Comparison>}
90 initialize: function(options) {
91 OpenLayers.Filter.prototype.initialize.apply(this, [options]);
96 * Evaluates this filter in a specific context. Should be implemented by
100 * context - {Object} Context to use in evaluating the filter.
103 * {Boolean} The filter applies.
105 evaluate: function(context) {
108 case OpenLayers.Filter.Comparison.EQUAL_TO:
109 var got = context[this.property];
110 var exp = this.value;
111 if(!this.matchCase &&
112 typeof got == "string" && typeof exp == "string") {
113 result = (got.toUpperCase() == exp.toUpperCase());
115 result = (got == exp);
118 case OpenLayers.Filter.Comparison.NOT_EQUAL_TO:
119 var got = context[this.property];
120 var exp = this.value;
121 if(!this.matchCase &&
122 typeof got == "string" && typeof exp == "string") {
123 result = (got.toUpperCase() != exp.toUpperCase());
125 result = (got != exp);
128 case OpenLayers.Filter.Comparison.LESS_THAN:
129 result = context[this.property] < this.value;
131 case OpenLayers.Filter.Comparison.GREATER_THAN:
132 result = context[this.property] > this.value;
134 case OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO:
135 result = context[this.property] <= this.value;
137 case OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO:
138 result = context[this.property] >= this.value;
140 case OpenLayers.Filter.Comparison.BETWEEN:
141 result = (context[this.property] >= this.lowerBoundary) &&
142 (context[this.property] <= this.upperBoundary);
144 case OpenLayers.Filter.Comparison.LIKE:
145 var regexp = new RegExp(this.value, "gi");
146 result = regexp.test(context[this.property]);
153 * APIMethod: value2regex
154 * Converts the value of this rule into a regular expression string,
155 * according to the wildcard characters specified. This method has to
156 * be called after instantiation of this class, if the value is not a
157 * regular expression already.
160 * wildCard - {<Char>} wildcard character in the above value, default
162 * singleChar - {<Char>) single-character wildcard in the above value
164 * escape - {<Char>) escape character in the above value, default is
168 * {String} regular expression string
170 value2regex: function(wildCard, singleChar, escapeChar) {
171 if (wildCard == ".") {
172 var msg = "'.' is an unsupported wildCard character for "+
173 "OpenLayers.Filter.Comparison";
174 OpenLayers.Console.error(msg);
179 // set UMN MapServer defaults for unspecified parameters
180 wildCard = wildCard ? wildCard : "*";
181 singleChar = singleChar ? singleChar : ".";
182 escapeChar = escapeChar ? escapeChar : "!";
184 this.value = this.value.replace(
185 new RegExp("\\"+escapeChar+"(.|$)", "g"), "\\$1");
186 this.value = this.value.replace(
187 new RegExp("\\"+singleChar, "g"), ".");
188 this.value = this.value.replace(
189 new RegExp("\\"+wildCard, "g"), ".*");
190 this.value = this.value.replace(
191 new RegExp("\\\\.\\*", "g"), "\\"+wildCard);
192 this.value = this.value.replace(
193 new RegExp("\\\\\\.", "g"), "\\"+singleChar);
199 * Method: regex2value
200 * Convert the value of this rule from a regular expression string into an
201 * ogc literal string using a wildCard of *, a singleChar of ., and an
202 * escape of !. Leaves the <value> property unmodified.
205 * {String} A string value.
207 regex2value: function() {
209 var value = this.value;
212 value = value.replace(/!/g, "!!");
214 // replace \. with !. (watching out for \\.)
215 value = value.replace(/(\\)?\\\./g, function($0, $1) {
216 return $1 ? $0 : "!.";
219 // replace \* with #* (watching out for \\*)
220 value = value.replace(/(\\)?\\\*/g, function($0, $1) {
221 return $1 ? $0 : "!*";
225 value = value.replace(/\\\\/g, "\\");
227 // convert .* to * (the sequence #.* is not allowed)
228 value = value.replace(/\.\*/g, "*");
235 * Clones this filter.
238 * {<OpenLayers.Filter.Comparison>} Clone of this filter.
241 return OpenLayers.Util.extend(new OpenLayers.Filter.Comparison(), this);
244 CLASS_NAME: "OpenLayers.Filter.Comparison"
248 OpenLayers.Filter.Comparison.EQUAL_TO = "==";
249 OpenLayers.Filter.Comparison.NOT_EQUAL_TO = "!=";
250 OpenLayers.Filter.Comparison.LESS_THAN = "<";
251 OpenLayers.Filter.Comparison.GREATER_THAN = ">";
252 OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO = "<=";
253 OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO = ">=";
254 OpenLayers.Filter.Comparison.BETWEEN = "..";
255 OpenLayers.Filter.Comparison.LIKE = "~";