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