]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Filter/Comparison.js
fixes notices
[syp.git] / openlayers / lib / OpenLayers / Filter / Comparison.js
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. */
4
5 /**
6  * @requires OpenLayers/Filter.js
7  * @requires OpenLayers/Console.js
8  */
9
10 /**
11  * Class: OpenLayers.Filter.Comparison
12  * This class represents a comparison filter.
13  * 
14  * Inherits from
15  * - <OpenLayers.Filter>
16  */
17 OpenLayers.Filter.Comparison = OpenLayers.Class(OpenLayers.Filter, {
18
19     /**
20      * APIProperty: type
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                     = "~"; 
30      */
31     type: null,
32     
33     /**
34      * APIProperty: property
35      * {String}
36      * name of the context property to compare
37      */
38     property: null,
39     
40     /**
41      * APIProperty: value
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}"
46      */
47     value: null,
48     
49     /**
50      * Property: matchCase
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.
58      */
59     matchCase: true,
60     
61     /**
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}"
67      */
68     lowerBoundary: null,
69     
70     /**
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}"
76      */
77     upperBoundary: null,
78
79     /** 
80      * Constructor: OpenLayers.Filter.Comparison
81      * Creates a comparison rule.
82      *
83      * Parameters:
84      * options - {Object} An optional object with properties to set on the
85      *           rule
86      * 
87      * Returns:
88      * {<OpenLayers.Filter.Comparison>}
89      */
90     initialize: function(options) {
91         OpenLayers.Filter.prototype.initialize.apply(this, [options]);
92     },
93
94     /**
95      * APIMethod: evaluate
96      * Evaluates this filter in a specific context.  Should be implemented by
97      *     subclasses.
98      * 
99      * Parameters:
100      * context - {Object} Context to use in evaluating the filter.
101      * 
102      * Returns:
103      * {Boolean} The filter applies.
104      */
105     evaluate: function(context) {
106         var result = false;
107         switch(this.type) {
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());
114                 } else {
115                     result = (got == exp);
116                 }
117                 break;
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());
124                 } else {
125                     result = (got != exp);
126                 }
127                 break;
128             case OpenLayers.Filter.Comparison.LESS_THAN:
129                 result = context[this.property] < this.value;
130                 break;
131             case OpenLayers.Filter.Comparison.GREATER_THAN:
132                 result = context[this.property] > this.value;
133                 break;
134             case OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO:
135                 result = context[this.property] <= this.value;
136                 break;
137             case OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO:
138                 result = context[this.property] >= this.value;
139                 break;
140             case OpenLayers.Filter.Comparison.BETWEEN:
141                 result = (context[this.property] >= this.lowerBoundary) &&
142                     (context[this.property] <= this.upperBoundary);
143                 break;
144             case OpenLayers.Filter.Comparison.LIKE:
145                 var regexp = new RegExp(this.value, "gi");
146                 result = regexp.test(context[this.property]);
147                 break;
148         }
149         return result;
150     },
151     
152     /**
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.
158      * 
159      * Parameters:
160      * wildCard   - {<Char>} wildcard character in the above value, default
161      *              is "*"
162      * singleChar - {<Char>) single-character wildcard in the above value
163      *              default is "."
164      * escape     - {<Char>) escape character in the above value, default is
165      *              "!"
166      * 
167      * Returns:
168      * {String} regular expression string
169      */
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);
175             return null;
176         }
177         
178
179         // set UMN MapServer defaults for unspecified parameters
180         wildCard = wildCard ? wildCard : "*";
181         singleChar = singleChar ? singleChar : ".";
182         escapeChar = escapeChar ? escapeChar : "!";
183         
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);
194         
195         return this.value;
196     },
197     
198     /**
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.
203      * 
204      * Returns:
205      * {String} A string value.
206      */
207     regex2value: function() {
208         
209         var value = this.value;
210         
211         // replace ! with !!
212         value = value.replace(/!/g, "!!");
213
214         // replace \. with !. (watching out for \\.)
215         value = value.replace(/(\\)?\\\./g, function($0, $1) {
216             return $1 ? $0 : "!.";
217         });
218         
219         // replace \* with #* (watching out for \\*)
220         value = value.replace(/(\\)?\\\*/g, function($0, $1) {
221             return $1 ? $0 : "!*";
222         });
223         
224         // replace \\ with \
225         value = value.replace(/\\\\/g, "\\");
226
227         // convert .* to * (the sequence #.* is not allowed)
228         value = value.replace(/\.\*/g, "*");
229         
230         return value;
231     },
232     
233     /**
234      * APIMethod: clone
235      * Clones this filter.
236      * 
237      * Returns:
238      * {<OpenLayers.Filter.Comparison>} Clone of this filter.
239      */
240     clone: function() {
241         return OpenLayers.Util.extend(new OpenLayers.Filter.Comparison(), this);
242     },
243     
244     CLASS_NAME: "OpenLayers.Filter.Comparison"
245 });
246
247
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                     = "~";