Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / wms / wms_1_1_1 / WMSProtocolHandler1_1_1.java @ 40559

History | View | Annotate | Download (12.5 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.remoteclient.wms.wms_1_1_1;
26

    
27
import java.io.ByteArrayInputStream;
28
import java.io.File;
29
import java.io.IOException;
30
import java.util.ArrayList;
31
import java.util.TreeMap;
32

    
33
import org.kxml2.io.KXmlParser;
34
import org.xmlpull.v1.XmlPullParserException;
35

    
36
import org.gvsig.remoteclient.utils.CapabilitiesTags;
37
import org.gvsig.remoteclient.utils.EncodingXMLParser;
38
import org.gvsig.remoteclient.utils.ExceptionTags;
39
import org.gvsig.remoteclient.wms.WMSProtocolHandler;
40
import org.gvsig.remoteclient.wms.WMSServiceInformation;
41
import org.gvsig.remoteclient.wms.WMSStatus;
42
import org.gvsig.remoteclient.wms.request.WMSGetCapabilitiesRequest;
43
import org.gvsig.remoteclient.wms.request.WMSGetFeatureInfoRequest;
44
import org.gvsig.remoteclient.wms.request.WMSGetLegendGraphicRequest;
45
import org.gvsig.remoteclient.wms.request.WMSGetMapRequest;
46
import org.gvsig.remoteclient.wms.wms_1_1_1.request.WMSGetCapabilitiesRequest1_1_1;
47
import org.gvsig.remoteclient.wms.wms_1_1_1.request.WMSGetFeatureInfoRequest1_1_1;
48
import org.gvsig.remoteclient.wms.wms_1_1_1.request.WMSGetLegendGraphicRequest1_1_1;
49
import org.gvsig.remoteclient.wms.wms_1_1_1.request.WMSGetMapRequest1_1_1;
50

    
51
/**
52
 * <p>
53
 * Describes the handler to comunicate to a WMS 1.1.1
54
 * </p>
55
 */
56
public class WMSProtocolHandler1_1_1 extends WMSProtocolHandler {
57
        private WMSLayer1_1_1 fakeRootLayer;
58

    
59
        public WMSProtocolHandler1_1_1()
60
        {
61
                this.version = "1.1.1";
62
                this.name = "WMS1.1.1";
63
                this.serviceInfo = new WMSServiceInformation();
64
                this.layers = new TreeMap();
65
        }
66

    
67
//------------------------------------------------------------------------------
68
// Parsing methods....
69
//------------------------------------------------------------------------------
70

    
71
    public boolean parseCapabilities(File f)
72
    {
73
            rootLayer = null;
74
            rootLayer = null;
75
            int tag;
76
            EncodingXMLParser kxmlParser = null;
77
            kxmlParser = new EncodingXMLParser();
78
            try
79
            {
80
//                    FileReader reader = new FileReader(f);
81
//                    BufferedReader br = new BufferedReader(reader);
82
//
83
//                     // patch for ArcIMS + WMS connector > 9.0 bug
84
//                    char[] buffer = new char[(int) f.length()];
85
//                    br.read(buffer);
86
//                    String string = new String(buffer);
87
//                    int a = string.toLowerCase().indexOf("<?xml");
88
//            if (a !=-1) {
89
//                    string = string.substring(a, string.length());
90
//                    kxmlParser.setInput(string);
91
//            } else
92
                    // end patch
93
                    kxmlParser.setInput(f);
94

    
95
                        kxmlParser.nextTag();
96
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
97
                    {
98
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);
99
                            tag = kxmlParser.nextTag();
100
                                 while(tag != KXmlParser.END_DOCUMENT)
101
                                 {
102
                     switch(tag)
103
                                         {
104

    
105
                                                case KXmlParser.START_TAG:
106
                                                        if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
107
                                                        {
108
                                                                parseServiceTag(kxmlParser);
109
                                                        }
110
                                                        else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
111
                                                        {
112
                                                                parseCapabilityTag(kxmlParser);
113
                                                        }
114
                                                        break;
115
                                                case KXmlParser.END_TAG:
116
                                                        break;
117
                                                case KXmlParser.TEXT:
118
                                                        //System.out.println("[TEXT]["+kxmlParser.getText()+"]");
119
                                                break;
120
                                         }
121
                                     tag = kxmlParser.next();
122
                             }
123
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
124
                    }
125
            }
126
            catch(XmlPullParserException parser_ex){
127
                    parser_ex.printStackTrace();
128
                    return false;
129
            }
130
                   catch (IOException ioe) {
131
                           ioe.printStackTrace();
132
                           return false;
133
                } finally {
134
                        return true;
135
        }
136
                   // In the parsing process the layer has been filled
137
    }
138

    
139
    /**
140
     * <p>Parses the Service Information </p>
141
     */
142
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException
143
    {
144
            int currentTag;
145
            boolean end = false;
146

    
147
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
148
            currentTag = parser.next();
149

    
150
            while (!end)
151
            {
152
                         switch(currentTag)
153
                         {
154
                                case KXmlParser.START_TAG:
155
                                        if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
156
                                        {
157
                                                serviceInfo.name = parser.nextText();
158
                                        }
159
                                        else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
160
                                        {
161
                                                serviceInfo.title = parser.nextText();
162
                                        }
163
                                        else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
164
                                        {
165
                                                serviceInfo.abstr = parser.nextText();
166
                                        }
167
                                        else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
168
                                        {
169
                                            String value = new String();
170
                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
171
                                        if (value != null){
172
                                                serviceInfo.online_resource = value;
173
                                        }
174
                                        }
175
                                        else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
176
                                                        (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
177
                                        {
178
                                                parser.skipSubTree();
179
                                        }
180
                                        break;
181
                                case KXmlParser.END_TAG:
182
                                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
183
                                                end = true;
184
                                        break;
185
                                case KXmlParser.TEXT:
186
                                break;
187
                         }
188
             if (!end)
189
                 currentTag = parser.next();
190
            }
191
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
192
    }
193
    /**
194
     * <p>Parses the Capability Tag </p>
195
     */
196
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
197
    {
198
            int currentTag;
199
            boolean end = false;
200

    
201
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
202
            currentTag = parser.next();
203

    
204
            while (!end)
205
            {
206
                         switch(currentTag)
207
                         {
208
                                case KXmlParser.START_TAG:
209
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
210
                                        {
211
                                                parseRequestTag(parser);
212
                                        }
213
                                        else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
214
                                        {
215
                                                //TODO:
216
                                                //Add to serviceInfo the supported formats for the exceptions????
217
                                        }
218
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
219
                                        {
220
                                                WMSLayer1_1_1 lyr = new WMSLayer1_1_1();
221
                                                lyr.parse(parser, layers);
222

    
223
                        if (rootLayer == null)
224
                            rootLayer = lyr;
225
                        else {
226
                            // Handles when there is no general root layer, will use
227
                            // a fake non-queryable one.
228
                            if (!rootLayer.equals(getFakeRootLayer())){
229
                                WMSLayer1_1_1 aux = (WMSLayer1_1_1) rootLayer;
230
                                rootLayer  = getFakeRootLayer();
231
                                rootLayer.getChildren().add(aux);
232
                            }
233
                            rootLayer.getChildren().add(lyr);
234
                        }
235

    
236
                        if (lyr.getName()!=null)
237
                                                    layers.put(lyr.getName(), lyr);
238

    
239
//                        Collection layerCollection = layers.values();
240
//                        Iterator iter = layerCollection.iterator();
241
//                        while (iter.hasNext())
242
//                        {
243
//                                WMSLayer1_1_1 layer = (WMSLayer1_1_1)iter.next();
244
//                                                    //Add all the SRS that the layer supports to the WMSProtocolHandler if they dont exist already
245
//                                                    for (i=0;i<layer.getAllSrs().size();i++)
246
//                                                    {
247
////                                                        if (!layer.srs.contains(layer.getAllSrs().elementAt(i)))
248
////                                                        {
249
////                                                            this.srs.add(layer.getAllSrs().elementAt(i));
250
////                                                        }
251
//                                                    }
252
//                        }
253
                                        }
254
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
255
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
256

    
257
                                        {
258
                                                parser.skipSubTree();
259
                                        }
260
                                        break;
261
                                case KXmlParser.END_TAG:
262
                                        if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
263
                                                end = true;
264
                                        break;
265
                                case KXmlParser.TEXT:
266
                                break;
267
                         }
268
                         if (!end)
269
                                 currentTag = parser.next();
270
            }
271
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);
272
    }    
273

    
274
    private WMSLayer1_1_1 getFakeRootLayer(){
275
        if (fakeRootLayer == null){
276
            fakeRootLayer = new WMSLayer1_1_1();
277
            fakeRootLayer.setTitle(serviceInfo.title);
278
            fakeRootLayer.setQueryable(false);
279
            fakeRootLayer.setName(null);
280
        }
281
        return fakeRootLayer;
282
    }
283
    /* (non-Javadoc)
284
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
285
     */
286
    protected String parseException(byte[] data) {
287
        ArrayList errors = new ArrayList();
288
        KXmlParser kxmlParser = new KXmlParser();
289
        try
290
        {
291
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);
292
            int tag;
293

    
294
            boolean end = false;
295
            tag = kxmlParser.nextTag();
296

    
297
            //Comentar temporalmente para subsanar el hecho de que SimonCit me devuelve las capabilities en un GetLegendGraphic!!!
298
            kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
299

    
300
            while (!end)
301
            {
302
                switch(tag)
303
                {
304
                    case KXmlParser.START_TAG:
305
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
306
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
307
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
308
                                String errorMessage = kxmlParser.nextText();
309
                                errors.add(errorCode+errorMessage);
310
                            }
311
                            break;
312
                    case KXmlParser.END_TAG:
313
                            if (kxmlParser.getName().compareTo(ExceptionTags.EXCEPTION_ROOT) == 0)
314
                                end = true;
315
                            break;
316
                     }
317
                        if (!end)
318
                        {
319
                                tag = kxmlParser.nextTag();
320
                        }
321
                 }
322
                }
323
        catch(XmlPullParserException parser_ex){
324
            System.out.println(parser_ex.getMessage());
325
            parser_ex.printStackTrace();
326
        }
327
        catch (IOException ioe) {
328
            ioe.printStackTrace();
329
        }
330
        String message = errors.size()>0? "" : null;
331
        for (int i = 0; i < errors.size(); i++) {
332
            message += (String) errors.get(i)+"\n";
333
        }
334
        return message;
335
    }
336
    
337
    /*
338
     * (non-Javadoc)
339
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#createGetFeatureInfoRequest(org.gvsig.remoteClient.wms.WMSStatus, int, int)
340
     */
341
        protected WMSGetFeatureInfoRequest createGetFeatureInfoRequest(
342
                        WMSStatus status, int x, int y) {
343
                return new WMSGetFeatureInfoRequest1_1_1(status, this, x, y);
344
        }
345

    
346
        /*
347
         * (non-Javadoc)
348
         * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#createGetMapRequest(org.gvsig.remoteClient.wms.WMSStatus)
349
         */
350
        protected WMSGetMapRequest createGetMapRequest(WMSStatus status) {
351
                return new WMSGetMapRequest1_1_1(status, this);
352
        }
353

    
354
        /*
355
         * (non-Javadoc)
356
         * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#createGetLegendGraphicRequest(org.gvsig.remoteClient.wms.WMSStatus, java.lang.String)
357
         */
358
        protected WMSGetLegendGraphicRequest createGetLegendGraphicRequest(
359
                        WMSStatus status, String layerName) {
360
                return new WMSGetLegendGraphicRequest1_1_1(status, this, layerName);
361
        }
362
        
363

    
364
        /* (non-Javadoc)
365
         * @see org.gvsig.remoteclient.wms.WMSProtocolHandler#createGetCapabilitiesRequest(org.gvsig.remoteclient.wms.WMSStatus)
366
         */        
367
        protected WMSGetCapabilitiesRequest createGetCapabilitiesRequest(
368
                        WMSStatus status) {
369
                return new WMSGetCapabilitiesRequest1_1_1(status, this);
370
        }
371
  }