Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / wms_1_1_1 / WMSProtocolHandler1_1_1.java @ 4623

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.io.InputStream;
12
import java.io.InputStreamReader;
13
import java.io.Reader;
14
import java.io.UnsupportedEncodingException;
15
import java.util.ArrayList;
16
import java.util.TreeMap;
17

    
18
import org.gvsig.remoteClient.utils.CapabilitiesTags;
19
import org.gvsig.remoteClient.utils.ExceptionTags;
20
import org.kxml2.io.KXmlParser;
21
import org.xmlpull.v1.XmlPullParserException;
22

    
23
/**
24
 * <p>
25
 * Describes the handler to comunicate to a WMS 1.1.1
26
 * </p>
27
 */
28
public class WMSProtocolHandler1_1_1 extends org.gvsig.remoteClient.wms.WMSProtocolHandler {
29
        private WMSLayer1_1_1 fakeRootLayer;
30
    
31
        public WMSProtocolHandler1_1_1()
32
        {
33
                this.version = "1.1.1";
34
                this.name = "WMS1.1.1";
35
                this.serviceInfo = new ServiceInformation(); 
36
                this.layers = new TreeMap();
37
        }
38
    
39
//------------------------------------------------------------------------------
40
// Parsing methods....    
41
//------------------------------------------------------------------------------    
42
/**
43
 * <p>Parse the xml data retrieved from the WMS, it will parse the WMS Capabilities</p>
44
 * 
45
 */
46
    public void parse(File f)
47
    {      
48
            FileReader reader = null;            
49
            try
50
            {
51
                    reader = new FileReader(f);
52
                    BufferedReader br = new BufferedReader(reader);
53
                    char[] buffer = new char[100];
54
                    br.read(buffer);
55
                    StringBuffer st = new StringBuffer(new String(buffer));
56
                    String searchText = "encoding=\"";
57
                    int index = st.indexOf(searchText);
58
                    if (index>-1) {
59
                            st.delete(0, index+searchText.length());
60
                            encoding = st.substring(0, st.indexOf("\""));
61
                    }
62
            } catch(FileNotFoundException ex)        {
63
                    ex.printStackTrace();
64
            } catch (IOException e) {
65
                        e.printStackTrace();
66
                }
67
            
68
            int tag;
69
            KXmlParser kxmlParser = null;
70
            kxmlParser = new KXmlParser();
71
            try
72
            {
73
                    kxmlParser.setInput(new FileInputStream(f), encoding);
74
                        kxmlParser.nextTag();
75
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
76
                    {                    
77
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);                            
78
                            tag = kxmlParser.nextTag();
79
                                 while(tag != KXmlParser.END_DOCUMENT)
80
                                 {
81
                     switch(tag)
82
                                         {
83
                         
84
                                                case KXmlParser.START_TAG:
85
                                                        if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
86
                                                        {
87
                                                                parseServiceTag(kxmlParser);
88
                                                        }        
89
                                                        else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
90
                                                        {
91
                                                                parseCapabilityTag(kxmlParser);
92
                                                        }
93
                                                        break;
94
                                                case KXmlParser.END_TAG:                                                        
95
                                                        break;
96
                                                case KXmlParser.TEXT:
97
                                                        //System.out.println("[TEXT]["+kxmlParser.getText()+"]");                                                        
98
                                                break;
99
                                         }
100
                                     tag = kxmlParser.next();
101
                             }
102
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
103
                    }
104
            }
105
            catch(XmlPullParserException parser_ex){
106
                    parser_ex.printStackTrace();
107
            }
108
                   catch (IOException ioe) {                        
109
                           ioe.printStackTrace();
110
                } finally {
111
            
112
        }
113
                   // In the parsing process the layer has been filled                  
114
    } 
115
    
116
    /**
117
     * <p>Parses the Service Information </p>
118
     */    
119
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException 
120
    {
121
            int currentTag;
122
            boolean end = false;
123
            
124
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
125
            currentTag = parser.next();
126
            
127
            while (!end) 
128
            {
129
                         switch(currentTag)
130
                         {
131
                                case KXmlParser.START_TAG:
132
                                        if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
133
                                        {
134
                                                serviceInfo.name = parser.nextText(); 
135
                                        }        
136
                                        else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
137
                                        {
138
                                                serviceInfo.title = parser.nextText(); 
139
                                        }
140
                                        else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
141
                                        {
142
                                                serviceInfo.abstr = parser.nextText(); 
143
                                        }
144
                                        else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
145
                                        {
146
                                            String value = new String();
147
                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
148
                                        if (value != null){
149
                                                serviceInfo.online_resource = value;
150
                                        }
151
                                        }
152
                                        else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
153
                                                        (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
154
                                        {
155
                                                parser.skipSubTree();
156
                                        }                                        
157
                                        break;
158
                                case KXmlParser.END_TAG:
159
                                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
160
                                                end = true;
161
                                        break;
162
                                case KXmlParser.TEXT:                                        
163
                                break;
164
                         }
165
             if (!end)
166
                 currentTag = parser.next();
167
            }
168
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
169
    }
170
    /**
171
     * <p>Parses the Capability Tag </p>
172
     */    
173
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
174
    {         
175
            int currentTag;
176
            boolean end = false;
177
 
178
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
179
            currentTag = parser.next();
180
            
181
            while (!end) 
182
            {
183
                         switch(currentTag)
184
                         {
185
                                case KXmlParser.START_TAG:
186
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
187
                                        {
188
                                                parseRequestTag(parser); 
189
                                        }        
190
                                        else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
191
                                        {
192
                                                //TODO:
193
                                                //Add to serviceInfo the supported formats for the exceptions????
194
                                        }
195
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
196
                                        {
197
                                                WMSLayer1_1_1 lyr = new WMSLayer1_1_1();
198
                                                lyr.parse(parser, layers);
199
                                                
200
                        if (rootLayer == null)
201
                            rootLayer = lyr;
202
                        else {
203
                            // Handles when there is no general root layer, will use
204
                            // a fake non-queryable one.
205
                            if (!rootLayer.equals(getFakeRootLayer())){
206
                                WMSLayer1_1_1 aux = (WMSLayer1_1_1) rootLayer;
207
                                rootLayer  = getFakeRootLayer();
208
                                rootLayer.getChildren().add(aux);
209
                            }
210
                            rootLayer.getChildren().add(lyr);
211
                        }
212
                        
213
                        if (lyr.getName()!=null)
214
                                                    layers.put(lyr.getName(), lyr); 
215
                                                
216
//                        Collection layerCollection = layers.values(); 
217
//                        Iterator iter = layerCollection.iterator();
218
//                        while (iter.hasNext())
219
//                        {
220
//                                WMSLayer1_1_1 layer = (WMSLayer1_1_1)iter.next(); 
221
//                                                    //Add all the SRS that the layer supports to the WMSProtocolHandler if they dont exist already
222
//                                                    for (i=0;i<layer.getAllSrs().size();i++)
223
//                                                    {                                                           
224
////                                                        if (!layer.srs.contains(layer.getAllSrs().elementAt(i)))
225
////                                                        {
226
////                                                            this.srs.add(layer.getAllSrs().elementAt(i));
227
////                                                        }
228
//                                                    }                                
229
//                        }                                                
230
                                        }
231
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
232
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
233
                            
234
                                        {
235
                                                parser.skipSubTree();
236
                                        }                                        
237
                                        break;
238
                                case KXmlParser.END_TAG:
239
                                        if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
240
                                                end = true;
241
                                        break;
242
                                case KXmlParser.TEXT:                                        
243
                                break;
244
                         }
245
                         if (!end)
246
                                 currentTag = parser.next();
247
            }
248
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);            
249
    }
250
    
251
    /**
252
     * <p>Parses the Request tag </p>
253
     */ 
254
    private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
255
    {        
256
            int currentTag;
257
            boolean end = false;
258
            
259
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
260
            currentTag = parser.next();
261
            
262
            while (!end) 
263
            {
264
                         switch(currentTag)
265
                         {
266
                                case KXmlParser.START_TAG:
267
                                        if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
268
                                        {
269
                                                serviceInfo.operations.put(CapabilitiesTags.GETCAPABILITIES, null); 
270
                                        }        
271
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
272
                                        {
273
                                                // put a null to this key?
274
                                                // or leave it like it was with a CODE in a vector specifying this operation.
275
                                                // WMSProtocolHandler.GETMAP_OPERATION 
276
                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, null); 
277
                                                parseGetMapTag(parser);
278
                                        }
279
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
280
                                        {
281
                                                //serviceInfo.operations.put(WMSProtocolHandler.GETFEATUREINFO_OPERATION)
282
                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, null);
283
                                                parseGetFeatureInfoTag(parser);
284
                                        }                
285
                                        else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
286
                                        {
287
                                                //serviceInfo.operations.put(WMSProtocolHandler.DESCRIBELAYER_OPERATION)
288
                                                serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null); 
289
                                        }                                        
290
                                        break;
291
                                case KXmlParser.END_TAG:
292
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
293
                                                end = true;
294
                                        break;
295
                                case KXmlParser.TEXT:                                        
296
                                break;
297
                         }
298
                         if(!end)
299
                                 currentTag = parser.next();
300
            }
301
            // TODO: does not get such a tag when arrives here!!!!!!
302
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);            
303
    }
304
    
305
    /**
306
     * <p>Parses the GetMap tag </p>
307
     */ 
308
    private void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException
309
    {        
310
            int currentTag;
311
            boolean end = false;
312
            
313
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
314
            currentTag = parser.next();
315
            
316
            while (!end) 
317
            {
318
                         switch(currentTag)
319
                         {
320
                                case KXmlParser.START_TAG:
321
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
322
                                        {
323
                                                serviceInfo.formats.add(parser.nextText());
324
                                        }        
325
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
326
                                        {                        
327
                                                currentTag = parser.nextTag();
328
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
329
                                                {
330
                                                        currentTag = parser.nextTag();
331
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
332
                                                        {
333
                                                                currentTag = parser.nextTag();
334
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
335
                                                                {
336
                                                                        String value = new String();
337
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
338
                                                                        if (value != null){
339
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, value);
340
                                                                        }
341
                                                                }
342
                                                        }
343
                                                }
344
                                        }                        
345
                                        break;
346
                                case KXmlParser.END_TAG:
347
                                        if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0)
348
                                                end = true;
349
                                        break;
350
                                case KXmlParser.TEXT:                                        
351
                                break;
352
                         }
353
                         if(!end)
354
                                 currentTag = parser.next();
355
            }        
356
    }    
357
    
358
    /**
359
     * <p>Parses the GetMap tag </p>
360
     */ 
361
    private void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException
362
    {        
363
            int currentTag;
364
            boolean end = false;
365
            
366
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
367
            currentTag = parser.next();
368
            
369
            while (!end) 
370
            {
371
                         switch(currentTag)
372
                         {
373
                                case KXmlParser.START_TAG:
374
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
375
                                        {
376
                                                //TODO:
377
                                                // add the supported formats by the GetFeatureInfo request
378
                                                //serviceInfo.formats.add(parser.nextText());
379
                                        }        
380
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
381
                                        {                        
382
                                                currentTag = parser.nextTag();
383
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
384
                                                {
385
                                                        currentTag = parser.nextTag();
386
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
387
                                                        {
388
                                                                currentTag = parser.nextTag();
389
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
390
                                                                {
391
                                                                        String value = new String();
392
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
393
                                                                        if (value != null){
394
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, value);
395
                                                                        }
396
                                                                }
397
                                                        }
398
                                                }
399
                                        }                        
400
                                        break;
401
                                case KXmlParser.END_TAG:
402
                                        if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0)
403
                                                end = true;
404
                                        break;
405
                                case KXmlParser.TEXT:                                        
406
                                break;
407
                         }
408
                         if(!end)
409
                                 currentTag = parser.next();
410
            }        
411
    }         
412

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