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 @ 41285

History | View | Annotate | Download (12.7 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
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53
/**
54
 * <p>
55
 * Describes the handler to comunicate to a WMS 1.1.1
56
 * </p>
57
 */
58
public class WMSProtocolHandler1_1_1 extends WMSProtocolHandler {
59
    
60
    private static final Logger logger = LoggerFactory.getLogger(WMSProtocolHandler1_1_1.class);
61
    
62
        private WMSLayer1_1_1 fakeRootLayer;
63

    
64
        public WMSProtocolHandler1_1_1()
65
        {
66
                this.version = "1.1.1";
67
                this.name = "WMS1.1.1";
68
                this.serviceInfo = new WMSServiceInformation();
69
                this.layers = new TreeMap();
70
        }
71

    
72
//------------------------------------------------------------------------------
73
// Parsing methods....
74
//------------------------------------------------------------------------------
75

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

    
100
                        kxmlParser.nextTag();
101
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
102
                    {
103
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);
104
                            tag = kxmlParser.nextTag();
105
                                 while(tag != KXmlParser.END_DOCUMENT)
106
                                 {
107
                     switch(tag)
108
                                         {
109

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

    
144
    /**
145
     * <p>Parses the Service Information </p>
146
     */
147
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException
148
    {
149
            int currentTag;
150
            boolean end = false;
151

    
152
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
153
            currentTag = parser.next();
154

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

    
206
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
207
            currentTag = parser.next();
208

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

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

    
241
                        if (lyr.getName()!=null)
242
                                                    layers.put(lyr.getName(), lyr);
243

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

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

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

    
299
            boolean end = false;
300
            tag = kxmlParser.nextTag();
301

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

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

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

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

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