Statistics
| Revision:

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

History | View | Annotate | Download (13.3 KB)

1 3687 ldiaz
2
package org.gvsig.remoteClient.wms.wms_1_1_0;
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
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 4036 ldiaz
public class WMSProtocolHandler1_1_0 extends org.gvsig.remoteClient.wms.WMSProtocolHandler
24
{
25 3687 ldiaz
        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
            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 4036 ldiaz
83 3687 ldiaz
                                                break;
84
                                         }
85
                                     tag = kxmlParser.next();
86
                             }
87 4036 ldiaz
88 3687 ldiaz
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
89
                    }
90
            }
91
            catch(XmlPullParserException parser_ex){
92
                    parser_ex.printStackTrace();
93
            }
94
                   catch (IOException ioe) {
95
                           ioe.printStackTrace();
96
                } finally {
97
98
        }
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 4019 ldiaz
                                        else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
130
                                        {
131
                                            String value = null;
132
                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
133
                                        if (value != null){
134
                                                serviceInfo.online_resource = value;
135
                                        }
136
                                        }
137 3687 ldiaz
                                        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 4036 ldiaz
                                                // TODO:
178
                                                // add the exception formats supported.
179 3687 ldiaz
                                        }
180
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
181
                                        {
182
                                                WMSLayer1_1_0 lyr = new WMSLayer1_1_0();
183
                        if (rootLayer == null)
184
                            rootLayer = lyr;
185
                        else {
186
                            // Handles when there is no general root layer, will use
187
                            // a fake non-queryable one.
188
                            if (!rootLayer.equals(getFakeRootLayer())){
189
                                WMSLayer1_1_0 aux = (WMSLayer1_1_0) rootLayer;
190
                                rootLayer  = getFakeRootLayer();
191
                                rootLayer.getChildren().add(aux);
192
                            }
193
                            rootLayer.getChildren().add(lyr);
194
                        }
195
                                                lyr.parse(parser, layers);
196
197
                        if (lyr.getName()!=null)
198 4036 ldiaz
                                                    layers.put(lyr.getName(), lyr);
199 3687 ldiaz
                                        }
200
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
201
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
202
203
                                        {
204
                                                parser.skipSubTree();
205
                                        }
206
                                        break;
207
                                case KXmlParser.END_TAG:
208
                                        if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
209
                                                end = true;
210
                                        break;
211
                                case KXmlParser.TEXT:
212
                                break;
213
                         }
214 4049 ldiaz
                         if (!end)
215 3687 ldiaz
                         currentTag = parser.next();
216
            }
217
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);
218
    }
219
220
    /**
221
     * <p>Parses the Request tag </p>
222
     */
223
    private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
224
    {
225
            int currentTag;
226
            boolean end = false;
227
228
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
229
            currentTag = parser.next();
230
231
            while (!end)
232
            {
233
                         switch(currentTag)
234
                         {
235
                                case KXmlParser.START_TAG:
236
                                        if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
237
                                        {
238 4036 ldiaz
                                                serviceInfo.operations.put(CapabilitiesTags.GETCAPABILITIES , null);
239 3687 ldiaz
                                        }
240
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
241 4036 ldiaz
                                        {
242
                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, null);
243 4019 ldiaz
                                                parseGetMapTag(parser);
244 3687 ldiaz
                                        }
245
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
246
                                        {
247 4036 ldiaz
                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, null);
248 4019 ldiaz
                                                parseGetFeatureInfoTag(parser);
249 3687 ldiaz
                                        }
250
                                        else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
251
                                        {
252 4036 ldiaz
                                                serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null);
253 3687 ldiaz
                                        }
254
                                        break;
255
                                case KXmlParser.END_TAG:
256
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
257
                                                end = true;
258
                                        break;
259
                                case KXmlParser.TEXT:
260
                                break;
261
                         }
262 4049 ldiaz
                         if(!end)
263
                                 currentTag = parser.next();
264 3687 ldiaz
            }
265
            // TODO: does not get such a tag when arrives here!!!!!!
266
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);
267
    }
268
269 4019 ldiaz
    /**
270
     * <p>Parses the GetMap tag </p>
271
     */
272
    private void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException
273
    {
274
            int currentTag;
275
            boolean end = false;
276
277
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
278
            currentTag = parser.next();
279
280
            while (!end)
281
            {
282
                         switch(currentTag)
283
                         {
284
                                case KXmlParser.START_TAG:
285
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
286
                                        {
287
                                                serviceInfo.formats.add(parser.nextText());
288
                                        }
289
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
290
                                        {
291
                                                currentTag = parser.nextTag();
292
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
293
                                                {
294
                                                        currentTag = parser.nextTag();
295
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
296
                                                        {
297
                                                                currentTag = parser.nextTag();
298
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
299
                                                                {
300
                                                                        String value = new String();
301
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
302
                                                                        if (value != null){
303 4036 ldiaz
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETMAP, value);
304 4019 ldiaz
                                                                        }
305
                                                                }
306
                                                        }
307
                                                }
308
                                        }
309
                                        break;
310
                                case KXmlParser.END_TAG:
311
                                        if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0)
312
                                                end = true;
313
                                        break;
314
                                case KXmlParser.TEXT:
315
                                break;
316
                         }
317 4049 ldiaz
                         if(!end)
318
                                 currentTag = parser.next();
319 4019 ldiaz
            }
320
    }
321
322
    /**
323
     * <p>Parses the GetMap tag </p>
324
     */
325
    private void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException
326
    {
327
            int currentTag;
328
            boolean end = false;
329
330
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
331
            currentTag = parser.next();
332
333
            while (!end)
334
            {
335
                         switch(currentTag)
336
                         {
337
                                case KXmlParser.START_TAG:
338
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
339
                                        {
340
                                                //TODO:
341
                                                // add the supported formats by the GetFeatureInfo request
342
                                                //serviceInfo.formats.add(parser.nextText());
343
                                        }
344
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
345
                                        {
346
                                                currentTag = parser.nextTag();
347
                                                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
348
                                                {
349
                                                        currentTag = parser.nextTag();
350
                                                        if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
351
                                                        {
352
                                                                currentTag = parser.nextTag();
353
                                                                if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
354
                                                                {
355
                                                                        String value = new String();
356
                                                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
357
                                                                        if (value != null){
358 5073 jaume
                                                                                serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, value);
359 4019 ldiaz
                                                                        }
360
                                                                }
361
                                                        }
362
                                                }
363
                                        }
364
                                        break;
365
                                case KXmlParser.END_TAG:
366
                                        if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0)
367
                                                end = true;
368
                                        break;
369
                                case KXmlParser.TEXT:
370
                                break;
371
                         }
372 4049 ldiaz
                         if(!end)
373
                                 currentTag = parser.next();
374 4019 ldiaz
            }
375
    }
376
377 3687 ldiaz
    private WMSLayer1_1_0 getFakeRootLayer(){
378
        if (fakeRootLayer == null){
379
            fakeRootLayer = new WMSLayer1_1_0();
380
            fakeRootLayer.setTitle(serviceInfo.title);
381
            fakeRootLayer.setQueryable(false);
382
            fakeRootLayer.setName(null);
383
        }
384
        return fakeRootLayer;
385
    }
386
    /* (non-Javadoc)
387
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
388
     */
389
    protected String parseException(byte[] data) {
390
        ArrayList errors = new ArrayList();
391
        KXmlParser kxmlParser = new KXmlParser();
392
        Reader reader = new InputStreamReader(new ByteArrayInputStream(data));
393
        try
394
        {
395
            kxmlParser.setInput(reader);
396
            kxmlParser.nextTag();
397
            int tag;
398
            if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
399
            {
400
                kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
401
                tag = kxmlParser.nextTag();
402
                 while(tag != KXmlParser.END_DOCUMENT)
403
                 {
404
                     switch(tag)
405
                     {
406
                        case KXmlParser.START_TAG:
407
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
408
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
409
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
410
                                String errorMessage = kxmlParser.nextText();
411
                                errors.add(errorCode+errorMessage);
412
                            }
413
                            break;
414
                        case KXmlParser.END_TAG:
415
                            break;
416
417
                     }
418
                     tag = kxmlParser.nextTag();
419
                 }
420
                 //kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
421
            }
422
        }
423
        catch(XmlPullParserException parser_ex){
424
            System.out.println(parser_ex.getMessage());
425
            parser_ex.printStackTrace();
426
        }
427
        catch (IOException ioe) {
428
            ioe.printStackTrace();
429
        }
430
        String message = errors.size()>0? "" : null;
431
        for (int i = 0; i < errors.size(); i++) {
432
            message += (String) errors.get(i)+"\n";
433
        }
434
        return message;
435
    }
436
437
  }