Statistics
| Revision:

root / branches / v05 / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / wms_1_1_1 / WMSProtocolHandler1_1_1.java @ 4036

History | View | Annotate | Download (14.4 KB)

1

    
2
package org.gvsig.remoteClient.wms.wms_1_1_1;
3

    
4
import java.io.ByteArrayInputStream;
5
import java.io.File;
6
import java.io.FileNotFoundException;
7
import java.io.FileReader;
8
import java.io.IOException;
9
import java.io.InputStreamReader;
10
import java.io.Reader;
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
            FileReader reader = null;            
45
            try
46
            {
47
                    reader = new FileReader(f);
48
            }
49
            catch(FileNotFoundException ex)        {
50
                    ex.printStackTrace();
51
            }
52
            
53
            int tag;
54
            KXmlParser kxmlParser = null;
55
            kxmlParser = new KXmlParser();
56
            try
57
            {
58
                    kxmlParser.setInput(reader);                
59
                        kxmlParser.nextTag();
60
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
61
                    {                    
62
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);                            
63
                            tag = kxmlParser.nextTag();
64
                                 while(tag != KXmlParser.END_DOCUMENT)
65
                                 {
66
                     switch(tag)
67
                                         {
68
                         
69
                                                case KXmlParser.START_TAG:
70
                                                        if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
71
                                                        {
72
                                                                parseServiceTag(kxmlParser);
73
                                                        }        
74
                                                        else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
75
                                                        {
76
                                                                parseCapabilityTag(kxmlParser);
77
                                                        }
78
                                                        break;
79
                                                case KXmlParser.END_TAG:                                                        
80
                                                        break;
81
                                                case KXmlParser.TEXT:
82
                                                        //System.out.println("[TEXT]["+kxmlParser.getText()+"]");                                                        
83
                                                break;
84
                                         }
85
                                     tag = kxmlParser.next();
86
                             }
87
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
88
                    }
89
            }
90
            catch(XmlPullParserException parser_ex){
91
                    parser_ex.printStackTrace();
92
            }
93
                   catch (IOException ioe) {                        
94
                           ioe.printStackTrace();
95
                } finally {
96
            
97
        }
98
                   // In the parsing process the layer has been filled                  
99
    } 
100
    
101
    /**
102
     * <p>Parses the Service Information </p>
103
     */    
104
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException 
105
    {
106
            int currentTag;
107
            boolean end = false;
108
            
109
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
110
            currentTag = parser.next();
111
            
112
            while (!end) 
113
            {
114
                         switch(currentTag)
115
                         {
116
                                case KXmlParser.START_TAG:
117
                                        if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
118
                                        {
119
                                                serviceInfo.name = parser.nextText(); 
120
                                        }        
121
                                        else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
122
                                        {
123
                                                serviceInfo.title = parser.nextText(); 
124
                                        }
125
                                        else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
126
                                        {
127
                                                serviceInfo.abstr = parser.nextText(); 
128
                                        }
129
                                        else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
130
                                        {
131
                                            String value = new String();
132
                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
133
                                        if (value != null){
134
                                                serviceInfo.online_resource = value;
135
                                        }
136
                                        }
137
                                        else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
138
                                                        (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
139
                                        {
140
                                                parser.skipSubTree();
141
                                        }                                        
142
                                        break;
143
                                case KXmlParser.END_TAG:
144
                                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
145
                                                end = true;
146
                                        break;
147
                                case KXmlParser.TEXT:                                        
148
                                break;
149
                         }
150
             if (!end)
151
                 currentTag = parser.next();
152
            }
153
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
154
    }
155
    /**
156
     * <p>Parses the Capability Tag </p>
157
     */    
158
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
159
    {         
160
            int currentTag;
161
            boolean end = false;
162
 
163
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
164
            currentTag = parser.next();
165
            
166
            while (!end) 
167
            {
168
                         switch(currentTag)
169
                         {
170
                                case KXmlParser.START_TAG:
171
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
172
                                        {
173
                                                parseRequestTag(parser); 
174
                                        }        
175
                                        else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
176
                                        {
177
                                                //TODO:
178
                                                //Add to serviceInfo the supported formats for the exceptions????
179
                                        }
180
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
181
                                        {
182
                                                WMSLayer1_1_1 lyr = new WMSLayer1_1_1();
183
                                                lyr.parse(parser, layers);
184
                                                
185
                        if (rootLayer == null)
186
                            rootLayer = lyr;
187
                        else {
188
                            // Handles when there is no general root layer, will use
189
                            // a fake non-queryable one.
190
                            if (!rootLayer.equals(getFakeRootLayer())){
191
                                WMSLayer1_1_1 aux = (WMSLayer1_1_1) rootLayer;
192
                                rootLayer  = getFakeRootLayer();
193
                                rootLayer.getChildren().add(aux);
194
                            }
195
                            rootLayer.getChildren().add(lyr);
196
                        }
197
                        
198
                        if (lyr.getName()!=null)
199
                                                    layers.put(lyr.getName(), lyr); 
200
                                                
201
//                        Collection layerCollection = layers.values(); 
202
//                        Iterator iter = layerCollection.iterator();
203
//                        while (iter.hasNext())
204
//                        {
205
//                                WMSLayer1_1_1 layer = (WMSLayer1_1_1)iter.next(); 
206
//                                                    //Add all the SRS that the layer supports to the WMSProtocolHandler if they dont exist already
207
//                                                    for (i=0;i<layer.getAllSrs().size();i++)
208
//                                                    {                                                           
209
////                                                        if (!layer.srs.contains(layer.getAllSrs().elementAt(i)))
210
////                                                        {
211
////                                                            this.srs.add(layer.getAllSrs().elementAt(i));
212
////                                                        }
213
//                                                    }                                
214
//                        }                                                
215
                                        }
216
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
217
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
218
                            
219
                                        {
220
                                                parser.skipSubTree();
221
                                        }                                        
222
                                        break;
223
                                case KXmlParser.END_TAG:
224
                                        if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
225
                                                end = true;
226
                                        break;
227
                                case KXmlParser.TEXT:                                        
228
                                break;
229
                         }
230
                         currentTag = parser.next();
231
            }
232
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);            
233
    }
234
    
235
    /**
236
     * <p>Parses the Request tag </p>
237
     */ 
238
    private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
239
    {        
240
            int currentTag;
241
            boolean end = false;
242
            
243
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
244
            currentTag = parser.next();
245
            
246
            while (!end) 
247
            {
248
                         switch(currentTag)
249
                         {
250
                                case KXmlParser.START_TAG:
251
                                        if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
252
                                        {
253
                                                serviceInfo.operations.put(CapabilitiesTags.GETCAPABILITIES, null); 
254
                                        }        
255
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
256
                                        {
257
                                                // put a null to this key?
258
                                                // or leave it like it was with a CODE in a vector specifying this operation.
259
                                                // WMSProtocolHandler.GETMAP_OPERATION 
260
                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, null); 
261
                                                parseGetMapTag(parser);
262
                                        }
263
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
264
                                        {
265
                                                //serviceInfo.operations.put(WMSProtocolHandler.GETFEATUREINFO_OPERATION)
266
                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, null);
267
                                                parseGetFeatureInfoTag(parser);
268
                                        }                
269
                                        else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
270
                                        {
271
                                                //serviceInfo.operations.put(WMSProtocolHandler.DESCRIBELAYER_OPERATION)
272
                                                serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null); 
273
                                        }                                        
274
                                        break;
275
                                case KXmlParser.END_TAG:
276
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
277
                                                end = true;
278
                                        break;
279
                                case KXmlParser.TEXT:                                        
280
                                break;
281
                         }
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
                         currentTag = parser.next();
337
            }        
338
    }    
339
    
340
    /**
341
     * <p>Parses the GetMap tag </p>
342
     */ 
343
    private void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException
344
    {        
345
            int currentTag;
346
            boolean end = false;
347
            
348
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
349
            currentTag = parser.next();
350
            
351
            while (!end) 
352
            {
353
                         switch(currentTag)
354
                         {
355
                                case KXmlParser.START_TAG:
356
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
357
                                        {
358
                                                //TODO:
359
                                                // add the supported formats by the GetFeatureInfo request
360
                                                //serviceInfo.formats.add(parser.nextText());
361
                                        }        
362
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
363
                                        {                        
364
                                                currentTag = parser.nextTag();
365
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
366
                                                {
367
                                                        currentTag = parser.nextTag();
368
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
369
                                                        {
370
                                                                currentTag = parser.nextTag();
371
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
372
                                                                {
373
                                                                        String value = new String();
374
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
375
                                                                        if (value != null){
376
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, value);
377
                                                                        }
378
                                                                }
379
                                                        }
380
                                                }
381
                                        }                        
382
                                        break;
383
                                case KXmlParser.END_TAG:
384
                                        if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0)
385
                                                end = true;
386
                                        break;
387
                                case KXmlParser.TEXT:                                        
388
                                break;
389
                         }
390
                         currentTag = parser.next();
391
            }        
392
    }         
393

    
394
    private WMSLayer1_1_1 getFakeRootLayer(){
395
        if (fakeRootLayer == null){
396
            fakeRootLayer = new WMSLayer1_1_1();
397
            fakeRootLayer.setTitle(serviceInfo.title);
398
            fakeRootLayer.setQueryable(false);
399
            fakeRootLayer.setName(null);
400
        }
401
        return fakeRootLayer;
402
    }
403
    /* (non-Javadoc)
404
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
405
     */
406
    protected String parseException(byte[] data) {
407
        ArrayList errors = new ArrayList();
408
        KXmlParser kxmlParser = new KXmlParser();
409
        Reader reader = new InputStreamReader(new ByteArrayInputStream(data));
410
        try
411
        {
412
            kxmlParser.setInput(reader);        
413
            int tag;
414
            
415
            boolean end = false;
416
            tag = kxmlParser.nextTag();
417
            
418
            kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
419
            
420
            while (!end) 
421
            {
422
                switch(tag)
423
                {
424
                    case KXmlParser.START_TAG:
425
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
426
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
427
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
428
                                String errorMessage = kxmlParser.nextText();
429
                                errors.add(errorCode+errorMessage);
430
                            }
431
                            break;
432
                    case KXmlParser.END_TAG:   
433
                            if (kxmlParser.getName().compareTo(ExceptionTags.EXCEPTION_ROOT) == 0)
434
                                end = true;
435
                            break; 
436
                     }
437
                        if (!end)
438
                        {
439
                                tag = kxmlParser.nextTag();
440
                        }
441
                 }
442
                }
443
        catch(XmlPullParserException parser_ex){
444
            System.out.println(parser_ex.getMessage());
445
            parser_ex.printStackTrace();
446
        }
447
        catch (IOException ioe) {           
448
            ioe.printStackTrace();            
449
        }
450
        String message = errors.size()>0? "" : null;
451
        for (int i = 0; i < errors.size(); i++) {
452
            message += (String) errors.get(i)+"\n";
453
        }
454
        return message;
455
    }
456
    
457
  }