Statistics
| Revision:

root / branches / v10 / applications / appCatalogAndGazetteerClient / src / es / gva / cit / catalog / languages / FilterEncoding.java @ 16805

History | View | Annotate | Download (10.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 es.gva.cit.catalog.languages;
43
import java.util.Iterator;
44

    
45
import es.gva.cit.catalog.querys.Coordinates;
46

    
47
/**
48
 * This class implements the Filter Encoding Language. It is used to
49
 * create queries in this language
50
 * @author Jorge Piera Llodra (jorge.piera@iver.es)
51
 * @see http://portal.opengeospatial.org/files/?artifact_id=8340
52
 */
53
public class FilterEncoding extends AbstractGeneralLanguage {
54
        //Properties
55
        public static final String PROPERTY_IS_LIKE = "PropertyIsLike";
56
        public static final String PROPERTY_IS_LESS = "PropertyIsLess";
57
        public static final String PROPERTY_IS_GREATER = "PropertyIsGreater";
58
        public static final String PROPERTY_IS_GREATER_THAN = "PropertyIsGreaterThan";
59
        public static final String PROPERTY_IS_LESS_THAN = "PropertyIsLessThan";
60
        public static final String PROPERTY_IS_EQUALS_TO = "PropertyIsEqualTo";
61
        //Type options
62
        public static final String TYPE_LITERAL = "Literal";
63
        public static final String TYPE_TWO_PROPERTIES = "PropertyName";        
64
        //Default values
65
        public static final String DEFAULT_PREFIX = "ogc";
66
        public static final String DEFAULT_WILDCARD = "*";
67
        public static final String DEFAULT_SINGLECHAR = "?";
68
        public static final String DEFAULT_ESCAPE = "\\";
69
        public static final String DEFAULT_NAMESPACE = "xmlns:ogc=\"http://www.opengis.net/ogc\"";
70
        //Private labels
71
        private static final String FILTER = "Filter"; 
72

    
73
        private String prefix = null;
74
        private String wildCard = null;
75
        private String singleChar = null;
76
        private String escape = null;
77
        private String namespace = null;
78
        private String wildCardLabel = "wildCard";
79
        private String escapeCharLabel = "escapeChar";
80
        private String singleCharLabel = "singleChar";
81

    
82
        /**
83
         * Create a new Filter Encoding Parser
84
         * @param prefix Prefix of the labels (if its necessary). 
85
         * Typically "ogc".
86
         * @param wildCard It Depends of the server
87
         * @param singleChar It Depends of the server
88
         * @param escape It Depends of the server
89
         */
90
        public  FilterEncoding(String prefix, String wildCard, String singleChar, String escape) {        
91
                this.prefix = prefix + ":";
92
                this.wildCard = wildCard;
93
                this.singleChar = singleChar;
94
                this.escape = escape;                
95
        } 
96

    
97
        /**
98
         * Create a new Filter Encoding Parser with the 
99
         * deafault values
100
         */
101
        public  FilterEncoding() {        
102
                this.prefix = DEFAULT_PREFIX + ":";
103
                this.wildCard = DEFAULT_WILDCARD;
104
                this.singleChar = DEFAULT_SINGLECHAR;
105
                this.escape = DEFAULT_ESCAPE;                
106
        } 
107

    
108
        /**
109
         * It Adds a new clause of the query
110
         * @param propertyName The property name
111
         * @param propertyValue The property value
112
         * @param concordancia "E" (Exact phrase), "A" (All words)
113
         * or "Y" (anY word).
114
         * @param relationship PropertyIsLike, PropertyIsLess, PropertyIsGreater,... See the File encoding
115
         * Documentation.
116
         * @param type Values: "P" (to comparate two propertyes) or "L" (to comparate one property
117
         * and one literal value)
118
         * @param operator "And" or "Or". Operator between fields
119
         */
120
        public void addClauses(String propertyName, String propertyValue, 
121
                        String concordancia, String relationship, String type, 
122
                        String operator) {        
123
                currentClause = null;
124
                //Seperating the words
125
                Iterator values = parseValues(propertyValue, concordancia, relationship, wildCard);
126
                //Filling the words
127
                addClauses(propertyName, values, concordancia, relationship, type, operator);
128
        } 
129

    
130
        /**
131
         * It Adds a new clause of the query
132
         * @param propertyName The property name
133
         * @param propertyValue The property value
134
         * @param concordancia "E" (Exact phrase), "A" (All words)
135
         * or "Y" (anY word).
136
         */
137
        public void addClauses(String propertyName, String propertyValue, 
138
                        String concordancia) {           
139
                addClauses(propertyName, propertyValue, concordancia,
140
                                FilterEncoding.PROPERTY_IS_LIKE, 
141
                                FilterEncoding.TYPE_LITERAL,
142
                                FilterEncoding.AND);
143
        } 
144

    
145
        /**
146
         * It Adds a new clause of the query
147
         * @param propertyName The property name
148
         * @param propertyValues The property value separated by blank spaces
149
         * @param concordancia "E" (Exact phrase), "A" (All words)
150
         * or "Y" (anY word).
151
         * @param relationship PropertyIsLike, PropertyIsLess, PropertyIsGreater,... See the File encoding
152
         * Documentation.
153
         * @param type Values: "P" (to comparate two propertyes) or "L" (to comparate one property
154
         * and one literal value)
155
         * @param operator "And" or "Or". Operator between fields
156
         */
157
        public void addClauses(String propertyName, Iterator propertyValues, 
158
                        String concordancia, String relationship, String type,
159
                        String operator) {        
160
                while (propertyValues.hasNext())
161
                        addTerm(propertyName, (String) propertyValues.next(), concordancia,
162
                                        relationship, type);
163
                addCurrentClauseQuery(operator);
164
        } 
165

    
166
        /**
167
         * It adds a new term to the full query
168
         * @param propertyName The property name
169
         * @param propertyValue The property value
170
         * @param concordancia "E" (Exact phrase), "A" (All words) or "Y" (anY word).
171
         * @param relationship PropertyIsLike, PropertyIsLess, PropertyIsGreater,... See the File encoding
172
         * Documentation.
173
         * @param type Values: "P" (to comparate two propertyes) or "L" (to comparate one property
174
         * and one literal value)
175
         */
176
        private void addTerm(String propertyName, String propertyValue, 
177
                        String concordancia, String relationship, String type) {        
178
                StringBuffer term = new StringBuffer();
179
                term.append(propertyIsXXX(relationship, propertyName, propertyValue, type));
180
                if (currentClause == null) {
181
                        currentClause = term.toString();
182
                } else {
183
                        currentClause = currentClause + term.toString();
184
                        currentClause = enterLabel(currentClause, getOperator(concordancia));
185
                }
186
        } 
187

    
188
        /**
189
         * It adds the "and" label to join different operations
190
         * @param operator 
191
         */
192
        protected void addCurrentClauseQuery(String operator) {        
193
                if (currentClause != null) {
194
                        if (currentQuery == null) {
195
                                currentQuery = currentClause;
196
                        } else {
197
                                currentQuery = currentQuery + currentClause;
198
                                currentQuery = enterLabel(currentQuery, operator);
199
                        }
200
                }
201
        } 
202

    
203
        /**
204
         * It returns the encoded query
205
         * @return 
206
         */
207
        public String toString() {        
208
                return enterLabel(currentQuery, FILTER);
209
        } 
210

    
211
        /**
212
         * Involves a query with a label
213
         * @param query Query to involve
214
         * @param label Label name
215
         * @return a filter encoding query
216
         */
217
        private String enterLabel(String query, String label) {        
218
                if (label.equals(FILTER) && (this.namespace != null)) {
219
                        return "<" + prefix + label + " " + this.namespace + ">" +
220
                        query + "</" + prefix + label + ">";
221
                } else {
222
                        return "<" + prefix + label + ">" + query + "</" + prefix +
223
                        label + ">";
224
                }
225
        } 
226

    
227
        /**
228
         * It writes a "PropertyIsXXX" part of a filter encoding query
229
         * @param relationship Possible Values: PropertIsLike, PropertyIsLess,
230
         * PropertyIsGreater,... See the Filter Encoding documentation
231
         * @param propertyName The property name
232
         * @param propertyValue The property value
233
         * @param type Values: "P" (to comparate two propertyes) or "L" (to comparate one property
234
         * and one literal value)
235
         * @return The part of the query
236
         */
237
        private String propertyIsXXX(String relationship, String propertyName,
238
                        String propertyValue, String type) {        
239
                String cadena = "";
240
                cadena = "<" + prefix + relationship;
241
                if (relationship.equals("PropertyIsLike")) {
242
                        if (this.wildCard != null) {
243
                                cadena = cadena + " " + getWildCardLabel() + "=\"" + this.wildCard + "\"";
244
                        }
245
                        if (this.singleChar != null) {
246
                                cadena = cadena + " " + getSingleCharLabel() + "=\"" + this.singleChar + "\"";
247
                        }
248
                        if (this.escape != null) {
249
                                cadena = cadena + " " + getEscapeCharLabel() + "=\"" + this.escape + "\"";
250
                        }
251
                }
252
                cadena = cadena + ">" + enterLabel(propertyName, TYPE_TWO_PROPERTIES);
253
                cadena = cadena + enterLabel(propertyValue, type);                
254
                return cadena + "</" + prefix + relationship + ">";
255
        } 
256

    
257
        /**
258
         * It Adds a Bounding Box query
259
         * 
260
         * 
261
         * @param coordinates Coordinates to find
262
         * @param propertyName Property that contains the geom field
263
         * @param not If we have to envolve the query with the "NOT" tag.
264
         */
265
        public void addBoundingBox(Coordinates coordinates, String propertyName, boolean not) {        
266
                // sNorth -> Uly();
267
                // sWest -> Ulx();
268
                // sSouth -> Bry();
269
                // sEast -> Brx();
270
                String bbox = "<ogc:BBOX>" + "<ogc:PropertyName>" + propertyName +
271
                "</ogc:PropertyName>" + "<gml:Box>" + "<gml:coord>" + "<gml:X>" +
272
                coordinates.ulx + "</gml:X>" + "<gml:Y>" + coordinates.bry +
273
                "</gml:Y>" + "</gml:coord>" + "<gml:coord>" + "<gml:X>" +
274
                coordinates.brx + "</gml:X>" + "<gml:Y>" + coordinates.uly +
275
                "</gml:Y>" + "</gml:coord>" + "</gml:Box>" + "</ogc:BBOX>";
276
                if (not){
277
                        bbox = "<ogc:Not>" + bbox + "</ogc:Not>"; 
278
                }
279
                if (currentQuery == null) {
280
                        currentQuery = bbox;
281
                } else {
282
                        currentQuery = currentQuery + bbox;
283
                        currentQuery = enterLabel(currentQuery, "And");
284
                }
285
        }
286

    
287
        /**
288
         * @return the wildCard
289
         */
290
        public String getWildCard() {
291
                return wildCard;
292
        }
293

    
294
        /**
295
         * @return the wildCardLabel
296
         */
297
        public String getWildCardLabel() {
298
                return wildCardLabel;
299
        }
300

    
301
        /**
302
         * @param wildCardLabel the wildCardLabel to set
303
         */
304
        public void setWildCardLabel(String wildCardLabel) {
305
                this.wildCardLabel = wildCardLabel;
306
        }
307

    
308
        /**
309
         * @return the escapeCharLabel
310
         */
311
        public String getEscapeCharLabel() {
312
                return escapeCharLabel;
313
        }
314

    
315
        /**
316
         * @param escapeCharLabel the escapeCharLabel to set
317
         */
318
        public void setEscapeCharLabel(String escapeCharLabel) {
319
                this.escapeCharLabel = escapeCharLabel;
320
        }
321

    
322
        /**
323
         * @return the singleCharLabel
324
         */
325
        public String getSingleCharLabel() {
326
                return singleCharLabel;
327
        }
328

    
329
        /**
330
         * @param singleCharLabel the singleCharLabel to set
331
         */
332
        public void setSingleCharLabel(String singleCharLabel) {
333
                this.singleCharLabel = singleCharLabel;
334
        } 
335
}