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
10
* Class: OpenLayers.Filter.Comparison
11
* This class represents a comparison filter.
14
* - <OpenLayers.Filter>
16
OpenLayers.Filter.Comparison = OpenLayers.Class(OpenLayers.Filter, {
20
* {String} type: type of the comparison. This is one of
21
* - OpenLayers.Filter.Comparison.EQUAL_TO = "==";
22
* - OpenLayers.Filter.Comparison.NOT_EQUAL_TO = "!=";
23
* - OpenLayers.Filter.Comparison.LESS_THAN = "<";
24
* - OpenLayers.Filter.Comparison.GREATER_THAN = ">";
25
* - OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO = "<=";
26
* - OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO = ">=";
27
* - OpenLayers.Filter.Comparison.BETWEEN = "..";
28
* - OpenLayers.Filter.Comparison.LIKE = "~";
33
* APIProperty: property
35
* name of the context property to compare
41
* {Number} or {String}
42
* comparison value for binary comparisons. In the case of a String, this
43
* can be a combination of text and propertyNames in the form
44
* "literal ${propertyName}"
49
* APIProperty: lowerBoundary
50
* {Number} or {String}
51
* lower boundary for between comparisons. In the case of a String, this
52
* can be a combination of text and propertyNames in the form
53
* "literal ${propertyName}"
58
* APIProperty: upperBoundary
59
* {Number} or {String}
60
* upper boundary for between comparisons. In the case of a String, this
61
* can be a combination of text and propertyNames in the form
62
* "literal ${propertyName}"
67
* Constructor: OpenLayers.Filter.Comparison
68
* Creates a comparison rule.
71
* options - {Object} An optional object with properties to set on the
75
* {<OpenLayers.Filter.Comparison>}
77
initialize: function(options) {
78
OpenLayers.Filter.prototype.initialize.apply(this, [options]);
83
* Evaluates this filter in a specific context. Should be implemented by
87
* context - {Object} Context to use in evaluating the filter.
90
* {Boolean} The filter applies.
92
evaluate: function(context) {
94
case OpenLayers.Filter.Comparison.EQUAL_TO:
95
case OpenLayers.Filter.Comparison.LESS_THAN:
96
case OpenLayers.Filter.Comparison.GREATER_THAN:
97
case OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO:
98
case OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO:
99
return this.binaryCompare(context, this.property, this.value);
101
case OpenLayers.Filter.Comparison.BETWEEN:
103
context[this.property] >= this.lowerBoundary;
105
context[this.property] <= this.upperBoundary;
107
case OpenLayers.Filter.Comparison.LIKE:
108
var regexp = new RegExp(this.value,
110
return regexp.test(context[this.property]);
115
* APIMethod: value2regex
116
* Converts the value of this rule into a regular expression string,
117
* according to the wildcard characters specified. This method has to
118
* be called after instantiation of this class, if the value is not a
119
* regular expression already.
122
* wildCard - {<Char>} wildcard character in the above value, default
124
* singleChar - {<Char>) single-character wildcard in the above value
126
* escape - {<Char>) escape character in the above value, default is
130
* {String} regular expression string
132
value2regex: function(wildCard, singleChar, escapeChar) {
133
if (wildCard == ".") {
134
var msg = "'.' is an unsupported wildCard character for "+
135
"OpenLayers.Filter.Comparison";
136
OpenLayers.Console.error(msg);
141
// set UMN MapServer defaults for unspecified parameters
142
wildCard = wildCard ? wildCard : "*";
143
singleChar = singleChar ? singleChar : ".";
144
escapeChar = escapeChar ? escapeChar : "!";
146
this.value = this.value.replace(
147
new RegExp("\\"+escapeChar+"(.|$)", "g"), "\\$1");
148
this.value = this.value.replace(
149
new RegExp("\\"+singleChar, "g"), ".");
150
this.value = this.value.replace(
151
new RegExp("\\"+wildCard, "g"), ".*");
152
this.value = this.value.replace(
153
new RegExp("\\\\.\\*", "g"), "\\"+wildCard);
154
this.value = this.value.replace(
155
new RegExp("\\\\\\.", "g"), "\\"+singleChar);
161
* Method: regex2value
162
* Convert the value of this rule from a regular expression string into an
163
* ogc literal string using a wildCard of *, a singleChar of ., and an
164
* escape of !. Leaves the <value> property unmodified.
167
* {String} A string value.
169
regex2value: function() {
171
var value = this.value;
174
value = value.replace(/!/g, "!!");
176
// replace \. with !. (watching out for \\.)
177
value = value.replace(/(\\)?\\\./g, function($0, $1) {
178
return $1 ? $0 : "!.";
181
// replace \* with #* (watching out for \\*)
182
value = value.replace(/(\\)?\\\*/g, function($0, $1) {
183
return $1 ? $0 : "!*";
187
value = value.replace(/\\\\/g, "\\");
189
// convert .* to * (the sequence #.* is not allowed)
190
value = value.replace(/\.\*/g, "*");
196
* Function: binaryCompare
197
* Compares a feature property to a rule value
201
* property - {String} or {Number}
202
* value - {String} or {Number}, same as property
207
binaryCompare: function(context, property, value) {
209
case OpenLayers.Filter.Comparison.EQUAL_TO:
210
return context[property] == value;
211
case OpenLayers.Filter.Comparison.NOT_EQUAL_TO:
212
return context[property] != value;
213
case OpenLayers.Filter.Comparison.LESS_THAN:
214
return context[property] < value;
215
case OpenLayers.Filter.Comparison.GREATER_THAN:
216
return context[property] > value;
217
case OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO:
218
return context[property] <= value;
219
case OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO:
220
return context[property] >= value;
224
CLASS_NAME: "OpenLayers.Filter.Comparison"
228
OpenLayers.Filter.Comparison.EQUAL_TO = "==";
229
OpenLayers.Filter.Comparison.NOT_EQUAL_TO = "!=";
230
OpenLayers.Filter.Comparison.LESS_THAN = "<";
231
OpenLayers.Filter.Comparison.GREATER_THAN = ">";
232
OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO = "<=";
233
OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO = ">=";
234
OpenLayers.Filter.Comparison.BETWEEN = "..";
235
OpenLayers.Filter.Comparison.LIKE = "~";