[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @requires OpenLayers/Filter.js |
---|
| 8 | * @requires OpenLayers/Console.js |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * Class: OpenLayers.Filter.Comparison |
---|
| 13 | * This class represents a comparison filter. |
---|
| 14 | * |
---|
| 15 | * Inherits from |
---|
| 16 | * - <OpenLayers.Filter> |
---|
| 17 | */ |
---|
| 18 | OpenLayers.Filter.Comparison = OpenLayers.Class(OpenLayers.Filter, { |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * APIProperty: type |
---|
| 22 | * {String} type: type of the comparison. This is one of |
---|
| 23 | * - OpenLayers.Filter.Comparison.EQUAL_TO = "=="; |
---|
| 24 | * - OpenLayers.Filter.Comparison.NOT_EQUAL_TO = "!="; |
---|
| 25 | * - OpenLayers.Filter.Comparison.LESS_THAN = "<"; |
---|
| 26 | * - OpenLayers.Filter.Comparison.GREATER_THAN = ">"; |
---|
| 27 | * - OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO = "<="; |
---|
| 28 | * - OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO = ">="; |
---|
| 29 | * - OpenLayers.Filter.Comparison.BETWEEN = ".."; |
---|
| 30 | * - OpenLayers.Filter.Comparison.LIKE = "~"; |
---|
| 31 | */ |
---|
| 32 | type: null, |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * APIProperty: property |
---|
| 36 | * {String} |
---|
| 37 | * name of the context property to compare |
---|
| 38 | */ |
---|
| 39 | property: null, |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * APIProperty: value |
---|
| 43 | * {Number} or {String} |
---|
| 44 | * comparison value for binary comparisons. In the case of a String, this |
---|
| 45 | * can be a combination of text and propertyNames in the form |
---|
| 46 | * "literal ${propertyName}" |
---|
| 47 | */ |
---|
| 48 | value: null, |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * Property: matchCase |
---|
| 52 | * {Boolean} Force case sensitive searches for EQUAL_TO and NOT_EQUAL_TO |
---|
| 53 | * comparisons. The Filter Encoding 1.1 specification added a matchCase |
---|
| 54 | * attribute to ogc:PropertyIsEqualTo and ogc:PropertyIsNotEqualTo |
---|
| 55 | * elements. This property will be serialized with those elements only |
---|
| 56 | * if using the v1.1.0 filter format. However, when evaluating filters |
---|
| 57 | * here, the matchCase property will always be respected (for EQUAL_TO |
---|
| 58 | * and NOT_EQUAL_TO). Default is true. |
---|
| 59 | */ |
---|
| 60 | matchCase: true, |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * APIProperty: lowerBoundary |
---|
| 64 | * {Number} or {String} |
---|
| 65 | * lower boundary for between comparisons. In the case of a String, this |
---|
| 66 | * can be a combination of text and propertyNames in the form |
---|
| 67 | * "literal ${propertyName}" |
---|
| 68 | */ |
---|
| 69 | lowerBoundary: null, |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * APIProperty: upperBoundary |
---|
| 73 | * {Number} or {String} |
---|
| 74 | * upper boundary for between comparisons. In the case of a String, this |
---|
| 75 | * can be a combination of text and propertyNames in the form |
---|
| 76 | * "literal ${propertyName}" |
---|
| 77 | */ |
---|
| 78 | upperBoundary: null, |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Constructor: OpenLayers.Filter.Comparison |
---|
| 82 | * Creates a comparison rule. |
---|
| 83 | * |
---|
| 84 | * Parameters: |
---|
| 85 | * options - {Object} An optional object with properties to set on the |
---|
| 86 | * rule |
---|
| 87 | * |
---|
| 88 | * Returns: |
---|
| 89 | * {<OpenLayers.Filter.Comparison>} |
---|
| 90 | */ |
---|
| 91 | initialize: function(options) { |
---|
| 92 | OpenLayers.Filter.prototype.initialize.apply(this, [options]); |
---|
| 93 | }, |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * APIMethod: evaluate |
---|
| 97 | * Evaluates this filter in a specific context. |
---|
| 98 | * |
---|
| 99 | * Parameters: |
---|
| 100 | * context - {Object} Context to use in evaluating the filter. If a vector |
---|
| 101 | * feature is provided, the feature.attributes will be used as context. |
---|
| 102 | * |
---|
| 103 | * Returns: |
---|
| 104 | * {Boolean} The filter applies. |
---|
| 105 | */ |
---|
| 106 | evaluate: function(context) { |
---|
| 107 | if (context instanceof OpenLayers.Feature.Vector) { |
---|
| 108 | context = context.attributes; |
---|
| 109 | } |
---|
| 110 | var result = false; |
---|
| 111 | var got = context[this.property]; |
---|
| 112 | switch(this.type) { |
---|
| 113 | case OpenLayers.Filter.Comparison.EQUAL_TO: |
---|
| 114 | var exp = this.value; |
---|
| 115 | if(!this.matchCase && |
---|
| 116 | typeof got == "string" && typeof exp == "string") { |
---|
| 117 | result = (got.toUpperCase() == exp.toUpperCase()); |
---|
| 118 | } else { |
---|
| 119 | result = (got == exp); |
---|
| 120 | } |
---|
| 121 | break; |
---|
| 122 | case OpenLayers.Filter.Comparison.NOT_EQUAL_TO: |
---|
| 123 | var exp = this.value; |
---|
| 124 | if(!this.matchCase && |
---|
| 125 | typeof got == "string" && typeof exp == "string") { |
---|
| 126 | result = (got.toUpperCase() != exp.toUpperCase()); |
---|
| 127 | } else { |
---|
| 128 | result = (got != exp); |
---|
| 129 | } |
---|
| 130 | break; |
---|
| 131 | case OpenLayers.Filter.Comparison.LESS_THAN: |
---|
| 132 | result = got < this.value; |
---|
| 133 | break; |
---|
| 134 | case OpenLayers.Filter.Comparison.GREATER_THAN: |
---|
| 135 | result = got > this.value; |
---|
| 136 | break; |
---|
| 137 | case OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO: |
---|
| 138 | result = got <= this.value; |
---|
| 139 | break; |
---|
| 140 | case OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO: |
---|
| 141 | result = got >= this.value; |
---|
| 142 | break; |
---|
| 143 | case OpenLayers.Filter.Comparison.BETWEEN: |
---|
| 144 | result = (got >= this.lowerBoundary) && |
---|
| 145 | (got <= this.upperBoundary); |
---|
| 146 | break; |
---|
| 147 | case OpenLayers.Filter.Comparison.LIKE: |
---|
| 148 | var regexp = new RegExp(this.value, "gi"); |
---|
| 149 | result = regexp.test(got); |
---|
| 150 | break; |
---|
| 151 | } |
---|
| 152 | return result; |
---|
| 153 | }, |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * APIMethod: value2regex |
---|
| 157 | * Converts the value of this rule into a regular expression string, |
---|
| 158 | * according to the wildcard characters specified. This method has to |
---|
| 159 | * be called after instantiation of this class, if the value is not a |
---|
| 160 | * regular expression already. |
---|
| 161 | * |
---|
| 162 | * Parameters: |
---|
| 163 | * wildCard - {<Char>} wildcard character in the above value, default |
---|
| 164 | * is "*" |
---|
| 165 | * singleChar - {<Char>) single-character wildcard in the above value |
---|
| 166 | * default is "." |
---|
| 167 | * escape - {<Char>) escape character in the above value, default is |
---|
| 168 | * "!" |
---|
| 169 | * |
---|
| 170 | * Returns: |
---|
| 171 | * {String} regular expression string |
---|
| 172 | */ |
---|
| 173 | value2regex: function(wildCard, singleChar, escapeChar) { |
---|
| 174 | if (wildCard == ".") { |
---|
| 175 | var msg = "'.' is an unsupported wildCard character for "+ |
---|
| 176 | "OpenLayers.Filter.Comparison"; |
---|
| 177 | OpenLayers.Console.error(msg); |
---|
| 178 | return null; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | // set UMN MapServer defaults for unspecified parameters |
---|
| 183 | wildCard = wildCard ? wildCard : "*"; |
---|
| 184 | singleChar = singleChar ? singleChar : "."; |
---|
| 185 | escapeChar = escapeChar ? escapeChar : "!"; |
---|
| 186 | |
---|
| 187 | this.value = this.value.replace( |
---|
| 188 | new RegExp("\\"+escapeChar+"(.|$)", "g"), "\\$1"); |
---|
| 189 | this.value = this.value.replace( |
---|
| 190 | new RegExp("\\"+singleChar, "g"), "."); |
---|
| 191 | this.value = this.value.replace( |
---|
| 192 | new RegExp("\\"+wildCard, "g"), ".*"); |
---|
| 193 | this.value = this.value.replace( |
---|
| 194 | new RegExp("\\\\.\\*", "g"), "\\"+wildCard); |
---|
| 195 | this.value = this.value.replace( |
---|
| 196 | new RegExp("\\\\\\.", "g"), "\\"+singleChar); |
---|
| 197 | |
---|
| 198 | return this.value; |
---|
| 199 | }, |
---|
| 200 | |
---|
| 201 | /** |
---|
| 202 | * Method: regex2value |
---|
| 203 | * Convert the value of this rule from a regular expression string into an |
---|
| 204 | * ogc literal string using a wildCard of *, a singleChar of ., and an |
---|
| 205 | * escape of !. Leaves the <value> property unmodified. |
---|
| 206 | * |
---|
| 207 | * Returns: |
---|
| 208 | * {String} A string value. |
---|
| 209 | */ |
---|
| 210 | regex2value: function() { |
---|
| 211 | |
---|
| 212 | var value = this.value; |
---|
| 213 | |
---|
| 214 | // replace ! with !! |
---|
| 215 | value = value.replace(/!/g, "!!"); |
---|
| 216 | |
---|
| 217 | // replace \. with !. (watching out for \\.) |
---|
| 218 | value = value.replace(/(\\)?\\\./g, function($0, $1) { |
---|
| 219 | return $1 ? $0 : "!."; |
---|
| 220 | }); |
---|
| 221 | |
---|
| 222 | // replace \* with #* (watching out for \\*) |
---|
| 223 | value = value.replace(/(\\)?\\\*/g, function($0, $1) { |
---|
| 224 | return $1 ? $0 : "!*"; |
---|
| 225 | }); |
---|
| 226 | |
---|
| 227 | // replace \\ with \ |
---|
| 228 | value = value.replace(/\\\\/g, "\\"); |
---|
| 229 | |
---|
| 230 | // convert .* to * (the sequence #.* is not allowed) |
---|
| 231 | value = value.replace(/\.\*/g, "*"); |
---|
| 232 | |
---|
| 233 | return value; |
---|
| 234 | }, |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | * APIMethod: clone |
---|
| 238 | * Clones this filter. |
---|
| 239 | * |
---|
| 240 | * Returns: |
---|
| 241 | * {<OpenLayers.Filter.Comparison>} Clone of this filter. |
---|
| 242 | */ |
---|
| 243 | clone: function() { |
---|
| 244 | return OpenLayers.Util.extend(new OpenLayers.Filter.Comparison(), this); |
---|
| 245 | }, |
---|
| 246 | |
---|
| 247 | CLASS_NAME: "OpenLayers.Filter.Comparison" |
---|
| 248 | }); |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | OpenLayers.Filter.Comparison.EQUAL_TO = "=="; |
---|
| 252 | OpenLayers.Filter.Comparison.NOT_EQUAL_TO = "!="; |
---|
| 253 | OpenLayers.Filter.Comparison.LESS_THAN = "<"; |
---|
| 254 | OpenLayers.Filter.Comparison.GREATER_THAN = ">"; |
---|
| 255 | OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO = "<="; |
---|
| 256 | OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO = ">="; |
---|
| 257 | OpenLayers.Filter.Comparison.BETWEEN = ".."; |
---|
| 258 | OpenLayers.Filter.Comparison.LIKE = "~"; |
---|