~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Filter/Comparison.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
8
 
 
9
/**
 
10
 * Class: OpenLayers.Filter.Comparison
 
11
 * This class represents a comparison filter.
 
12
 * 
 
13
 * Inherits from
 
14
 * - <OpenLayers.Filter>
 
15
 */
 
16
OpenLayers.Filter.Comparison = OpenLayers.Class(OpenLayers.Filter, {
 
17
 
 
18
    /**
 
19
     * APIProperty: type
 
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                     = "~"; 
 
29
     */
 
30
    type: null,
 
31
    
 
32
    /**
 
33
     * APIProperty: property
 
34
     * {String}
 
35
     * name of the context property to compare
 
36
     */
 
37
    property: null,
 
38
    
 
39
    /**
 
40
     * APIProperty: value
 
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}"
 
45
     */
 
46
    value: null,
 
47
    
 
48
    /**
 
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}"
 
54
     */
 
55
    lowerBoundary: null,
 
56
    
 
57
    /**
 
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}"
 
63
     */
 
64
    upperBoundary: null,
 
65
 
 
66
    /** 
 
67
     * Constructor: OpenLayers.Filter.Comparison
 
68
     * Creates a comparison rule.
 
69
     *
 
70
     * Parameters:
 
71
     * options - {Object} An optional object with properties to set on the
 
72
     *           rule
 
73
     * 
 
74
     * Returns:
 
75
     * {<OpenLayers.Filter.Comparison>}
 
76
     */
 
77
    initialize: function(options) {
 
78
        OpenLayers.Filter.prototype.initialize.apply(this, [options]);
 
79
    },
 
80
 
 
81
    /**
 
82
     * APIMethod: evaluate
 
83
     * Evaluates this filter in a specific context.  Should be implemented by
 
84
     *     subclasses.
 
85
     * 
 
86
     * Parameters:
 
87
     * context - {Object} Context to use in evaluating the filter.
 
88
     * 
 
89
     * Returns:
 
90
     * {Boolean} The filter applies.
 
91
     */
 
92
    evaluate: function(context) {
 
93
        switch(this.type) {
 
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);
 
100
            
 
101
            case OpenLayers.Filter.Comparison.BETWEEN:
 
102
                var result =
 
103
                        context[this.property] >= this.lowerBoundary;
 
104
                result = result &&
 
105
                        context[this.property] <= this.upperBoundary;
 
106
                return result;
 
107
            case OpenLayers.Filter.Comparison.LIKE:
 
108
                var regexp = new RegExp(this.value,
 
109
                                "gi");
 
110
                return regexp.test(context[this.property]); 
 
111
        }
 
112
    },
 
113
    
 
114
    /**
 
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.
 
120
     * 
 
121
     * Parameters:
 
122
     * wildCard   - {<Char>} wildcard character in the above value, default
 
123
     *              is "*"
 
124
     * singleChar - {<Char>) single-character wildcard in the above value
 
125
     *              default is "."
 
126
     * escape     - {<Char>) escape character in the above value, default is
 
127
     *              "!"
 
128
     * 
 
129
     * Returns:
 
130
     * {String} regular expression string
 
131
     */
 
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);
 
137
            return null;
 
138
        }
 
139
        
 
140
 
 
141
        // set UMN MapServer defaults for unspecified parameters
 
142
        wildCard = wildCard ? wildCard : "*";
 
143
        singleChar = singleChar ? singleChar : ".";
 
144
        escapeChar = escapeChar ? escapeChar : "!";
 
145
        
 
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);
 
156
        
 
157
        return this.value;
 
158
    },
 
159
    
 
160
    /**
 
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.
 
165
     * 
 
166
     * Returns:
 
167
     * {String} A string value.
 
168
     */
 
169
    regex2value: function() {
 
170
        
 
171
        var value = this.value;
 
172
        
 
173
        // replace ! with !!
 
174
        value = value.replace(/!/g, "!!");
 
175
 
 
176
        // replace \. with !. (watching out for \\.)
 
177
        value = value.replace(/(\\)?\\\./g, function($0, $1) {
 
178
            return $1 ? $0 : "!.";
 
179
        });
 
180
        
 
181
        // replace \* with #* (watching out for \\*)
 
182
        value = value.replace(/(\\)?\\\*/g, function($0, $1) {
 
183
            return $1 ? $0 : "!*";
 
184
        });
 
185
        
 
186
        // replace \\ with \
 
187
        value = value.replace(/\\\\/g, "\\");
 
188
 
 
189
        // convert .* to * (the sequence #.* is not allowed)
 
190
        value = value.replace(/\.\*/g, "*");
 
191
        
 
192
        return value;
 
193
    },
 
194
 
 
195
    /**
 
196
     * Function: binaryCompare
 
197
     * Compares a feature property to a rule value
 
198
     * 
 
199
     * Parameters:
 
200
     * context  - {Object}
 
201
     * property - {String} or {Number}
 
202
     * value    - {String} or {Number}, same as property
 
203
     * 
 
204
     * Returns:
 
205
     * {Boolean}
 
206
     */
 
207
    binaryCompare: function(context, property, value) {
 
208
        switch (this.type) {
 
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;
 
221
        }      
 
222
    },
 
223
    
 
224
    CLASS_NAME: "OpenLayers.Filter.Comparison"
 
225
});
 
226
 
 
227
 
 
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                     = "~";