Statistics
| Revision:

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

History | View | Annotate | Download (17 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.ExceptionTags;
17
import org.kxml2.io.KXmlParser;
18
import org.xmlpull.v1.XmlPullParserException;
19

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

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

    
36
//------------------------------------------------------------------------------
37
// Parsing methods....
38
//------------------------------------------------------------------------------
39
/**
40
 * <p>Parse the xml data retrieved from the WMS, it will parse the WMS Capabilities</p>
41
 *
42
 */
43
    public void parse(File f)
44
    {
45
            rootLayer = null;
46
            FileReader reader = null;
47
            try
48
            {
49
                    reader = new FileReader(f);
50
                    BufferedReader br = new BufferedReader(reader);
51
                    char[] buffer = new char[100];
52
                    br.read(buffer);
53
                    String string = new String(buffer);
54

    
55
                     // patch for ArcIMS + WMS connector > 9.0 bug
56
            int a = string.toLowerCase().indexOf("<?xml");
57
            if (a !=-1)
58
                    string = string.substring(a, string.length());
59
            // end patch
60

    
61
                    StringBuffer st = new StringBuffer(string);
62
                    String searchText = "encoding=\"";
63
                    int index = st.indexOf(searchText);
64
                    if (index>-1) {
65
                            st.delete(0, index+searchText.length());
66
                            encoding = st.substring(0, st.indexOf("\""));
67
                    }
68
            } catch(FileNotFoundException ex)        {
69
                    ex.printStackTrace();
70
            } catch (IOException e) {
71
                        e.printStackTrace();
72
                }
73

    
74
            int tag;
75
            KXmlParser kxmlParser = null;
76
            kxmlParser = new KXmlParser();
77
            try
78
            {
79
                    reader = new FileReader(f);
80
                    BufferedReader br = new BufferedReader(reader);
81

    
82
                     // patch for ArcIMS + WMS connector > 9.0 bug
83
                    char[] buffer = new char[(int) f.length()];
84
                    br.read(buffer);
85
                    String string = new String(buffer);
86
                    int a = string.toLowerCase().indexOf("<?xml");
87
            if (a !=-1) {
88
                    string = string.substring(a, string.length());
89
                    kxmlParser.setInput(new StringReader(string));
90
            } else
91
                    // end patch
92
                    kxmlParser.setInput(new FileInputStream(f), encoding);
93

    
94
                        kxmlParser.nextTag();
95
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
96
                    {
97
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);
98
                            tag = kxmlParser.nextTag();
99
                                 while(tag != KXmlParser.END_DOCUMENT)
100
                                 {
101
                     switch(tag)
102
                                         {
103

    
104
                                                case KXmlParser.START_TAG:
105
                                                        if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
106
                                                        {
107
                                                                parseServiceTag(kxmlParser);
108
                                                        }
109
                                                        else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
110
                                                        {
111
                                                                parseCapabilityTag(kxmlParser);
112
                                                        }
113
                                                        break;
114
                                                case KXmlParser.END_TAG:
115
                                                        break;
116
                                                case KXmlParser.TEXT:
117
                                                        //System.out.println("[TEXT]["+kxmlParser.getText()+"]");
118
                                                break;
119
                                         }
120
                                     tag = kxmlParser.next();
121
                             }
122
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
123
                    }
124
            }
125
            catch(XmlPullParserException parser_ex){
126
                    parser_ex.printStackTrace();
127
            }
128
                   catch (IOException ioe) {
129
                           ioe.printStackTrace();
130
                } finally {
131

    
132
        }
133
                   // In the parsing process the layer has been filled
134
    }
135

    
136
    /**
137
     * <p>Parses the Service Information </p>
138
     */
139
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException
140
    {
141
            int currentTag;
142
            boolean end = false;
143

    
144
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
145
            currentTag = parser.next();
146

    
147
            while (!end)
148
            {
149
                         switch(currentTag)
150
                         {
151
                                case KXmlParser.START_TAG:
152
                                        if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
153
                                        {
154
                                                serviceInfo.name = parser.nextText();
155
                                        }
156
                                        else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
157
                                        {
158
                                                serviceInfo.title = parser.nextText();
159
                                        }
160
                                        else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
161
                                        {
162
                                                serviceInfo.abstr = parser.nextText();
163
                                        }
164
                                        else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
165
                                        {
166
                                            String value = new String();
167
                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
168
                                        if (value != null){
169
                                                serviceInfo.online_resource = value;
170
                                        }
171
                                        }
172
                                        else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
173
                                                        (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
174
                                        {
175
                                                parser.skipSubTree();
176
                                        }
177
                                        break;
178
                                case KXmlParser.END_TAG:
179
                                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
180
                                                end = true;
181
                                        break;
182
                                case KXmlParser.TEXT:
183
                                break;
184
                         }
185
             if (!end)
186
                 currentTag = parser.next();
187
            }
188
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
189
    }
190
    /**
191
     * <p>Parses the Capability Tag </p>
192
     */
193
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
194
    {
195
            int currentTag;
196
            boolean end = false;
197

    
198
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
199
            currentTag = parser.next();
200

    
201
            while (!end)
202
            {
203
                         switch(currentTag)
204
                         {
205
                                case KXmlParser.START_TAG:
206
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
207
                                        {
208
                                                parseRequestTag(parser);
209
                                        }
210
                                        else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
211
                                        {
212
                                                //TODO:
213
                                                //Add to serviceInfo the supported formats for the exceptions????
214
                                        }
215
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
216
                                        {
217
                                                WMSLayer1_1_1 lyr = new WMSLayer1_1_1();
218
                                                lyr.parse(parser, layers);
219

    
220
                        if (rootLayer == null)
221
                            rootLayer = lyr;
222
                        else {
223
                            // Handles when there is no general root layer, will use
224
                            // a fake non-queryable one.
225
                            if (!rootLayer.equals(getFakeRootLayer())){
226
                                WMSLayer1_1_1 aux = (WMSLayer1_1_1) rootLayer;
227
                                rootLayer  = getFakeRootLayer();
228
                                rootLayer.getChildren().add(aux);
229
                            }
230
                            rootLayer.getChildren().add(lyr);
231
                        }
232

    
233
                        if (lyr.getName()!=null)
234
                                                    layers.put(lyr.getName(), lyr);
235

    
236
//                        Collection layerCollection = layers.values();
237
//                        Iterator iter = layerCollection.iterator();
238
//                        while (iter.hasNext())
239
//                        {
240
//                                WMSLayer1_1_1 layer = (WMSLayer1_1_1)iter.next();
241
//                                                    //Add all the SRS that the layer supports to the WMSProtocolHandler if they dont exist already
242
//                                                    for (i=0;i<layer.getAllSrs().size();i++)
243
//                                                    {
244
////                                                        if (!layer.srs.contains(layer.getAllSrs().elementAt(i)))
245
////                                                        {
246
////                                                            this.srs.add(layer.getAllSrs().elementAt(i));
247
////                                                        }
248
//                                                    }
249
//                        }
250
                                        }
251
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
252
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
253

    
254
                                        {
255
                                                parser.skipSubTree();
256
                                        }
257
                                        break;
258
                                case KXmlParser.END_TAG:
259
                                        if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
260
                                                end = true;
261
                                        break;
262
                                case KXmlParser.TEXT:
263
                                break;
264
                         }
265
                         if (!end)
266
                                 currentTag = parser.next();
267
            }
268
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);
269
    }
270

    
271
    /**
272
     * <p>Parses the Request tag </p>
273
     */
274
    private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
275
    {
276
            int currentTag;
277
            boolean end = false;
278

    
279
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
280
            currentTag = parser.next();
281

    
282
            while (!end)
283
            {
284
                         switch(currentTag)
285
                         {
286
                                case KXmlParser.START_TAG:
287
                                        if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
288
                                        {
289
                                                serviceInfo.operations.put(CapabilitiesTags.GETCAPABILITIES, null);
290
                                        }
291
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
292
                                        {
293
                                                // put a null to this key?
294
                                                // or leave it like it was with a CODE in a vector specifying this operation.
295
                                                // WMSProtocolHandler.GETMAP_OPERATION
296
                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, null);
297
                                                parseGetMapTag(parser);
298
                                        }
299
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
300
                                        {
301
                                                //serviceInfo.operations.put(WMSProtocolHandler.GETFEATUREINFO_OPERATION)
302
                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, null);
303
                                                parseGetFeatureInfoTag(parser);
304
                                        }
305
                                        else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
306
                                        {
307
                                                //serviceInfo.operations.put(WMSProtocolHandler.DESCRIBELAYER_OPERATION)
308
                                                serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null);
309
                                        }
310
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC)==0)
311
                                        {
312
                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, null);
313
                                                parseGetLegendGraphicTag(parser);
314
                                        }
315
                                        break;
316
                                case KXmlParser.END_TAG:
317
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
318
                                                end = true;
319
                                        break;
320
                                case KXmlParser.TEXT:
321
                                break;
322
                         }
323
                         if(!end)
324
                                 currentTag = parser.next();
325
            }
326
            // TODO: does not get such a tag when arrives here!!!!!!
327
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);
328
    }
329

    
330
    /**
331
     * <p>Parses the GetMap tag </p>
332
     */
333
    private void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException
334
    {
335
            int currentTag;
336
            boolean end = false;
337

    
338
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
339
            currentTag = parser.next();
340

    
341
            while (!end)
342
            {
343
                         switch(currentTag)
344
                         {
345
                                case KXmlParser.START_TAG:
346
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
347
                                        {
348
                                                serviceInfo.formats.add(parser.nextText());
349
                                        }
350
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
351
                                        {
352
                                                currentTag = parser.nextTag();
353
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
354
                                                {
355
                                                        currentTag = parser.nextTag();
356
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
357
                                                        {
358
                                                                currentTag = parser.nextTag();
359
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
360
                                                                {
361
                                                                        String value = new String();
362
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
363
                                                                        if (value != null){
364
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, value);
365
                                                                        }
366
                                                                }
367
                                                        }
368
                                                }
369
                                        }
370
                                        break;
371
                                case KXmlParser.END_TAG:
372
                                        if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0)
373
                                                end = true;
374
                                        break;
375
                                case KXmlParser.TEXT:
376
                                break;
377
                         }
378
                         if(!end)
379
                                 currentTag = parser.next();
380
            }
381
    }
382

    
383
    /**
384
     * <p>Parses the GetFeatureInfoTag tag </p>
385
     */
386
    private void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException
387
    {
388
            int currentTag;
389
            boolean end = false;
390

    
391
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
392
            currentTag = parser.next();
393

    
394
            while (!end)
395
            {
396
                         switch(currentTag)
397
                         {
398
                                case KXmlParser.START_TAG:
399
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
400
                                        {
401
                                                //TODO:
402
                                                // add the supported formats by the GetFeatureInfo request
403
                                                //serviceInfo.formats.add(parser.nextText());
404
                                        }
405
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
406
                                        {
407
                                                currentTag = parser.nextTag();
408
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
409
                                                {
410
                                                        currentTag = parser.nextTag();
411
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
412
                                                        {
413
                                                                currentTag = parser.nextTag();
414
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
415
                                                                {
416
                                                                        String value = new String();
417
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
418
                                                                        if (value != null){
419
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, value);
420
                                                                        }
421
                                                                }
422
                                                        }
423
                                                }
424
                                        }
425
                                        break;
426
                                case KXmlParser.END_TAG:
427
                                        if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0)
428
                                                end = true;
429
                                        break;
430
                                case KXmlParser.TEXT:
431
                                break;
432
                         }
433
                         if(!end)
434
                                 currentTag = parser.next();
435
            }
436
    }
437

    
438
    /**
439
     * <p>Parses the GetFeatureInfoTag tag </p>
440
     */
441
    private void parseGetLegendGraphicTag(KXmlParser parser) throws IOException, XmlPullParserException
442
    {
443
            int currentTag;
444
            boolean end = false;
445

    
446
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETLEGENDGRAPHIC);
447
            currentTag = parser.next();
448

    
449
            while (!end)
450
            {
451
                         switch(currentTag)
452
                         {
453
                                case KXmlParser.START_TAG:
454
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
455
                                        {
456
                                                //TODO:
457
                                                // add the supported formats by the GetLegendGraphic request
458
                                                //serviceInfo.formats.add(parser.nextText());
459
                                        }
460
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
461
                                        {
462
                                                currentTag = parser.nextTag();
463
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
464
                                                {
465
                                                        currentTag = parser.nextTag();
466
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
467
                                                        {
468
                                                                currentTag = parser.nextTag();
469
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
470
                                                                {
471
                                                                        String value = new String();
472
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
473
                                                                        if (value != null){
474
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, value);
475
                                                                        }
476
                                                                }
477
                                                        }
478
                                                }
479
                                        }
480
                                        break;
481
                                case KXmlParser.END_TAG:
482
                                        if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC) == 0)
483
                                                end = true;
484
                                        break;
485
                                case KXmlParser.TEXT:
486
                                break;
487
                         }
488
                         if(!end)
489
                                 currentTag = parser.next();
490
            }
491
    }
492

    
493

    
494
    private WMSLayer1_1_1 getFakeRootLayer(){
495
        if (fakeRootLayer == null){
496
            fakeRootLayer = new WMSLayer1_1_1();
497
            fakeRootLayer.setTitle(serviceInfo.title);
498
            fakeRootLayer.setQueryable(false);
499
            fakeRootLayer.setName(null);
500
        }
501
        return fakeRootLayer;
502
    }
503
    /* (non-Javadoc)
504
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
505
     */
506
    protected String parseException(byte[] data) {
507
        ArrayList errors = new ArrayList();
508
        KXmlParser kxmlParser = new KXmlParser();
509
        try
510
        {
511
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);
512
            int tag;
513

    
514
            boolean end = false;
515
            tag = kxmlParser.nextTag();
516

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

    
520
            while (!end)
521
            {
522
                switch(tag)
523
                {
524
                    case KXmlParser.START_TAG:
525
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
526
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
527
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
528
                                String errorMessage = kxmlParser.nextText();
529
                                errors.add(errorCode+errorMessage);
530
                            }
531
                            break;
532
                    case KXmlParser.END_TAG:
533
                            if (kxmlParser.getName().compareTo(ExceptionTags.EXCEPTION_ROOT) == 0)
534
                                end = true;
535
                            break;
536
                     }
537
                        if (!end)
538
                        {
539
                                tag = kxmlParser.nextTag();
540
                        }
541
                 }
542
                }
543
        catch(XmlPullParserException parser_ex){
544
            System.out.println(parser_ex.getMessage());
545
            parser_ex.printStackTrace();
546
        }
547
        catch (IOException ioe) {
548
            ioe.printStackTrace();
549
        }
550
        String message = errors.size()>0? "" : null;
551
        for (int i = 0; i < errors.size(); i++) {
552
            message += (String) errors.get(i)+"\n";
553
        }
554
        return message;
555
    }
556

    
557
  }