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