Statistics
| Revision:

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

History | View | Annotate | Download (16.8 KB)

1 3323 ldiaz
2
package org.gvsig.remoteClient.wms.wms_1_1_1;
3
4 4500 jaume
import java.io.BufferedReader;
5 3516 jaume
import java.io.ByteArrayInputStream;
6 3323 ldiaz
import java.io.File;
7 4500 jaume
import java.io.FileInputStream;
8 3323 ldiaz
import java.io.FileNotFoundException;
9
import java.io.FileReader;
10
import java.io.IOException;
11 3516 jaume
import java.util.ArrayList;
12 3323 ldiaz
import java.util.TreeMap;
13
14
import org.gvsig.remoteClient.utils.CapabilitiesTags;
15 3516 jaume
import org.gvsig.remoteClient.utils.ExceptionTags;
16
import org.kxml2.io.KXmlParser;
17 3323 ldiaz
import org.xmlpull.v1.XmlPullParserException;
18
19
/**
20
 * <p>
21
 * Describes the handler to comunicate to a WMS 1.1.1
22
 * </p>
23
 */
24
public class WMSProtocolHandler1_1_1 extends org.gvsig.remoteClient.wms.WMSProtocolHandler {
25 3592 jaume
        private WMSLayer1_1_1 fakeRootLayer;
26
27 3323 ldiaz
        public WMSProtocolHandler1_1_1()
28
        {
29 3341 ldiaz
                this.version = "1.1.1";
30
                this.name = "WMS1.1.1";
31
                this.serviceInfo = new ServiceInformation();
32
                this.layers = new TreeMap();
33 3323 ldiaz
        }
34
35
//------------------------------------------------------------------------------
36
// Parsing methods....
37
//------------------------------------------------------------------------------
38
/**
39
 * <p>Parse the xml data retrieved from the WMS, it will parse the WMS Capabilities</p>
40
 *
41
 */
42
    public void parse(File f)
43 4500 jaume
    {
44 5539 jaume
            rootLayer = null;
45 3323 ldiaz
            FileReader reader = null;
46
            try
47
            {
48
                    reader = new FileReader(f);
49 4500 jaume
                    BufferedReader br = new BufferedReader(reader);
50
                    char[] buffer = new char[100];
51
                    br.read(buffer);
52
                    StringBuffer st = new StringBuffer(new String(buffer));
53
                    String searchText = "encoding=\"";
54
                    int index = st.indexOf(searchText);
55
                    if (index>-1) {
56
                            st.delete(0, index+searchText.length());
57
                            encoding = st.substring(0, st.indexOf("\""));
58
                    }
59
            } catch(FileNotFoundException ex)        {
60 3404 jaume
                    ex.printStackTrace();
61 4500 jaume
            } catch (IOException e) {
62
                        e.printStackTrace();
63
                }
64 3323 ldiaz
65
            int tag;
66
            KXmlParser kxmlParser = null;
67
            kxmlParser = new KXmlParser();
68
            try
69
            {
70 4500 jaume
                    kxmlParser.setInput(new FileInputStream(f), encoding);
71 3323 ldiaz
                        kxmlParser.nextTag();
72
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
73
                    {
74 3687 ldiaz
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);
75 3323 ldiaz
                            tag = kxmlParser.nextTag();
76
                                 while(tag != KXmlParser.END_DOCUMENT)
77
                                 {
78 3655 jaume
                     switch(tag)
79 3323 ldiaz
                                         {
80 3655 jaume
81 3323 ldiaz
                                                case KXmlParser.START_TAG:
82
                                                        if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
83
                                                        {
84
                                                                parseServiceTag(kxmlParser);
85
                                                        }
86
                                                        else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
87
                                                        {
88
                                                                parseCapabilityTag(kxmlParser);
89
                                                        }
90
                                                        break;
91
                                                case KXmlParser.END_TAG:
92
                                                        break;
93
                                                case KXmlParser.TEXT:
94 4222 jaume
                                                        //System.out.println("[TEXT]["+kxmlParser.getText()+"]");
95 3323 ldiaz
                                                break;
96
                                         }
97
                                     tag = kxmlParser.next();
98
                             }
99 3516 jaume
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
100 3323 ldiaz
                    }
101
            }
102
            catch(XmlPullParserException parser_ex){
103 3404 jaume
                    parser_ex.printStackTrace();
104 3323 ldiaz
            }
105
                   catch (IOException ioe) {
106 3404 jaume
                           ioe.printStackTrace();
107 3592 jaume
                } finally {
108
109
        }
110 3323 ldiaz
                   // In the parsing process the layer has been filled
111
    }
112
113
    /**
114
     * <p>Parses the Service Information </p>
115
     */
116
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException
117
    {
118
            int currentTag;
119
            boolean end = false;
120
121
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
122
            currentTag = parser.next();
123
124
            while (!end)
125
            {
126
                         switch(currentTag)
127
                         {
128
                                case KXmlParser.START_TAG:
129
                                        if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
130
                                        {
131
                                                serviceInfo.name = parser.nextText();
132
                                        }
133
                                        else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
134
                                        {
135
                                                serviceInfo.title = parser.nextText();
136
                                        }
137
                                        else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
138
                                        {
139
                                                serviceInfo.abstr = parser.nextText();
140
                                        }
141 4222 jaume
                                        else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
142
                                        {
143
                                            String value = new String();
144
                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
145
                                        if (value != null){
146
                                                serviceInfo.online_resource = value;
147
                                        }
148
                                        }
149 3323 ldiaz
                                        else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
150
                                                        (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
151
                                        {
152
                                                parser.skipSubTree();
153
                                        }
154
                                        break;
155
                                case KXmlParser.END_TAG:
156
                                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
157
                                                end = true;
158
                                        break;
159
                                case KXmlParser.TEXT:
160
                                break;
161
                         }
162 3655 jaume
             if (!end)
163
                 currentTag = parser.next();
164 3323 ldiaz
            }
165 3655 jaume
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
166 3323 ldiaz
    }
167
    /**
168
     * <p>Parses the Capability Tag </p>
169
     */
170
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
171
    {
172
            int currentTag;
173
            boolean end = false;
174 4222 jaume
175 3323 ldiaz
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
176
            currentTag = parser.next();
177
178
            while (!end)
179
            {
180
                         switch(currentTag)
181
                         {
182
                                case KXmlParser.START_TAG:
183
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
184
                                        {
185
                                                parseRequestTag(parser);
186
                                        }
187
                                        else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
188
                                        {
189 4222 jaume
                                                //TODO:
190
                                                //Add to serviceInfo the supported formats for the exceptions????
191 3323 ldiaz
                                        }
192
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
193
                                        {
194
                                                WMSLayer1_1_1 lyr = new WMSLayer1_1_1();
195 4222 jaume
                                                lyr.parse(parser, layers);
196
197 3592 jaume
                        if (rootLayer == null)
198
                            rootLayer = lyr;
199
                        else {
200
                            // Handles when there is no general root layer, will use
201
                            // a fake non-queryable one.
202
                            if (!rootLayer.equals(getFakeRootLayer())){
203
                                WMSLayer1_1_1 aux = (WMSLayer1_1_1) rootLayer;
204
                                rootLayer  = getFakeRootLayer();
205
                                rootLayer.getChildren().add(aux);
206
                            }
207
                            rootLayer.getChildren().add(lyr);
208
                        }
209 4222 jaume
210 3483 jaume
                        if (lyr.getName()!=null)
211 4222 jaume
                                                    layers.put(lyr.getName(), lyr);
212
213
//                        Collection layerCollection = layers.values();
214
//                        Iterator iter = layerCollection.iterator();
215
//                        while (iter.hasNext())
216
//                        {
217
//                                WMSLayer1_1_1 layer = (WMSLayer1_1_1)iter.next();
218
//                                                    //Add all the SRS that the layer supports to the WMSProtocolHandler if they dont exist already
219
//                                                    for (i=0;i<layer.getAllSrs().size();i++)
220
//                                                    {
221
////                                                        if (!layer.srs.contains(layer.getAllSrs().elementAt(i)))
222
////                                                        {
223
////                                                            this.srs.add(layer.getAllSrs().elementAt(i));
224
////                                                        }
225
//                                                    }
226
//                        }
227 3323 ldiaz
                                        }
228
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
229
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
230 3516 jaume
231 3323 ldiaz
                                        {
232
                                                parser.skipSubTree();
233
                                        }
234
                                        break;
235
                                case KXmlParser.END_TAG:
236
                                        if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
237
                                                end = true;
238
                                        break;
239
                                case KXmlParser.TEXT:
240
                                break;
241
                         }
242 4222 jaume
                         if (!end)
243
                                 currentTag = parser.next();
244 3323 ldiaz
            }
245 3341 ldiaz
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);
246 3323 ldiaz
    }
247
248
    /**
249
     * <p>Parses the Request tag </p>
250
     */
251
    private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
252
    {
253
            int currentTag;
254
            boolean end = false;
255
256
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
257
            currentTag = parser.next();
258
259
            while (!end)
260
            {
261
                         switch(currentTag)
262
                         {
263
                                case KXmlParser.START_TAG:
264
                                        if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
265
                                        {
266 4222 jaume
                                                serviceInfo.operations.put(CapabilitiesTags.GETCAPABILITIES, null);
267 3323 ldiaz
                                        }
268
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
269
                                        {
270 4222 jaume
                                                // put a null to this key?
271
                                                // or leave it like it was with a CODE in a vector specifying this operation.
272
                                                // WMSProtocolHandler.GETMAP_OPERATION
273
                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, null);
274
                                                parseGetMapTag(parser);
275 3323 ldiaz
                                        }
276
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
277
                                        {
278 4222 jaume
                                                //serviceInfo.operations.put(WMSProtocolHandler.GETFEATUREINFO_OPERATION)
279
                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, null);
280
                                                parseGetFeatureInfoTag(parser);
281 3687 ldiaz
                                        }
282
                                        else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
283
                                        {
284 4222 jaume
                                                //serviceInfo.operations.put(WMSProtocolHandler.DESCRIBELAYER_OPERATION)
285
                                                serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null);
286 8765 jjdelcerro
                                        }
287
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC)==0)
288
                                        {
289
                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, null);
290
                                                parseGetLegendGraphicTag(parser);
291
                                        }
292 3323 ldiaz
                                        break;
293
                                case KXmlParser.END_TAG:
294
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
295
                                                end = true;
296
                                        break;
297
                                case KXmlParser.TEXT:
298
                                break;
299
                         }
300 4222 jaume
                         if(!end)
301
                                 currentTag = parser.next();
302 3323 ldiaz
            }
303
            // TODO: does not get such a tag when arrives here!!!!!!
304
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);
305
    }
306 4222 jaume
307
    /**
308
     * <p>Parses the GetMap tag </p>
309
     */
310
    private void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException
311
    {
312
            int currentTag;
313
            boolean end = false;
314
315
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
316
            currentTag = parser.next();
317
318
            while (!end)
319
            {
320
                         switch(currentTag)
321
                         {
322
                                case KXmlParser.START_TAG:
323
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
324
                                        {
325
                                                serviceInfo.formats.add(parser.nextText());
326
                                        }
327
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
328
                                        {
329
                                                currentTag = parser.nextTag();
330
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
331
                                                {
332
                                                        currentTag = parser.nextTag();
333
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
334
                                                        {
335
                                                                currentTag = parser.nextTag();
336
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
337
                                                                {
338
                                                                        String value = new String();
339
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
340
                                                                        if (value != null){
341
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, value);
342
                                                                        }
343
                                                                }
344
                                                        }
345
                                                }
346
                                        }
347
                                        break;
348
                                case KXmlParser.END_TAG:
349
                                        if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0)
350
                                                end = true;
351
                                        break;
352
                                case KXmlParser.TEXT:
353
                                break;
354
                         }
355
                         if(!end)
356
                                 currentTag = parser.next();
357
            }
358
    }
359
360
    /**
361 8765 jjdelcerro
     * <p>Parses the GetFeatureInfoTag tag </p>
362 4222 jaume
     */
363
    private void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException
364
    {
365
            int currentTag;
366
            boolean end = false;
367
368
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
369
            currentTag = parser.next();
370
371
            while (!end)
372
            {
373
                         switch(currentTag)
374
                         {
375
                                case KXmlParser.START_TAG:
376
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
377
                                        {
378
                                                //TODO:
379
                                                // add the supported formats by the GetFeatureInfo request
380
                                                //serviceInfo.formats.add(parser.nextText());
381
                                        }
382
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
383
                                        {
384
                                                currentTag = parser.nextTag();
385
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
386
                                                {
387
                                                        currentTag = parser.nextTag();
388
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
389
                                                        {
390
                                                                currentTag = parser.nextTag();
391
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
392
                                                                {
393
                                                                        String value = new String();
394
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
395
                                                                        if (value != null){
396 4623 ldiaz
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, value);
397 4222 jaume
                                                                        }
398
                                                                }
399
                                                        }
400
                                                }
401
                                        }
402
                                        break;
403
                                case KXmlParser.END_TAG:
404
                                        if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0)
405
                                                end = true;
406
                                        break;
407
                                case KXmlParser.TEXT:
408
                                break;
409
                         }
410
                         if(!end)
411
                                 currentTag = parser.next();
412
            }
413
    }
414 3516 jaume
415 8765 jjdelcerro
    /**
416
     * <p>Parses the GetFeatureInfoTag tag </p>
417
     */
418
    private void parseGetLegendGraphicTag(KXmlParser parser) throws IOException, XmlPullParserException
419
    {
420
            int currentTag;
421
            boolean end = false;
422
423
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETLEGENDGRAPHIC);
424
            currentTag = parser.next();
425
426
            while (!end)
427
            {
428
                         switch(currentTag)
429
                         {
430
                                case KXmlParser.START_TAG:
431
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
432
                                        {
433
                                                //TODO:
434
                                                // add the supported formats by the GetLegendGraphic request
435
                                                //serviceInfo.formats.add(parser.nextText());
436
                                        }
437
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
438
                                        {
439
                                                currentTag = parser.nextTag();
440
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
441
                                                {
442
                                                        currentTag = parser.nextTag();
443
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
444
                                                        {
445
                                                                currentTag = parser.nextTag();
446
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
447
                                                                {
448
                                                                        String value = new String();
449
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
450
                                                                        if (value != null){
451
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, value);
452
                                                                        }
453
                                                                }
454
                                                        }
455
                                                }
456
                                        }
457
                                        break;
458
                                case KXmlParser.END_TAG:
459
                                        if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC) == 0)
460
                                                end = true;
461
                                        break;
462
                                case KXmlParser.TEXT:
463
                                break;
464
                         }
465
                         if(!end)
466
                                 currentTag = parser.next();
467
            }
468
    }
469
470
471 3592 jaume
    private WMSLayer1_1_1 getFakeRootLayer(){
472
        if (fakeRootLayer == null){
473
            fakeRootLayer = new WMSLayer1_1_1();
474
            fakeRootLayer.setTitle(serviceInfo.title);
475
            fakeRootLayer.setQueryable(false);
476
            fakeRootLayer.setName(null);
477
        }
478
        return fakeRootLayer;
479
    }
480 3516 jaume
    /* (non-Javadoc)
481
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
482
     */
483
    protected String parseException(byte[] data) {
484
        ArrayList errors = new ArrayList();
485
        KXmlParser kxmlParser = new KXmlParser();
486
        try
487
        {
488 4500 jaume
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);
489 3516 jaume
            int tag;
490 3824 ldiaz
491
            boolean end = false;
492
            tag = kxmlParser.nextTag();
493
494 8765 jjdelcerro
            //Comentar temporalmente para subsanar el hecho de que SimonCit me devuelve las capabilities en un GetLegendGraphic!!!
495 3824 ldiaz
            kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
496
497
            while (!end)
498
            {
499
                switch(tag)
500
                {
501
                    case KXmlParser.START_TAG:
502 3516 jaume
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
503
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
504
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
505
                                String errorMessage = kxmlParser.nextText();
506
                                errors.add(errorCode+errorMessage);
507
                            }
508
                            break;
509 3824 ldiaz
                    case KXmlParser.END_TAG:
510
                            if (kxmlParser.getName().compareTo(ExceptionTags.EXCEPTION_ROOT) == 0)
511
                                end = true;
512
                            break;
513 3516 jaume
                     }
514 3824 ldiaz
                        if (!end)
515
                        {
516
                                tag = kxmlParser.nextTag();
517
                        }
518 3516 jaume
                 }
519 3824 ldiaz
                }
520 3516 jaume
        catch(XmlPullParserException parser_ex){
521
            System.out.println(parser_ex.getMessage());
522
            parser_ex.printStackTrace();
523
        }
524
        catch (IOException ioe) {
525
            ioe.printStackTrace();
526
        }
527
        String message = errors.size()>0? "" : null;
528
        for (int i = 0; i < errors.size(); i++) {
529
            message += (String) errors.get(i)+"\n";
530
        }
531
        return message;
532
    }
533 3323 ldiaz
534
  }