Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2020 / libraries / libRemoteServices / src / org / gvsig / remoteclient / wms / wms_1_1_1 / WMSProtocolHandler1_1_1.java @ 33910

History | View | Annotate | Download (11.6 KB)

1 3323 ldiaz
2 29648 jpiera
package org.gvsig.remoteclient.wms.wms_1_1_1;
3 3323 ldiaz
4 3516 jaume
import java.io.ByteArrayInputStream;
5 3323 ldiaz
import java.io.File;
6
import java.io.IOException;
7 3516 jaume
import java.util.ArrayList;
8 3323 ldiaz
import java.util.TreeMap;
9
10 33738 jpiera
import org.kxml2.io.KXmlParser;
11
import org.xmlpull.v1.XmlPullParserException;
12
13 29648 jpiera
import org.gvsig.remoteclient.utils.CapabilitiesTags;
14
import org.gvsig.remoteclient.utils.EncodingXMLParser;
15
import org.gvsig.remoteclient.utils.ExceptionTags;
16
import org.gvsig.remoteclient.wms.WMSProtocolHandler;
17
import org.gvsig.remoteclient.wms.WMSServiceInformation;
18
import org.gvsig.remoteclient.wms.WMSStatus;
19 30324 jpiera
import org.gvsig.remoteclient.wms.request.WMSGetCapabilitiesRequest;
20 29648 jpiera
import org.gvsig.remoteclient.wms.request.WMSGetFeatureInfoRequest;
21
import org.gvsig.remoteclient.wms.request.WMSGetLegendGraphicRequest;
22
import org.gvsig.remoteclient.wms.request.WMSGetMapRequest;
23 30324 jpiera
import org.gvsig.remoteclient.wms.wms_1_1_1.request.WMSGetCapabilitiesRequest1_1_1;
24 29648 jpiera
import org.gvsig.remoteclient.wms.wms_1_1_1.request.WMSGetFeatureInfoRequest1_1_1;
25
import org.gvsig.remoteclient.wms.wms_1_1_1.request.WMSGetLegendGraphicRequest1_1_1;
26
import org.gvsig.remoteclient.wms.wms_1_1_1.request.WMSGetMapRequest1_1_1;
27 3323 ldiaz
28
/**
29
 * <p>
30
 * Describes the handler to comunicate to a WMS 1.1.1
31
 * </p>
32
 */
33 27881 jpiera
public class WMSProtocolHandler1_1_1 extends WMSProtocolHandler {
34 3592 jaume
        private WMSLayer1_1_1 fakeRootLayer;
35 11381 jaume
36 3323 ldiaz
        public WMSProtocolHandler1_1_1()
37
        {
38 3341 ldiaz
                this.version = "1.1.1";
39
                this.name = "WMS1.1.1";
40 27881 jpiera
                this.serviceInfo = new WMSServiceInformation();
41 3341 ldiaz
                this.layers = new TreeMap();
42 3323 ldiaz
        }
43 11381 jaume
44 3323 ldiaz
//------------------------------------------------------------------------------
45 11381 jaume
// Parsing methods....
46
//------------------------------------------------------------------------------
47 27881 jpiera
48
    public boolean parseCapabilities(File f)
49 11381 jaume
    {
50 5539 jaume
            rootLayer = null;
51 14705 jpiera
            rootLayer = null;
52 3323 ldiaz
            int tag;
53 14705 jpiera
            EncodingXMLParser kxmlParser = null;
54
            kxmlParser = new EncodingXMLParser();
55 3323 ldiaz
            try
56
            {
57 14705 jpiera
//                    FileReader reader = new FileReader(f);
58
//                    BufferedReader br = new BufferedReader(reader);
59
//
60
//                     // patch for ArcIMS + WMS connector > 9.0 bug
61
//                    char[] buffer = new char[(int) f.length()];
62
//                    br.read(buffer);
63
//                    String string = new String(buffer);
64
//                    int a = string.toLowerCase().indexOf("<?xml");
65
//            if (a !=-1) {
66
//                    string = string.substring(a, string.length());
67
//                    kxmlParser.setInput(string);
68
//            } else
69 11381 jaume
                    // end patch
70 14705 jpiera
                    kxmlParser.setInput(f);
71 11381 jaume
72 3323 ldiaz
                        kxmlParser.nextTag();
73 11381 jaume
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
74
                    {
75
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);
76 3323 ldiaz
                            tag = kxmlParser.nextTag();
77
                                 while(tag != KXmlParser.END_DOCUMENT)
78
                                 {
79 3655 jaume
                     switch(tag)
80 3323 ldiaz
                                         {
81 11381 jaume
82 3323 ldiaz
                                                case KXmlParser.START_TAG:
83
                                                        if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
84
                                                        {
85
                                                                parseServiceTag(kxmlParser);
86 11381 jaume
                                                        }
87 3323 ldiaz
                                                        else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
88
                                                        {
89
                                                                parseCapabilityTag(kxmlParser);
90
                                                        }
91
                                                        break;
92 11381 jaume
                                                case KXmlParser.END_TAG:
93 3323 ldiaz
                                                        break;
94
                                                case KXmlParser.TEXT:
95 11381 jaume
                                                        //System.out.println("[TEXT]["+kxmlParser.getText()+"]");
96 3323 ldiaz
                                                break;
97
                                         }
98
                                     tag = kxmlParser.next();
99
                             }
100 3516 jaume
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
101 3323 ldiaz
                    }
102
            }
103
            catch(XmlPullParserException parser_ex){
104 3404 jaume
                    parser_ex.printStackTrace();
105 27881 jpiera
                    return false;
106 3323 ldiaz
            }
107 11381 jaume
                   catch (IOException ioe) {
108 3404 jaume
                           ioe.printStackTrace();
109 27881 jpiera
                           return false;
110 3592 jaume
                } finally {
111 27881 jpiera
                        return true;
112 3592 jaume
        }
113 11381 jaume
                   // In the parsing process the layer has been filled
114
    }
115
116 3323 ldiaz
    /**
117
     * <p>Parses the Service Information </p>
118 11381 jaume
     */
119
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException
120 3323 ldiaz
    {
121
            int currentTag;
122
            boolean end = false;
123 11381 jaume
124 3323 ldiaz
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
125
            currentTag = parser.next();
126 11381 jaume
127
            while (!end)
128 3323 ldiaz
            {
129
                         switch(currentTag)
130
                         {
131
                                case KXmlParser.START_TAG:
132
                                        if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
133
                                        {
134 11381 jaume
                                                serviceInfo.name = parser.nextText();
135
                                        }
136 3323 ldiaz
                                        else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
137
                                        {
138 11381 jaume
                                                serviceInfo.title = parser.nextText();
139 3323 ldiaz
                                        }
140
                                        else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
141
                                        {
142 11381 jaume
                                                serviceInfo.abstr = parser.nextText();
143 3323 ldiaz
                                        }
144 4222 jaume
                                        else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
145
                                        {
146
                                            String value = new String();
147
                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
148
                                        if (value != null){
149
                                                serviceInfo.online_resource = value;
150
                                        }
151
                                        }
152 3323 ldiaz
                                        else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
153
                                                        (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
154
                                        {
155
                                                parser.skipSubTree();
156 11381 jaume
                                        }
157 3323 ldiaz
                                        break;
158
                                case KXmlParser.END_TAG:
159
                                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
160
                                                end = true;
161
                                        break;
162 11381 jaume
                                case KXmlParser.TEXT:
163 3323 ldiaz
                                break;
164
                         }
165 3655 jaume
             if (!end)
166
                 currentTag = parser.next();
167 3323 ldiaz
            }
168 3655 jaume
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
169 3323 ldiaz
    }
170
    /**
171
     * <p>Parses the Capability Tag </p>
172 11381 jaume
     */
173 3323 ldiaz
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
174 11381 jaume
    {
175 3323 ldiaz
            int currentTag;
176
            boolean end = false;
177 11381 jaume
178 3323 ldiaz
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
179
            currentTag = parser.next();
180 11381 jaume
181
            while (!end)
182 3323 ldiaz
            {
183
                         switch(currentTag)
184
                         {
185
                                case KXmlParser.START_TAG:
186
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
187
                                        {
188 11381 jaume
                                                parseRequestTag(parser);
189
                                        }
190 3323 ldiaz
                                        else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
191
                                        {
192 4222 jaume
                                                //TODO:
193
                                                //Add to serviceInfo the supported formats for the exceptions????
194 3323 ldiaz
                                        }
195
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
196
                                        {
197
                                                WMSLayer1_1_1 lyr = new WMSLayer1_1_1();
198 4222 jaume
                                                lyr.parse(parser, layers);
199 11381 jaume
200 3592 jaume
                        if (rootLayer == null)
201
                            rootLayer = lyr;
202
                        else {
203
                            // Handles when there is no general root layer, will use
204
                            // a fake non-queryable one.
205
                            if (!rootLayer.equals(getFakeRootLayer())){
206
                                WMSLayer1_1_1 aux = (WMSLayer1_1_1) rootLayer;
207
                                rootLayer  = getFakeRootLayer();
208
                                rootLayer.getChildren().add(aux);
209
                            }
210
                            rootLayer.getChildren().add(lyr);
211
                        }
212 11381 jaume
213 3483 jaume
                        if (lyr.getName()!=null)
214 11381 jaume
                                                    layers.put(lyr.getName(), lyr);
215
216
//                        Collection layerCollection = layers.values();
217 4222 jaume
//                        Iterator iter = layerCollection.iterator();
218
//                        while (iter.hasNext())
219
//                        {
220 11381 jaume
//                                WMSLayer1_1_1 layer = (WMSLayer1_1_1)iter.next();
221 4222 jaume
//                                                    //Add all the SRS that the layer supports to the WMSProtocolHandler if they dont exist already
222
//                                                    for (i=0;i<layer.getAllSrs().size();i++)
223 11381 jaume
//                                                    {
224 4222 jaume
////                                                        if (!layer.srs.contains(layer.getAllSrs().elementAt(i)))
225
////                                                        {
226
////                                                            this.srs.add(layer.getAllSrs().elementAt(i));
227
////                                                        }
228 11381 jaume
//                                                    }
229
//                        }
230 3323 ldiaz
                                        }
231
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
232
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
233 11381 jaume
234 3323 ldiaz
                                        {
235
                                                parser.skipSubTree();
236 11381 jaume
                                        }
237 3323 ldiaz
                                        break;
238
                                case KXmlParser.END_TAG:
239
                                        if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
240
                                                end = true;
241
                                        break;
242 11381 jaume
                                case KXmlParser.TEXT:
243 3323 ldiaz
                                break;
244
                         }
245 4222 jaume
                         if (!end)
246
                                 currentTag = parser.next();
247 3323 ldiaz
            }
248 11381 jaume
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);
249 27881 jpiera
    }
250 11381 jaume
251 3592 jaume
    private WMSLayer1_1_1 getFakeRootLayer(){
252
        if (fakeRootLayer == null){
253
            fakeRootLayer = new WMSLayer1_1_1();
254
            fakeRootLayer.setTitle(serviceInfo.title);
255
            fakeRootLayer.setQueryable(false);
256
            fakeRootLayer.setName(null);
257
        }
258
        return fakeRootLayer;
259
    }
260 3516 jaume
    /* (non-Javadoc)
261
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
262
     */
263
    protected String parseException(byte[] data) {
264
        ArrayList errors = new ArrayList();
265
        KXmlParser kxmlParser = new KXmlParser();
266
        try
267
        {
268 11381 jaume
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);
269 3516 jaume
            int tag;
270 11381 jaume
271 3824 ldiaz
            boolean end = false;
272
            tag = kxmlParser.nextTag();
273 11381 jaume
274 8290 ldiaz
            //Comentar temporalmente para subsanar el hecho de que SimonCit me devuelve las capabilities en un GetLegendGraphic!!!
275 3824 ldiaz
            kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
276 11381 jaume
277
            while (!end)
278 3824 ldiaz
            {
279
                switch(tag)
280
                {
281
                    case KXmlParser.START_TAG:
282 3516 jaume
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
283
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
284
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
285
                                String errorMessage = kxmlParser.nextText();
286
                                errors.add(errorCode+errorMessage);
287
                            }
288
                            break;
289 11381 jaume
                    case KXmlParser.END_TAG:
290 3824 ldiaz
                            if (kxmlParser.getName().compareTo(ExceptionTags.EXCEPTION_ROOT) == 0)
291
                                end = true;
292 11381 jaume
                            break;
293 3516 jaume
                     }
294 3824 ldiaz
                        if (!end)
295
                        {
296
                                tag = kxmlParser.nextTag();
297
                        }
298 3516 jaume
                 }
299 3824 ldiaz
                }
300 3516 jaume
        catch(XmlPullParserException parser_ex){
301
            System.out.println(parser_ex.getMessage());
302
            parser_ex.printStackTrace();
303
        }
304 11381 jaume
        catch (IOException ioe) {
305
            ioe.printStackTrace();
306 3516 jaume
        }
307
        String message = errors.size()>0? "" : null;
308
        for (int i = 0; i < errors.size(); i++) {
309
            message += (String) errors.get(i)+"\n";
310
        }
311
        return message;
312
    }
313 27881 jpiera
314
    /*
315
     * (non-Javadoc)
316
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#createGetFeatureInfoRequest(org.gvsig.remoteClient.wms.WMSStatus, int, int)
317
     */
318
        protected WMSGetFeatureInfoRequest createGetFeatureInfoRequest(
319
                        WMSStatus status, int x, int y) {
320
                return new WMSGetFeatureInfoRequest1_1_1(status, this, x, y);
321
        }
322 11381 jaume
323 27881 jpiera
        /*
324
         * (non-Javadoc)
325
         * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#createGetMapRequest(org.gvsig.remoteClient.wms.WMSStatus)
326
         */
327
        protected WMSGetMapRequest createGetMapRequest(WMSStatus status) {
328
                return new WMSGetMapRequest1_1_1(status, this);
329
        }
330
331
        /*
332
         * (non-Javadoc)
333
         * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#createGetLegendGraphicRequest(org.gvsig.remoteClient.wms.WMSStatus, java.lang.String)
334
         */
335
        protected WMSGetLegendGraphicRequest createGetLegendGraphicRequest(
336
                        WMSStatus status, String layerName) {
337
                return new WMSGetLegendGraphicRequest1_1_1(status, this, layerName);
338
        }
339 30324 jpiera
340
341
        /* (non-Javadoc)
342
         * @see org.gvsig.remoteclient.wms.WMSProtocolHandler#createGetCapabilitiesRequest(org.gvsig.remoteclient.wms.WMSStatus)
343
         */
344
        protected WMSGetCapabilitiesRequest createGetCapabilitiesRequest(
345
                        WMSStatus status) {
346
                return new WMSGetCapabilitiesRequest1_1_1(status, this);
347
        }
348 3323 ldiaz
  }