Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/server/CapabilitiesParser.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-client/src/main/java/org/dive4elements/river/client/server/CapabilitiesParser.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.server; | |
2 | |
3 import java.io.InputStream; | |
4 import java.io.IOException; | |
5 import java.net.MalformedURLException; | |
6 import java.net.URL; | |
7 import java.net.URLConnection; | |
8 import java.util.ArrayList; | |
9 import java.util.List; | |
10 import java.util.regex.Matcher; | |
11 import java.util.regex.Pattern; | |
12 | |
13 import javax.xml.xpath.XPathConstants; | |
14 | |
15 import org.w3c.dom.Document; | |
16 import org.w3c.dom.Element; | |
17 import org.w3c.dom.Node; | |
18 import org.w3c.dom.NodeList; | |
19 | |
20 import org.apache.log4j.Logger; | |
21 | |
22 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
23 | |
24 import org.dive4elements.river.client.shared.exceptions.ServerException; | |
25 import org.dive4elements.river.client.shared.model.Capabilities; | |
26 import org.dive4elements.river.client.shared.model.ContactInformation; | |
27 import org.dive4elements.river.client.shared.model.WMSLayer; | |
28 | |
29 | |
30 public class CapabilitiesParser { | |
31 | |
32 private static final Logger logger = | |
33 Logger.getLogger(CapabilitiesParser.class); | |
34 | |
35 | |
36 public static final String ERR_GC_REQUEST_FAILED = | |
37 "error_gc_req_failed"; | |
38 | |
39 public static final String ERR_GC_DOC_NOT_VALID = | |
40 "error_gc_doc_not_valid"; | |
41 | |
42 public static final String ERR_MALFORMED_URL = | |
43 "error_malformed_url"; | |
44 | |
45 | |
46 public static final String XPATH_WMS_CAPS = | |
47 "/WMS_Capabilities"; | |
48 | |
49 public static final String XPATH_WMT_CAPS = | |
50 "/WMT_MS_Capabilities"; | |
51 | |
52 public static final String XPATH_TITLE = | |
53 "Service/Title/text()"; | |
54 | |
55 public static final String XPATH_ONLINE_RESOURCE = | |
56 "Service/OnlineResource/@href"; | |
57 | |
58 public static final String XPATH_CONTACT_INFORMATION = | |
59 "Service/ContactInformation"; | |
60 | |
61 public static final String XPATH_CI_PERSON = | |
62 "ContactPersonPrimary/ContactPerson/text()"; | |
63 | |
64 public static final String XPATH_CI_ORGANIZATION = | |
65 "ContactPersonPrimary/ContactOrganization/text()"; | |
66 | |
67 public static final String XPATH_CI_ADDRESS = | |
68 "ContactAddress/Address/text()"; | |
69 | |
70 public static final String XPATH_CI_CITY = | |
71 "ContactAddress/City/text()"; | |
72 | |
73 public static final String XPATH_CI_POSTCODE = | |
74 "ContactAddress/PostCode/text()"; | |
75 | |
76 public static final String XPATH_CI_PHONE = | |
77 "ContactVoiceTelephone/text()"; | |
78 | |
79 public static final String XPATH_CI_EMAIL = | |
80 "ContactElectronicMailAddress/text()"; | |
81 | |
82 public static final String XPATH_FEES = | |
83 "Service/Fees/text()"; | |
84 | |
85 public static final String XPATH_ACCESS_CONSTRAINTS = | |
86 "Service/AccessConstraints/text()"; | |
87 | |
88 public static final String XPATH_LAYERS = | |
89 "Capability/Layer"; | |
90 | |
91 public static final Pattern SRS_PATTERN = Pattern.compile("(EPSG:\\d+)*"); | |
92 | |
93 | |
94 private CapabilitiesParser() { | |
95 } | |
96 | |
97 | |
98 public static void main(String[] args) { | |
99 logger.info("Do static Capabilities request/parsing."); | |
100 | |
101 String log4jProperties = System.getenv(BaseServletContextListener.LOG4J_PROPERTIES); | |
102 LoggingConfigurator.init(log4jProperties); | |
103 | |
104 try { | |
105 Capabilities caps = getCapabilities(System.getProperty("test.wms")); | |
106 | |
107 logger.debug(caps.toString()); | |
108 } | |
109 catch (ServerException se) { | |
110 se.printStackTrace(); | |
111 } | |
112 | |
113 logger.info("Finished fetching capabiltiies."); | |
114 } | |
115 | |
116 | |
117 public static Capabilities getCapabilities(String urlStr) | |
118 throws ServerException | |
119 { | |
120 try { | |
121 URL url = new URL(urlStr); | |
122 | |
123 logger.debug("Open connection to url: " + urlStr); | |
124 | |
125 URLConnection conn = url.openConnection(); | |
126 conn.connect(); | |
127 | |
128 InputStream is = conn.getInputStream(); | |
129 | |
130 return parse(is); | |
131 } | |
132 catch (MalformedURLException mue) { | |
133 logger.warn(mue, mue); | |
134 throw new ServerException(ERR_MALFORMED_URL); | |
135 } | |
136 catch (IOException ioe) { | |
137 logger.warn(ioe, ioe); | |
138 } | |
139 | |
140 throw new ServerException(ERR_GC_REQUEST_FAILED); | |
141 } | |
142 | |
143 | |
144 protected static Capabilities parse(InputStream is) | |
145 throws ServerException | |
146 { | |
147 logger.debug("GCServiceImpl.parseCapabilitiesResponse"); | |
148 | |
149 Document doc = XMLUtils.parseDocument(is, false); | |
150 | |
151 if (doc == null) { | |
152 throw new ServerException(ERR_GC_DOC_NOT_VALID); | |
153 } | |
154 | |
155 return CapabilitiesParser.parse(doc); | |
156 } | |
157 | |
158 | |
159 public static Capabilities parse(Document doc) | |
160 throws ServerException | |
161 { | |
162 Node capabilities = getCapabilitiesNode(doc); | |
163 | |
164 String title = (String) XMLUtils.xpath( | |
165 capabilities, | |
166 XPATH_TITLE, | |
167 XPathConstants.STRING); | |
168 | |
169 String onlineResource = (String) XMLUtils.xpath( | |
170 capabilities, | |
171 XPATH_ONLINE_RESOURCE, | |
172 XPathConstants.STRING); | |
173 | |
174 String fees = (String) XMLUtils.xpath( | |
175 capabilities, | |
176 XPATH_FEES, | |
177 XPathConstants.STRING); | |
178 | |
179 String accessConstraints = (String) XMLUtils.xpath( | |
180 capabilities, | |
181 XPATH_ACCESS_CONSTRAINTS, | |
182 XPathConstants.STRING); | |
183 | |
184 Node contactInformation = (Node) XMLUtils.xpath( | |
185 capabilities, | |
186 XPATH_CONTACT_INFORMATION, | |
187 XPathConstants.NODE); | |
188 | |
189 ContactInformation ci = parseContactInformation(contactInformation); | |
190 | |
191 logger.debug("Found fees: " + fees); | |
192 logger.debug("Found access constraints: " + accessConstraints); | |
193 | |
194 NodeList layerNodes = (NodeList) XMLUtils.xpath( | |
195 capabilities, | |
196 XPATH_LAYERS, | |
197 XPathConstants.NODESET); | |
198 | |
199 List<WMSLayer> layers = parseLayers(layerNodes, onlineResource); | |
200 | |
201 return new Capabilities( | |
202 title, | |
203 onlineResource, | |
204 ci, | |
205 fees, | |
206 accessConstraints, | |
207 layers); | |
208 } | |
209 | |
210 | |
211 protected static Node getCapabilitiesNode(Document doc) | |
212 throws ServerException { | |
213 Node capabilities = (Node) XMLUtils.xpath( | |
214 doc, | |
215 XPATH_WMS_CAPS, | |
216 XPathConstants.NODE); | |
217 | |
218 if (capabilities == null) { | |
219 logger.info("No '/WMS_Capabilities' node found."); | |
220 logger.info("Try to find a '/WMT_MS_Capabilities' node."); | |
221 | |
222 capabilities = (Node) XMLUtils.xpath( | |
223 doc, | |
224 XPATH_WMT_CAPS, | |
225 XPathConstants.NODE); | |
226 } | |
227 | |
228 if (capabilities == null) { | |
229 throw new ServerException(ERR_GC_DOC_NOT_VALID); | |
230 } | |
231 | |
232 return capabilities; | |
233 } | |
234 | |
235 | |
236 protected static ContactInformation parseContactInformation(Node node) { | |
237 String person = (String) XMLUtils.xpath( | |
238 node, | |
239 XPATH_CI_PERSON, | |
240 XPathConstants.STRING); | |
241 | |
242 String organization = (String) XMLUtils.xpath( | |
243 node, | |
244 XPATH_CI_ORGANIZATION, | |
245 XPathConstants.STRING); | |
246 | |
247 String address = (String) XMLUtils.xpath( | |
248 node, | |
249 XPATH_CI_ADDRESS, | |
250 XPathConstants.STRING); | |
251 | |
252 String postcode = (String) XMLUtils.xpath( | |
253 node, | |
254 XPATH_CI_POSTCODE, | |
255 XPathConstants.STRING); | |
256 | |
257 String city = (String) XMLUtils.xpath( | |
258 node, | |
259 XPATH_CI_CITY, | |
260 XPathConstants.STRING); | |
261 | |
262 String phone = (String) XMLUtils.xpath( | |
263 node, | |
264 XPATH_CI_PHONE, | |
265 XPathConstants.STRING); | |
266 | |
267 String email = (String) XMLUtils.xpath( | |
268 node, | |
269 XPATH_CI_EMAIL, | |
270 XPathConstants.STRING); | |
271 | |
272 ContactInformation ci = new ContactInformation(); | |
273 ci.setPerson(person); | |
274 ci.setOrganization(organization); | |
275 ci.setAddress(address); | |
276 ci.setPostcode(postcode); | |
277 ci.setCity(city); | |
278 ci.setPhone(phone); | |
279 ci.setEmail(email); | |
280 | |
281 return ci; | |
282 } | |
283 | |
284 | |
285 /** | |
286 * @param layersNode | |
287 * @param onlineResource | |
288 * | |
289 * @return | |
290 */ | |
291 protected static List<WMSLayer> parseLayers( | |
292 NodeList layersNode, | |
293 String onlineResource | |
294 ) { | |
295 int len = layersNode != null ? layersNode.getLength() : 0; | |
296 | |
297 logger.debug("Node has " + len + " layers."); | |
298 | |
299 List<WMSLayer> layers = new ArrayList<WMSLayer>(len); | |
300 | |
301 for (int i = 0; i < len; i++) { | |
302 layers.add(parseLayer(layersNode.item(i), onlineResource)); | |
303 } | |
304 | |
305 return layers; | |
306 } | |
307 | |
308 | |
309 protected static WMSLayer parseLayer(Node layerNode, String onlineResource) { | |
310 String title = (String) XMLUtils.xpath( | |
311 layerNode, | |
312 "Title/text()", | |
313 XPathConstants.STRING); | |
314 | |
315 String name = (String) XMLUtils.xpath( | |
316 layerNode, | |
317 "Name/text()", | |
318 XPathConstants.STRING); | |
319 | |
320 logger.debug("Found layer: " + title + "(" + name + ")"); | |
321 | |
322 List<String> srs = parseSRS(layerNode); | |
323 | |
324 NodeList layersNodes = (NodeList) XMLUtils.xpath( | |
325 layerNode, | |
326 "Layer", | |
327 XPathConstants.NODESET); | |
328 | |
329 List<WMSLayer> layers = parseLayers(layersNodes, onlineResource); | |
330 | |
331 return new WMSLayer(onlineResource, title, name, srs, layers); | |
332 } | |
333 | |
334 | |
335 protected static List<String> parseSRS(Node layerNode) { | |
336 NodeList srsNodes = ((Element) layerNode).getElementsByTagName("SRS"); | |
337 | |
338 if (srsNodes.getLength() == 0) { | |
339 srsNodes = ((Element) layerNode).getElementsByTagName("CRS"); | |
340 | |
341 if (srsNodes.getLength() == 0) { | |
342 logger.debug("No explicit SRS for this layer specified."); | |
343 return null; | |
344 } | |
345 } | |
346 | |
347 List<String> allSRS = new ArrayList<String>(); | |
348 | |
349 for (int i = 0, n = srsNodes.getLength(); i < n; i++) { | |
350 List<String> srs = parseSRSItem(srsNodes.item(i).getTextContent()); | |
351 | |
352 if (srs != null && srs.size() > 0) { | |
353 allSRS.addAll(srs); | |
354 } | |
355 } | |
356 | |
357 return allSRS; | |
358 } | |
359 | |
360 | |
361 protected static List<String> parseSRSItem(String srsStr) { | |
362 if (srsStr == null || srsStr.length() == 0) { | |
363 return null; | |
364 } | |
365 | |
366 List<String> allSRS = new ArrayList<String>(); | |
367 | |
368 if (srsStr.indexOf(" ") <= 0) { | |
369 String srs = getSRSFromString(srsStr); | |
370 if (srs != null && srs.length() > 0) { | |
371 allSRS.add(srs); | |
372 } | |
373 | |
374 return allSRS; | |
375 } | |
376 | |
377 String[] splittedSrs = srsStr.split(" "); | |
378 | |
379 for (String singleSrs: splittedSrs) { | |
380 String srs = getSRSFromString(singleSrs); | |
381 if (srs != null && srs.length() > 0) { | |
382 allSRS.add(srs); | |
383 } | |
384 } | |
385 | |
386 return allSRS; | |
387 } | |
388 | |
389 | |
390 protected static String getSRSFromString(String singleSrs) { | |
391 Matcher m = SRS_PATTERN.matcher(singleSrs); | |
392 | |
393 if (m.matches()) { | |
394 logger.debug("Found SRS '" + m.group(1) + "'"); | |
395 return m.group(1); | |
396 } | |
397 | |
398 return null; | |
399 } | |
400 } | |
401 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |