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