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