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