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 @ 40769
History | View | Annotate | Download (8.48 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 |
package org.gvsig.remoteclient.wfs.filters; |
26 |
import java.io.ByteArrayInputStream; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Iterator; |
29 |
import java.util.StringTokenizer; |
30 |
import java.util.Vector; |
31 |
|
32 |
import org.gvsig.fmap.geom.Geometry; |
33 |
import org.gvsig.fmap.geom.primitive.Envelope; |
34 |
import org.gvsig.remoteclient.wfs.filters.operations.WFSEnvelopeFilterOperation; |
35 |
import org.gvsig.remoteclient.wfs.filters.operations.WFSGeometryFilterOperation; |
36 |
import org.gvsig.remoteclient.wfs.filters.operations.WFSSpatialFilterOperation; |
37 |
|
38 |
/**
|
39 |
* All classes that implement a "Query Language" must to inherit of
|
40 |
* this class
|
41 |
*
|
42 |
* @author Jorge Piera Llodra (piera_jor@gva.es)
|
43 |
*/
|
44 |
public abstract class AbstractFilter implements Filter { |
45 |
private BinaryTree root;
|
46 |
private String currentClause; |
47 |
private ISQLExpressionFormat formatter;
|
48 |
private ArrayList spatialFilterOperations; |
49 |
private ArrayList ids = null; |
50 |
|
51 |
public AbstractFilter(ISQLExpressionFormat formatter){
|
52 |
root = new BinaryTree();
|
53 |
this.formatter = formatter;
|
54 |
spatialFilterOperations = new ArrayList(); |
55 |
} |
56 |
|
57 |
/**
|
58 |
* It returns the Query like a String
|
59 |
*/
|
60 |
public abstract String toString(BinaryTree tree, String version); |
61 |
|
62 |
/**
|
63 |
* returns the String that represents the logic
|
64 |
* operator in this query language
|
65 |
* @param operator
|
66 |
* Logic operator
|
67 |
* @return
|
68 |
*/
|
69 |
public abstract String getLogicalOperator(int operator); |
70 |
|
71 |
/**
|
72 |
* returns the String that represents the relational
|
73 |
* operator in this query language
|
74 |
* @param operator
|
75 |
* Logic operator
|
76 |
* @return
|
77 |
*/
|
78 |
public abstract String getRelationalOperator(int operator); |
79 |
|
80 |
/**
|
81 |
* returns the String that represents the geometric
|
82 |
* operator in this query language
|
83 |
* @param operator
|
84 |
* Logic operator
|
85 |
* @return
|
86 |
*/
|
87 |
public abstract String getGeometricOperator(int operator); |
88 |
|
89 |
/**
|
90 |
* returns the String that represents the separator
|
91 |
* operator in this query language
|
92 |
* @param separator
|
93 |
* LSeparator "(" or ")"
|
94 |
* @return
|
95 |
*/
|
96 |
public abstract String getSeparator(int separator); |
97 |
|
98 |
/**
|
99 |
* Adds a feature id
|
100 |
* @param id
|
101 |
* The feature id
|
102 |
*/
|
103 |
public void addFeatureById(Object id){ |
104 |
if (ids == null){ |
105 |
ids = new ArrayList(); |
106 |
} |
107 |
ids.add(id); |
108 |
} |
109 |
|
110 |
/**
|
111 |
* @return the ids
|
112 |
*/
|
113 |
protected ArrayList getIds() { |
114 |
return ids;
|
115 |
} |
116 |
|
117 |
public void setQueryByAttribute(String query){ |
118 |
if (query != null){ |
119 |
ByteArrayInputStream is = new ByteArrayInputStream(query.getBytes()); |
120 |
String sql = formatter.format(query);
|
121 |
if (sql != null){ |
122 |
ParseExpressions expressions = new ParseExpressions();
|
123 |
ArrayList tokens = expressions.parseExpression(sql);
|
124 |
|
125 |
for (int i=0 ; i<tokens.size() ; i++){ |
126 |
String token = (String)tokens.get(i); |
127 |
root.addTerm(token); |
128 |
} |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
/**
|
134 |
* It adds a new property and value using the AND
|
135 |
* operation
|
136 |
* @param propertyName
|
137 |
* @param propertyValue
|
138 |
*/
|
139 |
public void addAndClause(String propertyName, String propertyValue){ |
140 |
root.addTerm(propertyName + " = " + propertyValue,
|
141 |
getLogicalOperator(LOGICAL_OPERATOR_AND)); |
142 |
} |
143 |
|
144 |
public int getSpatialFiltersCount(){ |
145 |
return spatialFilterOperations.size();
|
146 |
} |
147 |
|
148 |
public WFSSpatialFilterOperation getSpatialFilterAt(int index){ |
149 |
if (index < spatialFilterOperations.size()){
|
150 |
return (WFSSpatialFilterOperation)spatialFilterOperations.get(index);
|
151 |
} |
152 |
return null; |
153 |
} |
154 |
|
155 |
public void clearSpatialFilters(){ |
156 |
spatialFilterOperations.clear(); |
157 |
} |
158 |
|
159 |
public void addSpatialFilter(WFSSpatialFilterOperation spatialOperation){ |
160 |
if (spatialOperation != null){ |
161 |
spatialFilterOperations.add(spatialOperation); |
162 |
} |
163 |
} |
164 |
|
165 |
public void addSpatialFilter(Geometry geometry,String attributeName, String nameSpacePrefix, String nameSpaceLocation, String srs, int operation) { |
166 |
addSpatialFilter(new WFSGeometryFilterOperation(geometry, operation, attributeName, nameSpacePrefix, nameSpaceLocation, srs));
|
167 |
} |
168 |
|
169 |
public void addSpatialFilter(String version, Envelope envelope, String attributeName, String nameSpacePrefix, String nameSpaceLocation, String srs, int operation) { |
170 |
addSpatialFilter(new WFSEnvelopeFilterOperation(envelope, operation, attributeName, nameSpacePrefix, nameSpaceLocation, srs));
|
171 |
} |
172 |
|
173 |
public void addClause(String value){ |
174 |
if (currentClause == null){ |
175 |
currentClause = new String(""); |
176 |
} |
177 |
currentClause = currentClause + value; |
178 |
} |
179 |
|
180 |
public String toString(String version){ |
181 |
if (currentClause != null){ |
182 |
setQueryByAttribute(currentClause); |
183 |
} |
184 |
return toString(root, version);
|
185 |
} |
186 |
|
187 |
/**
|
188 |
* Return true if the token is a operator
|
189 |
* @param operator
|
190 |
* @return
|
191 |
*/
|
192 |
public String getOperator(int operator){ |
193 |
if (isLogical(operator)){
|
194 |
return getLogicalOperator(operator);
|
195 |
}else if(isRelational(operator)){ |
196 |
return getRelationalOperator(operator);
|
197 |
}else if(isGeometric(operator)){ |
198 |
return getGeometricOperator(operator);
|
199 |
} |
200 |
return String.valueOf(operator); |
201 |
} |
202 |
|
203 |
public int getRelationalOperator(String operator){ |
204 |
if (operator.equals("=")){ |
205 |
return RELATIONAL_OPERATOR_IS_EQUALS_TO;
|
206 |
}else if(operator.equals("!=")){ |
207 |
return RELATIONAL_OPERATOR_IS_NOT_EQUALS_TO;
|
208 |
}else if(operator.equals("<>")){ // This is another way to tell not-equal |
209 |
return RELATIONAL_OPERATOR_IS_NOT_EQUALS_TO;
|
210 |
}else if(operator.equals(">")){ |
211 |
return RELATIONAL_OPERATOR_IS_GREATER_THAN;
|
212 |
}else if(operator.equals(">=")){ |
213 |
return RELATIONAL_OPERATOR_IS_GREATER_THAN_OR_EQUAL_TO;
|
214 |
}else if(operator.equals("<")){ |
215 |
return RELATIONAL_OPERATOR_IS_LESS_THAN;
|
216 |
}else if(operator.equals("<=")){ |
217 |
return RELATIONAL_OPERATOR_IS_LESS_THAN_OR_EQUAL_TO;
|
218 |
}else if(operator.toUpperCase().equals("LIKE")){ |
219 |
return RELATIONAL_OPERATOR_IS_LIKE;
|
220 |
} |
221 |
return RELATIONAL_OPERATOR_IS_EQUALS_TO;
|
222 |
} |
223 |
|
224 |
public int getLogicalOperator(String operator){ |
225 |
if (operator.toUpperCase().equals("AND")){ |
226 |
return LOGICAL_OPERATOR_AND;
|
227 |
}else if(operator.toUpperCase().equals("NOT")){ |
228 |
return LOGICAL_OPERATOR_NOT;
|
229 |
}else if(operator.toUpperCase().equals("OR")){ |
230 |
return LOGICAL_OPERATOR_OR;
|
231 |
} |
232 |
return LOGICAL_OPERATOR_AND;
|
233 |
} |
234 |
|
235 |
/**
|
236 |
* Return true if is a geometric operator
|
237 |
* @param type
|
238 |
* @return
|
239 |
*/
|
240 |
private boolean isGeometric(int type) { |
241 |
if ((type > 19) && (type < 40)){ |
242 |
return true; |
243 |
} |
244 |
return false; |
245 |
} |
246 |
|
247 |
/**
|
248 |
* Return true if is a relational operator
|
249 |
* @param type
|
250 |
* @return
|
251 |
*/
|
252 |
private boolean isRelational(int type) { |
253 |
if ((type > 39) && (type < 60)){ |
254 |
return true; |
255 |
} |
256 |
return false; |
257 |
} |
258 |
|
259 |
/**
|
260 |
* Return true if is a logical operator
|
261 |
* @param type
|
262 |
* @return
|
263 |
*/
|
264 |
private boolean isLogical(int type){ |
265 |
if ((type > 9) && (type < 20)){ |
266 |
return true; |
267 |
} |
268 |
return false; |
269 |
} |
270 |
|
271 |
|
272 |
|
273 |
/**
|
274 |
* Return true if is a seperator
|
275 |
* @param type
|
276 |
* @return
|
277 |
*/
|
278 |
private boolean isSeparator(int type){ |
279 |
if ((type > 59) && (type < 70)){ |
280 |
return true; |
281 |
} |
282 |
return false; |
283 |
} |
284 |
|
285 |
/**
|
286 |
* Divide a line in a set of words
|
287 |
* @param line
|
288 |
* Line to divide
|
289 |
* @param option
|
290 |
* If the option is EXACT it returns the same line
|
291 |
* @return Iteraror
|
292 |
* A set of words
|
293 |
*/
|
294 |
public Iterator parseValues(String line, int option) { |
295 |
Vector values = new Vector(); |
296 |
|
297 |
if (option == CONCORDANCIA_EXACT) {
|
298 |
values.add(line); |
299 |
return values.iterator();
|
300 |
} |
301 |
|
302 |
StringTokenizer doubleQuotesTokenizer = new StringTokenizer(line, "\"", |
303 |
true);
|
304 |
boolean inside = false; |
305 |
while (doubleQuotesTokenizer.hasMoreTokens()) {
|
306 |
String token = doubleQuotesTokenizer.nextToken();
|
307 |
if (token.equals("\"")) { |
308 |
inside = !inside; |
309 |
} else if (inside) { |
310 |
values.add(token); |
311 |
} else {
|
312 |
StringTokenizer spaceTokenizer = new StringTokenizer(token, " "); |
313 |
while (spaceTokenizer.hasMoreTokens()) {
|
314 |
String value = spaceTokenizer.nextToken();
|
315 |
values.add(value); |
316 |
} |
317 |
} |
318 |
} |
319 |
return values.iterator();
|
320 |
} |
321 |
} |