1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.imagery;
|
---|
3 |
|
---|
4 | import java.awt.HeadlessException;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.io.StringReader;
|
---|
8 | import java.io.StringWriter;
|
---|
9 | import java.net.MalformedURLException;
|
---|
10 | import java.net.URL;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.HashSet;
|
---|
15 | import java.util.Iterator;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Locale;
|
---|
18 | import java.util.NoSuchElementException;
|
---|
19 | import java.util.Set;
|
---|
20 | import java.util.regex.Pattern;
|
---|
21 | import java.util.stream.Collectors;
|
---|
22 | import java.util.stream.Stream;
|
---|
23 | import java.util.stream.StreamSupport;
|
---|
24 |
|
---|
25 | import javax.imageio.ImageIO;
|
---|
26 | import javax.xml.parsers.DocumentBuilder;
|
---|
27 | import javax.xml.parsers.ParserConfigurationException;
|
---|
28 | import javax.xml.transform.TransformerException;
|
---|
29 | import javax.xml.transform.TransformerFactory;
|
---|
30 | import javax.xml.transform.TransformerFactoryConfigurationError;
|
---|
31 | import javax.xml.transform.dom.DOMSource;
|
---|
32 | import javax.xml.transform.stream.StreamResult;
|
---|
33 |
|
---|
34 | import org.openstreetmap.josm.data.Bounds;
|
---|
35 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
36 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
37 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
38 | import org.openstreetmap.josm.tools.HttpClient.Response;
|
---|
39 | import org.openstreetmap.josm.tools.Logging;
|
---|
40 | import org.openstreetmap.josm.tools.Utils;
|
---|
41 | import org.w3c.dom.Document;
|
---|
42 | import org.w3c.dom.Element;
|
---|
43 | import org.w3c.dom.Node;
|
---|
44 | import org.w3c.dom.NodeList;
|
---|
45 | import org.xml.sax.InputSource;
|
---|
46 | import org.xml.sax.SAXException;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * This class represents the capabilities of a WMS imagery server.
|
---|
50 | */
|
---|
51 | public class WMSImagery {
|
---|
52 |
|
---|
53 | private static final class ChildIterator implements Iterator<Element> {
|
---|
54 | private Element child;
|
---|
55 |
|
---|
56 | ChildIterator(Element parent) {
|
---|
57 | child = advanceToElement(parent.getFirstChild());
|
---|
58 | }
|
---|
59 |
|
---|
60 | private static Element advanceToElement(Node firstChild) {
|
---|
61 | Node node = firstChild;
|
---|
62 | while (node != null && !(node instanceof Element)) {
|
---|
63 | node = node.getNextSibling();
|
---|
64 | }
|
---|
65 | return (Element) node;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public boolean hasNext() {
|
---|
70 | return child != null;
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public Element next() {
|
---|
75 | if (!hasNext()) {
|
---|
76 | throw new NoSuchElementException("No next sibling.");
|
---|
77 | }
|
---|
78 | Element next = child;
|
---|
79 | child = advanceToElement(child.getNextSibling());
|
---|
80 | return next;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * An exception that is thrown if there was an error while getting the capabilities of the WMS server.
|
---|
86 | */
|
---|
87 | public static class WMSGetCapabilitiesException extends Exception {
|
---|
88 | private final String incomingData;
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Constructs a new {@code WMSGetCapabilitiesException}
|
---|
92 | * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method)
|
---|
93 | * @param incomingData the answer from WMS server
|
---|
94 | */
|
---|
95 | public WMSGetCapabilitiesException(Throwable cause, String incomingData) {
|
---|
96 | super(cause);
|
---|
97 | this.incomingData = incomingData;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Constructs a new {@code WMSGetCapabilitiesException}
|
---|
102 | * @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method
|
---|
103 | * @param incomingData the answer from the server
|
---|
104 | * @since 10520
|
---|
105 | */
|
---|
106 | public WMSGetCapabilitiesException(String message, String incomingData) {
|
---|
107 | super(message);
|
---|
108 | this.incomingData = incomingData;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * The data that caused this exception.
|
---|
113 | * @return The server response to the capabilities request.
|
---|
114 | */
|
---|
115 | public String getIncomingData() {
|
---|
116 | return incomingData;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | private List<LayerDetails> layers;
|
---|
121 | private URL serviceUrl;
|
---|
122 | private List<String> formats;
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Returns the list of layers.
|
---|
126 | * @return the list of layers
|
---|
127 | */
|
---|
128 | public List<LayerDetails> getLayers() {
|
---|
129 | return Collections.unmodifiableList(layers);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Returns the service URL.
|
---|
134 | * @return the service URL
|
---|
135 | */
|
---|
136 | public URL getServiceUrl() {
|
---|
137 | return serviceUrl;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Returns the list of supported formats.
|
---|
142 | * @return the list of supported formats
|
---|
143 | */
|
---|
144 | public List<String> getFormats() {
|
---|
145 | return Collections.unmodifiableList(formats);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Gets the preffered format for this imagery layer.
|
---|
150 | * @return The preffered format as mime type.
|
---|
151 | */
|
---|
152 | public String getPreferredFormats() {
|
---|
153 | if (formats.contains("image/jpeg")) {
|
---|
154 | return "image/jpeg";
|
---|
155 | } else if (formats.contains("image/png")) {
|
---|
156 | return "image/png";
|
---|
157 | } else if (formats.isEmpty()) {
|
---|
158 | return null;
|
---|
159 | } else {
|
---|
160 | return formats.get(0);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | String buildRootUrl() {
|
---|
165 | if (serviceUrl == null) {
|
---|
166 | return null;
|
---|
167 | }
|
---|
168 | StringBuilder a = new StringBuilder(serviceUrl.getProtocol());
|
---|
169 | a.append("://").append(serviceUrl.getHost());
|
---|
170 | if (serviceUrl.getPort() != -1) {
|
---|
171 | a.append(':').append(serviceUrl.getPort());
|
---|
172 | }
|
---|
173 | a.append(serviceUrl.getPath()).append('?');
|
---|
174 | if (serviceUrl.getQuery() != null) {
|
---|
175 | a.append(serviceUrl.getQuery());
|
---|
176 | if (!serviceUrl.getQuery().isEmpty() && !serviceUrl.getQuery().endsWith("&")) {
|
---|
177 | a.append('&');
|
---|
178 | }
|
---|
179 | }
|
---|
180 | return a.toString();
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Returns the URL for the "GetMap" WMS request in JPEG format.
|
---|
185 | * @param selectedLayers the list of selected layers, matching the "LAYERS" WMS request argument
|
---|
186 | * @return the URL for the "GetMap" WMS request
|
---|
187 | */
|
---|
188 | public String buildGetMapUrl(Collection<LayerDetails> selectedLayers) {
|
---|
189 | return buildGetMapUrl(selectedLayers, "image/jpeg");
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Returns the URL for the "GetMap" WMS request.
|
---|
194 | * @param selectedLayers the list of selected layers, matching the "LAYERS" WMS request argument
|
---|
195 | * @param format the requested image format, matching the "FORMAT" WMS request argument
|
---|
196 | * @return the URL for the "GetMap" WMS request
|
---|
197 | */
|
---|
198 | public String buildGetMapUrl(Collection<LayerDetails> selectedLayers, String format) {
|
---|
199 | return buildRootUrl() + "FORMAT=" + format + (imageFormatHasTransparency(format) ? "&TRANSPARENT=TRUE" : "")
|
---|
200 | + "&VERSION=1.1.1&SERVICE=WMS&REQUEST=GetMap&LAYERS="
|
---|
201 | + selectedLayers.stream().map(x -> x.ident).collect(Collectors.joining(","))
|
---|
202 | + "&STYLES=&SRS={proj}&WIDTH={width}&HEIGHT={height}&BBOX={bbox}";
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Attempts WMS "GetCapabilities" request and initializes internal variables if successful.
|
---|
207 | * @param serviceUrlStr WMS service URL
|
---|
208 | * @throws IOException if any I/O errors occurs
|
---|
209 | * @throws WMSGetCapabilitiesException if the WMS server replies a ServiceException
|
---|
210 | */
|
---|
211 | public void attemptGetCapabilities(String serviceUrlStr) throws IOException, WMSGetCapabilitiesException {
|
---|
212 | URL getCapabilitiesUrl = null;
|
---|
213 | try {
|
---|
214 | if (!Pattern.compile(".*GetCapabilities.*", Pattern.CASE_INSENSITIVE).matcher(serviceUrlStr).matches()) {
|
---|
215 | // If the url doesn't already have GetCapabilities, add it in
|
---|
216 | getCapabilitiesUrl = new URL(serviceUrlStr);
|
---|
217 | final String getCapabilitiesQuery = "VERSION=1.1.1&SERVICE=WMS&REQUEST=GetCapabilities";
|
---|
218 | if (getCapabilitiesUrl.getQuery() == null) {
|
---|
219 | getCapabilitiesUrl = new URL(serviceUrlStr + '?' + getCapabilitiesQuery);
|
---|
220 | } else if (!getCapabilitiesUrl.getQuery().isEmpty() && !getCapabilitiesUrl.getQuery().endsWith("&")) {
|
---|
221 | getCapabilitiesUrl = new URL(serviceUrlStr + '&' + getCapabilitiesQuery);
|
---|
222 | } else {
|
---|
223 | getCapabilitiesUrl = new URL(serviceUrlStr + getCapabilitiesQuery);
|
---|
224 | }
|
---|
225 | } else {
|
---|
226 | // Otherwise assume it's a good URL and let the subsequent error
|
---|
227 | // handling systems deal with problems
|
---|
228 | getCapabilitiesUrl = new URL(serviceUrlStr);
|
---|
229 | }
|
---|
230 | // Make sure we don't keep GetCapabilities request in service URL
|
---|
231 | serviceUrl = new URL(serviceUrlStr.replace("REQUEST=GetCapabilities", "").replace("&&", "&"));
|
---|
232 | } catch (HeadlessException e) {
|
---|
233 | Logging.warn(e);
|
---|
234 | return;
|
---|
235 | }
|
---|
236 |
|
---|
237 | doAttemptGetCapabilities(serviceUrlStr, getCapabilitiesUrl);
|
---|
238 | }
|
---|
239 |
|
---|
240 | private void doAttemptGetCapabilities(String serviceUrlStr, URL getCapabilitiesUrl)
|
---|
241 | throws IOException, WMSGetCapabilitiesException {
|
---|
242 | final Response response = HttpClient.create(getCapabilitiesUrl).connect();
|
---|
243 |
|
---|
244 | if (response.getResponseCode() >= 400) {
|
---|
245 | throw new WMSGetCapabilitiesException(response.getResponseMessage(), response.fetchContent());
|
---|
246 | }
|
---|
247 |
|
---|
248 | try {
|
---|
249 | parseCapabilities(serviceUrlStr, response.getContent());
|
---|
250 | } catch (WMSGetCapabilitiesException e) {
|
---|
251 | String url = getCapabilitiesUrl.toExternalForm();
|
---|
252 | // ServiceException for servers handling only WMS 1.3.0 ?
|
---|
253 | if (e.getCause() == null && url.contains("VERSION=1.1.1")) {
|
---|
254 | doAttemptGetCapabilities(serviceUrlStr, new URL(url.replace("VERSION=1.1.1", "VERSION=1.3.0")));
|
---|
255 | if (serviceUrl.toExternalForm().contains("VERSION=1.1.1")) {
|
---|
256 | serviceUrl = new URL(serviceUrl.toExternalForm().replace("VERSION=1.1.1", "VERSION=1.3.0"));
|
---|
257 | }
|
---|
258 | } else {
|
---|
259 | throw e;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | void parseCapabilities(String serviceUrlStr, InputStream contentStream) throws IOException, WMSGetCapabilitiesException {
|
---|
265 | String incomingData = null;
|
---|
266 | try {
|
---|
267 | DocumentBuilder builder = Utils.newSafeDOMBuilder();
|
---|
268 | builder.setEntityResolver((publicId, systemId) -> {
|
---|
269 | Logging.info("Ignoring DTD " + publicId + ", " + systemId);
|
---|
270 | return new InputSource(new StringReader(""));
|
---|
271 | });
|
---|
272 | Document document = builder.parse(contentStream);
|
---|
273 | Element root = document.getDocumentElement();
|
---|
274 |
|
---|
275 | try {
|
---|
276 | StringWriter writer = new StringWriter();
|
---|
277 | TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(writer));
|
---|
278 | incomingData = writer.getBuffer().toString();
|
---|
279 | Logging.debug("Server response to Capabilities request:");
|
---|
280 | Logging.debug(incomingData);
|
---|
281 | } catch (TransformerFactoryConfigurationError | TransformerException e) {
|
---|
282 | Logging.warn(e);
|
---|
283 | }
|
---|
284 |
|
---|
285 | // Check if the request resulted in ServiceException
|
---|
286 | if ("ServiceException".equals(root.getTagName())) {
|
---|
287 | throw new WMSGetCapabilitiesException(root.getTextContent(), incomingData);
|
---|
288 | }
|
---|
289 |
|
---|
290 | // Some WMS service URLs specify a different base URL for their GetMap service
|
---|
291 | Element child = getChild(root, "Capability");
|
---|
292 | child = getChild(child, "Request");
|
---|
293 | child = getChild(child, "GetMap");
|
---|
294 |
|
---|
295 | formats = getChildrenStream(child, "Format")
|
---|
296 | .map(Node::getTextContent)
|
---|
297 | .filter(WMSImagery::isImageFormatSupportedWarn)
|
---|
298 | .collect(Collectors.toList());
|
---|
299 |
|
---|
300 | child = getChild(child, "DCPType");
|
---|
301 | child = getChild(child, "HTTP");
|
---|
302 | child = getChild(child, "Get");
|
---|
303 | child = getChild(child, "OnlineResource");
|
---|
304 | if (child != null) {
|
---|
305 | String baseURL = child.getAttribute("xlink:href");
|
---|
306 | if (!baseURL.equals(serviceUrlStr)) {
|
---|
307 | URL newURL = new URL(baseURL);
|
---|
308 | if (newURL.getAuthority() != null) {
|
---|
309 | Logging.info("GetCapabilities specifies a different service URL: " + baseURL);
|
---|
310 | serviceUrl = newURL;
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | Element capabilityElem = getChild(root, "Capability");
|
---|
316 | List<Element> children = getChildren(capabilityElem, "Layer");
|
---|
317 | layers = parseLayers(children, new HashSet<String>());
|
---|
318 | } catch (MalformedURLException | ParserConfigurationException | SAXException e) {
|
---|
319 | throw new WMSGetCapabilitiesException(e, incomingData);
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | private static boolean isImageFormatSupportedWarn(String format) {
|
---|
324 | boolean isFormatSupported = isImageFormatSupported(format);
|
---|
325 | if (!isFormatSupported) {
|
---|
326 | Logging.info("Skipping unsupported image format {0}", format);
|
---|
327 | }
|
---|
328 | return isFormatSupported;
|
---|
329 | }
|
---|
330 |
|
---|
331 | static boolean isImageFormatSupported(final String format) {
|
---|
332 | return ImageIO.getImageReadersByMIMEType(format).hasNext()
|
---|
333 | // handles image/tiff image/tiff8 image/geotiff image/geotiff8
|
---|
334 | || isImageFormatSupported(format, "tiff", "geotiff")
|
---|
335 | || isImageFormatSupported(format, "png")
|
---|
336 | || isImageFormatSupported(format, "svg")
|
---|
337 | || isImageFormatSupported(format, "bmp");
|
---|
338 | }
|
---|
339 |
|
---|
340 | static boolean isImageFormatSupported(String format, String... mimeFormats) {
|
---|
341 | for (String mime : mimeFormats) {
|
---|
342 | if (format.startsWith("image/" + mime)) {
|
---|
343 | return ImageIO.getImageReadersBySuffix(mimeFormats[0]).hasNext();
|
---|
344 | }
|
---|
345 | }
|
---|
346 | return false;
|
---|
347 | }
|
---|
348 |
|
---|
349 | static boolean imageFormatHasTransparency(final String format) {
|
---|
350 | return format != null && (format.startsWith("image/png") || format.startsWith("image/gif")
|
---|
351 | || format.startsWith("image/svg") || format.startsWith("image/tiff"));
|
---|
352 | }
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * Returns a new {@code ImageryInfo} describing the given service name and selected WMS layers.
|
---|
356 | * @param name service name
|
---|
357 | * @param selectedLayers selected WMS layers
|
---|
358 | * @return a new {@code ImageryInfo} describing the given service name and selected WMS layers
|
---|
359 | */
|
---|
360 | public ImageryInfo toImageryInfo(String name, Collection<LayerDetails> selectedLayers) {
|
---|
361 | ImageryInfo i = new ImageryInfo(name, buildGetMapUrl(selectedLayers));
|
---|
362 | if (selectedLayers != null) {
|
---|
363 | Set<String> proj = new HashSet<>();
|
---|
364 | for (WMSImagery.LayerDetails l : selectedLayers) {
|
---|
365 | proj.addAll(l.getProjections());
|
---|
366 | }
|
---|
367 | i.setServerProjections(proj);
|
---|
368 | }
|
---|
369 | return i;
|
---|
370 | }
|
---|
371 |
|
---|
372 | private List<LayerDetails> parseLayers(List<Element> children, Set<String> parentCrs) {
|
---|
373 | List<LayerDetails> details = new ArrayList<>(children.size());
|
---|
374 | for (Element element : children) {
|
---|
375 | details.add(parseLayer(element, parentCrs));
|
---|
376 | }
|
---|
377 | return details;
|
---|
378 | }
|
---|
379 |
|
---|
380 | private LayerDetails parseLayer(Element element, Set<String> parentCrs) {
|
---|
381 | String name = getChildContent(element, "Title", null, null);
|
---|
382 | String ident = getChildContent(element, "Name", null, null);
|
---|
383 | String abstr = getChildContent(element, "Abstract", null, null);
|
---|
384 |
|
---|
385 | // The set of supported CRS/SRS for this layer
|
---|
386 | Set<String> crsList = new HashSet<>();
|
---|
387 | // ...including this layer's already-parsed parent projections
|
---|
388 | crsList.addAll(parentCrs);
|
---|
389 |
|
---|
390 | // Parse the CRS/SRS pulled out of this layer's XML element
|
---|
391 | // I think CRS and SRS are the same at this point
|
---|
392 | getChildrenStream(element)
|
---|
393 | .filter(child -> "CRS".equals(child.getNodeName()) || "SRS".equals(child.getNodeName()))
|
---|
394 | .map(WMSImagery::getContent)
|
---|
395 | .filter(crs -> !crs.isEmpty())
|
---|
396 | .map(crs -> crs.trim().toUpperCase(Locale.ENGLISH))
|
---|
397 | .forEach(crsList::add);
|
---|
398 |
|
---|
399 | // Check to see if any of the specified projections are supported by JOSM
|
---|
400 | boolean josmSupportsThisLayer = false;
|
---|
401 | for (String crs : crsList) {
|
---|
402 | josmSupportsThisLayer |= isProjSupported(crs);
|
---|
403 | }
|
---|
404 |
|
---|
405 | Bounds bounds = null;
|
---|
406 | Element bboxElem = getChild(element, "EX_GeographicBoundingBox");
|
---|
407 | if (bboxElem != null) {
|
---|
408 | // Attempt to use EX_GeographicBoundingBox for bounding box
|
---|
409 | double left = Double.parseDouble(getChildContent(bboxElem, "westBoundLongitude", null, null));
|
---|
410 | double top = Double.parseDouble(getChildContent(bboxElem, "northBoundLatitude", null, null));
|
---|
411 | double right = Double.parseDouble(getChildContent(bboxElem, "eastBoundLongitude", null, null));
|
---|
412 | double bot = Double.parseDouble(getChildContent(bboxElem, "southBoundLatitude", null, null));
|
---|
413 | bounds = new Bounds(bot, left, top, right);
|
---|
414 | } else {
|
---|
415 | // If that's not available, try LatLonBoundingBox
|
---|
416 | bboxElem = getChild(element, "LatLonBoundingBox");
|
---|
417 | if (bboxElem != null) {
|
---|
418 | double left = getDecimalDegree(bboxElem, "minx");
|
---|
419 | double top = getDecimalDegree(bboxElem, "maxy");
|
---|
420 | double right = getDecimalDegree(bboxElem, "maxx");
|
---|
421 | double bot = getDecimalDegree(bboxElem, "miny");
|
---|
422 | bounds = new Bounds(bot, left, top, right);
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | List<Element> layerChildren = getChildren(element, "Layer");
|
---|
427 | List<LayerDetails> childLayers = parseLayers(layerChildren, crsList);
|
---|
428 |
|
---|
429 | return new LayerDetails(name, ident, abstr, crsList, josmSupportsThisLayer, bounds, childLayers);
|
---|
430 | }
|
---|
431 |
|
---|
432 | private static double getDecimalDegree(Element elem, String attr) {
|
---|
433 | // Some real-world WMS servers use a comma instead of a dot as decimal separator (seen in Polish WMS server)
|
---|
434 | return Double.parseDouble(elem.getAttribute(attr).replace(',', '.'));
|
---|
435 | }
|
---|
436 |
|
---|
437 | private static boolean isProjSupported(String crs) {
|
---|
438 | return Projections.getProjectionByCode(crs) != null;
|
---|
439 | }
|
---|
440 |
|
---|
441 | private static String getChildContent(Element parent, String name, String missing, String empty) {
|
---|
442 | Element child = getChild(parent, name);
|
---|
443 | if (child == null)
|
---|
444 | return missing;
|
---|
445 | else {
|
---|
446 | String content = getContent(child);
|
---|
447 | return (!content.isEmpty()) ? content : empty;
|
---|
448 | }
|
---|
449 | }
|
---|
450 |
|
---|
451 | private static String getContent(Element element) {
|
---|
452 | NodeList nl = element.getChildNodes();
|
---|
453 | StringBuilder content = new StringBuilder();
|
---|
454 | for (int i = 0; i < nl.getLength(); i++) {
|
---|
455 | Node node = nl.item(i);
|
---|
456 | switch (node.getNodeType()) {
|
---|
457 | case Node.ELEMENT_NODE:
|
---|
458 | content.append(getContent((Element) node));
|
---|
459 | break;
|
---|
460 | case Node.CDATA_SECTION_NODE:
|
---|
461 | case Node.TEXT_NODE:
|
---|
462 | content.append(node.getNodeValue());
|
---|
463 | break;
|
---|
464 | default: // Do nothing
|
---|
465 | }
|
---|
466 | }
|
---|
467 | return content.toString().trim();
|
---|
468 | }
|
---|
469 |
|
---|
470 | private static Stream<Element> getChildrenStream(Element parent) {
|
---|
471 | if (parent == null) {
|
---|
472 | // ignore missing elements
|
---|
473 | return Stream.empty();
|
---|
474 | } else {
|
---|
475 | Iterable<Element> it = () -> new ChildIterator(parent);
|
---|
476 | return StreamSupport.stream(it.spliterator(), false);
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | private static Stream<Element> getChildrenStream(Element parent, String name) {
|
---|
481 | return getChildrenStream(parent).filter(child -> name.equals(child.getNodeName()));
|
---|
482 | }
|
---|
483 |
|
---|
484 | private static List<Element> getChildren(Element parent, String name) {
|
---|
485 | return getChildrenStream(parent, name).collect(Collectors.toList());
|
---|
486 | }
|
---|
487 |
|
---|
488 | private static Element getChild(Element parent, String name) {
|
---|
489 | return getChildrenStream(parent, name).findFirst().orElse(null);
|
---|
490 | }
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * The details of a layer of this WMS server.
|
---|
494 | */
|
---|
495 | public static class LayerDetails {
|
---|
496 |
|
---|
497 | /**
|
---|
498 | * The layer name (WMS {@code Title})
|
---|
499 | */
|
---|
500 | public final String name;
|
---|
501 | /**
|
---|
502 | * The layer ident (WMS {@code Name})
|
---|
503 | */
|
---|
504 | public final String ident;
|
---|
505 | /**
|
---|
506 | * The layer abstract (WMS {@code Abstract})
|
---|
507 | * @since 13199
|
---|
508 | */
|
---|
509 | public final String abstr;
|
---|
510 | /**
|
---|
511 | * The child layers of this layer
|
---|
512 | */
|
---|
513 | public final List<LayerDetails> children;
|
---|
514 | /**
|
---|
515 | * The bounds this layer can be used for
|
---|
516 | */
|
---|
517 | public final Bounds bounds;
|
---|
518 | /**
|
---|
519 | * the CRS/SRS pulled out of this layer's XML element
|
---|
520 | */
|
---|
521 | public final Set<String> crsList;
|
---|
522 | /**
|
---|
523 | * {@code true} if any of the specified projections are supported by JOSM
|
---|
524 | */
|
---|
525 | public final boolean supported;
|
---|
526 |
|
---|
527 | /**
|
---|
528 | * Constructs a new {@code LayerDetails}.
|
---|
529 | * @param name The layer name (WMS {@code Title})
|
---|
530 | * @param ident The layer ident (WMS {@code Name})
|
---|
531 | * @param abstr The layer abstract (WMS {@code Abstract})
|
---|
532 | * @param crsList The CRS/SRS pulled out of this layer's XML element
|
---|
533 | * @param supportedLayer {@code true} if any of the specified projections are supported by JOSM
|
---|
534 | * @param bounds The bounds this layer can be used for
|
---|
535 | * @param childLayers The child layers of this layer
|
---|
536 | * @since 13199
|
---|
537 | */
|
---|
538 | public LayerDetails(String name, String ident, String abstr, Set<String> crsList, boolean supportedLayer, Bounds bounds,
|
---|
539 | List<LayerDetails> childLayers) {
|
---|
540 | this.name = name;
|
---|
541 | this.ident = ident;
|
---|
542 | this.abstr = abstr;
|
---|
543 | this.supported = supportedLayer;
|
---|
544 | this.children = childLayers;
|
---|
545 | this.bounds = bounds;
|
---|
546 | this.crsList = crsList;
|
---|
547 | }
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * Determines if any of the specified projections are supported by JOSM.
|
---|
551 | * @return {@code true} if any of the specified projections are supported by JOSM
|
---|
552 | */
|
---|
553 | public boolean isSupported() {
|
---|
554 | return this.supported;
|
---|
555 | }
|
---|
556 |
|
---|
557 | /**
|
---|
558 | * Returns the CRS/SRS pulled out of this layer's XML element.
|
---|
559 | * @return the CRS/SRS pulled out of this layer's XML element
|
---|
560 | */
|
---|
561 | public Set<String> getProjections() {
|
---|
562 | return crsList;
|
---|
563 | }
|
---|
564 |
|
---|
565 | @Override
|
---|
566 | public String toString() {
|
---|
567 | String baseName = (name == null || name.isEmpty()) ? ident : name;
|
---|
568 | return abstr == null || abstr.equalsIgnoreCase(baseName) ? baseName : baseName + " (" + abstr + ')';
|
---|
569 | }
|
---|
570 | }
|
---|
571 | }
|
---|