📄 capabilities.java
字号:
/*Copyright (C) 2001, 2006 United States Governmentas represented by the Administrator of theNational Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.wms;import gov.nasa.worldwind.exception.*;import gov.nasa.worldwind.retrieve.*;import gov.nasa.worldwind.util.*;import org.w3c.dom.*;import org.xml.sax.SAXException;import javax.xml.XMLConstants;import javax.xml.parsers.*;import javax.xml.xpath.*;import java.io.*;import java.net.*;import java.nio.ByteBuffer;import java.util.*;import java.util.logging.Level;/** * @author tag * @version $Id: Capabilities.java 9600 2009-03-22 20:04:40Z tgaskins $ */public abstract class Capabilities{ public static final String WMS_SERVICE_NAME = "OGC:WMS"; protected Document doc; protected Element service; protected Element capability; protected XPath xpath; public static Capabilities retrieve(URI uri, String service) throws Exception { return retrieve(uri, service, null, null); } public static Capabilities retrieve(URI uri, Integer connectTimeout, Integer readTimeout) throws Exception { return retrieve(uri, null, connectTimeout, readTimeout); } public static Capabilities retrieve(URI uri, String service, Integer connectTimeout, Integer readTimeout) throws Exception { if (uri == null) { String message = Logging.getMessage("nullValue.URIIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } InputStream is = null; try { // Request the capabilities document from the server. CapabilitiesRequest req = new CapabilitiesRequest(uri, service); URL capsURL = req.getUri().toURL(); URLRetriever retriever = URLRetriever.createRetriever(capsURL, new RetrievalPostProcessor() { public ByteBuffer run(Retriever retriever) { return retriever.getBuffer(); } }); if (retriever == null) { String message = Logging.getMessage("generic.UnrecognizedProtocol"); Logging.logger().severe(message); throw new WWRuntimeException(message); } if (connectTimeout != null) retriever.setConnectTimeout(connectTimeout); if (readTimeout != null) retriever.setReadTimeout(readTimeout); retriever.call(); if (!retriever.getState().equals(URLRetriever.RETRIEVER_STATE_SUCCESSFUL)) { String message = Logging.getMessage("generic.RetrievalFailed", uri.toString()); Logging.logger().severe(message); throw new WWRuntimeException(message); } if (retriever.getBuffer() == null || retriever.getBuffer().limit() == 0) { String message = Logging.getMessage("generic.RetrievalReturnedNoContent", uri.toString()); Logging.logger().severe(message); throw new WWRuntimeException(message); } if (retriever.getContentType().equalsIgnoreCase("application/vnd.ogc.se_xml")) { String exceptionMessage = WWXML.extractOGCServiceException(retriever.getBuffer()); String message = Logging.getMessage("OGC.ServiceException", uri.toString() + ": " + exceptionMessage != null ? exceptionMessage : ""); Logging.logger().severe(message); throw new WWRuntimeException(message); } // Parse the DOM as a capabilities document. is = WWIO.getInputStreamFromByteBuffer(retriever.getBuffer()); return Capabilities.parse(WWXML.createDocumentBuilder(true).parse(is)); } catch (URISyntaxException e) { Logging.logger().log(java.util.logging.Level.SEVERE, Logging.getMessage("generic.URIInvalid", uri.toString()), e); throw e; } catch (ParserConfigurationException e) { Logging.logger().fine(Logging.getMessage("WMS.ParserConfigurationException", uri.toString())); throw e; } catch (IOException e) { Logging.logger().log(java.util.logging.Level.SEVERE, Logging.getMessage("generic.ExceptionAttemptingToReadFrom", uri.toString()), e); throw e; } catch (SAXException e) { Logging.logger().fine(Logging.getMessage("WMS.ParsingError", uri.toString())); throw e; } finally { WWIO.closeStream(is, uri.toString()); } } public static Capabilities parse(Document doc) { XPathFactory xpFactory = XPathFactory.newInstance(); XPath xpath = xpFactory.newXPath(); SimpleNamespaceContext nsc = new SimpleNamespaceContext(); nsc.addNamespace(XMLConstants.DEFAULT_NS_PREFIX, "http://www.opengis.net/wms"); xpath.setNamespaceContext(new SimpleNamespaceContext()); try { String exceptionMessage = WWXML.checkOGCException(doc); if (exceptionMessage != null) { String message = Logging.getMessage("WMS.ServiceException", exceptionMessage); Logging.logger().severe(message); throw new ServiceException(exceptionMessage); } String version = xpath.evaluate(altPaths("*/@wms:version"), doc); if (version == null || version.length() == 0) return null; if (version.compareTo("1.3") < 0) return new CapabilitiesV111(doc, xpath); else return new CapabilitiesV130(doc, xpath); } catch (XPathExpressionException e) { Logging.logger().log(Level.SEVERE, "WMS.ParsingError", e); return null; } } protected Capabilities(Document doc, XPath xpath) { this.doc = doc; this.xpath = xpath; try { this.service = (Element) this.xpath.evaluate(altPaths("*/wms:Service"), doc, XPathConstants.NODE); if (this.service == null) { String message = Logging.getMessage("WMS.NoServiceElement", "XML document"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } this.capability = (Element) this.xpath.evaluate(altPaths("*/wms:Capability"), doc, XPathConstants.NODE); if (this.capability == null) { String message = Logging.getMessage("WMS.NoCapabilityElement", "XML document"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } } catch (XPathExpressionException e) { Logging.logger().log(Level.SEVERE, "WMS.ParsingError", e); } } private static String altPaths(String path) // hack for WW server layer names with leading pipe { return path != null ? path + "|" + path.replaceAll("wms:", "") : null; } protected String getText(String path) { return this.getText(null, path); } protected String getText(Element context, String path) { try { return this.xpath.evaluate(altPaths(path), context != null ? context : doc); } catch (XPathExpressionException e) { return null; } } protected String[] getTextArray(Element context, String path) { try { NodeList nodes = (NodeList) this.xpath.evaluate(altPaths(path), context != null ? context : doc, XPathConstants.NODESET); if (nodes == null || nodes.getLength() == 0) return null; String[] strings = new String[nodes.getLength()]; for (int i = 0; i < nodes.getLength(); i++) { strings[i] = nodes.item(i).getTextContent(); } return strings; } catch (XPathExpressionException e) { return null; } } protected String[] getUniqueText(Element context, String path) { String[] strings = this.getTextArray(context, path); if (strings == null) return null; ArrayList<String> sarl = new ArrayList<String>(); for (String s : strings) { if (!sarl.contains(s)) sarl.add(s); } return sarl.toArray(new String[1]); } protected Element getElement(Element context, String path) { try { Node node = (Node) this.xpath.evaluate(altPaths(path), context != null ? context : doc, XPathConstants.NODE); if (node == null) return null; return node instanceof Element ? (Element) node : null; } catch (XPathExpressionException e) { return null; } } protected Element[] getElements(Element context, String path) { try { NodeList nodes = (NodeList) this.xpath.evaluate(altPaths(path), context != null ? context : doc, XPathConstants.NODESET); if (nodes == null || nodes.getLength() == 0) return null; Element[] elements = new Element[nodes.getLength()]; for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element) elements[i] = (Element) node; } return elements; } catch (XPathExpressionException e) { return null; } } public Element[] getUniqueElements(Element context, String path, String uniqueTag) { Element[] elements = this.getElements(context, path); if (elements == null) return null; HashMap<String, Element> styles = new HashMap<String, Element>(); for (Element e : elements) { String name = this.getText(e, uniqueTag); if (name != null) styles.put(name, e); } return styles.values().toArray(new Element[1]); } private HashMap<Element, Layer> namedLayerElements = new HashMap<Element, Layer>(); private HashMap<String, Layer> namedLayers = new HashMap<String, Layer>(); private void fillLayerList() { if (this.namedLayers.size() == 0) { Element[] nels = this.getElements(this.capability, "descendant::wms:Layer[wms:Name]"); if (nels == null || nels.length == 0) return; for (Element le : nels) { String name = this.getLayerName(le); if (name != null) { Layer layer = new Layer(le); this.namedLayers.put(name, layer); this.namedLayerElements.put(le, layer); } } } } public Element[] getNamedLayers() { if (this.namedLayerElements.size() == 0) this.fillLayerList(); return this.namedLayerElements.keySet().toArray(new Element[this.namedLayerElements.size()]); } public Element getLayerByName(String layerName) { if (this.namedLayers.size() == 0) this.fillLayerList(); Layer l = this.namedLayers.get(layerName); return l != null ? l.element : null; } public Long getLayerLatestLastUpdateTime(Capabilities caps, String[] layerNames) { if (caps == null) { String message = Logging.getMessage("nullValue.WMSCapabilities"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (layerNames == null) { String message = Logging.getMessage("nullValue.WMSLayerNames"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } String lastUpdate = null; for (String name : layerNames) { Element layer = caps.getLayerByName(name); if (layer == null) continue; String update = caps.getLayerLastUpdate(layer); if (update != null && (lastUpdate == null || update.compareTo(lastUpdate) > 0)) lastUpdate = update; } if (lastUpdate != null) { try { return Long.parseLong(lastUpdate); } catch (NumberFormatException e) { String message = Logging.getMessage("generic.ConversionError", lastUpdate); Logging.logger().warning(message); } } return null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -