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