Statistics
| Revision:

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

History | View | Annotate | Download (15 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.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.1.1
22
 * </p>
23
 */
24
public class WMSProtocolHandler1_1_1 extends org.gvsig.remoteClient.wms.WMSProtocolHandler {
25
        private WMSLayer1_1_1 fakeRootLayer;
26
    
27
        public WMSProtocolHandler1_1_1()
28
        {
29
                this.version = "1.1.1";
30
                this.name = "WMS1.1.1";
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
            } catch(FileNotFoundException ex)        {
60
                    ex.printStackTrace();
61
            } catch (IOException e) {
62
                        e.printStackTrace();
63
                }
64
            
65
            int tag;
66
            KXmlParser kxmlParser = null;
67
            kxmlParser = new KXmlParser();
68
            try
69
            {
70
                    kxmlParser.setInput(new FileInputStream(f), encoding);
71
                        kxmlParser.nextTag();
72
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
73
                    {                    
74
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);                            
75
                            tag = kxmlParser.nextTag();
76
                                 while(tag != KXmlParser.END_DOCUMENT)
77
                                 {
78
                     switch(tag)
79
                                         {
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
                             }
99
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
100
                    }
101
            }
102
            catch(XmlPullParserException parser_ex){
103
                    parser_ex.printStackTrace();
104
            }
105
                   catch (IOException ioe) {                        
106
                           ioe.printStackTrace();
107
                } finally {
108
            
109
        }
110
                   // 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
                                        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
                                        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
             if (!end)
163
                 currentTag = parser.next();
164
            }
165
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
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 serviceInfo the supported formats for the exceptions????
191
                                        }
192
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
193
                                        {
194
                                                WMSLayer1_1_1 lyr = new WMSLayer1_1_1();
195
                                                lyr.parse(parser, layers);
196
                                                
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_1 aux = (WMSLayer1_1_1) rootLayer;
204
                                rootLayer  = getFakeRootLayer();
205
                                rootLayer.getChildren().add(aux);
206
                            }
207
                            rootLayer.getChildren().add(lyr);
208
                        }
209
                        
210
                        if (lyr.getName()!=null)
211
                                                    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
                                        }
228
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
229
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
230
                            
231
                                        {
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
                         if (!end)
243
                                 currentTag = parser.next();
244
            }
245
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);            
246
    }
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
                                                serviceInfo.operations.put(CapabilitiesTags.GETCAPABILITIES, null); 
267
                                        }        
268
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
269
                                        {
270
                                                // 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
                                        }
276
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
277
                                        {
278
                                                //serviceInfo.operations.put(WMSProtocolHandler.GETFEATUREINFO_OPERATION)
279
                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, null);
280
                                                parseGetFeatureInfoTag(parser);
281
                                        }                
282
                                        else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
283
                                        {
284
                                                //serviceInfo.operations.put(WMSProtocolHandler.DESCRIBELAYER_OPERATION)
285
                                                serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null); 
286
                                        }                
287
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC)==0)
288
                                        {
289
                                                serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, null);
290
                                        }
291
                                        break;
292
                                case KXmlParser.END_TAG:
293
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
294
                                                end = true;
295
                                        break;
296
                                case KXmlParser.TEXT:                                        
297
                                break;
298
                         }
299
                         if(!end)
300
                                 currentTag = parser.next();
301
            }
302
            // TODO: does not get such a tag when arrives here!!!!!!
303
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);            
304
    }
305
    
306
    /**
307
     * <p>Parses the GetMap tag </p>
308
     */ 
309
    private void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException
310
    {        
311
            int currentTag;
312
            boolean end = false;
313
            
314
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
315
            currentTag = parser.next();
316
            
317
            while (!end) 
318
            {
319
                         switch(currentTag)
320
                         {
321
                                case KXmlParser.START_TAG:
322
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
323
                                        {
324
                                                serviceInfo.formats.add(parser.nextText());
325
                                        }        
326
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
327
                                        {                        
328
                                                currentTag = parser.nextTag();
329
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
330
                                                {
331
                                                        currentTag = parser.nextTag();
332
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
333
                                                        {
334
                                                                currentTag = parser.nextTag();
335
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
336
                                                                {
337
                                                                        String value = new String();
338
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
339
                                                                        if (value != null){
340
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, value);
341
                                                                        }
342
                                                                }
343
                                                        }
344
                                                }
345
                                        }                        
346
                                        break;
347
                                case KXmlParser.END_TAG:
348
                                        if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0)
349
                                                end = true;
350
                                        break;
351
                                case KXmlParser.TEXT:                                        
352
                                break;
353
                         }
354
                         if(!end)
355
                                 currentTag = parser.next();
356
            }        
357
    }    
358
    
359
    /**
360
     * <p>Parses the GetMap tag </p>
361
     */ 
362
    private void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException
363
    {        
364
            int currentTag;
365
            boolean end = false;
366
            
367
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
368
            currentTag = parser.next();
369
            
370
            while (!end) 
371
            {
372
                         switch(currentTag)
373
                         {
374
                                case KXmlParser.START_TAG:
375
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
376
                                        {
377
                                                //TODO:
378
                                                // add the supported formats by the GetFeatureInfo request
379
                                                //serviceInfo.formats.add(parser.nextText());
380
                                        }        
381
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
382
                                        {                        
383
                                                currentTag = parser.nextTag();
384
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
385
                                                {
386
                                                        currentTag = parser.nextTag();
387
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
388
                                                        {
389
                                                                currentTag = parser.nextTag();
390
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
391
                                                                {
392
                                                                        String value = new String();
393
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
394
                                                                        if (value != null){
395
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, value);
396
                                                                        }
397
                                                                }
398
                                                        }
399
                                                }
400
                                        }                        
401
                                        break;
402
                                case KXmlParser.END_TAG:
403
                                        if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0)
404
                                                end = true;
405
                                        break;
406
                                case KXmlParser.TEXT:                                        
407
                                break;
408
                         }
409
                         if(!end)
410
                                 currentTag = parser.next();
411
            }        
412
    }         
413

    
414
    private WMSLayer1_1_1 getFakeRootLayer(){
415
        if (fakeRootLayer == null){
416
            fakeRootLayer = new WMSLayer1_1_1();
417
            fakeRootLayer.setTitle(serviceInfo.title);
418
            fakeRootLayer.setQueryable(false);
419
            fakeRootLayer.setName(null);
420
        }
421
        return fakeRootLayer;
422
    }
423
    /* (non-Javadoc)
424
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
425
     */
426
    protected String parseException(byte[] data) {
427
        ArrayList errors = new ArrayList();
428
        KXmlParser kxmlParser = new KXmlParser();
429
        try
430
        {
431
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);        
432
            int tag;
433
            
434
            boolean end = false;
435
            tag = kxmlParser.nextTag();
436
            
437
            kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
438
            
439
            while (!end) 
440
            {
441
                switch(tag)
442
                {
443
                    case KXmlParser.START_TAG:
444
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
445
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
446
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
447
                                String errorMessage = kxmlParser.nextText();
448
                                errors.add(errorCode+errorMessage);
449
                            }
450
                            break;
451
                    case KXmlParser.END_TAG:   
452
                            if (kxmlParser.getName().compareTo(ExceptionTags.EXCEPTION_ROOT) == 0)
453
                                end = true;
454
                            break; 
455
                     }
456
                        if (!end)
457
                        {
458
                                tag = kxmlParser.nextTag();
459
                        }
460
                 }
461
                }
462
        catch(XmlPullParserException parser_ex){
463
            System.out.println(parser_ex.getMessage());
464
            parser_ex.printStackTrace();
465
        }
466
        catch (IOException ioe) {           
467
            ioe.printStackTrace();            
468
        }
469
        String message = errors.size()>0? "" : null;
470
        for (int i = 0; i < errors.size(); i++) {
471
            message += (String) errors.get(i)+"\n";
472
        }
473
        return message;
474
    }
475
    
476
  }