Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wfs / filters / AFilter.java @ 17104

History | View | Annotate | Download (11 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
*
4
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
*
20
* For more information, contact:
21
*
22
*  Generalitat Valenciana
23
*   Conselleria d'Infraestructures i Transport
24
*   Av. Blasco Ib??ez, 50
25
*   46010 VALENCIA
26
*   SPAIN
27
*
28
*      +34 963862235
29
*   gvsig@gva.es
30
*      www.gvsig.gva.es
31
*
32
*    or
33
*
34
*   IVER T.I. S.A
35
*   Salamanca 50
36
*   46005 Valencia
37
*   Spain
38
*
39
*   +34 963163400
40
*   dac@iver.es
41
*/
42
package org.gvsig.remoteClient.wfs.filters;
43
import java.awt.geom.Rectangle2D;
44
import java.io.ByteArrayInputStream;
45
import java.util.ArrayList;
46
import java.util.Hashtable;
47
import java.util.Iterator;
48
import java.util.Set;
49
import java.util.StringTokenizer;
50
import java.util.Vector;
51

    
52
/**
53
 * All classes that implement a "Query Language" must to inherit of 
54
 * this class
55
 * 
56
 * @author Jorge Piera Llodra (piera_jor@gva.es)
57
 */
58
public abstract class AFilter {
59
        private BinaryTree root;
60
        private String currentClause;
61
        private ISQLExpressionFormat formatter;
62
        private Rectangle2D bbox = null;
63
        private String bboxPropertyName = null;
64
        private String srs = null;
65
        private int bboxOption = -1;
66
        //Concordancia values: Between 0 and 9
67
    public static final int CONCORDANCIA_EXACT = 0;
68
    public static final int CONCORDANCIA_ANY_WORD = 1;
69
    public static final int CONCORDANCIA_ALL_WORDS = 2;
70
    //Locgical operators: Between 10 and 19
71
    public static final int LOGICAL_OPERATOR_AND = 10;
72
    public static final int LOGICAL_OPERATOR_OR = 11;
73
    public static final int LOGICAL_OPERATOR_NOT = 12;
74
    //Geometric operators: Between 20 and 39
75
    public static final int GEOMETRIC_OPERATOR_EQUALS = 21;
76
    public static final int GEOMETRIC_OPERATOR_DISJOINT = 22;
77
    public static final int GEOMETRIC_OPERATOR_TOUCHES = 23;
78
    public static final int GEOMETRIC_OPERATOR_WITHIN = 24;
79
        public static final int GEOMETRIC_OPERATOR_OVERLAPS = 25;
80
        public static final int GEOMETRIC_OPERATOR_CROSSES = 26;
81
        public static final int GEOMETRIC_OPERATOR_INTERSECT = 27;
82
        public static final int GEOMETRIC_OPERATOR_CONTAINS = 28;
83
        public static final int GEOMETRIC_OPERATOR_DWITHIN = 29;
84
        public static final int GEOMETRIC_OPERATOR_BEYOND = 30;
85
        public static final int GEOMETRIC_OPERATOR_BBOX = 31;
86
        //Relational operator: Between 40 and 60
87
        public static final int RELATIONAL_OPERATOR_IS_EQUALS_TO = 40;
88
        public static final int RELATIONAL_OPERATOR_IS_NOT_EQUALS_TO = 41;
89
        public static final int RELATIONAL_OPERATOR_IS_LESS_THAN = 42;
90
        public static final int RELATIONAL_OPERATOR_IS_GREATER_THAN = 43;
91
        public static final int RELATIONAL_OPERATOR_IS_LESS_THAN_OR_EQUAL_TO = 44;
92
        public static final int RELATIONAL_OPERATOR_IS_GREATER_THAN_OR_EQUAL_TO = 45;
93
        public static final int RELATIONAL_OPERATOR_IS_LIKE = 46;
94
        public static final int RELATIONAL_OPERATOR_IS_NULL = 47;
95
        public static final int RELATIONAL_OPERATOR_IS_BETWEEN = 48;
96
        //Separators: Between 60 and 69
97
        public static final int SEPARATOR_OPENED = 60;
98
        public static final int SEPARATOR_CLOSED = 61; 
99
        //Other: greater that 70
100
        public static final int STRING_VALUE = 70;
101
        //BBOX
102
        public static final int BBOX_ENCLOSES = 81;
103
        
104
        public AFilter(ISQLExpressionFormat formatter){
105
                root = new BinaryTree();        
106
                this.formatter = formatter;
107
        }
108
                
109
        public void setQuery(String query){
110
                if (query != null){                        
111
                        ByteArrayInputStream is = new ByteArrayInputStream(query.getBytes());
112
                        String sql = formatter.format(query);
113
                        if (sql != null){
114
                                ParseExpressions expressions = new ParseExpressions();
115
                                ArrayList tokens = expressions.parseExpression(sql);
116

    
117
                                for (int i=0 ; i<tokens.size() ; i++){
118
                                        String token = (String)tokens.get(i);
119
                                        root.addTerm(token);                                
120
                                }                        
121
                        }        
122
                }
123
        }
124
        
125
        public void setBBox(Rectangle2D bbox,String bboxPropertyName, String srs, int option){
126
                if ((bbox != null) && (bboxPropertyName != null) &&(srs != null)){
127
                        this.bbox = bbox;
128
                        this.bboxOption = option;
129
                        this.srs = srs;
130
                        this.bboxPropertyName = bboxPropertyName;
131
                }
132
        }
133
        
134
        public void addClause(String value){
135
                if (currentClause == null){
136
                        currentClause = new String("");
137
                }
138
                currentClause = currentClause + value;
139
        }
140
        
141
        public String toString(){
142
                if (currentClause != null){
143
                        setQuery(currentClause);
144
                }
145
                return toString(root);
146
        }
147
        
148
    /**
149
     * It returns the Query like a String
150
     */
151
    protected abstract String toString(BinaryTree tree);
152
    
153
    /**
154
     * returns the String that represents the logic
155
     * operator in this query language
156
     * @param operator
157
     * Logic operator
158
     * @return
159
     */
160
    public abstract String getLogicalOperator(int operator);
161
    
162
    /**
163
     * returns the String that represents the relational
164
     * operator in this query language
165
     * @param operator
166
     * Logic operator
167
     * @return
168
     */
169
    public abstract String getRelationalOperator(int operator);
170
    
171
    /**
172
     * returns the String that represents the geometric
173
     * operator in this query language
174
     * @param operator
175
     * Logic operator
176
     * @return
177
     */
178
    public abstract String getGeometricOperator(int operator);
179

    
180
    /**
181
     * returns the String that represents the separator
182
     * operator in this query language
183
     * @param separator
184
     * LSeparator "(" or ")" 
185
     * @return
186
     */
187
    public abstract String getSeparator(int separator);
188
    
189
    /**
190
     * Return true if the token is a operator
191
     * @param operator
192
     * @return
193
     */
194
    public String getOperator(int operator){
195
            if (isLogical(operator)){
196
                    return getLogicalOperator(operator);
197
            }else if(isRelational(operator)){
198
                    return getRelationalOperator(operator);
199
            }else if(isGeometric(operator)){
200
                    return getGeometricOperator(operator);
201
            }
202
            return String.valueOf(operator);
203
    }        
204
    
205
    public int getRelationalOperator(String operator){
206
            if (operator.equals("=")){
207
                    return RELATIONAL_OPERATOR_IS_EQUALS_TO;
208
            }else if(operator.equals("!=")){
209
                    return RELATIONAL_OPERATOR_IS_NOT_EQUALS_TO;
210
            }else if(operator.equals("<>")){ // This is another way to tell not-equal
211
                    return RELATIONAL_OPERATOR_IS_NOT_EQUALS_TO;
212
            }else if(operator.equals(">")){
213
                    return RELATIONAL_OPERATOR_IS_GREATER_THAN;
214
            }else if(operator.equals(">=")){
215
                    return RELATIONAL_OPERATOR_IS_GREATER_THAN_OR_EQUAL_TO;
216
            }else if(operator.equals("<")){
217
                    return RELATIONAL_OPERATOR_IS_LESS_THAN;
218
            }else if(operator.equals("<=")){
219
                    return RELATIONAL_OPERATOR_IS_LESS_THAN_OR_EQUAL_TO;
220
            }else if(operator.toUpperCase().equals("LIKE")){
221
                    return RELATIONAL_OPERATOR_IS_LIKE;
222
            }
223
            return RELATIONAL_OPERATOR_IS_EQUALS_TO;
224
    }
225
    
226
    public int getLogicalOperator(String operator){
227
            if (operator.toUpperCase().equals("AND")){
228
                    return LOGICAL_OPERATOR_AND;
229
            }else if(operator.toUpperCase().equals("NOT")){
230
                    return LOGICAL_OPERATOR_NOT;
231
            }else if(operator.toUpperCase().equals("OR")){
232
                    return LOGICAL_OPERATOR_OR;
233
            }
234
            return LOGICAL_OPERATOR_AND;
235
    }
236
    
237
        /**
238
     * Return true if is a geometric operator
239
     * @param type
240
     * @return
241
     */
242
    private boolean isGeometric(int type) {
243
               if ((type > 19) && (type < 40)){
244
                       return true;
245
               }
246
        return false;
247
        }
248

    
249
        /**
250
     * Return true if is a relational operator
251
     * @param type
252
     * @return
253
     */
254
    private boolean isRelational(int type) {
255
            if ((type > 39) && (type < 60)){
256
                       return true;
257
               }
258
        return false;
259
        }
260

    
261
        /**
262
     * Return true if is a logical operator
263
     * @param type
264
     * @return
265
     */
266
    private boolean isLogical(int type){
267
             if ((type > 9) && (type < 20)){
268
                       return true;
269
               }
270
        return false;
271
    }
272
    
273
   
274
    
275
    /**
276
     * Return true if is a seperator
277
     * @param type
278
     * @return
279
     */
280
    private boolean isSeparator(int type){
281
            if ((type > 59) && (type < 70)){
282
                       return true;
283
               }
284
        return false;
285
    }      
286
    
287
    /**
288
     * Divide a line in a set of words
289
     * @param line
290
     * Line to divide
291
     * @param option
292
     * If the option is EXACT it returns the same line 
293
     * @return Iteraror
294
     * A set of words
295
     */
296
    public Iterator parseValues(String line, int option) {        
297
        Vector values = new Vector();
298
            
299
        if (option == CONCORDANCIA_EXACT) {
300
            values.add(line);
301
            return values.iterator();
302
        }
303
        
304
        StringTokenizer doubleQuotesTokenizer = new StringTokenizer(line, "\"",
305
                true);
306
        boolean inside = false;
307
        while (doubleQuotesTokenizer.hasMoreTokens()) {
308
            String token = doubleQuotesTokenizer.nextToken();
309
            if (token.equals("\"")) {
310
                inside = !inside;
311
            } else if (inside) {
312
                values.add(token);
313
            } else {
314
                StringTokenizer spaceTokenizer = new StringTokenizer(token, " ");
315
                while (spaceTokenizer.hasMoreTokens()) {
316
                    String value = spaceTokenizer.nextToken();
317
                    values.add(value);
318
                }
319
            }
320
        }
321
        return values.iterator();
322
    } 
323
    
324
    /**
325
     * Envolves a value with an XML tag
326
     * 
327
     * @return String
328
     * XML tag with its value
329
     * @param tagName 
330
     * XML tag name
331
     * @param attributes
332
     * XML tag attributes
333
     * @param value 
334
     * Tag value
335
     */
336
    public String setTag(String tagName, Hashtable attributes, String value) {        
337
            StringBuffer tag = new StringBuffer();
338
            tag.append("<" + tagName);
339
            if (attributes != null){
340
                    Set keys = attributes.keySet();
341
                    if (attributes.size() > 0){
342
                            Iterator it = keys.iterator();
343
                            while (it.hasNext()){
344
                                    String key = (String)it.next();
345
                                    tag.append(" " + key + "=" + (String)attributes.get(key));
346
                                    
347
                            }
348
                    }
349
            }
350
            tag.append(">" + value);
351
            tag.append("</" + tagName + ">");
352
            return tag.toString();
353
    }
354

    
355
        /**
356
         * @return Returns the bbox.
357
         */
358
        public Rectangle2D getBbox() {
359
                return bbox;
360
        }
361

    
362
        /**
363
         * @return Returns the bboxOption.
364
         */
365
        public int getBboxOption() {
366
                return bboxOption;
367
        }
368

    
369
        public String getBboxPropertyName() {
370
                return bboxPropertyName;
371
        }
372

    
373
        public void setBboxPropertyName(String bboxPropertyName) {
374
                this.bboxPropertyName = bboxPropertyName;
375
        }
376

    
377
        public String getSrs() {
378
                return srs;
379
        }
380

    
381
        public void setSrs(String srs) {
382
                this.srs = srs;
383
        }  
384
  
385
   
386
}