gvsig-raster / org.gvsig.raster.wms / trunk / org.gvsig.raster.wms / org.gvsig.raster.wms.remoteclient / src / main / java / org / gvsig / remoteclient / wms / wms_1_1_0 / WMSProtocolHandler1_1_0.java @ 3083
History | View | Annotate | Download (10.9 KB)
1 | 3083 | jjdelcerro | /**
|
---|---|---|---|
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 | import org.gvsig.remoteclient.wms.wms_1_3_0.WMSProtocolHandler1_3_0; |
||
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.0
|
||
56 | * </p>
|
||
57 | */
|
||
58 | public class WMSProtocolHandler1_1_0 extends org.gvsig.remoteclient.wms.WMSProtocolHandler |
||
59 | { |
||
60 | |||
61 | private static final Logger logger = LoggerFactory.getLogger(WMSProtocolHandler1_1_0.class); |
||
62 | private WMSLayer1_1_0 fakeRootLayer;
|
||
63 | |||
64 | public WMSProtocolHandler1_1_0()
|
||
65 | { |
||
66 | this.version = "1.1.0"; |
||
67 | this.name = "WMS1.1.0"; |
||
68 | this.serviceInfo = new WMSServiceInformation(); |
||
69 | this.layers = new TreeMap(); |
||
70 | } |
||
71 | |||
72 | //------------------------------------------------------------------------------
|
||
73 | // Parsing methods....
|
||
74 | //------------------------------------------------------------------------------
|
||
75 | |||
76 | |||
77 | public boolean parseCapabilities(File f) |
||
78 | { |
||
79 | rootLayer = null;
|
||
80 | int tag;
|
||
81 | EncodingXMLParser kxmlParser = null;
|
||
82 | kxmlParser = new EncodingXMLParser();
|
||
83 | try
|
||
84 | { |
||
85 | kxmlParser.setInput(f); |
||
86 | kxmlParser.nextTag(); |
||
87 | if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
|
||
88 | { |
||
89 | kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);
|
||
90 | tag = kxmlParser.nextTag(); |
||
91 | while(tag != KXmlParser.END_DOCUMENT)
|
||
92 | { |
||
93 | switch(tag)
|
||
94 | { |
||
95 | |||
96 | case KXmlParser.START_TAG:
|
||
97 | if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0) |
||
98 | { |
||
99 | parseServiceTag(kxmlParser); |
||
100 | } |
||
101 | else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0) |
||
102 | { |
||
103 | parseCapabilityTag(kxmlParser); |
||
104 | } |
||
105 | break;
|
||
106 | case KXmlParser.END_TAG:
|
||
107 | break;
|
||
108 | case KXmlParser.TEXT:
|
||
109 | |||
110 | break;
|
||
111 | } |
||
112 | tag = kxmlParser.next(); |
||
113 | } |
||
114 | |||
115 | kxmlParser.require(KXmlParser.END_DOCUMENT, null, null); |
||
116 | } |
||
117 | } |
||
118 | catch(XmlPullParserException parser_ex){
|
||
119 | logger.warn("Error parsing capabilites.",parser_ex);
|
||
120 | return false; |
||
121 | } |
||
122 | catch (IOException ioe) { |
||
123 | logger.warn("Error parsing capabilites.",ioe);
|
||
124 | return false; |
||
125 | } finally {
|
||
126 | return true; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /**
|
||
131 | * <p>Parses the Service Information </p>
|
||
132 | */
|
||
133 | private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException |
||
134 | { |
||
135 | int currentTag;
|
||
136 | boolean end = false; |
||
137 | |||
138 | parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
|
||
139 | currentTag = parser.next(); |
||
140 | |||
141 | while (!end)
|
||
142 | { |
||
143 | switch(currentTag)
|
||
144 | { |
||
145 | case KXmlParser.START_TAG:
|
||
146 | if (parser.getName().compareTo(CapabilitiesTags.NAME)==0) |
||
147 | { |
||
148 | serviceInfo.name = parser.nextText(); |
||
149 | } |
||
150 | else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0) |
||
151 | { |
||
152 | serviceInfo.title = parser.nextText(); |
||
153 | } |
||
154 | else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0) |
||
155 | { |
||
156 | serviceInfo.abstr = parser.nextText(); |
||
157 | } |
||
158 | else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0) |
||
159 | { |
||
160 | String value = null; |
||
161 | value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
|
||
162 | if (value != null){ |
||
163 | serviceInfo.online_resource = value; |
||
164 | } |
||
165 | } |
||
166 | else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) || |
||
167 | (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
|
||
168 | { |
||
169 | parser.skipSubTree(); |
||
170 | } |
||
171 | break;
|
||
172 | case KXmlParser.END_TAG:
|
||
173 | if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0) |
||
174 | end = true;
|
||
175 | break;
|
||
176 | case KXmlParser.TEXT:
|
||
177 | break;
|
||
178 | } |
||
179 | if (!end)
|
||
180 | currentTag = parser.next(); |
||
181 | } |
||
182 | parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
|
||
183 | } |
||
184 | |||
185 | /**
|
||
186 | * <p>Parses the Capability Tag </p>
|
||
187 | */
|
||
188 | private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException |
||
189 | { |
||
190 | int currentTag;
|
||
191 | boolean end = false; |
||
192 | |||
193 | parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
|
||
194 | currentTag = parser.next(); |
||
195 | |||
196 | while (!end)
|
||
197 | { |
||
198 | switch(currentTag)
|
||
199 | { |
||
200 | case KXmlParser.START_TAG:
|
||
201 | if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0) |
||
202 | { |
||
203 | parseRequestTag(parser); |
||
204 | } |
||
205 | else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0) |
||
206 | { |
||
207 | // TODO:
|
||
208 | // add the exception formats supported.
|
||
209 | } |
||
210 | else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0) |
||
211 | { |
||
212 | WMSLayer1_1_0 lyr = new WMSLayer1_1_0();
|
||
213 | if (rootLayer == null) |
||
214 | rootLayer = lyr; |
||
215 | else {
|
||
216 | // Handles when there is no general root layer, will use
|
||
217 | // a fake non-queryable one.
|
||
218 | if (!rootLayer.equals(getFakeRootLayer())){
|
||
219 | WMSLayer1_1_0 aux = (WMSLayer1_1_0) rootLayer; |
||
220 | rootLayer = getFakeRootLayer(); |
||
221 | rootLayer.getChildren().add(aux); |
||
222 | } |
||
223 | rootLayer.getChildren().add(lyr); |
||
224 | } |
||
225 | lyr.parse(parser, layers); |
||
226 | |||
227 | if (lyr.getName()!=null) |
||
228 | layers.put(lyr.getName(), lyr); |
||
229 | } |
||
230 | else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) || |
||
231 | (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
|
||
232 | |||
233 | { |
||
234 | parser.skipSubTree(); |
||
235 | } |
||
236 | break;
|
||
237 | case KXmlParser.END_TAG:
|
||
238 | if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0) |
||
239 | end = true;
|
||
240 | break;
|
||
241 | case KXmlParser.TEXT:
|
||
242 | break;
|
||
243 | } |
||
244 | if (!end)
|
||
245 | currentTag = parser.next(); |
||
246 | } |
||
247 | //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);
|
||
248 | } |
||
249 | |||
250 | |||
251 | private WMSLayer1_1_0 getFakeRootLayer(){
|
||
252 | if (fakeRootLayer == null){ |
||
253 | fakeRootLayer = new WMSLayer1_1_0();
|
||
254 | fakeRootLayer.setTitle(serviceInfo.title); |
||
255 | fakeRootLayer.setQueryable(false);
|
||
256 | fakeRootLayer.setName(null);
|
||
257 | } |
||
258 | return fakeRootLayer;
|
||
259 | } |
||
260 | |||
261 | protected String parseException(byte[] data) { |
||
262 | ArrayList errors = new ArrayList(); |
||
263 | KXmlParser kxmlParser = new KXmlParser();
|
||
264 | |||
265 | try
|
||
266 | { |
||
267 | kxmlParser.setInput(new ByteArrayInputStream(data), encoding); |
||
268 | kxmlParser.nextTag(); |
||
269 | int tag;
|
||
270 | if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
|
||
271 | { |
||
272 | kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
|
||
273 | tag = kxmlParser.nextTag(); |
||
274 | while(tag != KXmlParser.END_DOCUMENT)
|
||
275 | { |
||
276 | switch(tag)
|
||
277 | { |
||
278 | case KXmlParser.START_TAG:
|
||
279 | if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){ |
||
280 | String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE); |
||
281 | errorCode = (errorCode != null) ? "["+errorCode+"] " : ""; |
||
282 | String errorMessage = kxmlParser.nextText();
|
||
283 | errors.add(errorCode+errorMessage); |
||
284 | } |
||
285 | break;
|
||
286 | case KXmlParser.END_TAG:
|
||
287 | break;
|
||
288 | |||
289 | } |
||
290 | tag = kxmlParser.nextTag(); |
||
291 | } |
||
292 | //kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
|
||
293 | } |
||
294 | } |
||
295 | catch(XmlPullParserException parser_ex){
|
||
296 | logger.warn("",parser_ex);
|
||
297 | } |
||
298 | catch (IOException ioe) { |
||
299 | logger.warn("",ioe);
|
||
300 | } |
||
301 | String message = errors.size()>0? "" : null; |
||
302 | for (int i = 0; i < errors.size(); i++) { |
||
303 | message += (String) errors.get(i)+"\n"; |
||
304 | } |
||
305 | return message;
|
||
306 | } |
||
307 | |||
308 | protected WMSGetFeatureInfoRequest createGetFeatureInfoRequest(
|
||
309 | WMSStatus status, int x, int y) { |
||
310 | return new WMSGetFeatureInfoRequest1_1_0(status, this, x, y); |
||
311 | } |
||
312 | |||
313 | protected WMSGetMapRequest createGetMapRequest(WMSStatus status) {
|
||
314 | return new WMSGetMapRequest1_1_0(status, this); |
||
315 | } |
||
316 | |||
317 | protected WMSGetLegendGraphicRequest createGetLegendGraphicRequest(
|
||
318 | WMSStatus status, String layerName) {
|
||
319 | return new WMSGetLegendGraphicRequest1_1_0(status, this, layerName); |
||
320 | } |
||
321 | |||
322 | protected WMSGetCapabilitiesRequest createGetCapabilitiesRequest(
|
||
323 | WMSStatus status) { |
||
324 | return new WMSGetCapabilitiesRequest1_1_0(status, this); |
||
325 | } |
||
326 | |||
327 | } |