Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_894 / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / wms_1_3_0 / WMSProtocolHandler1_3_0.java @ 10309

History | View | Annotate | Download (15.7 KB)

1

    
2
package org.gvsig.remoteClient.wms.wms_1_3_0;
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.util.ArrayList;
12
import java.util.TreeMap;
13

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

    
19
/**
20
 * <p>
21
 * Describes the handler to comunicate to a WMS 1.3.0
22
 * </p>
23
 */
24
public class WMSProtocolHandler1_3_0 extends org.gvsig.remoteClient.wms.WMSProtocolHandler {
25
        private WMSLayer1_3_0 fakeRootLayer;
26
    
27
        public WMSProtocolHandler1_3_0()
28
        {
29
                this.version = "1.3.0";
30
                this.name = "WMS1.3.0";
31
                this.serviceInfo = new ServiceInformation(); 
32
                this.layers = new TreeMap();                   
33
        }
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
    {   
44
            rootLayer = null;
45
            FileReader reader = null;            
46
            try
47
            {
48
                    reader = new FileReader(f);
49
                    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
            }
60
            catch(FileNotFoundException ex)        {
61
                    ex.printStackTrace();
62
            } catch (IOException e) {
63
                        e.printStackTrace();
64
                }
65
            
66
            int tag;
67
            KXmlParser kxmlParser = null;
68
            kxmlParser = new KXmlParser();
69
            try
70
            {
71
                    kxmlParser.setInput(new FileInputStream(f), encoding);                
72
                        kxmlParser.nextTag();
73
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
74
                    {                    
75
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_3_0);                            
76
                            tag = kxmlParser.nextTag();
77
                                 while(tag != KXmlParser.END_DOCUMENT)
78
                                 {
79
                     switch(tag)
80
                                         {                       
81
                                                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
                                                        //System.out.println("[TEXT]["+kxmlParser.getText()+"]");                                                        
95
                                                break;
96
                                         }
97
                                     tag = kxmlParser.next();
98
                             }//while !END_DOCUMENT
99
                    }
100
            }
101
            catch(XmlPullParserException parser_ex){                    
102
                    parser_ex.printStackTrace();
103
            }
104
                   catch (IOException ioe) {                        
105
                           ioe.printStackTrace();
106
                 } finally {
107
            
108
        }
109
                   // In the parsing process the layer has been filled                  
110
    } 
111
    
112
    /**
113
     * <p>Parses the Service Information </p>
114
     */    
115
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException 
116
    {
117
            int currentTag;
118
            boolean end = false;
119
            
120
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
121
            currentTag = parser.next();
122
            
123
            while (!end) 
124
            {
125
                         switch(currentTag)
126
                         {
127
                                case KXmlParser.START_TAG:
128
                                        if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
129
                                        {
130
                                                serviceInfo.name = parser.nextText(); 
131
                                        }        
132
                                        else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
133
                                        {
134
                                                serviceInfo.title = parser.nextText(); 
135
                                        }
136
                                        else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
137
                                        {
138
                                                serviceInfo.abstr = parser.nextText(); 
139
                                        }
140
                                        else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
141
                                        {
142
                                            String value = new String();
143
                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
144
                                        if (value != null){
145
                                                serviceInfo.online_resource = value;
146
                                        }
147
                                        }                                        
148
                                        else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
149
                                                        (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
150
                                        {
151
                                                parser.skipSubTree();
152
                                        }                                        
153
                                        break;
154
                                case KXmlParser.END_TAG:
155
                                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
156
                                                end = true;
157
                                        break;
158
                                case KXmlParser.TEXT:                                        
159
                                break;
160
                         }
161
             if (!end)
162
                 currentTag = parser.next();
163
            }
164
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
165
    }
166
    
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
            
175
            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
                                                //TODO:
190
                                                //Add to serviceInformation the supported exception formats.
191
                                        }
192
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
193
                                        {
194
                                                WMSLayer1_3_0 lyr = new WMSLayer1_3_0();
195
                        if (rootLayer == null)
196
                            rootLayer = lyr;
197
                        else {
198
                            // Handles when there is no general root layer, will use
199
                            // a fake non-queryable one.
200
                            if (!rootLayer.equals(getFakeRootLayer())){
201
                                WMSLayer1_3_0 aux = (WMSLayer1_3_0) rootLayer;
202
                                rootLayer  = getFakeRootLayer();
203
                                rootLayer.getChildren().add(aux);
204
                            }
205
                            rootLayer.getChildren().add(lyr);
206
                        }
207
                                                lyr.parse(parser, layers);
208
                                                
209
                        if (lyr.getName()!=null)
210
                                                    layers.put(lyr.getName(), lyr);                                                                                         
211
                                        }
212
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
213
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))                            
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
                                                //we could parse here the info of Capabilities request, but we actually never use it.
250
                                        }        
251
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
252
                                        {        
253
                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, null);
254
                                                parseGetMapTag(parser);                
255
                                        }
256
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
257
                                        {
258
                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, null);
259
                                                parseGetFeatureInfoTag(parser);                
260
                                        }        
261
                                        else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
262
                                        {
263
                                                serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null); 
264
                                        }        
265
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC)==0)
266
                                        {
267
                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, null);
268
                                                parseGetLegendGraphicTag(parser);
269
                                        }
270
                                        break;
271
                                case KXmlParser.END_TAG:
272
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
273
                                                end = true;
274
                                        break;
275
                                case KXmlParser.TEXT:                                        
276
                                break;
277
                         }
278
                         if (!end)
279
                                 currentTag = parser.next();
280
            }
281
            // TODO: does not get such a tag when arrives here!!!!!!
282
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);            
283
    }
284
    
285
    /**
286
     * <p>Parses the GetMap tag </p>
287
     */ 
288
    private void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException
289
    {        
290
            int currentTag;
291
            boolean end = false;
292
            
293
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
294
            currentTag = parser.next();
295
            
296
            while (!end) 
297
            {
298
                         switch(currentTag)
299
                         {
300
                                case KXmlParser.START_TAG:
301
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
302
                                        {
303
                                                serviceInfo.formats.add(parser.nextText());
304
                                        }        
305
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
306
                                        {                        
307
                                                currentTag = parser.nextTag();
308
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
309
                                                {
310
                                                        currentTag = parser.nextTag();
311
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
312
                                                        {
313
                                                                currentTag = parser.nextTag();
314
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
315
                                                                {
316
                                                                        String value = new String();
317
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
318
                                                                        if (value != null){
319
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, value);
320
                                                                        }                                                                        
321
                                                                }
322
                                                        }
323
                                                }
324
                                        }                        
325
                                        break;
326
                                case KXmlParser.END_TAG:
327
                                        if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0)
328
                                                end = true;
329
                                        break;
330
                                case KXmlParser.TEXT:                                        
331
                                break;
332
                         }
333
                         if(!end)
334
                                 currentTag = parser.next();
335
            }        
336
    }    
337
    
338
    /**
339
     * <p>Parses the GetFeatureInfo tag </p>
340
     */ 
341
    private void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException
342
    {        
343
            int currentTag;
344
            boolean end = false;
345
            
346
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
347
            currentTag = parser.next();
348
            
349
            while (!end) 
350
            {
351
                         switch(currentTag)
352
                         {
353
                                case KXmlParser.START_TAG:
354
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
355
                                        {
356
                                                //TODO:
357
                                                // add the supported formats by the GetFeatureInfo request
358
                                                //serviceInfo.formats.add(parser.nextText());
359
                                        }        
360
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
361
                                        {                        
362
                                                currentTag = parser.nextTag();
363
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
364
                                                {
365
                                                        currentTag = parser.nextTag();
366
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
367
                                                        {
368
                                                                currentTag = parser.nextTag();
369
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
370
                                                                {
371
                                                                        String value = new String();
372
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
373
                                                                        if (value != null){
374
                                                                                //serviceInfo.online_resource = value;
375
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, value);
376
                                                                        }
377
                                                                }
378
                                                        }
379
                                                }
380
                                        }                        
381
                                        break;
382
                                case KXmlParser.END_TAG:
383
                                        if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0)
384
                                                end = true;
385
                                        break;
386
                                case KXmlParser.TEXT:                                        
387
                                break;
388
                         }
389
                         if(!end)
390
                                 currentTag = parser.next();
391
            }        
392
    }         
393

    
394
    /**
395
     * <p>Parses the GetLegendGraphic tag </p>
396
     */ 
397
    private void parseGetLegendGraphicTag(KXmlParser parser) throws IOException, XmlPullParserException
398
    {        
399
            int currentTag;
400
            boolean end = false;
401
            
402
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETLEGENDGRAPHIC);
403
            currentTag = parser.next();
404
            
405
            while (!end) 
406
            {
407
                         switch(currentTag)
408
                         {
409
                                case KXmlParser.START_TAG:
410
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
411
                                        {
412
                                                //TODO:
413
                                                // add the supported formats by the GetLegendGraphic request
414
                                                //serviceInfo.formats.add(parser.nextText());
415
                                        }        
416
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
417
                                        {                        
418
                                                currentTag = parser.nextTag();
419
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
420
                                                {
421
                                                        currentTag = parser.nextTag();
422
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
423
                                                        {
424
                                                                currentTag = parser.nextTag();
425
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
426
                                                                {
427
                                                                        String value = new String();
428
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
429
                                                                        if (value != null){
430
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, value);
431
                                                                        }
432
                                                                }
433
                                                        }
434
                                                }
435
                                        }                        
436
                                        break;
437
                                case KXmlParser.END_TAG:
438
                                        if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC) == 0)
439
                                                end = true;
440
                                        break;
441
                                case KXmlParser.TEXT:                                        
442
                                break;
443
                         }
444
                         if(!end)
445
                                 currentTag = parser.next();
446
            }        
447
    } 
448
    
449
    private WMSLayer1_3_0 getFakeRootLayer(){
450
        if (fakeRootLayer == null){
451
            fakeRootLayer = new WMSLayer1_3_0();
452
            fakeRootLayer.setTitle(serviceInfo.title);
453
            fakeRootLayer.setQueryable(false);
454
            fakeRootLayer.setName(null);
455
        }
456
        return fakeRootLayer;
457
    }
458
    /* (non-Javadoc)
459
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
460
     */
461
    protected String parseException(byte[] data) {
462
        ArrayList errors = new ArrayList();
463
        KXmlParser kxmlParser = new KXmlParser();
464
        try
465
        {
466
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);        
467
            kxmlParser.nextTag();
468
            int tag;
469
            if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
470
            { 
471
                kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);             
472
                tag = kxmlParser.nextTag();
473
                 while(tag != KXmlParser.END_DOCUMENT)
474
                 {
475
                     switch(tag)
476
                     {
477
                        case KXmlParser.START_TAG:
478
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
479
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
480
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
481
                                String errorMessage = kxmlParser.nextText();
482
                                errors.add(errorCode+errorMessage);
483
                            }
484
                            break;
485
                        case KXmlParser.END_TAG:                            
486
                            break;
487
                        
488
                     }
489
                     tag = kxmlParser.nextTag();
490
                 }
491
                 //kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
492
            }
493
        }
494
        catch(XmlPullParserException parser_ex){
495
            System.out.println(parser_ex.getMessage());
496
            parser_ex.printStackTrace();
497
        }
498
        catch (IOException ioe) {           
499
            ioe.printStackTrace();            
500
        }
501
        String message = errors.size()>0? "" : null;
502
        for (int i = 0; i < errors.size(); i++) {
503
            message += (String) errors.get(i)+"\n";
504
        }
505
        return message;
506
    }
507
    
508
  }