Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / wms_1_1_1 / WMSProtocolHandler1_1_1.java @ 37961

History | View | Annotate | Download (16.3 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
            rootLayer = null;
48
            int tag;
49
            EncodingXMLParser kxmlParser = null;
50
            kxmlParser = new EncodingXMLParser();
51
            try
52
            {
53
//                    FileReader reader = new FileReader(f);
54
//                    BufferedReader br = new BufferedReader(reader);
55
//
56
//                     // patch for ArcIMS + WMS connector > 9.0 bug
57
//                    char[] buffer = new char[(int) f.length()];
58
//                    br.read(buffer);
59
//                    String string = new String(buffer);
60
//                    int a = string.toLowerCase().indexOf("<?xml");
61
//            if (a !=-1) {
62
//                    string = string.substring(a, string.length());
63
//                    kxmlParser.setInput(string);
64
//            } else
65
                    // end patch
66
                    kxmlParser.setInput(f);
67

    
68
                        kxmlParser.nextTag();
69
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
70
                    {
71
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);
72
                            tag = kxmlParser.nextTag();
73
                                 while(tag != KXmlParser.END_DOCUMENT)
74
                                 {
75
                     switch(tag)
76
                                         {
77

    
78
                                                case KXmlParser.START_TAG:
79
                                                        if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
80
                                                        {
81
                                                                parseServiceTag(kxmlParser);
82
                                                        }
83
                                                        else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
84
                                                        {
85
                                                                parseCapabilityTag(kxmlParser);
86
                                                        }
87
                                                        break;
88
                                                case KXmlParser.END_TAG:
89
                                                        break;
90
                                                case KXmlParser.TEXT:
91
                                                        //System.out.println("[TEXT]["+kxmlParser.getText()+"]");
92
                                                break;
93
                                         }
94
                                     tag = kxmlParser.next();
95
                             }
96
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
97
                    }
98
            }
99
            catch(XmlPullParserException parser_ex){
100
                    parser_ex.printStackTrace();
101
            }
102
                   catch (IOException ioe) {
103
                           ioe.printStackTrace();
104
                } finally {
105

    
106
        }
107
                   // In the parsing process the layer has been filled
108
    }
109

    
110
    /**
111
     * <p>Parses the Service Information </p>
112
     */
113
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException
114
    {
115
            int currentTag;
116
            boolean end = false;
117

    
118
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
119
            currentTag = parser.next();
120

    
121
            while (!end)
122
            {
123
                         switch(currentTag)
124
                         {
125
                                case KXmlParser.START_TAG:
126
                                        if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
127
                                        {
128
                                                serviceInfo.name = parser.nextText();
129
                                        }
130
                                        else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
131
                                        {
132
                                                serviceInfo.title = parser.nextText();
133
                                        }
134
                                        else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
135
                                        {
136
                                                serviceInfo.abstr = parser.nextText();
137
                                        }
138
                                        else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
139
                                        {
140
                                            String value = new String();
141
                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
142
                                        if (value != null){
143
                                                serviceInfo.online_resource = value;
144
                                        }
145
                                        }
146
                                        else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
147
                                                        (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
148
                                        {
149
                                                parser.skipSubTree();
150
                                        }
151
                                        break;
152
                                case KXmlParser.END_TAG:
153
                                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
154
                                                end = true;
155
                                        break;
156
                                case KXmlParser.TEXT:
157
                                break;
158
                         }
159
             if (!end)
160
                 currentTag = parser.next();
161
            }
162
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
163
    }
164
    /**
165
     * <p>Parses the Capability Tag </p>
166
     */
167
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
168
    {
169
            int currentTag;
170
            boolean end = false;
171

    
172
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
173
            currentTag = parser.next();
174

    
175
            while (!end)
176
            {
177
                         switch(currentTag)
178
                         {
179
                                case KXmlParser.START_TAG:
180
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
181
                                        {
182
                                                parseRequestTag(parser);
183
                                        }
184
                                        else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
185
                                        {
186
                                                //TODO:
187
                                                //Add to serviceInfo the supported formats for the exceptions????
188
                                        }
189
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
190
                                        {
191
                                                WMSLayer1_1_1 lyr = new WMSLayer1_1_1();
192
                                                lyr.parse(parser, layers);
193

    
194
                        if (rootLayer == null)
195
                            rootLayer = lyr;
196
                        else {
197
                            // Handles when there is no general root layer, will use
198
                            // a fake non-queryable one.
199
                            if (!rootLayer.equals(getFakeRootLayer())){
200
                                WMSLayer1_1_1 aux = (WMSLayer1_1_1) rootLayer;
201
                                rootLayer  = getFakeRootLayer();
202
                                rootLayer.getChildren().add(aux);
203
                            }
204
                            rootLayer.getChildren().add(lyr);
205
                        }
206

    
207
                        if (lyr.getName()!=null)
208
                                                    layers.put(lyr.getName(), lyr);
209

    
210
//                        Collection layerCollection = layers.values();
211
//                        Iterator iter = layerCollection.iterator();
212
//                        while (iter.hasNext())
213
//                        {
214
//                                WMSLayer1_1_1 layer = (WMSLayer1_1_1)iter.next();
215
//                                                    //Add all the SRS that the layer supports to the WMSProtocolHandler if they dont exist already
216
//                                                    for (i=0;i<layer.getAllSrs().size();i++)
217
//                                                    {
218
////                                                        if (!layer.srs.contains(layer.getAllSrs().elementAt(i)))
219
////                                                        {
220
////                                                            this.srs.add(layer.getAllSrs().elementAt(i));
221
////                                                        }
222
//                                                    }
223
//                        }
224
                                        }
225
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
226
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
227

    
228
                                        {
229
                                                parser.skipSubTree();
230
                                        }
231
                                        break;
232
                                case KXmlParser.END_TAG:
233
                                        if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
234
                                                end = true;
235
                                        break;
236
                                case KXmlParser.TEXT:
237
                                break;
238
                         }
239
                         if (!end)
240
                                 currentTag = parser.next();
241
            }
242
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);
243
    }
244

    
245
    /**
246
     * <p>Parses the Request tag </p>
247
     */
248
    private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
249
    {
250
            int currentTag;
251
            boolean end = false;
252

    
253
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
254
            currentTag = parser.next();
255

    
256
            while (!end)
257
            {
258
                         switch(currentTag)
259
                         {
260
                                case KXmlParser.START_TAG:
261
                                        if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
262
                                        {
263
                                                serviceInfo.operations.put(CapabilitiesTags.GETCAPABILITIES, null);
264
                                        }
265
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
266
                                        {
267
                                                // put a null to this key?
268
                                                // or leave it like it was with a CODE in a vector specifying this operation.
269
                                                // WMSProtocolHandler.GETMAP_OPERATION
270
                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, null);
271
                                                parseGetMapTag(parser);
272
                                        }
273
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
274
                                        {
275
                                                //serviceInfo.operations.put(WMSProtocolHandler.GETFEATUREINFO_OPERATION)
276
                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, null);
277
                                                parseGetFeatureInfoTag(parser);
278
                                        }
279
                                        else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
280
                                        {
281
                                                //serviceInfo.operations.put(WMSProtocolHandler.DESCRIBELAYER_OPERATION)
282
                                                serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null);
283
                                        }
284
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC)==0)
285
                                        {
286
                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, null);
287
                                                parseGetLegendGraphicTag(parser);
288
                                        }
289
                                        break;
290
                                case KXmlParser.END_TAG:
291
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
292
                                                end = true;
293
                                        break;
294
                                case KXmlParser.TEXT:
295
                                break;
296
                         }
297
                         if(!end)
298
                                 currentTag = parser.next();
299
            }
300
            // TODO: does not get such a tag when arrives here!!!!!!
301
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);
302
    }
303

    
304
    /**
305
     * <p>Parses the GetMap tag </p>
306
     */
307
    private void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException
308
    {
309
            int currentTag;
310
            boolean end = false;
311

    
312
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
313
            currentTag = parser.next();
314

    
315
            while (!end)
316
            {
317
                         switch(currentTag)
318
                         {
319
                                case KXmlParser.START_TAG:
320
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
321
                                        {
322
                                                serviceInfo.formats.add(parser.nextText());
323
                                        }
324
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
325
                                        {
326
                                                currentTag = parser.nextTag();
327
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
328
                                                {
329
                                                        currentTag = parser.nextTag();
330
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
331
                                                        {
332
                                                                currentTag = parser.nextTag();
333
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
334
                                                                {
335
                                                                        String value = new String();
336
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
337
                                                                        if (value != null){
338
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, value);
339
                                                                        }
340
                                                                }
341
                                                        }
342
                                                }
343
                                        }
344
                                        break;
345
                                case KXmlParser.END_TAG:
346
                                        if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0)
347
                                                end = true;
348
                                        break;
349
                                case KXmlParser.TEXT:
350
                                break;
351
                         }
352
                         if(!end)
353
                                 currentTag = parser.next();
354
            }
355
    }
356

    
357
    /**
358
     * <p>Parses the GetFeatureInfoTag tag </p>
359
     */
360
    private void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException
361
    {
362
            int currentTag;
363
            boolean end = false;
364

    
365
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
366
            currentTag = parser.next();
367

    
368
            while (!end)
369
            {
370
                         switch(currentTag)
371
                         {
372
                                case KXmlParser.START_TAG:
373
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
374
                                        {
375
                                                // add the supported formats by the GetFeatureInfo request
376
                                                serviceInfo.infoFormats.add(parser.nextText());
377
                                        }
378
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
379
                                        {
380
                                                currentTag = parser.nextTag();
381
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
382
                                                {
383
                                                        currentTag = parser.nextTag();
384
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
385
                                                        {
386
                                                                currentTag = parser.nextTag();
387
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
388
                                                                {
389
                                                                        String value = new String();
390
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
391
                                                                        if (value != null){
392
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, value);
393
                                                                        }
394
                                                                }
395
                                                        }
396
                                                }
397
                                        }
398
                                        break;
399
                                case KXmlParser.END_TAG:
400
                                        if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0)
401
                                                end = true;
402
                                        break;
403
                                case KXmlParser.TEXT:
404
                                break;
405
                         }
406
                         if(!end)
407
                                 currentTag = parser.next();
408
            }
409
    }
410

    
411
    /**
412
     * <p>Parses the GetFeatureInfoTag tag </p>
413
     */
414
    private void parseGetLegendGraphicTag(KXmlParser parser) throws IOException, XmlPullParserException
415
    {
416
            int currentTag;
417
            boolean end = false;
418

    
419
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETLEGENDGRAPHIC);
420
            currentTag = parser.next();
421

    
422
            while (!end)
423
            {
424
                         switch(currentTag)
425
                         {
426
                                case KXmlParser.START_TAG:
427
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
428
                                        {
429
                                                //TODO:
430
                                                // add the supported formats by the GetLegendGraphic request
431
                                                //serviceInfo.formats.add(parser.nextText());
432
                                        }
433
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
434
                                        {
435
                                                currentTag = parser.nextTag();
436
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
437
                                                {
438
                                                        currentTag = parser.nextTag();
439
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
440
                                                        {
441
                                                                currentTag = parser.nextTag();
442
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
443
                                                                {
444
                                                                        String value = new String();
445
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
446
                                                                        if (value != null){
447
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, value);
448
                                                                        }
449
                                                                }
450
                                                        }
451
                                                }
452
                                        }
453
                                        break;
454
                                case KXmlParser.END_TAG:
455
                                        if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC) == 0)
456
                                                end = true;
457
                                        break;
458
                                case KXmlParser.TEXT:
459
                                break;
460
                         }
461
                         if(!end)
462
                                 currentTag = parser.next();
463
            }
464
    }
465

    
466

    
467
    private WMSLayer1_1_1 getFakeRootLayer(){
468
        if (fakeRootLayer == null){
469
            fakeRootLayer = new WMSLayer1_1_1();
470
            fakeRootLayer.setTitle(serviceInfo.title);
471
            fakeRootLayer.setQueryable(false);
472
            fakeRootLayer.setName(null);
473
        }
474
        return fakeRootLayer;
475
    }
476
    /* (non-Javadoc)
477
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
478
     */
479
    protected String parseException(byte[] data) {
480
        ArrayList errors = new ArrayList();
481
        KXmlParser kxmlParser = new KXmlParser();
482
        try
483
        {
484
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);
485
            int tag;
486

    
487
            boolean end = false;
488
            tag = kxmlParser.nextTag();
489

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

    
493
            while (!end)
494
            {
495
                switch(tag)
496
                {
497
                    case KXmlParser.START_TAG:
498
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
499
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
500
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
501
                                String errorMessage = kxmlParser.nextText();
502
                                errors.add(errorCode+errorMessage);
503
                            }
504
                            break;
505
                    case KXmlParser.END_TAG:
506
                            if (kxmlParser.getName().compareTo(ExceptionTags.EXCEPTION_ROOT) == 0)
507
                                end = true;
508
                            break;
509
                     }
510
                        if (!end)
511
                        {
512
                                tag = kxmlParser.nextTag();
513
                        }
514
                 }
515
                }
516
        catch(XmlPullParserException parser_ex){
517
            System.out.println(parser_ex.getMessage());
518
            parser_ex.printStackTrace();
519
        }
520
        catch (IOException ioe) {
521
            ioe.printStackTrace();
522
        }
523
        String message = errors.size()>0? "" : null;
524
        for (int i = 0; i < errors.size(); i++) {
525
            message += (String) errors.get(i)+"\n";
526
        }
527
        return message;
528
    }
529

    
530
  }