Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wfs / filters / FilterEncoding.java @ 8528

History | View | Annotate | Download (8.65 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.wfs.filters.BinaryTree.Node;
46

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