Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1011 / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / wms_1_1_1 / WMSProtocolHandler1_1_1.java @ 12904

History | View | Annotate | Download (15.8 KB)

1

    
2
package org.gvsig.remoteClient.wms.wms_1_1_1;
3

    
4
import java.io.BufferedReader;
5
import java.io.ByteArrayInputStream;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.FileReader;
10
import java.io.IOException;
11
import java.io.StringReader;
12
import java.util.ArrayList;
13
import java.util.TreeMap;
14

    
15
import org.gvsig.remoteClient.utils.CapabilitiesTags;
16
import org.gvsig.remoteClient.utils.EncodingXMLParser;
17
import org.gvsig.remoteClient.utils.ExceptionTags;
18
import org.kxml2.io.KXmlParser;
19
import org.xmlpull.v1.XmlPullParserException;
20

    
21
/**
22
 * <p>
23
 * Describes the handler to comunicate to a WMS 1.1.1
24
 * </p>
25
 */
26
public class WMSProtocolHandler1_1_1 extends org.gvsig.remoteClient.wms.WMSProtocolHandler {
27
        private WMSLayer1_1_1 fakeRootLayer;
28

    
29
        public WMSProtocolHandler1_1_1()
30
        {
31
                this.version = "1.1.1";
32
                this.name = "WMS1.1.1";
33
                this.serviceInfo = new ServiceInformation();
34
                this.layers = new TreeMap();
35
        }
36

    
37
//------------------------------------------------------------------------------
38
// Parsing methods....
39
//------------------------------------------------------------------------------
40
/**
41
 * <p>Parse the xml data retrieved from the WMS, it will parse the WMS Capabilities</p>
42
 *
43
 */
44
    public void parse(File f)
45
    {
46
            rootLayer = null;
47
            
48
            int tag;
49
              EncodingXMLParser kxmlParser = null;
50
            kxmlParser = new EncodingXMLParser();
51
            try
52
            {
53
                       kxmlParser.setInput(f);
54
                        kxmlParser.nextTag();
55
                        if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
56
                    {
57
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);
58
                            tag = kxmlParser.nextTag();
59
                                 while(tag != KXmlParser.END_DOCUMENT)
60
                                 {
61
                     switch(tag)
62
                                         {
63

    
64
                                                case KXmlParser.START_TAG:
65
                                                        if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
66
                                                        {
67
                                                                parseServiceTag(kxmlParser);
68
                                                        }
69
                                                        else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
70
                                                        {
71
                                                                parseCapabilityTag(kxmlParser);
72
                                                        }
73
                                                        break;
74
                                                case KXmlParser.END_TAG:
75
                                                        break;
76
                                                case KXmlParser.TEXT:
77
                                                        //System.out.println("[TEXT]["+kxmlParser.getText()+"]");
78
                                                break;
79
                                         }
80
                                     tag = kxmlParser.next();
81
                             }
82
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
83
                    }
84
            }
85
            catch(XmlPullParserException parser_ex){
86
                    parser_ex.printStackTrace();
87
            }
88
                   catch (IOException ioe) {
89
                           ioe.printStackTrace();
90
                } finally {
91

    
92
        }
93
                   // In the parsing process the layer has been filled
94
    }
95

    
96
    /**
97
     * <p>Parses the Service Information </p>
98
     */
99
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException
100
    {
101
            int currentTag;
102
            boolean end = false;
103

    
104
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
105
            currentTag = parser.next();
106

    
107
            while (!end)
108
            {
109
                         switch(currentTag)
110
                         {
111
                                case KXmlParser.START_TAG:
112
                                        if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
113
                                        {
114
                                                serviceInfo.name = parser.nextText();
115
                                        }
116
                                        else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
117
                                        {
118
                                                serviceInfo.title = parser.nextText();
119
                                        }
120
                                        else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
121
                                        {
122
                                                serviceInfo.abstr = parser.nextText();
123
                                        }
124
                                        else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
125
                                        {
126
                                            String value = new String();
127
                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
128
                                        if (value != null){
129
                                                serviceInfo.online_resource = value;
130
                                        }
131
                                        }
132
                                        else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
133
                                                        (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
134
                                        {
135
                                                parser.skipSubTree();
136
                                        }
137
                                        break;
138
                                case KXmlParser.END_TAG:
139
                                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
140
                                                end = true;
141
                                        break;
142
                                case KXmlParser.TEXT:
143
                                break;
144
                         }
145
             if (!end)
146
                 currentTag = parser.next();
147
            }
148
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
149
    }
150
    /**
151
     * <p>Parses the Capability Tag </p>
152
     */
153
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
154
    {
155
            int currentTag;
156
            boolean end = false;
157

    
158
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
159
            currentTag = parser.next();
160

    
161
            while (!end)
162
            {
163
                         switch(currentTag)
164
                         {
165
                                case KXmlParser.START_TAG:
166
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
167
                                        {
168
                                                parseRequestTag(parser);
169
                                        }
170
                                        else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
171
                                        {
172
                                                //TODO:
173
                                                //Add to serviceInfo the supported formats for the exceptions????
174
                                        }
175
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
176
                                        {
177
                                                WMSLayer1_1_1 lyr = new WMSLayer1_1_1();
178
                                                lyr.parse(parser, layers);
179

    
180
                        if (rootLayer == null)
181
                            rootLayer = lyr;
182
                        else {
183
                            // Handles when there is no general root layer, will use
184
                            // a fake non-queryable one.
185
                            if (!rootLayer.equals(getFakeRootLayer())){
186
                                WMSLayer1_1_1 aux = (WMSLayer1_1_1) rootLayer;
187
                                rootLayer  = getFakeRootLayer();
188
                                rootLayer.getChildren().add(aux);
189
                            }
190
                            rootLayer.getChildren().add(lyr);
191
                        }
192

    
193
                        if (lyr.getName()!=null)
194
                                                    layers.put(lyr.getName(), lyr);
195

    
196
//                        Collection layerCollection = layers.values();
197
//                        Iterator iter = layerCollection.iterator();
198
//                        while (iter.hasNext())
199
//                        {
200
//                                WMSLayer1_1_1 layer = (WMSLayer1_1_1)iter.next();
201
//                                                    //Add all the SRS that the layer supports to the WMSProtocolHandler if they dont exist already
202
//                                                    for (i=0;i<layer.getAllSrs().size();i++)
203
//                                                    {
204
////                                                        if (!layer.srs.contains(layer.getAllSrs().elementAt(i)))
205
////                                                        {
206
////                                                            this.srs.add(layer.getAllSrs().elementAt(i));
207
////                                                        }
208
//                                                    }
209
//                        }
210
                                        }
211
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
212
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
213

    
214
                                        {
215
                                                parser.skipSubTree();
216
                                        }
217
                                        break;
218
                                case KXmlParser.END_TAG:
219
                                        if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
220
                                                end = true;
221
                                        break;
222
                                case KXmlParser.TEXT:
223
                                break;
224
                         }
225
                         if (!end)
226
                                 currentTag = parser.next();
227
            }
228
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);
229
    }
230

    
231
    /**
232
     * <p>Parses the Request tag </p>
233
     */
234
    private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
235
    {
236
            int currentTag;
237
            boolean end = false;
238

    
239
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
240
            currentTag = parser.next();
241

    
242
            while (!end)
243
            {
244
                         switch(currentTag)
245
                         {
246
                                case KXmlParser.START_TAG:
247
                                        if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
248
                                        {
249
                                                serviceInfo.operations.put(CapabilitiesTags.GETCAPABILITIES, null);
250
                                        }
251
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
252
                                        {
253
                                                // put a null to this key?
254
                                                // or leave it like it was with a CODE in a vector specifying this operation.
255
                                                // WMSProtocolHandler.GETMAP_OPERATION
256
                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, null);
257
                                                parseGetMapTag(parser);
258
                                        }
259
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
260
                                        {
261
                                                //serviceInfo.operations.put(WMSProtocolHandler.GETFEATUREINFO_OPERATION)
262
                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, null);
263
                                                parseGetFeatureInfoTag(parser);
264
                                        }
265
                                        else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
266
                                        {
267
                                                //serviceInfo.operations.put(WMSProtocolHandler.DESCRIBELAYER_OPERATION)
268
                                                serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null);
269
                                        }
270
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC)==0)
271
                                        {
272
                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, null);
273
                                                parseGetLegendGraphicTag(parser);
274
                                        }
275
                                        break;
276
                                case KXmlParser.END_TAG:
277
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
278
                                                end = true;
279
                                        break;
280
                                case KXmlParser.TEXT:
281
                                break;
282
                         }
283
                         if(!end)
284
                                 currentTag = parser.next();
285
            }
286
            // TODO: does not get such a tag when arrives here!!!!!!
287
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);
288
    }
289

    
290
    /**
291
     * <p>Parses the GetMap tag </p>
292
     */
293
    private void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException
294
    {
295
            int currentTag;
296
            boolean end = false;
297

    
298
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
299
            currentTag = parser.next();
300

    
301
            while (!end)
302
            {
303
                         switch(currentTag)
304
                         {
305
                                case KXmlParser.START_TAG:
306
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
307
                                        {
308
                                                serviceInfo.formats.add(parser.nextText());
309
                                        }
310
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
311
                                        {
312
                                                currentTag = parser.nextTag();
313
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
314
                                                {
315
                                                        currentTag = parser.nextTag();
316
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
317
                                                        {
318
                                                                currentTag = parser.nextTag();
319
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
320
                                                                {
321
                                                                        String value = new String();
322
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
323
                                                                        if (value != null){
324
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, value);
325
                                                                        }
326
                                                                }
327
                                                        }
328
                                                }
329
                                        }
330
                                        break;
331
                                case KXmlParser.END_TAG:
332
                                        if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0)
333
                                                end = true;
334
                                        break;
335
                                case KXmlParser.TEXT:
336
                                break;
337
                         }
338
                         if(!end)
339
                                 currentTag = parser.next();
340
            }
341
    }
342

    
343
    /**
344
     * <p>Parses the GetFeatureInfoTag tag </p>
345
     */
346
    private void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException
347
    {
348
            int currentTag;
349
            boolean end = false;
350

    
351
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
352
            currentTag = parser.next();
353

    
354
            while (!end)
355
            {
356
                         switch(currentTag)
357
                         {
358
                                case KXmlParser.START_TAG:
359
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
360
                                        {
361
                                                //TODO:
362
                                                // add the supported formats by the GetFeatureInfo request
363
                                                //serviceInfo.formats.add(parser.nextText());
364
                                        }
365
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
366
                                        {
367
                                                currentTag = parser.nextTag();
368
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
369
                                                {
370
                                                        currentTag = parser.nextTag();
371
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
372
                                                        {
373
                                                                currentTag = parser.nextTag();
374
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
375
                                                                {
376
                                                                        String value = new String();
377
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
378
                                                                        if (value != null){
379
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, value);
380
                                                                        }
381
                                                                }
382
                                                        }
383
                                                }
384
                                        }
385
                                        break;
386
                                case KXmlParser.END_TAG:
387
                                        if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 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
     * <p>Parses the GetFeatureInfoTag tag </p>
400
     */
401
    private void parseGetLegendGraphicTag(KXmlParser parser) throws IOException, XmlPullParserException
402
    {
403
            int currentTag;
404
            boolean end = false;
405

    
406
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETLEGENDGRAPHIC);
407
            currentTag = parser.next();
408

    
409
            while (!end)
410
            {
411
                         switch(currentTag)
412
                         {
413
                                case KXmlParser.START_TAG:
414
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
415
                                        {
416
                                                //TODO:
417
                                                // add the supported formats by the GetLegendGraphic request
418
                                                //serviceInfo.formats.add(parser.nextText());
419
                                        }
420
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
421
                                        {
422
                                                currentTag = parser.nextTag();
423
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
424
                                                {
425
                                                        currentTag = parser.nextTag();
426
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
427
                                                        {
428
                                                                currentTag = parser.nextTag();
429
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
430
                                                                {
431
                                                                        String value = new String();
432
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
433
                                                                        if (value != null){
434
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, value);
435
                                                                        }
436
                                                                }
437
                                                        }
438
                                                }
439
                                        }
440
                                        break;
441
                                case KXmlParser.END_TAG:
442
                                        if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC) == 0)
443
                                                end = true;
444
                                        break;
445
                                case KXmlParser.TEXT:
446
                                break;
447
                         }
448
                         if(!end)
449
                                 currentTag = parser.next();
450
            }
451
    }
452

    
453

    
454
    private WMSLayer1_1_1 getFakeRootLayer(){
455
        if (fakeRootLayer == null){
456
            fakeRootLayer = new WMSLayer1_1_1();
457
            fakeRootLayer.setTitle(serviceInfo.title);
458
            fakeRootLayer.setQueryable(false);
459
            fakeRootLayer.setName(null);
460
        }
461
        return fakeRootLayer;
462
    }
463
    /* (non-Javadoc)
464
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
465
     */
466
    protected String parseException(byte[] data) {
467
        ArrayList errors = new ArrayList();
468
        KXmlParser kxmlParser = new KXmlParser();
469
        try
470
        {
471
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);
472
            int tag;
473

    
474
            boolean end = false;
475
            tag = kxmlParser.nextTag();
476

    
477
            //Comentar temporalmente para subsanar el hecho de que SimonCit me devuelve las capabilities en un GetLegendGraphic!!!
478
            kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
479

    
480
            while (!end)
481
            {
482
                switch(tag)
483
                {
484
                    case KXmlParser.START_TAG:
485
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
486
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
487
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
488
                                String errorMessage = kxmlParser.nextText();
489
                                errors.add(errorCode+errorMessage);
490
                            }
491
                            break;
492
                    case KXmlParser.END_TAG:
493
                            if (kxmlParser.getName().compareTo(ExceptionTags.EXCEPTION_ROOT) == 0)
494
                                end = true;
495
                            break;
496
                     }
497
                        if (!end)
498
                        {
499
                                tag = kxmlParser.nextTag();
500
                        }
501
                 }
502
                }
503
        catch(XmlPullParserException parser_ex){
504
            System.out.println(parser_ex.getMessage());
505
            parser_ex.printStackTrace();
506
        }
507
        catch (IOException ioe) {
508
            ioe.printStackTrace();
509
        }
510
        String message = errors.size()>0? "" : null;
511
        for (int i = 0; i < errors.size(); i++) {
512
            message += (String) errors.get(i)+"\n";
513
        }
514
        return message;
515
    }
516

    
517
  }