Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wfs / wfs_1_0_0 / WFSProtocolHandler1_0_0.java @ 14705

History | View | Annotate | Download (13.8 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.Vector;
10

    
11
import org.gvsig.exceptions.BaseException;
12
import org.gvsig.remoteClient.gml.factories.XMLElementsFactory;
13
import org.gvsig.remoteClient.gml.schemas.XMLElement;
14
import org.gvsig.remoteClient.gml.schemas.XMLSchemaParser;
15
import org.gvsig.remoteClient.utils.CapabilitiesTags;
16
import org.gvsig.remoteClient.utils.EncodingXMLParser;
17
import org.gvsig.remoteClient.wfs.WFSOperation;
18
import org.gvsig.remoteClient.wfs.WFSProtocolHandler;
19
import org.gvsig.remoteClient.wfs.exceptions.ExceptionsFactory;
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 14705 2007-10-17 14:35:10Z jpiera $
66
 * $Log$
67
 * Revision 1.12  2007-02-19 11:43:10  jorpiell
68
 * A?adidos los filtros por ?rea
69
 *
70
 * Revision 1.11  2007/02/09 14:11:01  jorpiell
71
 * Primer piloto del soporte para WFS 1.1 y para WFS-T
72
 *
73
 * Revision 1.10  2007/01/16 08:30:22  csanchez
74
 * Sistema de Warnings y Excepciones adaptado a BasicException
75
 *
76
 * Revision 1.9  2006/12/22 11:31:01  jorpiell
77
 * Cambio del parser 2.x
78
 *
79
 * Revision 1.8  2006/10/31 09:36:51  jorpiell
80
 * Se devuelve el tipo de la entidad completo, y no sus hijos
81
 *
82
 * Revision 1.7  2006/10/10 12:52:28  jorpiell
83
 * Soporte para features complejas.
84
 *
85
 * Revision 1.6  2006/06/14 07:54:18  jorpiell
86
 * Se parsea el online resource que antes se ignoraba
87
 *
88
 * Revision 1.5  2006/05/25 10:20:57  jorpiell
89
 * Se ha cambiado el nombre de la clase WFSField por la clase WFSAttribute, porque resultaba confuso
90
 *
91
 * Revision 1.4  2006/05/23 13:23:22  jorpiell
92
 * Se ha cambiado el final del bucle de parseado y se tiene en cuenta el online resource
93
 *
94
 * Revision 1.2  2006/04/20 16:39:16  jorpiell
95
 * A?adida la operacion de describeFeatureType y el parser correspondiente.
96
 *
97
 * Revision 1.1  2006/04/19 12:51:35  jorpiell
98
 * A?adidas algunas de las clases del servicio WFS
99
 *
100
 *
101
 */
102
/**
103
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
104
 */
105
public class WFSProtocolHandler1_0_0 extends WFSProtocolHandler{
106
        public WFSProtocolHandler1_0_0(){
107
                this.version = "1.0.0";
108
                this.name = "WFS1.0.0";                
109
        }        
110
        
111
        public boolean parseCapabilities(File f) {
112
                int tag;
113
            EncodingXMLParser parser = null;
114
              parser = new EncodingXMLParser();
115
            try
116
            {
117
                    parser.setInput(f);
118
                    parser.nextTag();
119
                        
120
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ) 
121
                        {     
122
                                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.WFS_CAPABILITIES_ROOT1_0_0);             
123
                                tag = parser.nextTag();
124
                                while(tag != KXmlParser.END_DOCUMENT)
125
                                {
126
                                        switch(tag)
127
                                        {
128
                                        case KXmlParser.START_TAG:
129
                                                if (parser.getName().compareTo(CapabilitiesTags.SERVICE)==0)
130
                                                {
131
                                                        parseServiceTag(parser);
132
                                                } 
133
                                                else if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
134
                                                {
135
                                                        parseCapabilityTag(parser);
136
                                                } 
137
                                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_FEATURETYPELIST)==0)
138
                                                {
139
                                                        parseFeatureTypeListTag(parser);
140
                                                } 
141
                                                break;
142
                                        case KXmlParser.END_TAG:                            
143
                                                break;
144
                                        case KXmlParser.TEXT:
145
                                                if (parser.getName()!=null)
146
                                                        System.out.println("[TEXT]["+parser.getText().trim()+"]");                         
147
                                                break;
148
                                        }
149
                                        tag = parser.next();
150
                                }
151
                                parser.require(KXmlParser.END_DOCUMENT, null, null);                
152
                        }
153
                }
154
                catch(XmlPullParserException parser_ex){
155
                        System.out.println(parser_ex.getMessage());
156
                        parser_ex.printStackTrace();
157
                        return false;
158
                }
159
                catch (IOException ioe) {           
160
                        ioe.printStackTrace();
161
                        return false;
162
                }
163
                return true;
164
        }
165
        
166
        /**
167
         * <p>Parses the Service Information </p>
168
         */    
169
        private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException 
170
        {
171
                int currentTag;
172
                boolean end = false;
173
                
174
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
175
                currentTag = parser.next();
176
                
177
                while (!end) 
178
                {
179
                        switch(currentTag)
180
                        {
181
                        case KXmlParser.START_TAG:
182
                                if (parser.getName().compareToIgnoreCase(CapabilitiesTags.NAME)==0)
183
                                {
184
                                        serviceInfo.name = parser.nextText(); 
185
                                }   
186
                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_TITLE)==0)
187
                                {
188
                                        serviceInfo.title = parser.nextText(); 
189
                                }
190
                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_ABSTRACT)==0)
191
                                {
192
                                        serviceInfo.abstr = parser.nextText(); 
193
                                } 
194
                                else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
195
                                {
196
                                       serviceInfo.online_resource = parser.nextText();
197
                              
198
                                }                               
199
                                break;
200
                        case KXmlParser.END_TAG:
201
                                if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
202
                                        end = true;
203
                                break;
204
                        case KXmlParser.TEXT:                   
205
                                break;
206
                        }
207
                        if (!end){
208
                                currentTag = parser.next();
209
                        }
210
                }     
211
        }
212
        
213
        private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
214
        {   
215
                int currentTag;
216
                boolean end = false;
217
                
218
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
219
                currentTag = parser.next();
220
                
221
                while (!end) 
222
                {
223
                        switch(currentTag)
224
                        {
225
                        case KXmlParser.START_TAG:
226
                                if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
227
                                {
228
                                        parseRequestTag(parser); 
229
                                }   
230
                                else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
231
                                {
232
                                        //TODO Parse exception tags...
233
                                        parser.skipSubTree();
234
                                }
235
                                else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
236
                                                (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
237
                                {
238
                                        parser.skipSubTree();
239
                                }                   
240
                                break;
241
                        case KXmlParser.END_TAG:
242
                                if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
243
                                        end = true;
244
                                break;
245
                        case KXmlParser.TEXT:                   
246
                                break;
247
                        }
248
                        if (!end){
249
                                currentTag = parser.next();
250
                        }
251
                }
252
        }
253
        
254
        /**
255
         * <p>Parses the Request tag </p>
256
         */ 
257
        private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
258
        {   
259
                int currentTag;
260
                boolean end = false;
261
                
262
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
263
                currentTag = parser.next();
264
                
265
                while (!end) 
266
                {
267
                        switch(currentTag)
268
                        {
269
                        case KXmlParser.START_TAG:
270
                                if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
271
                                {
272
                                        currentTag = parser.nextTag();
273
                                        if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
274
                                        {                        
275
                                                currentTag = parser.nextTag();
276
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
277
                                                {
278
                                                        currentTag = parser.nextTag();
279
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
280
                                                        {
281
                                                                String value = null;
282
                                                                for (int i=0 ; i<parser.getAttributeCount() ; i++){
283
                                                                    if (parser.getAttributeName(i).toUpperCase().compareTo(CapabilitiesTags.ONLINERESOURCE.toUpperCase()) == 0){
284
                                                                            value = parser.getAttributeValue(i);
285
                                                                    }
286
                                                                }                                                                
287
                                                                if (value == null){
288
                                                                        serviceInfo.addOperation(WFSOperation.GETCAPABILITIES);
289
                                                                }else{
290
                                                                        serviceInfo.addOperation(WFSOperation.GETCAPABILITIES,value);
291
                                                                }                                                                                                                
292
                                                        }
293
                                                }
294
                                        }        
295
                                        
296
                                }   
297
                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_DESCRIBEFEATURETYPE)==0)
298
                                {
299
                                        currentTag = parser.nextTag();
300
                                        if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
301
                                        {                        
302
                                                currentTag = parser.nextTag();
303
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
304
                                                {
305
                                                        currentTag = parser.nextTag();
306
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
307
                                                        {
308
                                                                String value = null;
309
                                                                for (int i=0 ; i<parser.getAttributeCount() ; i++){
310
                                                                    if (parser.getAttributeName(i).toUpperCase().compareTo(CapabilitiesTags.ONLINERESOURCE.toUpperCase()) == 0){
311
                                                                            value = parser.getAttributeValue(i);
312
                                                                    }
313
                                                                }                                                                
314
                                                                if (value == null){
315
                                                                        serviceInfo.addOperation(WFSOperation.DESCRIBEFEATURETYPE);
316
                                                                }else{
317
                                                                        serviceInfo.addOperation(WFSOperation.DESCRIBEFEATURETYPE,value);
318
                                                                }                                                
319
                                                        }
320
                                                }
321
                                        }                                                                                
322
                                }
323
                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_GETFEATURE)==0)
324
                                {
325
                                        currentTag = parser.nextTag();
326
                                        if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
327
                                        {                        
328
                                                currentTag = parser.nextTag();
329
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
330
                                                {
331
                                                        currentTag = parser.nextTag();
332
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
333
                                                        {
334
                                                                String value = null;
335
                                                                for (int i=0 ; i<parser.getAttributeCount() ; i++){
336
                                                                    if (parser.getAttributeName(i).toUpperCase().compareTo(CapabilitiesTags.ONLINERESOURCE.toUpperCase()) == 0){
337
                                                                            value = parser.getAttributeValue(i);
338
                                                                    }
339
                                                                }                                                                
340
                                                                if (value == null){
341
                                                                        serviceInfo.addOperation(WFSOperation.GETFEATURE);
342
                                                                }else{
343
                                                                        serviceInfo.addOperation(WFSOperation.GETFEATURE,value);
344
                                                                }                        
345
                                                        }
346
                                                }
347
                                        }        
348
                                }               
349
                                break;
350
                        case KXmlParser.END_TAG:
351
                                if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
352
                                        end = true;
353
                                break;
354
                        case KXmlParser.TEXT:                   
355
                                break;
356
                        }
357
                        if (!end){
358
                                currentTag = parser.next();
359
                        }
360
                }
361
                // TODO: does not get such a tag when arrives here!!!!!!
362
                //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);       
363
        }
364
        
365
        
366
        private void parseFeatureTypeListTag(KXmlParser parser) throws IOException, XmlPullParserException
367
        {
368
                int currentTag;
369
                boolean end = false;
370
                
371
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.WFS_FEATURETYPELIST);
372
                currentTag = parser.next();
373
                
374
                while (!end) 
375
                {
376
                        switch(currentTag)
377
                        {
378
                        case KXmlParser.START_TAG:
379
                                if (parser.getName().compareToIgnoreCase(CapabilitiesTags.WFS_FEATURETYPE)==0)
380
                                {
381
                                        WFSFeature1_0_0 feature = new WFSFeature1_0_0();
382
                                        feature.parse(parser);        
383
                                        features.put(feature.getName(),feature);
384
                                }                                          
385
                                break;
386
                        case KXmlParser.END_TAG:
387
                                if (parser.getName().compareTo(CapabilitiesTags.WFS_FEATURETYPELIST) == 0)
388
                                        end = true;
389
                                break;
390
                        case KXmlParser.TEXT:                   
391
                                break;
392
                        }
393
                        if (!end){
394
                                currentTag = parser.next();
395
                        }
396
                }     
397
        }        
398
        
399
        protected boolean parseDescribeFeatureType(File f,String nameSpace) {
400
                XMLSchemaParser schemaParser = new XMLSchemaParser();
401
                try {
402
                        schemaParser.parse(f,nameSpace);
403
                } catch (XmlPullParserException e) {
404
                        // TODO Auto-generated catch block
405
                        e.printStackTrace();
406
                } catch (IOException e) {
407
                        // TODO Auto-generated catch block
408
                        e.printStackTrace();
409
                }
410
                
411
                String layerName = getCurrentFeature();
412
                if (getCurrentFeature().split(":").length>1){
413
                        layerName = getCurrentFeature().split(":")[1];
414
                }
415
                XMLElement entity = XMLElementsFactory.getElement(layerName);
416
                if (entity != null){
417
                        Vector vector = new Vector();
418
                        vector.add(entity);
419
                        setFields(vector);
420
                }
421
                return true;
422
        }
423

    
424
        /*
425
         *  (non-Javadoc)
426
         * @see org.gvsig.remoteClient.wfs.WFSProtocolHandler#parseGetFeature(java.io.File, java.lang.String)
427
         */
428
        protected boolean parseGetFeature(File f, String nameSpace) throws BaseException {
429
                FileReader reader = null;       
430
                try
431
                {
432
                        reader = new FileReader(f);
433
                        BufferedReader br = new BufferedReader(reader);
434
                        char[] buffer = new char[100];
435
                        br.read(buffer);
436
                        StringBuffer st = new StringBuffer(new String(buffer));
437
                        String searchText = "encoding=\"";
438
                        int index = st.indexOf(searchText);
439
                        if (index>-1) {
440
                                st.delete(0, index+searchText.length());
441
                                encoding = st.substring(0, st.indexOf("\""));
442
                        }
443
                } catch (FileNotFoundException ex) {
444
                        ex.printStackTrace();
445
                } catch (IOException e) {
446
                        e.printStackTrace();
447
                }
448
                
449
                int tag;
450
                KXmlParser parser = null;
451
                parser = new KXmlParser();
452
                
453
                try {
454
                        parser.setInput(new FileInputStream(f), encoding);
455
                        parser.nextTag();                                
456
                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE_EXCEPTION_REPORT)==0){
457
                                throw ExceptionsFactory.parseExceptionReport(parser);
458
                        }                        
459
                } catch (FileNotFoundException e) {
460
                        // TODO Auto-generated catch block
461
                        e.printStackTrace();
462
                } catch (XmlPullParserException e) {
463
                        // TODO Auto-generated catch block
464
                        e.printStackTrace();
465
                } catch (IOException e) {
466
                        // TODO Auto-generated catch block
467
                        e.printStackTrace();
468
                }        
469
                
470
                return true;
471
        }
472

    
473
        protected boolean parseTransaction(File f, String nameSpace) {
474
                // TODO Auto-generated method stub
475
                return false;
476
        }
477

    
478
        protected boolean parseLockFeature(File f, String nameSpace) {
479
                // TODO Auto-generated method stub
480
                return false;
481
        }
482
        
483
        
484
}