Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wfs / wfs_1_0_0 / WFSProtocolHandler1_0_0.java @ 8017

History | View | Annotate | Download (12.2 KB)

1
package org.gvsig.remoteClient.wfs.wfs_1_0_0;
2

    
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileNotFoundException;
7
import java.io.FileReader;
8
import java.io.IOException;
9
import java.util.Hashtable;
10
import java.util.Vector;
11

    
12
import org.gvsig.remoteClient.gml.factories.XMLElementsFactory;
13
import org.gvsig.remoteClient.gml.factories.XMLTypesFactory;
14
import org.gvsig.remoteClient.gml.schemas.IXMLType;
15
import org.gvsig.remoteClient.gml.schemas.XMLComplexType;
16
import org.gvsig.remoteClient.gml.schemas.XMLElement;
17
import org.gvsig.remoteClient.gml.schemas.XMLSchemaParser;
18
import org.gvsig.remoteClient.utils.CapabilitiesTags;
19
import org.gvsig.remoteClient.wfs.WFSProtocolHandler;
20
import org.kxml2.io.KXmlParser;
21
import org.xmlpull.v1.XmlPullParserException;
22

    
23
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
24
 *
25
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
26
 *
27
 * This program is free software; you can redistribute it and/or
28
 * modify it under the terms of the GNU General Public License
29
 * as published by the Free Software Foundation; either version 2
30
 * of the License, or (at your option) any later version.
31
 *
32
 * This program is distributed in the hope that it will be useful,
33
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35
 * GNU General Public License for more details.
36
 *
37
 * You should have received a copy of the GNU General Public License
38
 * along with this program; if not, write to the Free Software
39
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
40
 *
41
 * For more information, contact:
42
 *
43
 *  Generalitat Valenciana
44
 *   Conselleria d'Infraestructures i Transport
45
 *   Av. Blasco Ib??ez, 50
46
 *   46010 VALENCIA
47
 *   SPAIN
48
 *
49
 *      +34 963862235
50
 *   gvsig@gva.es
51
 *      www.gvsig.gva.es
52
 *
53
 *    or
54
 *
55
 *   IVER T.I. S.A
56
 *   Salamanca 50
57
 *   46005 Valencia
58
 *   Spain
59
 *
60
 *   +34 963163400
61
 *   dac@iver.es
62
 */
63
/* CVS MESSAGES:
64
 *
65
 * $Id: WFSProtocolHandler1_0_0.java 8017 2006-10-10 12:52:28Z jorpiell $
66
 * $Log$
67
 * Revision 1.7  2006-10-10 12:52:28  jorpiell
68
 * Soporte para features complejas.
69
 *
70
 * Revision 1.6  2006/06/14 07:54:18  jorpiell
71
 * Se parsea el online resource que antes se ignoraba
72
 *
73
 * Revision 1.5  2006/05/25 10:20:57  jorpiell
74
 * Se ha cambiado el nombre de la clase WFSField por la clase WFSAttribute, porque resultaba confuso
75
 *
76
 * Revision 1.4  2006/05/23 13:23:22  jorpiell
77
 * Se ha cambiado el final del bucle de parseado y se tiene en cuenta el online resource
78
 *
79
 * Revision 1.2  2006/04/20 16:39:16  jorpiell
80
 * A?adida la operacion de describeFeatureType y el parser correspondiente.
81
 *
82
 * Revision 1.1  2006/04/19 12:51:35  jorpiell
83
 * A?adidas algunas de las clases del servicio WFS
84
 *
85
 *
86
 */
87
/**
88
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
89
 */
90
public class WFSProtocolHandler1_0_0 extends WFSProtocolHandler{
91
        public WFSProtocolHandler1_0_0(){
92
                this.version = "1.0.0";
93
                this.name = "WFS1.0.0";                
94
        }        
95
        
96
        public boolean parseCapabilities(File f) {
97
                FileReader reader = null;       
98
                try
99
                {
100
                        reader = new FileReader(f);
101
                        BufferedReader br = new BufferedReader(reader);
102
                        char[] buffer = new char[100];
103
                        br.read(buffer);
104
                        StringBuffer st = new StringBuffer(new String(buffer));
105
                        String searchText = "encoding=\"";
106
                        int index = st.indexOf(searchText);
107
                        if (index>-1) {
108
                                st.delete(0, index+searchText.length());
109
                                encoding = st.substring(0, st.indexOf("\""));
110
                        }
111
                } catch (FileNotFoundException ex) {
112
                        ex.printStackTrace();
113
                } catch (IOException e) {
114
                        e.printStackTrace();
115
                }
116
                
117
                int tag;
118
                KXmlParser parser = null;
119
                parser = new KXmlParser();
120
                try
121
                {
122
                        parser.setInput(new FileInputStream(f), encoding);        
123
                        parser.nextTag();
124
                        
125
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ) 
126
                        {     
127
                                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.WFS_CAPABILITIES_ROOT1_0_0);             
128
                                tag = parser.nextTag();
129
                                while(tag != KXmlParser.END_DOCUMENT)
130
                                {
131
                                        switch(tag)
132
                                        {
133
                                        case KXmlParser.START_TAG:
134
                                                if (parser.getName().compareTo(CapabilitiesTags.SERVICE)==0)
135
                                                {
136
                                                        parseServiceTag(parser);
137
                                                } 
138
                                                else if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
139
                                                {
140
                                                        parseCapabilityTag(parser);
141
                                                } 
142
                                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_FEATURETYPELIST)==0)
143
                                                {
144
                                                        parseFeatureTypeListTag(parser);
145
                                                } 
146
                                                break;
147
                                        case KXmlParser.END_TAG:                            
148
                                                break;
149
                                        case KXmlParser.TEXT:
150
                                                if (parser.getName()!=null)
151
                                                        System.out.println("[TEXT]["+parser.getText().trim()+"]");                         
152
                                                break;
153
                                        }
154
                                        tag = parser.next();
155
                                }
156
                                parser.require(KXmlParser.END_DOCUMENT, null, null);                
157
                        }
158
                }
159
                catch(XmlPullParserException parser_ex){
160
                        System.out.println(parser_ex.getMessage());
161
                        parser_ex.printStackTrace();
162
                        return false;
163
                }
164
                catch (IOException ioe) {           
165
                        ioe.printStackTrace();
166
                        return false;
167
                }
168
                return true;
169
        }
170
        
171
        /**
172
         * <p>Parses the Service Information </p>
173
         */    
174
        private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException 
175
        {
176
                int currentTag;
177
                boolean end = false;
178
                
179
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
180
                currentTag = parser.next();
181
                
182
                while (!end) 
183
                {
184
                        switch(currentTag)
185
                        {
186
                        case KXmlParser.START_TAG:
187
                                if (parser.getName().compareToIgnoreCase(CapabilitiesTags.NAME)==0)
188
                                {
189
                                        serviceInfo.name = parser.nextText(); 
190
                                }   
191
                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_TITLE)==0)
192
                                {
193
                                        serviceInfo.title = parser.nextText(); 
194
                                }
195
                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_ABSTRACT)==0)
196
                                {
197
                                        serviceInfo.abstr = parser.nextText(); 
198
                                } 
199
                                else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
200
                                {
201
                                       serviceInfo.online_resource = parser.nextText();
202
                              
203
                                }                               
204
                                break;
205
                        case KXmlParser.END_TAG:
206
                                if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
207
                                        end = true;
208
                                break;
209
                        case KXmlParser.TEXT:                   
210
                                break;
211
                        }
212
                        if (!end){
213
                                currentTag = parser.next();
214
                        }
215
                }     
216
        }
217
        
218
        private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
219
        {   
220
                int currentTag;
221
                boolean end = false;
222
                
223
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
224
                currentTag = parser.next();
225
                
226
                while (!end) 
227
                {
228
                        switch(currentTag)
229
                        {
230
                        case KXmlParser.START_TAG:
231
                                if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
232
                                {
233
                                        parseRequestTag(parser); 
234
                                }   
235
                                else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
236
                                {
237
                                        //TODO Parse exception tags...
238
                                        parser.skipSubTree();
239
                                }
240
                                else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
241
                                                (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
242
                                {
243
                                        parser.skipSubTree();
244
                                }                   
245
                                break;
246
                        case KXmlParser.END_TAG:
247
                                if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
248
                                        end = true;
249
                                break;
250
                        case KXmlParser.TEXT:                   
251
                                break;
252
                        }
253
                        if (!end){
254
                                currentTag = parser.next();
255
                        }
256
                }
257
        }
258
        
259
        /**
260
         * <p>Parses the Request tag </p>
261
         */ 
262
        private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
263
        {   
264
                int currentTag;
265
                boolean end = false;
266
                
267
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
268
                currentTag = parser.next();
269
                
270
                while (!end) 
271
                {
272
                        switch(currentTag)
273
                        {
274
                        case KXmlParser.START_TAG:
275
                                if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
276
                                {
277
                                        currentTag = parser.nextTag();
278
                                        if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
279
                                        {                        
280
                                                currentTag = parser.nextTag();
281
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
282
                                                {
283
                                                        currentTag = parser.nextTag();
284
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
285
                                                        {
286
                                                                String value = null;
287
                                                                for (int i=0 ; i<parser.getAttributeCount() ; i++){
288
                                                                    if (parser.getAttributeName(i).toUpperCase().compareTo(CapabilitiesTags.ONLINERESOURCE.toUpperCase()) == 0){
289
                                                                            value = parser.getAttributeValue(i);
290
                                                                    }
291
                                                                }                                                                
292
                                                                if (value == null){
293
                                                                        serviceInfo.addOperation(WFSOperation.GETCAPABILITIES);
294
                                                                }else{
295
                                                                        serviceInfo.addOperation(WFSOperation.GETCAPABILITIES,value);
296
                                                                }                                                                                                                
297
                                                        }
298
                                                }
299
                                        }        
300
                                        
301
                                }   
302
                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_DESCRIBEFEATURETYPE)==0)
303
                                {
304
                                        currentTag = parser.nextTag();
305
                                        if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
306
                                        {                        
307
                                                currentTag = parser.nextTag();
308
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
309
                                                {
310
                                                        currentTag = parser.nextTag();
311
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
312
                                                        {
313
                                                                String value = null;
314
                                                                for (int i=0 ; i<parser.getAttributeCount() ; i++){
315
                                                                    if (parser.getAttributeName(i).toUpperCase().compareTo(CapabilitiesTags.ONLINERESOURCE.toUpperCase()) == 0){
316
                                                                            value = parser.getAttributeValue(i);
317
                                                                    }
318
                                                                }                                                                
319
                                                                if (value == null){
320
                                                                        serviceInfo.addOperation(WFSOperation.DESCRIBEFEATURETYPE);
321
                                                                }else{
322
                                                                        serviceInfo.addOperation(WFSOperation.DESCRIBEFEATURETYPE,value);
323
                                                                }                                                
324
                                                        }
325
                                                }
326
                                        }                                                                                
327
                                }
328
                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_GETFEATURE)==0)
329
                                {
330
                                        currentTag = parser.nextTag();
331
                                        if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
332
                                        {                        
333
                                                currentTag = parser.nextTag();
334
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
335
                                                {
336
                                                        currentTag = parser.nextTag();
337
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
338
                                                        {
339
                                                                String value = null;
340
                                                                for (int i=0 ; i<parser.getAttributeCount() ; i++){
341
                                                                    if (parser.getAttributeName(i).toUpperCase().compareTo(CapabilitiesTags.ONLINERESOURCE.toUpperCase()) == 0){
342
                                                                            value = parser.getAttributeValue(i);
343
                                                                    }
344
                                                                }                                                                
345
                                                                if (value == null){
346
                                                                        serviceInfo.addOperation(WFSOperation.GETFEATURE);
347
                                                                }else{
348
                                                                        serviceInfo.addOperation(WFSOperation.GETFEATURE,value);
349
                                                                }                        
350
                                                        }
351
                                                }
352
                                        }        
353
                                }               
354
                                break;
355
                        case KXmlParser.END_TAG:
356
                                if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
357
                                        end = true;
358
                                break;
359
                        case KXmlParser.TEXT:                   
360
                                break;
361
                        }
362
                        if (!end){
363
                                currentTag = parser.next();
364
                        }
365
                }
366
                // TODO: does not get such a tag when arrives here!!!!!!
367
                //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);       
368
        }
369
        
370
        
371
        private void parseFeatureTypeListTag(KXmlParser parser) throws IOException, XmlPullParserException
372
        {
373
                int currentTag;
374
                boolean end = false;
375
                
376
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.WFS_FEATURETYPELIST);
377
                currentTag = parser.next();
378
                
379
                while (!end) 
380
                {
381
                        switch(currentTag)
382
                        {
383
                        case KXmlParser.START_TAG:
384
                                if (parser.getName().compareToIgnoreCase(CapabilitiesTags.WFS_FEATURETYPE)==0)
385
                                {
386
                                        WFSFeature1_0_0 feature = new WFSFeature1_0_0();
387
                                        feature.parse(parser);        
388
                                        features.put(feature.getName(),feature);
389
                                }                                          
390
                                break;
391
                        case KXmlParser.END_TAG:
392
                                if (parser.getName().compareTo(CapabilitiesTags.WFS_FEATURETYPELIST) == 0)
393
                                        end = true;
394
                                break;
395
                        case KXmlParser.TEXT:                   
396
                                break;
397
                        }
398
                        if (!end){
399
                                currentTag = parser.next();
400
                        }
401
                }     
402
        }        
403
        
404
        public boolean parseDescribeFeatureType(File f,String nameSpace) {
405
                XMLSchemaParser schemaParser = new XMLSchemaParser();
406
                schemaParser.parse(f,nameSpace);
407
                
408
                String layerName = getCurrentFeature();
409
                if (getCurrentFeature().split(":").length>1){
410
                        layerName = getCurrentFeature().split(":")[1];
411
                }
412
                XMLElement entity = XMLElementsFactory.getElement(layerName);
413
                if (entity != null){
414
                        IXMLType type = entity.getEntityType();
415
                        if ((type != null) && (type instanceof XMLComplexType)){
416
                                setFields(((XMLComplexType)type).getAttributes());
417
                        }
418
                }
419
                return true;
420
        }
421
        
422
}