Statistics
| Revision:

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

History | View | Annotate | Download (13.3 KB)

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