svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / wfs / filters / AbstractFilter.java @ 40559
History | View | Annotate | Download (9.67 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
26 |
*
|
27 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
28 |
*
|
29 |
* This program is free software; you can redistribute it and/or
|
30 |
* modify it under the terms of the GNU General Public License
|
31 |
* as published by the Free Software Foundation; either version 2
|
32 |
* of the License, or (at your option) any later version.
|
33 |
*
|
34 |
* This program is distributed in the hope that it will be useful,
|
35 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
36 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
37 |
* GNU General Public License for more details.
|
38 |
*
|
39 |
* You should have received a copy of the GNU General Public License
|
40 |
* along with this program; if not, write to the Free Software
|
41 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
42 |
*
|
43 |
* For more information, contact:
|
44 |
*
|
45 |
* Generalitat Valenciana
|
46 |
* Conselleria d'Infraestructures i Transport
|
47 |
* Av. Blasco Ib??ez, 50
|
48 |
* 46010 VALENCIA
|
49 |
* SPAIN
|
50 |
*
|
51 |
* +34 963862235
|
52 |
* gvsig@gva.es
|
53 |
* www.gvsig.gva.es
|
54 |
*
|
55 |
* or
|
56 |
*
|
57 |
* IVER T.I. S.A
|
58 |
* Salamanca 50
|
59 |
* 46005 Valencia
|
60 |
* Spain
|
61 |
*
|
62 |
* +34 963163400
|
63 |
* dac@iver.es
|
64 |
*/
|
65 |
package org.gvsig.remoteclient.wfs.filters; |
66 |
import java.io.ByteArrayInputStream; |
67 |
import java.util.ArrayList; |
68 |
import java.util.Iterator; |
69 |
import java.util.StringTokenizer; |
70 |
import java.util.Vector; |
71 |
|
72 |
import org.gvsig.fmap.geom.Geometry; |
73 |
import org.gvsig.fmap.geom.primitive.Envelope; |
74 |
import org.gvsig.remoteclient.wfs.filters.operations.WFSEnvelopeFilterOperation; |
75 |
import org.gvsig.remoteclient.wfs.filters.operations.WFSGeometryFilterOperation; |
76 |
import org.gvsig.remoteclient.wfs.filters.operations.WFSSpatialFilterOperation; |
77 |
|
78 |
/**
|
79 |
* All classes that implement a "Query Language" must to inherit of
|
80 |
* this class
|
81 |
*
|
82 |
* @author Jorge Piera Llodra (piera_jor@gva.es)
|
83 |
*/
|
84 |
public abstract class AbstractFilter implements Filter { |
85 |
private BinaryTree root;
|
86 |
private String currentClause; |
87 |
private ISQLExpressionFormat formatter;
|
88 |
private ArrayList spatialFilterOperations; |
89 |
private ArrayList ids = null; |
90 |
|
91 |
public AbstractFilter(ISQLExpressionFormat formatter){
|
92 |
root = new BinaryTree();
|
93 |
this.formatter = formatter;
|
94 |
spatialFilterOperations = new ArrayList(); |
95 |
} |
96 |
|
97 |
/**
|
98 |
* It returns the Query like a String
|
99 |
*/
|
100 |
public abstract String toString(BinaryTree tree, String version); |
101 |
|
102 |
/**
|
103 |
* returns the String that represents the logic
|
104 |
* operator in this query language
|
105 |
* @param operator
|
106 |
* Logic operator
|
107 |
* @return
|
108 |
*/
|
109 |
public abstract String getLogicalOperator(int operator); |
110 |
|
111 |
/**
|
112 |
* returns the String that represents the relational
|
113 |
* operator in this query language
|
114 |
* @param operator
|
115 |
* Logic operator
|
116 |
* @return
|
117 |
*/
|
118 |
public abstract String getRelationalOperator(int operator); |
119 |
|
120 |
/**
|
121 |
* returns the String that represents the geometric
|
122 |
* operator in this query language
|
123 |
* @param operator
|
124 |
* Logic operator
|
125 |
* @return
|
126 |
*/
|
127 |
public abstract String getGeometricOperator(int operator); |
128 |
|
129 |
/**
|
130 |
* returns the String that represents the separator
|
131 |
* operator in this query language
|
132 |
* @param separator
|
133 |
* LSeparator "(" or ")"
|
134 |
* @return
|
135 |
*/
|
136 |
public abstract String getSeparator(int separator); |
137 |
|
138 |
/**
|
139 |
* Adds a feature id
|
140 |
* @param id
|
141 |
* The feature id
|
142 |
*/
|
143 |
public void addFeatureById(Object id){ |
144 |
if (ids == null){ |
145 |
ids = new ArrayList(); |
146 |
} |
147 |
ids.add(id); |
148 |
} |
149 |
|
150 |
/**
|
151 |
* @return the ids
|
152 |
*/
|
153 |
protected ArrayList getIds() { |
154 |
return ids;
|
155 |
} |
156 |
|
157 |
public void setQueryByAttribute(String query){ |
158 |
if (query != null){ |
159 |
ByteArrayInputStream is = new ByteArrayInputStream(query.getBytes()); |
160 |
String sql = formatter.format(query);
|
161 |
if (sql != null){ |
162 |
ParseExpressions expressions = new ParseExpressions();
|
163 |
ArrayList tokens = expressions.parseExpression(sql);
|
164 |
|
165 |
for (int i=0 ; i<tokens.size() ; i++){ |
166 |
String token = (String)tokens.get(i); |
167 |
root.addTerm(token); |
168 |
} |
169 |
} |
170 |
} |
171 |
} |
172 |
|
173 |
/**
|
174 |
* It adds a new property and value using the AND
|
175 |
* operation
|
176 |
* @param propertyName
|
177 |
* @param propertyValue
|
178 |
*/
|
179 |
public void addAndClause(String propertyName, String propertyValue){ |
180 |
root.addTerm(propertyName + " = " + propertyValue,
|
181 |
getLogicalOperator(LOGICAL_OPERATOR_AND)); |
182 |
} |
183 |
|
184 |
public int getSpatialFiltersCount(){ |
185 |
return spatialFilterOperations.size();
|
186 |
} |
187 |
|
188 |
public WFSSpatialFilterOperation getSpatialFilterAt(int index){ |
189 |
if (index < spatialFilterOperations.size()){
|
190 |
return (WFSSpatialFilterOperation)spatialFilterOperations.get(index);
|
191 |
} |
192 |
return null; |
193 |
} |
194 |
|
195 |
public void clearSpatialFilters(){ |
196 |
spatialFilterOperations.clear(); |
197 |
} |
198 |
|
199 |
public void addSpatialFilter(WFSSpatialFilterOperation spatialOperation){ |
200 |
if (spatialOperation != null){ |
201 |
spatialFilterOperations.add(spatialOperation); |
202 |
} |
203 |
} |
204 |
|
205 |
public void addSpatialFilter(Geometry geometry,String attributeName, String nameSpacePrefix, String nameSpaceLocation, String srs, int operation) { |
206 |
addSpatialFilter(new WFSGeometryFilterOperation(geometry, operation, attributeName, nameSpacePrefix, nameSpaceLocation, srs));
|
207 |
} |
208 |
|
209 |
public void addSpatialFilter(String version, Envelope envelope, String attributeName, String nameSpacePrefix, String nameSpaceLocation, String srs, int operation) { |
210 |
addSpatialFilter(new WFSEnvelopeFilterOperation(envelope, operation, attributeName, nameSpacePrefix, nameSpaceLocation, srs));
|
211 |
} |
212 |
|
213 |
public void addClause(String value){ |
214 |
if (currentClause == null){ |
215 |
currentClause = new String(""); |
216 |
} |
217 |
currentClause = currentClause + value; |
218 |
} |
219 |
|
220 |
public String toString(String version){ |
221 |
if (currentClause != null){ |
222 |
setQueryByAttribute(currentClause); |
223 |
} |
224 |
return toString(root, version);
|
225 |
} |
226 |
|
227 |
/**
|
228 |
* Return true if the token is a operator
|
229 |
* @param operator
|
230 |
* @return
|
231 |
*/
|
232 |
public String getOperator(int operator){ |
233 |
if (isLogical(operator)){
|
234 |
return getLogicalOperator(operator);
|
235 |
}else if(isRelational(operator)){ |
236 |
return getRelationalOperator(operator);
|
237 |
}else if(isGeometric(operator)){ |
238 |
return getGeometricOperator(operator);
|
239 |
} |
240 |
return String.valueOf(operator); |
241 |
} |
242 |
|
243 |
public int getRelationalOperator(String operator){ |
244 |
if (operator.equals("=")){ |
245 |
return RELATIONAL_OPERATOR_IS_EQUALS_TO;
|
246 |
}else if(operator.equals("!=")){ |
247 |
return RELATIONAL_OPERATOR_IS_NOT_EQUALS_TO;
|
248 |
}else if(operator.equals("<>")){ // This is another way to tell not-equal |
249 |
return RELATIONAL_OPERATOR_IS_NOT_EQUALS_TO;
|
250 |
}else if(operator.equals(">")){ |
251 |
return RELATIONAL_OPERATOR_IS_GREATER_THAN;
|
252 |
}else if(operator.equals(">=")){ |
253 |
return RELATIONAL_OPERATOR_IS_GREATER_THAN_OR_EQUAL_TO;
|
254 |
}else if(operator.equals("<")){ |
255 |
return RELATIONAL_OPERATOR_IS_LESS_THAN;
|
256 |
}else if(operator.equals("<=")){ |
257 |
return RELATIONAL_OPERATOR_IS_LESS_THAN_OR_EQUAL_TO;
|
258 |
}else if(operator.toUpperCase().equals("LIKE")){ |
259 |
return RELATIONAL_OPERATOR_IS_LIKE;
|
260 |
} |
261 |
return RELATIONAL_OPERATOR_IS_EQUALS_TO;
|
262 |
} |
263 |
|
264 |
public int getLogicalOperator(String operator){ |
265 |
if (operator.toUpperCase().equals("AND")){ |
266 |
return LOGICAL_OPERATOR_AND;
|
267 |
}else if(operator.toUpperCase().equals("NOT")){ |
268 |
return LOGICAL_OPERATOR_NOT;
|
269 |
}else if(operator.toUpperCase().equals("OR")){ |
270 |
return LOGICAL_OPERATOR_OR;
|
271 |
} |
272 |
return LOGICAL_OPERATOR_AND;
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* Return true if is a geometric operator
|
277 |
* @param type
|
278 |
* @return
|
279 |
*/
|
280 |
private boolean isGeometric(int type) { |
281 |
if ((type > 19) && (type < 40)){ |
282 |
return true; |
283 |
} |
284 |
return false; |
285 |
} |
286 |
|
287 |
/**
|
288 |
* Return true if is a relational operator
|
289 |
* @param type
|
290 |
* @return
|
291 |
*/
|
292 |
private boolean isRelational(int type) { |
293 |
if ((type > 39) && (type < 60)){ |
294 |
return true; |
295 |
} |
296 |
return false; |
297 |
} |
298 |
|
299 |
/**
|
300 |
* Return true if is a logical operator
|
301 |
* @param type
|
302 |
* @return
|
303 |
*/
|
304 |
private boolean isLogical(int type){ |
305 |
if ((type > 9) && (type < 20)){ |
306 |
return true; |
307 |
} |
308 |
return false; |
309 |
} |
310 |
|
311 |
|
312 |
|
313 |
/**
|
314 |
* Return true if is a seperator
|
315 |
* @param type
|
316 |
* @return
|
317 |
*/
|
318 |
private boolean isSeparator(int type){ |
319 |
if ((type > 59) && (type < 70)){ |
320 |
return true; |
321 |
} |
322 |
return false; |
323 |
} |
324 |
|
325 |
/**
|
326 |
* Divide a line in a set of words
|
327 |
* @param line
|
328 |
* Line to divide
|
329 |
* @param option
|
330 |
* If the option is EXACT it returns the same line
|
331 |
* @return Iteraror
|
332 |
* A set of words
|
333 |
*/
|
334 |
public Iterator parseValues(String line, int option) { |
335 |
Vector values = new Vector(); |
336 |
|
337 |
if (option == CONCORDANCIA_EXACT) {
|
338 |
values.add(line); |
339 |
return values.iterator();
|
340 |
} |
341 |
|
342 |
StringTokenizer doubleQuotesTokenizer = new StringTokenizer(line, "\"", |
343 |
true);
|
344 |
boolean inside = false; |
345 |
while (doubleQuotesTokenizer.hasMoreTokens()) {
|
346 |
String token = doubleQuotesTokenizer.nextToken();
|
347 |
if (token.equals("\"")) { |
348 |
inside = !inside; |
349 |
} else if (inside) { |
350 |
values.add(token); |
351 |
} else {
|
352 |
StringTokenizer spaceTokenizer = new StringTokenizer(token, " "); |
353 |
while (spaceTokenizer.hasMoreTokens()) {
|
354 |
String value = spaceTokenizer.nextToken();
|
355 |
values.add(value); |
356 |
} |
357 |
} |
358 |
} |
359 |
return values.iterator();
|
360 |
} |
361 |
} |