Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wfs / filters / AbstractFilter.java @ 34026

History | View | Annotate | Download (8.7 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.io.ByteArrayInputStream;
44
import java.util.ArrayList;
45
import java.util.Iterator;
46
import java.util.StringTokenizer;
47
import java.util.Vector;
48

    
49
import org.gvsig.fmap.geom.Geometry;
50
import org.gvsig.fmap.geom.primitive.Envelope;
51
import org.gvsig.remoteclient.wfs.filters.operations.WFSEnvelopeFilterOperation;
52
import org.gvsig.remoteclient.wfs.filters.operations.WFSGeometryFilterOperation;
53
import org.gvsig.remoteclient.wfs.filters.operations.WFSSpatialFilterOperation;
54

    
55
/**
56
 * All classes that implement a "Query Language" must to inherit of 
57
 * this class
58
 * 
59
 * @author Jorge Piera Llodra (piera_jor@gva.es)
60
 */
61
public abstract class AbstractFilter implements Filter {
62
        private BinaryTree root;
63
        private String currentClause;
64
        private ISQLExpressionFormat formatter;
65
        private ArrayList spatialFilterOperations;
66
        private ArrayList ids = null;        
67

    
68
        public AbstractFilter(ISQLExpressionFormat formatter){
69
                root = new BinaryTree();        
70
                this.formatter = formatter;
71
                spatialFilterOperations = new ArrayList();
72
        }
73
        
74
        /**
75
         * It returns the Query like a String
76
         */
77
        public abstract String toString(BinaryTree tree, String version);
78
        
79
        /**
80
         * returns the String that represents the logic
81
         * operator in this query language
82
         * @param operator
83
         * Logic operator
84
         * @return
85
         */
86
        public abstract String getLogicalOperator(int operator);
87

    
88
        /**
89
         * returns the String that represents the relational
90
         * operator in this query language
91
         * @param operator
92
         * Logic operator
93
         * @return
94
         */
95
        public abstract String getRelationalOperator(int operator);
96

    
97
        /**
98
         * returns the String that represents the geometric
99
         * operator in this query language
100
         * @param operator
101
         * Logic operator
102
         * @return
103
         */
104
        public abstract String getGeometricOperator(int operator);
105

    
106
        /**
107
         * returns the String that represents the separator
108
         * operator in this query language
109
         * @param separator
110
         * LSeparator "(" or ")" 
111
         * @return
112
         */
113
        public abstract String getSeparator(int separator);
114

    
115
        /**
116
         * Adds a feature id
117
         * @param id
118
         * The feature id
119
         */
120
        public void addFeatureById(Object id){
121
                if (ids == null){
122
                        ids = new ArrayList();
123
                }
124
                ids.add(id);
125
        }
126

    
127
        /**
128
         * @return the ids
129
         */
130
        protected ArrayList getIds() {
131
                return ids;
132
        }
133

    
134
        public void setQueryByAttribute(String query){
135
                if (query != null){                        
136
                        ByteArrayInputStream is = new ByteArrayInputStream(query.getBytes());
137
                        String sql = formatter.format(query);
138
                        if (sql != null){
139
                                ParseExpressions expressions = new ParseExpressions();
140
                                ArrayList tokens = expressions.parseExpression(sql);
141

    
142
                                for (int i=0 ; i<tokens.size() ; i++){
143
                                        String token = (String)tokens.get(i);
144
                                        root.addTerm(token);                                
145
                                }                        
146
                        }        
147
                }
148
        }
149

    
150
        /**
151
         * It adds a new property and value using the AND
152
         * operation 
153
         * @param propertyName
154
         * @param propertyValue
155
         */
156
        public void addAndClause(String propertyName, String propertyValue){
157
                root.addTerm(propertyName + " = " + propertyValue,
158
                                getLogicalOperator(LOGICAL_OPERATOR_AND));
159
        }
160

    
161
        public int getSpatialFiltersCount(){
162
                return spatialFilterOperations.size();
163
        }
164

    
165
        public WFSSpatialFilterOperation getSpatialFilterAt(int index){
166
                if (index < spatialFilterOperations.size()){
167
                        return (WFSSpatialFilterOperation)spatialFilterOperations.get(index);
168
                }
169
                return null;
170
        }
171

    
172
        public void clearSpatialFilters(){
173
                spatialFilterOperations.clear();
174
        }
175
        
176
        public void addSpatialFilter(WFSSpatialFilterOperation spatialOperation){
177
                spatialFilterOperations.add(spatialOperation);
178
        }
179

    
180
        public void addSpatialFilter(Geometry geometry,String attributeName, String nameSpacePrefix, String nameSpaceLocation, String srs, int operation) {
181
                addSpatialFilter(new WFSGeometryFilterOperation(geometry, operation, attributeName, nameSpacePrefix, nameSpaceLocation, srs));
182
        }
183
        
184
        public void addSpatialFilter(String version, Envelope envelope, String attributeName, String nameSpacePrefix, String nameSpaceLocation, String srs, int operation) {
185
                addSpatialFilter(new WFSEnvelopeFilterOperation(envelope, operation, attributeName, nameSpacePrefix, nameSpaceLocation, srs));
186
        }
187

    
188
        public void addClause(String value){
189
                if (currentClause == null){
190
                        currentClause = new String("");
191
                }
192
                currentClause = currentClause + value;
193
        }
194

    
195
        public String toString(String version){
196
                if (currentClause != null){
197
                        setQueryByAttribute(currentClause);
198
                }
199
                return toString(root, version);
200
        }        
201

    
202
        /**
203
         * Return true if the token is a operator
204
         * @param operator
205
         * @return
206
         */
207
        public String getOperator(int operator){
208
                if (isLogical(operator)){
209
                        return getLogicalOperator(operator);
210
                }else if(isRelational(operator)){
211
                        return getRelationalOperator(operator);
212
                }else if(isGeometric(operator)){
213
                        return getGeometricOperator(operator);
214
                }
215
                return String.valueOf(operator);
216
        }        
217

    
218
        public int getRelationalOperator(String operator){
219
                if (operator.equals("=")){
220
                        return RELATIONAL_OPERATOR_IS_EQUALS_TO;
221
                }else if(operator.equals("!=")){
222
                        return RELATIONAL_OPERATOR_IS_NOT_EQUALS_TO;
223
                }else if(operator.equals("<>")){ // This is another way to tell not-equal
224
                        return RELATIONAL_OPERATOR_IS_NOT_EQUALS_TO;
225
                }else if(operator.equals(">")){
226
                        return RELATIONAL_OPERATOR_IS_GREATER_THAN;
227
                }else if(operator.equals(">=")){
228
                        return RELATIONAL_OPERATOR_IS_GREATER_THAN_OR_EQUAL_TO;
229
                }else if(operator.equals("<")){
230
                        return RELATIONAL_OPERATOR_IS_LESS_THAN;
231
                }else if(operator.equals("<=")){
232
                        return RELATIONAL_OPERATOR_IS_LESS_THAN_OR_EQUAL_TO;
233
                }else if(operator.toUpperCase().equals("LIKE")){
234
                        return RELATIONAL_OPERATOR_IS_LIKE;
235
                }
236
                return RELATIONAL_OPERATOR_IS_EQUALS_TO;
237
        }
238

    
239
        public int getLogicalOperator(String operator){
240
                if (operator.toUpperCase().equals("AND")){
241
                        return LOGICAL_OPERATOR_AND;
242
                }else if(operator.toUpperCase().equals("NOT")){
243
                        return LOGICAL_OPERATOR_NOT;
244
                }else if(operator.toUpperCase().equals("OR")){
245
                        return LOGICAL_OPERATOR_OR;
246
                }
247
                return LOGICAL_OPERATOR_AND;
248
        }
249

    
250
        /**
251
         * Return true if is a geometric operator
252
         * @param type
253
         * @return
254
         */
255
        private boolean isGeometric(int type) {
256
                if ((type > 19) && (type < 40)){
257
                        return true;
258
                }
259
                return false;
260
        }
261

    
262
        /**
263
         * Return true if is a relational operator
264
         * @param type
265
         * @return
266
         */
267
        private boolean isRelational(int type) {
268
                if ((type > 39) && (type < 60)){
269
                        return true;
270
                }
271
                return false;
272
        }
273

    
274
        /**
275
         * Return true if is a logical operator
276
         * @param type
277
         * @return
278
         */
279
        private boolean isLogical(int type){
280
                if ((type > 9) && (type < 20)){
281
                        return true;
282
                }
283
                return false;
284
        }
285

    
286

    
287

    
288
        /**
289
         * Return true if is a seperator
290
         * @param type
291
         * @return
292
         */
293
        private boolean isSeparator(int type){
294
                if ((type > 59) && (type < 70)){
295
                        return true;
296
                }
297
                return false;
298
        }      
299

    
300
        /**
301
         * Divide a line in a set of words
302
         * @param line
303
         * Line to divide
304
         * @param option
305
         * If the option is EXACT it returns the same line 
306
         * @return Iteraror
307
         * A set of words
308
         */
309
        public Iterator parseValues(String line, int option) {        
310
                Vector values = new Vector();
311

    
312
                if (option == CONCORDANCIA_EXACT) {
313
                        values.add(line);
314
                        return values.iterator();
315
                }
316

    
317
                StringTokenizer doubleQuotesTokenizer = new StringTokenizer(line, "\"",
318
                                true);
319
                boolean inside = false;
320
                while (doubleQuotesTokenizer.hasMoreTokens()) {
321
                        String token = doubleQuotesTokenizer.nextToken();
322
                        if (token.equals("\"")) {
323
                                inside = !inside;
324
                        } else if (inside) {
325
                                values.add(token);
326
                        } else {
327
                                StringTokenizer spaceTokenizer = new StringTokenizer(token, " ");
328
                                while (spaceTokenizer.hasMoreTokens()) {
329
                                        String value = spaceTokenizer.nextToken();
330
                                        values.add(value);
331
                                }
332
                        }
333
                }
334
                return values.iterator();
335
        } 
336
}