Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / wms_1_1_0 / WMSProtocolHandler1_1_0.java @ 5539

History | View | Annotate | Download (13.8 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
                                        break;
269
                                case KXmlParser.END_TAG:
270
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
271
                                                end = true;
272
                                        break;
273
                                case KXmlParser.TEXT:                                        
274
                                break;
275
                         }
276
                         if(!end)
277
                                 currentTag = parser.next();
278
            }
279
            // TODO: does not get such a tag when arrives here!!!!!!
280
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);            
281
    }
282

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