Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / wfs / wfs_1_0_0 / WFSProtocolHandler1_0_0.java @ 40596

History | View | Annotate | Download (16.5 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.wfs.wfs_1_0_0;
25

    
26
import java.io.File;
27
import java.io.FileNotFoundException;
28
import java.io.IOException;
29

    
30
import org.kxml2.io.KXmlParser;
31
import org.xmlpull.v1.XmlPullParserException;
32

    
33
import org.gvsig.compat.CompatLocator;
34
import org.gvsig.compat.lang.StringUtils;
35
import org.gvsig.remoteclient.utils.CapabilitiesTags;
36
import org.gvsig.remoteclient.wfs.WFSProtocolHandler;
37
import org.gvsig.remoteclient.wfs.WFSStatus;
38
import org.gvsig.remoteclient.wfs.edition.WFSTTags;
39
import org.gvsig.remoteclient.wfs.edition.WFSTTransaction;
40
import org.gvsig.remoteclient.wfs.exceptions.ExceptionsFactory;
41
import org.gvsig.remoteclient.wfs.exceptions.WFSException;
42
import org.gvsig.remoteclient.wfs.exceptions.WFSGetFeatureException;
43
import org.gvsig.remoteclient.wfs.request.WFSDescribeFeatureTypeRequest;
44
import org.gvsig.remoteclient.wfs.request.WFSGetFeatureRequest;
45
import org.gvsig.remoteclient.wfs.request.WFSTLockFeatureRequest;
46
import org.gvsig.remoteclient.wfs.request.WFSTransactionRequest;
47
import org.gvsig.remoteclient.wfs.request.WFSTransactionRequestInformation;
48
import org.gvsig.remoteclient.wfs.schema.XMLSchemaParser;
49
import org.gvsig.remoteclient.wfs.wfs_1_0_0.request.WFSDescribeFeatureTypeRequest1_0_0;
50
import org.gvsig.remoteclient.wfs.wfs_1_0_0.request.WFSGetFeatureRequest1_0_0;
51
import org.gvsig.remoteclient.wfs.wfs_1_0_0.request.WFSTLockFeatureRequest1_0_0;
52
import org.gvsig.remoteclient.wfs.wfs_1_0_0.request.WFSTransactionRequest1_0_0;
53

    
54
/**
55
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
56
 */
57
public class WFSProtocolHandler1_0_0 extends WFSProtocolHandler{
58
    
59
    private static final StringUtils stringUtils = CompatLocator.getStringUtils();
60
    
61
    public WFSProtocolHandler1_0_0(){
62
                this.version = "1.0.0";
63
                this.name = "WFS1.0.0";                
64
        }        
65
        
66
        /*
67
         * (non-Javadoc)
68
         * @see org.gvsig.remoteClient.OGCProtocolHandler#parseCapabilities(java.io.File)
69
         */
70
        public boolean parseCapabilities(File f) {
71
                int tag;
72
                XMLSchemaParser parser = null;
73
                parser = new XMLSchemaParser();
74
                try
75
                {
76
                        parser.setInput(f);
77
                        parser.nextTag();
78

    
79
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ) 
80
                        {     
81
                                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.WFS_CAPABILITIES_ROOT1_0_0);             
82
                                //Parses the Namespaces...
83
                                parseNamespaces(parser);        
84
                                //Parse the capabilities document
85
                                tag = parser.nextTag();
86
                                while(tag != KXmlParser.END_DOCUMENT)
87
                                {
88
                                        switch(tag)
89
                                        {
90
                                        case KXmlParser.START_TAG:
91
                                                if (parser.getName().compareTo(CapabilitiesTags.SERVICE)==0)
92
                                                {
93
                                                        parseServiceTag(parser);
94
                                                } 
95
                                                else if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
96
                                                {
97
                                                        parseCapabilityTag(parser);
98
                                                } 
99
                                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_FEATURETYPELIST)==0)
100
                                                {
101
                                                        parseFeatureTypeListTag(parser);
102
                                                } 
103
                                                break;
104
                                        case KXmlParser.END_TAG:                            
105
                                                break;
106
                                        case KXmlParser.TEXT:                                                                                                 
107
                                                break;
108
                                        }
109
                                        tag = parser.next();
110
                                }
111
                                parser.require(KXmlParser.END_DOCUMENT, null, null);                
112
                        }
113
                }
114
                catch(XmlPullParserException parser_ex){
115
                        parser_ex.printStackTrace();
116
                        return false;
117
                }
118
                catch (IOException ioe) {           
119
                        ioe.printStackTrace();
120
                        return false;
121
                }
122
                return true;
123
        }
124

    
125
        /**
126
         * <p>Parses the Service Information </p>
127
         */    
128
        private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException 
129
        {
130
                int currentTag;
131
                boolean end = false;
132

    
133
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
134
                currentTag = parser.next();
135

    
136
                while (!end) 
137
                {
138
                        switch(currentTag)
139
                        {
140
                        case KXmlParser.START_TAG:
141
                                if (parser.getName().compareToIgnoreCase(CapabilitiesTags.NAME)==0)
142
                                {
143
                                        serviceInfo.name = parser.nextText(); 
144
                                }   
145
                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_TITLE)==0)
146
                                {
147
                                        serviceInfo.title = parser.nextText(); 
148
                                }
149
                                else if (parser.getName().compareTo(CapabilitiesTags.WFS_ABSTRACT)==0)
150
                                {
151
                                        serviceInfo.abstr = parser.nextText(); 
152
                                } 
153
                                else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
154
                                {
155
                                        serviceInfo.online_resource = parser.nextText();
156

    
157
                                }                               
158
                                break;
159
                        case KXmlParser.END_TAG:
160
                                if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
161
                                        end = true;
162
                                break;
163
                        case KXmlParser.TEXT:                   
164
                                break;
165
                        }
166
                        if (!end){
167
                                currentTag = parser.next();
168
                        }
169
                }     
170
        }
171

    
172
        private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
173
        {   
174
                int currentTag;
175
                boolean end = false;
176

    
177
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
178
                currentTag = parser.next();
179

    
180
                while (!end) 
181
                {
182
                        switch(currentTag)
183
                        {
184
                        case KXmlParser.START_TAG:
185
                                if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
186
                                {
187
                                        parseRequestTag(parser); 
188
                                }   
189
                                else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
190
                                {
191
                                        //TODO Parse exception tags...
192
                                        parser.skipSubTree();
193
                                }
194
                                else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
195
                                                (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
196
                                {
197
                                        parser.skipSubTree();
198
                                }                   
199
                                break;
200
                        case KXmlParser.END_TAG:
201
                                if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 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
        /**
214
         * <p>Parses the Request tag </p>
215
         */ 
216
        private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
217
        {   
218
                int currentTag;
219
                boolean end = false;
220

    
221
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
222
                currentTag = parser.next();
223

    
224
                while (!end) 
225
                {
226
                        switch(currentTag)
227
                        {
228
                        case KXmlParser.START_TAG:
229
                                if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0){
230
                                        parserDcpType(parser, CapabilitiesTags.GETCAPABILITIES);                                        
231
                                } else if (parser.getName().compareTo(CapabilitiesTags.WFS_DESCRIBEFEATURETYPE)==0)        {
232
                                        parserDcpType(parser, CapabilitiesTags.WFS_DESCRIBEFEATURETYPE);                                                             
233
                                }else if (parser.getName().compareTo(CapabilitiesTags.WFS_GETFEATURE)==0){
234
                                        parserDcpType(parser, CapabilitiesTags.WFS_GETFEATURE);
235
                                }else if (parser.getName().compareTo(CapabilitiesTags.WFS_LOCKFEATURE)==0){
236
                                        parserDcpType(parser, CapabilitiesTags.WFS_LOCKFEATURE);
237
                                }else if (parser.getName().compareTo(CapabilitiesTags.WFS_TRANSACTION)==0){
238
                                        parserDcpType(parser, CapabilitiesTags.WFS_TRANSACTION);
239
                                }               
240
                                break;
241
                        case KXmlParser.END_TAG:
242
                                if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 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
         * It parses the feature typelist tag
256
         * @param parser
257
         * @throws IOException
258
         * @throws XmlPullParserException
259
         */
260
        private void parseFeatureTypeListTag(KXmlParser parser) throws IOException, XmlPullParserException{
261
                int currentTag;
262
                boolean end = false;
263

    
264
                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.WFS_FEATURETYPELIST);
265
                currentTag = parser.next();
266

    
267
                while (!end) 
268
                {
269
                        switch(currentTag)
270
                        {
271
                        case KXmlParser.START_TAG:
272
                                if (parser.getName().compareToIgnoreCase(CapabilitiesTags.WFS_FEATURETYPE)==0){
273
                                        //Parse the namespaces...
274
                                        parseNamespaces(parser);        
275
                                        WFSFeature1_0_0 feature = new WFSFeature1_0_0(serviceInfo);
276
                                        feature.parse(parser);        
277
                                        features.put(feature.getName(),feature);
278
                                }                                          
279
                                break;
280
                        case KXmlParser.END_TAG:
281
                                if (parser.getName().compareTo(CapabilitiesTags.WFS_FEATURETYPELIST) == 0)
282
                                        end = true;
283
                                break;
284
                        case KXmlParser.TEXT:                   
285
                                break;
286
                        }
287
                        if (!end){
288
                                currentTag = parser.next();
289
                        }
290
                }     
291
        }        
292

    
293
        /*
294
         *  (non-Javadoc)
295
         * @see org.gvsig.remoteClient.wfs.WFSProtocolHandler#parseGetFeature(java.io.File, java.lang.String)
296
         */
297
        protected boolean parseGetFeature(File f, String nameSpace) throws WFSException {
298
                XMLSchemaParser parser = null;
299
                parser = new XMLSchemaParser();
300
                try
301
                {
302
                        parser.setInput(f);
303
                        parser.nextTag();                                
304
                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE_EXCEPTION_REPORT)==0){
305
                                throw ExceptionsFactory.parseServiceExceptionReport(parser);
306
                        }                        
307
                } catch (FileNotFoundException e) {
308
                        e.printStackTrace();
309
                        throw new WFSGetFeatureException(e);
310
                } catch (XmlPullParserException e) {
311
                        e.printStackTrace();
312
                        throw new WFSGetFeatureException(e);
313
                } catch (IOException e) {
314
                        e.printStackTrace();
315
                        throw new WFSGetFeatureException(e);
316
                }        
317

    
318
                return true;
319
        }
320

    
321
        /*
322
         * (non-Javadoc)
323
         * @see org.gvsig.remoteClient.wfs.WFSProtocolHandler#parseTransaction(java.io.File, java.lang.String)
324
         */
325
        protected boolean parseTransaction(File f,  WFSStatus status) throws WFSException {
326
                int tag;
327
                XMLSchemaParser parser = null;
328
                parser = new XMLSchemaParser();
329
                
330
                WFSTransactionRequestInformation transactionRequestInformation =
331
                    (WFSTransactionRequestInformation)this.getLastWfsRequestInformation();
332
                
333
                try
334
                {
335
                        parser.setInput(f);
336
                        parser.nextTag();
337
                        if (CapabilitiesTags.SERVICE_EXCEPTION_REPORT.equals(parser.getName())) {
338
                                throw ExceptionsFactory.parseServiceExceptionReport(parser);
339
                        }else if (parser.getName().equals(CapabilitiesTags.EXCEPTION_REPORT)){
340
                throw ExceptionsFactory.parseExceptionReport(parser);
341
                    } 
342

    
343
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ) 
344
                        {                                           
345
                                tag = parser.nextTag();
346
                                while(tag != KXmlParser.END_DOCUMENT)
347
                                {
348
                                        switch(tag)
349
                                        {
350
                                        case KXmlParser.START_TAG:
351
                                                if (parser.getName().compareTo(WFSTTags.WFST_TRANSACTIONRESULT)==0){
352
                                                        parseTransactionResult(parser, transactionRequestInformation);
353
                                                }else if (parser.getName().compareTo(WFSTTags.WFST_TRANSACTIONRESPONSE)==0){
354
                                                        parseFeaturesLocked(parser);
355
                                                }                                                
356
                                                break;
357
                                        case KXmlParser.END_TAG:                            
358
                                                break;
359
                                        case KXmlParser.TEXT:
360
                                                break;
361
                                        }
362
                                        tag = parser.next();
363
                                }
364
                                parser.require(KXmlParser.END_DOCUMENT, null, null);                
365
                        }
366
                }
367
                catch(XmlPullParserException parser_ex){
368
                        parser_ex.printStackTrace();
369
                        return false;
370
                }
371
                catch (IOException ioe) {           
372
                        ioe.printStackTrace();
373
                        return false;
374
                }
375
                return true;
376
        }
377

    
378
        /**
379
         * Parse the transaction result XML sub tree
380
         * @param parser
381
         * @param transaction
382
         * @throws IOException 
383
         * @throws XmlPullParserException 
384
         */
385
        private void parseTransactionResult(XMLSchemaParser parser,
386
                        WFSTransactionRequestInformation transactionRequestInformation) throws XmlPullParserException, IOException {
387
                int currentTag;
388
                boolean end = false;                
389

    
390
                currentTag = parser.next();
391

    
392
                while (!end) 
393
                {
394
                        switch(currentTag)
395
                        {
396
                        case KXmlParser.START_TAG:
397
                                if (parser.getName().compareTo(WFSTTags.WFST_STATUS)==0){
398
                                        parseStatus(parser, transactionRequestInformation);
399
                                }else if (parser.getName().compareTo(WFSTTags.WFST_TRANSACTIONMESSAGE)==0){
400
                                        parser.next();
401
                                        transactionRequestInformation.setMessage(parser.getText());
402
                                }
403
                                break;
404
                        case KXmlParser.END_TAG:
405
                                if (parser.getName().compareTo(WFSTTags.WFST_TRANSACTIONRESULT)==0)
406
                                        end = true;
407
                                break;
408
                        case KXmlParser.TEXT:                   
409
                                break;
410
                        }
411
                        if (!end){
412
                                currentTag = parser.next();
413
                        }
414
                }     
415
        }
416

    
417
        /**
418
         * Parser WFST Status subtree
419
         * @param parser
420
         * Teh xML parser
421
         * @param transaction
422
         * The current transaction
423
         * @throws IOException 
424
         * @throws XmlPullParserException 
425
         */
426
        private void parseStatus(XMLSchemaParser parser, WFSTransactionRequestInformation transactionRequestInformation) throws XmlPullParserException, IOException {
427
                int currentTag;
428
                boolean end = false;                
429

    
430
                currentTag = parser.next();
431
                
432
                while (!end) 
433
                {
434
                        switch(currentTag)
435
                        {
436
                        case KXmlParser.START_TAG:
437
                                if (parser.getName().compareTo(WFSTTags.WFST_SUCCESS)==0){
438
                                    transactionRequestInformation.setStatus(WFSTransactionRequestInformation.STATUS_SUCCESS);
439
                                }else if (parser.getName().compareTo(WFSTTags.WFST_FAILED)==0){
440
                                    transactionRequestInformation.setStatus(WFSTransactionRequestInformation.STATUS_FAILED);
441
                                }
442
                                break;
443
                        case KXmlParser.END_TAG:
444
                                if (parser.getName().compareTo(WFSTTags.WFST_STATUS)==0)
445
                                        end = true;
446
                                break;
447
                        case KXmlParser.TEXT:                   
448
                                break;
449
                        }
450
                        if (!end){
451
                                currentTag = parser.next();
452
                        }
453
                }    
454
        }
455

    
456
        /*
457
         * (non-Javadoc)
458
         * @see org.gvsig.remoteClient.wfs.WFSProtocolHandler#parseLockFeature(java.io.File, java.lang.String)
459
         */
460
        protected boolean parseLockFeature(File f, String nameSpace, WFSStatus status) throws WFSException {
461
                int tag;
462
                XMLSchemaParser parser = null;
463
                parser = new XMLSchemaParser();
464
                try
465
                {
466
                        parser.setInput(f);
467
                        parser.nextTag();
468
                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE_EXCEPTION_REPORT)==0){
469
                                throw ExceptionsFactory.parseServiceExceptionReport(parser);
470
                        }                
471

    
472

    
473
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ) 
474
                        {                                           
475
                                tag = parser.nextTag();
476
                                while(tag != KXmlParser.END_DOCUMENT)
477
                                {
478
                                        switch(tag)
479
                                        {
480
                                        case KXmlParser.START_TAG:
481
                                                if (parser.getName().compareTo(WFSTTags.WFST_LOCKID)==0){
482
                                                        parser.next();
483
                                                        status.addFeatureLocked(parser.getText());
484
                                                }else if (parser.getName().compareTo(WFSTTags.WFST_FEATURESLOCKED)==0){
485
                                                        parseFeaturesLocked(parser);
486
                                                }                                                
487
                                                break;
488
                                        case KXmlParser.END_TAG:                            
489
                                                break;
490
                                        case KXmlParser.TEXT:
491
                                                break;
492
                                        }
493
                                        tag = parser.next();
494
                                }
495
                                parser.require(KXmlParser.END_DOCUMENT, null, null);                
496
                        }
497
                }
498
                catch(XmlPullParserException parser_ex){
499
                        parser_ex.printStackTrace();
500
                        return false;
501
                }
502
                catch (IOException ioe) {           
503
                        ioe.printStackTrace();
504
                        return false;
505
                }
506
                return true;
507
        }
508

    
509
        /**
510
         * It parses the featuresLocked tag
511
         * @param parser
512
         * @throws IOException 
513
         * @throws XmlPullParserException 
514
         */
515
        private void parseFeaturesLocked(XMLSchemaParser parser) throws XmlPullParserException, IOException {
516
                int currentTag;
517
                boolean end = false;                
518

    
519
                currentTag = parser.next();
520

    
521
                while (!end) 
522
                {
523
                        switch(currentTag)
524
                        {
525
                        case KXmlParser.START_TAG:
526
                                if (parser.getName().compareTo(WFSTTags.WFST_FEATURESID)==0){
527

    
528
                                }
529
                                break;
530
                        case KXmlParser.END_TAG:
531
                                if (parser.getName().compareTo(WFSTTags.WFST_FEATURESLOCKED)==0)
532
                                        end = true;
533
                                break;
534
                        case KXmlParser.TEXT:                   
535
                                break;
536
                        }
537
                        if (!end){
538
                                currentTag = parser.next();
539
                        }
540
                }     
541
        }
542

    
543
        /*
544
         * (non-Javadoc)
545
         * @see org.gvsig.remoteClient.wfs.WFSProtocolHandler#createDescribeFeatureTypeRequest(org.gvsig.remoteClient.wfs.WFSStatus)
546
         */
547
        protected WFSDescribeFeatureTypeRequest createDescribeFeatureTypeRequest(
548
                        WFSStatus status) {
549
                return new WFSDescribeFeatureTypeRequest1_0_0(status, this);
550
        }
551

    
552
        /*
553
         * (non-Javadoc)
554
         * @see org.gvsig.remoteClient.wfs.WFSProtocolHandler#createGetFeatureRequest(org.gvsig.remoteClient.wfs.WFSStatus)
555
         */
556
        protected WFSGetFeatureRequest createGetFeatureRequest(WFSStatus status) {
557
                return new WFSGetFeatureRequest1_0_0(status, this);
558
        }
559

    
560
        /*
561
         * (non-Javadoc)
562
         * @see org.gvsig.remoteClient.wfs.WFSProtocolHandler#createLockFeatureRequest(org.gvsig.remoteClient.wfs.WFSStatus)
563
         */
564
        protected WFSTLockFeatureRequest createLockFeatureRequest(WFSStatus status) {
565
                return new WFSTLockFeatureRequest1_0_0(status, this);
566
        }
567

    
568
    protected WFSTransactionRequest createTransactionRequest(WFSStatus status) {
569
        return new WFSTransactionRequest1_0_0(status, this);
570
    }
571
}