svn-gvsig-desktop / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wfs / filters / FilterEncoding.java @ 10563
History | View | Annotate | Download (10.4 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.util.Hashtable; |
44 |
|
45 |
import org.gvsig.remoteClient.gml.utils.GMLObjectsFactory; |
46 |
import org.gvsig.remoteClient.wfs.filters.BinaryTree.Node; |
47 |
|
48 |
import sun.security.action.GetBooleanAction; |
49 |
|
50 |
/**
|
51 |
* This class implements the Filter Encoding Language. It is used to
|
52 |
* create querys in this language
|
53 |
*
|
54 |
* Name: OpenGIS? Filter Encoding Implementation Specification
|
55 |
* Version: 1.1.0
|
56 |
* Project Document: OGC 04-095
|
57 |
* @see http://portal.opengeospatial.org/files/?artifact_id=8340
|
58 |
*
|
59 |
* @author Jorge Piera Llodra (piera_jor@gva.es)
|
60 |
*/
|
61 |
public class FilterEncoding extends AFilter { |
62 |
public static final int RELATIONSHIP_PROPERTY = 0; |
63 |
public static final int RELATIONSHIP_VAUES = 1; |
64 |
|
65 |
private StringBuffer currentQuery = null; |
66 |
|
67 |
/**
|
68 |
* Operation types
|
69 |
*/
|
70 |
private static final int OPERATION_PROPERTYNAME = 0; |
71 |
private static final int OPERATION_LITERAL = 1; |
72 |
|
73 |
/**
|
74 |
* Filter namespace. (typically "ogc")
|
75 |
*/
|
76 |
private String nameSpace = null; |
77 |
|
78 |
/**
|
79 |
* This character must be replaced by any set of characters (typically "*")
|
80 |
*/
|
81 |
private String wildCardChar = null; |
82 |
|
83 |
/**
|
84 |
* This character must be replaced by one character (typically "?")
|
85 |
*/
|
86 |
private String singleChar = null; |
87 |
|
88 |
/**
|
89 |
* Escape character (typically "\")
|
90 |
*/
|
91 |
private String escapeChar = null; |
92 |
|
93 |
private Hashtable filterAttributes = new Hashtable(); |
94 |
|
95 |
|
96 |
/**
|
97 |
* Create a new Filter Encoding Parser
|
98 |
*
|
99 |
*
|
100 |
* @param nameSpace
|
101 |
* Filter namespace. (typically "ogc")
|
102 |
* @param wildCardChar
|
103 |
* This character must be replaced by any set of characters (typically "*")
|
104 |
* @param singleChar
|
105 |
* This character must be replaced by one character (typically "?")
|
106 |
* @param escape
|
107 |
* Escape character
|
108 |
* @param filterAttribute Sometimes, "Field" label needs an attribute.
|
109 |
*/
|
110 |
public FilterEncoding(ISQLExpressionFormat formatter,String prefix, String wildCard, String singleChar, String escape, Hashtable filterAttributes) { |
111 |
super(formatter);
|
112 |
if (prefix == null){ |
113 |
this.nameSpace = ""; |
114 |
}else{
|
115 |
this.nameSpace = prefix + ":"; |
116 |
} |
117 |
this.wildCardChar = wildCard;
|
118 |
this.singleChar = singleChar;
|
119 |
this.escapeChar = escape;
|
120 |
this.filterAttributes = filterAttributes;
|
121 |
} |
122 |
|
123 |
|
124 |
/**
|
125 |
* Create a new Filter Encoding Parser
|
126 |
* @param formatter
|
127 |
*/
|
128 |
public FilterEncoding(ISQLExpressionFormat formatter) {
|
129 |
super(formatter);
|
130 |
this.nameSpace = ""; |
131 |
this.wildCardChar = "*"; |
132 |
this.singleChar = "?"; |
133 |
this.escapeChar = "\\"; |
134 |
this.filterAttributes = new Hashtable(); |
135 |
} |
136 |
|
137 |
/*
|
138 |
* (non-Javadoc)
|
139 |
* @see org.gvsig.remoteClient.filterEncoding.QueryLanguage#toString(org.gvsig.remoteClient.filterEncoding.BinaryTree)
|
140 |
*/
|
141 |
protected String toString(BinaryTree tree) { |
142 |
if ((tree.getRoot() == null) && (getBbox() == null)){ |
143 |
return null; |
144 |
} |
145 |
String filterQuery = null; |
146 |
String bboxQuery = null; |
147 |
if (tree.getRoot() != null){ |
148 |
currentQuery = new StringBuffer(); |
149 |
filterQuery = getFilterNode(tree.getRoot()); |
150 |
if (getBbox() == null){ |
151 |
return enclosesWithFilterTag(filterQuery);
|
152 |
} |
153 |
} |
154 |
if (getBbox() != null){ |
155 |
bboxQuery = enterLabel(getBboxPropertyName(), "PropertyName");
|
156 |
bboxQuery = bboxQuery + new GMLObjectsFactory().createBBOX(getBbox());
|
157 |
if (tree.getRoot() == null){ |
158 |
return enclosesWithFilterTag(bboxQuery);
|
159 |
} |
160 |
} |
161 |
//filterQuery = enterLabel(filterQuery, "Within");
|
162 |
return enclosesWithFilterTag(filterQuery);
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* Gets the filter code from a node
|
167 |
* @param node
|
168 |
*/
|
169 |
private String getFilterNode(Node node){ |
170 |
if (node.isField()){
|
171 |
return getExpression(node.getValue());
|
172 |
}else{
|
173 |
String left = ""; |
174 |
String rigth = ""; |
175 |
if (node.getLeftNode() != null){ |
176 |
left = getFilterNode(node.getLeftNode()); |
177 |
} |
178 |
if (node.getRigthNode() != null){ |
179 |
rigth = getFilterNode(node.getRigthNode()); |
180 |
} |
181 |
int operationCode = getLogicalOperator(node.getValue());
|
182 |
String operation = getLogicalOperator(operationCode);
|
183 |
return enterLabel(left+rigth, operation);
|
184 |
} |
185 |
} |
186 |
|
187 |
/**
|
188 |
* Parses a expresion like 'A op B' and returns the
|
189 |
* expresion using the filter encoding language
|
190 |
* @param expression
|
191 |
* @return
|
192 |
*/
|
193 |
private String getExpression(String expression){ |
194 |
String[] words = expression.split(" "); |
195 |
//Param
|
196 |
String param = words[0]; |
197 |
if (param.charAt(0) == '"'){ |
198 |
param = param.substring(1,param.length());
|
199 |
} |
200 |
if (param.charAt(param.length()-1) == '"'){ |
201 |
param = param.substring(0,param.length()-1); |
202 |
} |
203 |
//Operator
|
204 |
String operator = words[1]; |
205 |
//Value
|
206 |
String value = words[2]; |
207 |
for (int i=3 ; i<words.length ; i++){ |
208 |
value = value + " " + words[i];
|
209 |
} |
210 |
if (value.charAt(0) == '\''){ |
211 |
value = value.substring(1,value.length());
|
212 |
} |
213 |
if (value.charAt(value.length()-1) == '\''){ |
214 |
value = value.substring(0,value.length()-1); |
215 |
} |
216 |
int operatorCode = getRelationalOperator(operator);
|
217 |
operator = getRelationalOperator(operatorCode); |
218 |
return createExpression(operator,param,value);
|
219 |
} |
220 |
|
221 |
/**
|
222 |
* It writes a "PropertyIsXXX" part of a filtyer encoding query
|
223 |
*
|
224 |
*
|
225 |
* @return The part of the query
|
226 |
* @param property Possible Values: PropertIsLike, PropertyIsLess, PropertyIsGreater,... See
|
227 |
* the Filter Encoding documentation
|
228 |
* @param parameter Parameter name
|
229 |
* @param value Parameter value
|
230 |
* @param type Values: "P" (to comparate two propertyes) or "L" (to comparate one property
|
231 |
* and one literal value)
|
232 |
*/
|
233 |
private String createExpression(String property, String parameter, String value) { |
234 |
String cadena = ""; |
235 |
cadena = "<" + nameSpace + property;
|
236 |
if (property.equals("PropertyIsLike")) { |
237 |
if (wildCardChar != null) { |
238 |
cadena = cadena + " wildCard=\"" + this.wildCardChar + "\""; |
239 |
} |
240 |
if (singleChar != null) { |
241 |
cadena = cadena + " singleChar=\"" + this.singleChar + "\""; |
242 |
} |
243 |
if (escapeChar != null) { |
244 |
cadena = cadena + " escape=\"" + this.escapeChar + "\""; |
245 |
} |
246 |
} |
247 |
cadena = cadena + ">" + enterLabel(parameter, "PropertyName"); |
248 |
cadena = cadena + enterLabel(value, "Literal");
|
249 |
return cadena + "</" + nameSpace + property + ">"; |
250 |
} |
251 |
|
252 |
/**
|
253 |
* Envuelve a una pregunta con una etiqueta
|
254 |
*
|
255 |
*
|
256 |
* @return String : parte de la query en el lenguaje soportado
|
257 |
* @param pregunta Pregunta a envolver
|
258 |
* @param etiqueta Nombre de la etiqueta
|
259 |
*/
|
260 |
private String enterLabel(String value, String tagName) { |
261 |
if (tagName.equals("Filter") && (!(filterAttributes.isEmpty()))) { |
262 |
return setTag(tagName,filterAttributes,value);
|
263 |
} else {
|
264 |
return setTag(tagName,null,value); |
265 |
} |
266 |
} |
267 |
|
268 |
/**
|
269 |
* Encloses a query with the filter tag
|
270 |
* @param query
|
271 |
* @return
|
272 |
*/
|
273 |
private String enclosesWithFilterTag(String query){ |
274 |
StringBuffer filter = new StringBuffer(); |
275 |
filter.append("<Filter%20");
|
276 |
filter.append("xmlns:ogc=\"http://www.opengis.net/ogc\"%20");
|
277 |
filter.append("xmlns:gml=\"http://www.opengis.net/gml\"%20");
|
278 |
filter.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
|
279 |
filter.append("xsi:schemaLocation=\"http://www.opengis.net/ogc%20../filter/1.0.0/filter.xsd%20http://www.opengis.net/gml%20../gml/2.1.2/geometry.xsd\">");
|
280 |
filter.append(query); |
281 |
filter.append("</Filter>");
|
282 |
return filter.toString();
|
283 |
} |
284 |
|
285 |
/*
|
286 |
* (non-Javadoc)
|
287 |
* @see org.gvsig.remoteClient.filterEncoding.AQueryLanguage#getLogicOperator(int)
|
288 |
*/
|
289 |
public String getLogicalOperator(int operator) { |
290 |
switch (operator){
|
291 |
case LOGICAL_OPERATOR_AND:
|
292 |
return "And"; |
293 |
case LOGICAL_OPERATOR_OR:
|
294 |
return "Or"; |
295 |
case LOGICAL_OPERATOR_NOT:
|
296 |
return "Not"; |
297 |
default:
|
298 |
return "And"; |
299 |
} |
300 |
} |
301 |
|
302 |
/*
|
303 |
* (non-Javadoc)
|
304 |
* @see org.gvsig.remoteClient.filterEncoding.AQueryLanguage#getRelationalOperator(int)
|
305 |
*/
|
306 |
public String getRelationalOperator(int operator) { |
307 |
switch (operator){
|
308 |
case RELATIONAL_OPERATOR_IS_EQUALS_TO:
|
309 |
return "PropertyIsEqualTo"; |
310 |
case RELATIONAL_OPERATOR_IS_NOT_EQUALS_TO:
|
311 |
return "PropertyIsNotEqualTo"; |
312 |
case RELATIONAL_OPERATOR_IS_LESS_THAN:
|
313 |
return "PropertyIsLessThan"; |
314 |
case RELATIONAL_OPERATOR_IS_GREATER_THAN:
|
315 |
return "PropertyIsGreaterThan"; |
316 |
case RELATIONAL_OPERATOR_IS_LESS_THAN_OR_EQUAL_TO:
|
317 |
return "PropertyIsLessThanOrEqualTo"; |
318 |
case RELATIONAL_OPERATOR_IS_GREATER_THAN_OR_EQUAL_TO:
|
319 |
return "PropertyIsGreaterThanOrEqualTo"; |
320 |
case RELATIONAL_OPERATOR_IS_LIKE:
|
321 |
return "PropertyIsLike"; |
322 |
case RELATIONAL_OPERATOR_IS_NULL:
|
323 |
return "PropertyIsNull"; |
324 |
case RELATIONAL_OPERATOR_IS_BETWEEN:
|
325 |
return "PropertyIsBetween"; |
326 |
default:
|
327 |
return "PropertyIsLike"; |
328 |
} |
329 |
} |
330 |
|
331 |
/*
|
332 |
* (non-Javadoc)
|
333 |
* @see org.gvsig.remoteClient.filterEncoding.AQueryLanguage#getGeometricOperator(int)
|
334 |
*/
|
335 |
public String getGeometricOperator(int operator) { |
336 |
switch (operator){
|
337 |
case GEOMETRIC_OPERATOR_EQUALS:
|
338 |
return "Equals"; |
339 |
case GEOMETRIC_OPERATOR_DISJOINT:
|
340 |
return "Disjoint"; |
341 |
case GEOMETRIC_OPERATOR_TOUCHES:
|
342 |
return "Touches"; |
343 |
case GEOMETRIC_OPERATOR_WITHIN:
|
344 |
return "Within"; |
345 |
case GEOMETRIC_OPERATOR_OVERLAPS:
|
346 |
return "Overlaps"; |
347 |
case GEOMETRIC_OPERATOR_CROSSES:
|
348 |
return "Crosses"; |
349 |
case GEOMETRIC_OPERATOR_INTERSECT:
|
350 |
return "Intersect"; |
351 |
case GEOMETRIC_OPERATOR_CONTAINS:
|
352 |
return "Contains"; |
353 |
case GEOMETRIC_OPERATOR_DWITHIN:
|
354 |
return "Dwithin"; |
355 |
case GEOMETRIC_OPERATOR_BEYOND:
|
356 |
return "Beyond"; |
357 |
case GEOMETRIC_OPERATOR_BBOX:
|
358 |
return "BBOX"; |
359 |
default:
|
360 |
return "Equals"; |
361 |
} |
362 |
|
363 |
} |
364 |
|
365 |
public String getSeparator(int separator) { |
366 |
return null; |
367 |
} |
368 |
|
369 |
|
370 |
} |