Statistics
| Revision:

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

History | View | Annotate | Download (15.6 KB)

1

    
2
package org.gvsig.remoteClient.wms.wms_1_1_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
import org.gvsig.remoteClient.utils.CapabilitiesTags;
14
import org.gvsig.remoteClient.utils.ExceptionTags;
15
import org.kxml2.io.KXmlParser;
16
import org.xmlpull.v1.XmlPullParserException;
17

    
18
/**
19
 * <p>
20
 * Describes the handler to comunicate to a WMS 1.1.0
21
 * </p>
22
 */
23
public class WMSProtocolHandler1_1_0 extends org.gvsig.remoteClient.wms.WMSProtocolHandler
24
{
25
        private WMSLayer1_1_0 fakeRootLayer;
26
    
27
        public WMSProtocolHandler1_1_0()
28
        {
29
                this.version = "1.1.0";
30
                this.name = "WMS1.1.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_1_1);                            
76
                            tag = kxmlParser.nextTag();
77
                                 while(tag != KXmlParser.END_DOCUMENT)
78
                                 {
79
                     switch(tag)
80
                                         {
81
                         
82
                                                case KXmlParser.START_TAG:
83
                                                        if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
84
                                                        {
85
                                                                parseServiceTag(kxmlParser);
86
                                                        }        
87
                                                        else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
88
                                                        {
89
                                                                parseCapabilityTag(kxmlParser);
90
                                                        }
91
                                                        break;
92
                                                case KXmlParser.END_TAG:                                                        
93
                                                        break;
94
                                                case KXmlParser.TEXT:
95
                                                                                                
96
                                                break;
97
                                         }
98
                                     tag = kxmlParser.next();
99
                             }
100

    
101
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
102
                    }
103
            }
104
            catch(XmlPullParserException parser_ex){
105
                    parser_ex.printStackTrace();
106
            }
107
                   catch (IOException ioe) {                        
108
                           ioe.printStackTrace();
109
                } finally {
110
            
111
        }
112
    } 
113
    
114
    /**
115
     * <p>Parses the Service Information </p>
116
     */    
117
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException 
118
    {
119
            int currentTag;
120
            boolean end = false;
121
            
122
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
123
            currentTag = parser.next();
124
            
125
            while (!end) 
126
            {
127
                         switch(currentTag)
128
                         {
129
                                case KXmlParser.START_TAG:
130
                                        if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
131
                                        {
132
                                                serviceInfo.name = parser.nextText(); 
133
                                        }        
134
                                        else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
135
                                        {
136
                                                serviceInfo.title = parser.nextText(); 
137
                                        }
138
                                        else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
139
                                        {
140
                                                serviceInfo.abstr = parser.nextText(); 
141
                                        }
142
                                        else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
143
                                        {
144
                                            String value = null;
145
                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
146
                                        if (value != null){
147
                                                serviceInfo.online_resource = value;
148
                                        }
149
                                        }                                        
150
                                        else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
151
                                                        (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
152
                                        {
153
                                                parser.skipSubTree();
154
                                        }                                        
155
                                        break;
156
                                case KXmlParser.END_TAG:
157
                                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
158
                                                end = true;
159
                                        break;
160
                                case KXmlParser.TEXT:                                        
161
                                break;
162
                         }
163
             if (!end)
164
                 currentTag = parser.next();
165
            }
166
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
167
    }
168
    
169
    /**
170
     * <p>Parses the Capability Tag </p>
171
     */    
172
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
173
    {         
174
            int currentTag;
175
            boolean end = false;
176
            
177
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
178
            currentTag = parser.next();
179
            
180
            while (!end) 
181
            {
182
                         switch(currentTag)
183
                         {
184
                                case KXmlParser.START_TAG:
185
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
186
                                        {
187
                                                parseRequestTag(parser); 
188
                                        }        
189
                                        else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
190
                                        {
191
                                                // TODO:
192
                                                // add the exception formats supported.
193
                                        }
194
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
195
                                        {
196
                                                WMSLayer1_1_0 lyr = new WMSLayer1_1_0();
197
                        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_0 aux = (WMSLayer1_1_0) rootLayer;
204
                                rootLayer  = getFakeRootLayer();
205
                                rootLayer.getChildren().add(aux);
206
                            }
207
                            rootLayer.getChildren().add(lyr);
208
                        }
209
                                                lyr.parse(parser, layers);
210
                                                
211
                        if (lyr.getName()!=null)
212
                                                    layers.put(lyr.getName(), lyr);                                                                                                         
213
                                        }
214
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
215
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
216
                            
217
                                        {
218
                                                parser.skipSubTree();
219
                                        }                                        
220
                                        break;
221
                                case KXmlParser.END_TAG:
222
                                        if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
223
                                                end = true;
224
                                        break;
225
                                case KXmlParser.TEXT:                                        
226
                                break;
227
                         }
228
                         if (!end)
229
                         currentTag = parser.next();
230
            }
231
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);            
232
    }
233
    
234
    /**
235
     * <p>Parses the Request tag </p>
236
     */ 
237
    private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
238
    {        
239
            int currentTag;
240
            boolean end = false;
241
            
242
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
243
            currentTag = parser.next();
244
            
245
            while (!end) 
246
            {
247
                         switch(currentTag)
248
                         {
249
                                case KXmlParser.START_TAG:
250
                                        if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
251
                                        {
252
                                                serviceInfo.operations.put(CapabilitiesTags.GETCAPABILITIES , null); 
253
                                        }        
254
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
255
                                        {        
256
                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, null);
257
                                                parseGetMapTag(parser);                                                
258
                                        }
259
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
260
                                        {
261
                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, null); 
262
                                                parseGetFeatureInfoTag(parser);
263
                                        }                
264
                                        else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
265
                                        {
266
                                                serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null); 
267
                                        }        
268
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC)==0)
269
                                        {
270
                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, null); 
271
                                                parseGetLegendGraphicTag(parser);
272
                                        }                                        
273
                                        break;
274
                                case KXmlParser.END_TAG:
275
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
276
                                                end = true;
277
                                        break;
278
                                case KXmlParser.TEXT:                                        
279
                                break;
280
                         }
281
                         if(!end)
282
                                 currentTag = parser.next();
283
            }
284
            // TODO: does not get such a tag when arrives here!!!!!!
285
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);            
286
    }
287

    
288
    /**
289
     * <p>Parses the GetMap tag </p>
290
     */ 
291
    private void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException
292
    {        
293
            int currentTag;
294
            boolean end = false;
295
            
296
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
297
            currentTag = parser.next();
298
            
299
            while (!end) 
300
            {
301
                         switch(currentTag)
302
                         {
303
                                case KXmlParser.START_TAG:
304
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
305
                                        {
306
                                                serviceInfo.formats.add(parser.nextText());
307
                                        }        
308
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
309
                                        {                        
310
                                                currentTag = parser.nextTag();
311
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
312
                                                {
313
                                                        currentTag = parser.nextTag();
314
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
315
                                                        {
316
                                                                currentTag = parser.nextTag();
317
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
318
                                                                {
319
                                                                        String value = new String();
320
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
321
                                                                        if (value != null){
322
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, value);
323
                                                                        }
324
                                                                }
325
                                                        }
326
                                                }
327
                                        }                        
328
                                        break;
329
                                case KXmlParser.END_TAG:
330
                                        if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0)
331
                                                end = true;
332
                                        break;
333
                                case KXmlParser.TEXT:                                        
334
                                break;
335
                         }
336
                         if(!end)
337
                                 currentTag = parser.next();
338
            }        
339
    }    
340
    
341
    /**
342
     * <p>Parses the GetFeatureInfo tag </p>
343
     */ 
344
    private void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException
345
    {        
346
            int currentTag;
347
            boolean end = false;
348
            
349
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
350
            currentTag = parser.next();
351
            
352
            while (!end) 
353
            {
354
                         switch(currentTag)
355
                         {
356
                                case KXmlParser.START_TAG:
357
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
358
                                        {
359
                                                //TODO:
360
                                                // add the supported formats by the GetFeatureInfo request
361
                                                //serviceInfo.formats.add(parser.nextText());
362
                                        }        
363
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
364
                                        {                        
365
                                                currentTag = parser.nextTag();
366
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
367
                                                {
368
                                                        currentTag = parser.nextTag();
369
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
370
                                                        {
371
                                                                currentTag = parser.nextTag();
372
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
373
                                                                {
374
                                                                        String value = new String();
375
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
376
                                                                        if (value != null){
377
                                                                                //serviceInfo.online_resource = value;
378
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, value);
379
                                                                        }
380
                                                                }
381
                                                        }
382
                                                }
383
                                        }                        
384
                                        break;
385
                                case KXmlParser.END_TAG:
386
                                        if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0)
387
                                                end = true;
388
                                        break;
389
                                case KXmlParser.TEXT:                                        
390
                                break;
391
                         }
392
                         if(!end)
393
                                 currentTag = parser.next();
394
            }        
395
    }     
396
 
397
    /**
398
     * <p>Parses the GetLegendGraphic tag </p>
399
     */ 
400
    private void parseGetLegendGraphicTag(KXmlParser parser) throws IOException, XmlPullParserException
401
    {        
402
            int currentTag;
403
            boolean end = false;
404
            
405
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETLEGENDGRAPHIC);
406
            currentTag = parser.next();
407
            
408
            while (!end) 
409
            {
410
                         switch(currentTag)
411
                         {
412
                                case KXmlParser.START_TAG:
413
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
414
                                        {
415
                                                //TODO:
416
                                                // add the supported formats by the GetLegendGraphic request
417
                                                //serviceInfo.formats.add(parser.nextText());
418
                                        }        
419
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
420
                                        {                        
421
                                                currentTag = parser.nextTag();
422
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
423
                                                {
424
                                                        currentTag = parser.nextTag();
425
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
426
                                                        {
427
                                                                currentTag = parser.nextTag();
428
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
429
                                                                {
430
                                                                        String value = new String();
431
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
432
                                                                        if (value != null){
433
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, value);
434
                                                                        }
435
                                                                }
436
                                                        }
437
                                                }
438
                                        }                        
439
                                        break;
440
                                case KXmlParser.END_TAG:
441
                                        if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC) == 0)
442
                                                end = true;
443
                                        break;
444
                                case KXmlParser.TEXT:                                        
445
                                break;
446
                         }
447
                         if(!end)
448
                                 currentTag = parser.next();
449
            }        
450
    }
451
    
452
    private WMSLayer1_1_0 getFakeRootLayer(){
453
        if (fakeRootLayer == null){
454
            fakeRootLayer = new WMSLayer1_1_0();
455
            fakeRootLayer.setTitle(serviceInfo.title);
456
            fakeRootLayer.setQueryable(false);
457
            fakeRootLayer.setName(null);
458
        }
459
        return fakeRootLayer;
460
    }
461
    
462
    /* (non-Javadoc)
463
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
464
     */
465
    protected String parseException(byte[] data) {
466
        ArrayList errors = new ArrayList();
467
        KXmlParser kxmlParser = new KXmlParser();
468
        
469
        try
470
        {
471
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);        
472
            kxmlParser.nextTag();
473
            int tag;
474
            if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
475
            { 
476
                kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);             
477
                tag = kxmlParser.nextTag();
478
                 while(tag != KXmlParser.END_DOCUMENT)
479
                 {
480
                     switch(tag)
481
                     {
482
                        case KXmlParser.START_TAG:
483
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
484
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
485
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
486
                                String errorMessage = kxmlParser.nextText();
487
                                errors.add(errorCode+errorMessage);
488
                            }
489
                            break;
490
                        case KXmlParser.END_TAG:                            
491
                            break;
492
                        
493
                     }
494
                     tag = kxmlParser.nextTag();
495
                 }
496
                 //kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
497
            }
498
        }
499
        catch(XmlPullParserException parser_ex){
500
            System.out.println(parser_ex.getMessage());
501
            parser_ex.printStackTrace();
502
        }
503
        catch (IOException ioe) {           
504
            ioe.printStackTrace();            
505
        }
506
        String message = errors.size()>0? "" : null;
507
        for (int i = 0; i < errors.size(); i++) {
508
            message += (String) errors.get(i)+"\n";
509
        }
510
        return message;
511
    }
512
    
513
  }