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_0 / WMSProtocolHandler1_1_0.java @ 41242

History | View | Annotate | Download (10.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_0;
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.WMSServiceInformation;
40
import org.gvsig.remoteclient.wms.WMSStatus;
41
import org.gvsig.remoteclient.wms.request.WMSGetCapabilitiesRequest;
42
import org.gvsig.remoteclient.wms.request.WMSGetFeatureInfoRequest;
43
import org.gvsig.remoteclient.wms.request.WMSGetLegendGraphicRequest;
44
import org.gvsig.remoteclient.wms.request.WMSGetMapRequest;
45
import org.gvsig.remoteclient.wms.wms_1_1_0.request.WMSGetCapabilitiesRequest1_1_0;
46
import org.gvsig.remoteclient.wms.wms_1_1_0.request.WMSGetFeatureInfoRequest1_1_0;
47
import org.gvsig.remoteclient.wms.wms_1_1_0.request.WMSGetLegendGraphicRequest1_1_0;
48
import org.gvsig.remoteclient.wms.wms_1_1_0.request.WMSGetMapRequest1_1_0;
49

    
50
/**
51
 * <p>
52
 * Describes the handler to comunicate to a WMS 1.1.0
53
 * </p>
54
 */
55
public class WMSProtocolHandler1_1_0 extends org.gvsig.remoteclient.wms.WMSProtocolHandler
56
{
57
        private WMSLayer1_1_0 fakeRootLayer;
58
    
59
        public WMSProtocolHandler1_1_0()
60
        {
61
                this.version = "1.1.0";
62
                this.name = "WMS1.1.0";
63
                this.serviceInfo = new WMSServiceInformation(); 
64
                this.layers = new TreeMap();
65
        }
66
    
67
//------------------------------------------------------------------------------
68
// Parsing methods....    
69
//------------------------------------------------------------------------------    
70

    
71

    
72
        public boolean parseCapabilities(File f)
73
    {       
74
            rootLayer = null;
75
            int tag;
76
            EncodingXMLParser kxmlParser = null;
77
            kxmlParser = new EncodingXMLParser();
78
            try
79
            {
80
                    kxmlParser.setInput(f);
81
                    kxmlParser.nextTag();
82
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
83
                    {                    
84
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);                            
85
                            tag = kxmlParser.nextTag();
86
                                 while(tag != KXmlParser.END_DOCUMENT)
87
                                 {
88
                     switch(tag)
89
                                         {
90
                         
91
                                                case KXmlParser.START_TAG:
92
                                                        if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
93
                                                        {
94
                                                                parseServiceTag(kxmlParser);
95
                                                        }        
96
                                                        else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
97
                                                        {
98
                                                                parseCapabilityTag(kxmlParser);
99
                                                        }
100
                                                        break;
101
                                                case KXmlParser.END_TAG:                                                        
102
                                                        break;
103
                                                case KXmlParser.TEXT:
104
                                                                                                
105
                                                break;
106
                                         }
107
                                     tag = kxmlParser.next();
108
                             }
109

    
110
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
111
                    }
112
            }
113
            catch(XmlPullParserException parser_ex){
114
                    parser_ex.printStackTrace();
115
                    return false;
116
            }
117
                   catch (IOException ioe) {                        
118
                           ioe.printStackTrace();
119
                           return false;
120
                } finally {
121
            return true;
122
        }
123
    } 
124
    
125
    /**
126
     * <p>Parses the Service Information </p>
127
     */    
128
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException 
129
    {
130
            int currentTag;
131
            boolean end = false;
132
            
133
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
134
            currentTag = parser.next();
135
            
136
            while (!end) 
137
            {
138
                         switch(currentTag)
139
                         {
140
                                case KXmlParser.START_TAG:
141
                                        if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
142
                                        {
143
                                                serviceInfo.name = parser.nextText(); 
144
                                        }        
145
                                        else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
146
                                        {
147
                                                serviceInfo.title = parser.nextText(); 
148
                                        }
149
                                        else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
150
                                        {
151
                                                serviceInfo.abstr = parser.nextText(); 
152
                                        }
153
                                        else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
154
                                        {
155
                                            String value = null;
156
                                        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
157
                                        if (value != null){
158
                                                serviceInfo.online_resource = value;
159
                                        }
160
                                        }                                        
161
                                        else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
162
                                                        (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
163
                                        {
164
                                                parser.skipSubTree();
165
                                        }                                        
166
                                        break;
167
                                case KXmlParser.END_TAG:
168
                                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
169
                                                end = true;
170
                                        break;
171
                                case KXmlParser.TEXT:                                        
172
                                break;
173
                         }
174
             if (!end)
175
                 currentTag = parser.next();
176
            }
177
            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
178
    }
179
    
180
    /**
181
     * <p>Parses the Capability Tag </p>
182
     */    
183
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
184
    {         
185
            int currentTag;
186
            boolean end = false;
187
            
188
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
189
            currentTag = parser.next();
190
            
191
            while (!end) 
192
            {
193
                         switch(currentTag)
194
                         {
195
                                case KXmlParser.START_TAG:
196
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
197
                                        {
198
                                                parseRequestTag(parser); 
199
                                        }        
200
                                        else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
201
                                        {
202
                                                // TODO:
203
                                                // add the exception formats supported.
204
                                        }
205
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
206
                                        {
207
                                                WMSLayer1_1_0 lyr = new WMSLayer1_1_0();
208
                        if (rootLayer == null)
209
                            rootLayer = lyr;
210
                        else {
211
                            // Handles when there is no general root layer, will use
212
                            // a fake non-queryable one.
213
                            if (!rootLayer.equals(getFakeRootLayer())){
214
                                WMSLayer1_1_0 aux = (WMSLayer1_1_0) rootLayer;
215
                                rootLayer  = getFakeRootLayer();
216
                                rootLayer.getChildren().add(aux);
217
                            }
218
                            rootLayer.getChildren().add(lyr);
219
                        }
220
                                                lyr.parse(parser, layers);
221
                                                
222
                        if (lyr.getName()!=null)
223
                                                    layers.put(lyr.getName(), lyr);                                                                                                         
224
                                        }
225
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
226
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
227
                            
228
                                        {
229
                                                parser.skipSubTree();
230
                                        }                                        
231
                                        break;
232
                                case KXmlParser.END_TAG:
233
                                        if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
234
                                                end = true;
235
                                        break;
236
                                case KXmlParser.TEXT:                                        
237
                                break;
238
                         }
239
                         if (!end)
240
                         currentTag = parser.next();
241
            }
242
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);            
243
    }   
244
    
245
    
246
    private WMSLayer1_1_0 getFakeRootLayer(){
247
        if (fakeRootLayer == null){
248
            fakeRootLayer = new WMSLayer1_1_0();
249
            fakeRootLayer.setTitle(serviceInfo.title);
250
            fakeRootLayer.setQueryable(false);
251
            fakeRootLayer.setName(null);
252
        }
253
        return fakeRootLayer;
254
    }
255
    
256
    protected String parseException(byte[] data) {
257
        ArrayList errors = new ArrayList();
258
        KXmlParser kxmlParser = new KXmlParser();
259
        
260
        try
261
        {
262
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);        
263
            kxmlParser.nextTag();
264
            int tag;
265
            if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
266
            { 
267
                kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);             
268
                tag = kxmlParser.nextTag();
269
                 while(tag != KXmlParser.END_DOCUMENT)
270
                 {
271
                     switch(tag)
272
                     {
273
                        case KXmlParser.START_TAG:
274
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
275
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
276
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
277
                                String errorMessage = kxmlParser.nextText();
278
                                errors.add(errorCode+errorMessage);
279
                            }
280
                            break;
281
                        case KXmlParser.END_TAG:                            
282
                            break;
283
                        
284
                     }
285
                     tag = kxmlParser.nextTag();
286
                 }
287
                 //kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
288
            }
289
        }
290
        catch(XmlPullParserException parser_ex){
291
            System.out.println(parser_ex.getMessage());
292
            parser_ex.printStackTrace();
293
        }
294
        catch (IOException ioe) {           
295
            ioe.printStackTrace();            
296
        }
297
        String message = errors.size()>0? "" : null;
298
        for (int i = 0; i < errors.size(); i++) {
299
            message += (String) errors.get(i)+"\n";
300
        }
301
        return message;
302
    }
303

    
304
        protected WMSGetFeatureInfoRequest createGetFeatureInfoRequest(
305
                        WMSStatus status, int x, int y) {
306
                return new WMSGetFeatureInfoRequest1_1_0(status, this, x, y);
307
        }
308

    
309
        protected WMSGetMapRequest createGetMapRequest(WMSStatus status) {
310
                return new WMSGetMapRequest1_1_0(status, this);
311
        }
312

    
313
        protected WMSGetLegendGraphicRequest createGetLegendGraphicRequest(
314
                        WMSStatus status, String layerName) {
315
                return new WMSGetLegendGraphicRequest1_1_0(status, this, layerName);
316
        }
317

    
318
        protected WMSGetCapabilitiesRequest createGetCapabilitiesRequest(
319
                        WMSStatus status) {
320
                return new WMSGetCapabilitiesRequest1_1_0(status, this);
321
        }
322
    
323
  }