/**
* OrbisGIS is a java GIS application dedicated to research in GIScience.
* OrbisGIS is developed by the GIS group of the DECIDE team of the
* Lab-STICC CNRS laboratory, see <http://www.lab-sticc.fr/>.
*
* The GIS group of the DECIDE team is located at :
*
* Laboratoire Lab-STICC – CNRS UMR 6285
* Equipe DECIDE
* UNIVERSITÉ DE BRETAGNE-SUD
* Institut Universitaire de Technologie de Vannes
* 8, Rue Montaigne - BP 561 56017 Vannes Cedex
*
* OrbisGIS is distributed under GPL 3 license.
*
* Copyright (C) 2007-2014 CNRS (IRSTV FR CNRS 2488)
* Copyright (C) 2015-2017 CNRS (Lab-STICC UMR CNRS 6285)
*
* This file is part of OrbisGIS.
*
* OrbisGIS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* OrbisGIS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* OrbisGIS. If not, see <http://www.gnu.org/licenses/>.
*
* For more information, please consult: <http://www.orbisgis.org/>
* or contact directly:
* info_at_ orbisgis.org
*/
// Changed by Uwe Dalluege, uwe.dalluege@rzcn.haw-hamburg.de
// to differ between LatLonBoundingBox and BoundingBox
// 2005-08-09
// Completely refactored by Michael Michaud on 2013-04-13
package com.vividsolutions.wms;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import org.apache.xerces.parsers.DOMParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.vividsolutions.wms.util.XMLTools;
/**
* Pulls WMS objects out of the XML
* @author Chris Hodgson chodgson@refractions.net
* @author Michael Michaud michael.michaud@free.fr
*/
public abstract class AbstractParser implements IParser {
private static Logger LOG = LoggerFactory.getLogger(AbstractParser.class);
/**
* Creates a Parser for dealing with WMS XML.
*/
public AbstractParser() {}
/**
* Parses the WMT_MS_Capabilities XML from the given InputStream into
* a Capabilities object.
* @param service the WMService from which this MapDescriptor is derived
* @param inStream the inputStream containing the WMT_MS_Capabilities XML to parse
* @return the MapDescriptor object created from the specified XML InputStream
*/
public Capabilities parseCapabilities(WMService service, InputStream inStream) throws IOException {
Document doc;
try {
DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation", false);
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
parser.parse(new InputSource(inStream));
doc = parser.getDocument();
checkCapabilities(doc);
} catch(SAXException saxe) {
throw new IOException(saxe.toString());
}
return parseCapabilities(service, doc);
}
abstract protected String getRootPath();
protected void checkCapabilities(Document doc) throws IOException {
if (XMLTools.simpleXPath(doc, getRootPath()) == null) {
DOMImplementationRegistry registry;
String str = "";
try {
registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
str = writer.writeToString(doc);
} catch (Exception e1) {
e1.printStackTrace();
}
throw new WMSException("Missing root node <" + getRootPath() + "> : you probably use a wrong URL or a wrong version of WMS", str);
}
}
abstract public Capabilities parseCapabilities(WMService service, Document doc) throws IOException;
protected String getTitlePath() {
return getRootPath() + "/Service/Title";
}
protected String getTitle(Document doc) throws IOException {
String title;
try {
title = ((CharacterData)XMLTools.simpleXPath(doc, getTitlePath()).getFirstChild()).getData();
} catch (Exception e) {
// possible NullPointerException if there is no firstChild()
// also possible miscast causing an Exception
// [uwe dalluege]
throw new IOException( "Element <" + getTitlePath() + "> not found, maybe a WMS version problem! " );
}
return title;
}
// path is common for wms 1.1 and more (overriden by WMS 1.0 which followed
// a different path
protected LinkedList<String> getFormatList(Document doc) throws IOException {
LinkedList<String> formatList = new LinkedList<>();
final Node formatNode = XMLTools.simpleXPath(doc, getRootPath() + "/Capability/Request/GetMap");
NodeList nl = formatNode.getChildNodes();
for( int i=0; i < nl.getLength(); i++ ) {
Node n = nl.item( i );
if(n.getNodeType() == Node.ELEMENT_NODE && "Format".equals(n.getNodeName())) {
String format = n.getFirstChild().getNodeValue();
if (format.matches("^image/(png|jpeg|gif).*")) {
formatList.add(format);
}
}
}
return formatList;
}
/**
* Traverses the DOM tree underneath the specified Node and generates
* a corresponding WMSLayer object tree. The returned WMSLayer will be set to
* have the specified parent.
* @param layerNode a DOM Node which is a <layer> XML element
* @return a WMSLayer with complete subLayer tree that corresponds
* to the DOM Node provided
*/
public MapLayer wmsLayerFromNode( Node layerNode ) {
String name = null;
String title = null;
LinkedList<String> srsList = new LinkedList<>();
LinkedList<MapLayer> subLayers = new LinkedList<>();
BoundingBox geographicBBox = null;
Map<String, BoundingBox> boundingBoxMap = new HashMap<>();
NodeList nl = layerNode.getChildNodes();
for( int i = 0; i < nl.getLength(); i++ ) {
Node n = nl.item( i );
try {
if( n.getNodeType() == Node.ELEMENT_NODE ) {
String nodeName = n.getNodeName();
if( nodeName.equals( "Name" ) ) {
name = ((CharacterData)n.getFirstChild()).getData();
} else if( nodeName.equals( "Title" ) ) {
title = ((CharacterData)n.getFirstChild()).getData();
} else if(nodeName.equals( getSRSName() ) ) {
addSRSNode(n, srsList);
} else if(nodeName.equals( "LatLonBoundingBox" ) ) {
geographicBBox = latLonBoundingBoxFromNode( n );
boundingBoxMap.put(BoundingBox.GEOGRAPHICS_EPSG, geographicBBox);
boundingBoxMap.put(BoundingBox.GEOGRAPHICS,
new BoundingBox("Geographics", geographicBBox.getEnvelope()));
} else if(nodeName.equals( "BoundingBox" ) ) {
BoundingBox fromNode = boundingBoxFromNode(n);
boundingBoxMap.put(fromNode.getSRS(),fromNode);
} else if(nodeName.equals( "EX_GeographicBoundingBox" ) ) {
geographicBBox = exGeographicBoundingBoxFromNode( n );
boundingBoxMap.put(BoundingBox.GEOGRAPHICS_EPSG, geographicBBox);
boundingBoxMap.put(BoundingBox.GEOGRAPHICS,
new BoundingBox("Geographics", geographicBBox.getEnvelope()));
} else if( n.getNodeName().equals( "Layer" ) ) {
subLayers.add( wmsLayerFromNode( n ) );
}
}
} catch( Exception e ) {
e.printStackTrace();
LOG.error( "Exception caught in wmsLayerFromNode(): " + e.toString() );
}
}
// call the new constructor with boundingBoxList in MapLayer [uwe dalluege]
return new MapLayer(name, title, srsList, subLayers, geographicBBox, boundingBoxMap);
}
protected void addSRSNode(Node n, List<String> srsList) throws Exception {
Node firstChildNode = n.getFirstChild();
if(firstChildNode instanceof CharacterData) {
String srsString = ((CharacterData) firstChildNode).getData();
if(srsString != null) {
String[] tokens = srsString.split("\\s+");
Collections.addAll(srsList, tokens);
}
}
}
protected BoundingBox boundingBoxFromNode(Node n) throws Exception {
try {
NamedNodeMap nm = n.getAttributes();
String srs = nm.getNamedItem(getSRSName()).getNodeValue();
double minx = getCoord("minx", nm);
double miny = getCoord("miny", nm);
double maxx = getCoord("maxx", nm);
double maxy = getCoord("maxy", nm);
return new BoundingBox(srs, minx, miny, maxx, maxy);
} catch( Exception e ) {
// possible NullPointerException from getNamedItem returning a null
// also possible NumberFormatException
throw new Exception( "Invalid bounding box element node: " + e.toString() );
}
}
protected BoundingBox latLonBoundingBoxFromNode(Node n) throws Exception {
try {
NamedNodeMap nm = n.getAttributes();
String srs = "EPSG:4326";
double minx = getCoord("minx", nm);
double miny = getCoord("miny", nm);
double maxx = getCoord("maxx", nm);
double maxy = getCoord("maxy", nm);
return new BoundingBox(srs, minx, miny, maxx, maxy);
} catch( Exception e ) {
throw new Exception( "Invalid bounding box element node: " + e.toString() );
}
}
public BoundingBox exGeographicBoundingBoxFromNode(Node n) throws Exception {
try {
String srs = "EPSG:4326";
double minx = 0.0;
double miny = 0.0;
double maxx = 0.0;
double maxy = 0.0;
NodeList childNodes = n.getChildNodes();
for( int i = 0; i < childNodes.getLength(); i++ ) {
Node childNode = childNodes.item( i );
if( childNode.getNodeType() == Node.ELEMENT_NODE ) {
if( childNode.getNodeName().equals( "westBoundLongitude" ) ) {
minx = getCoord(childNode.getTextContent().trim());
} else if( childNode.getNodeName().equals( "eastBoundLongitude" ) ) {
maxx = getCoord(childNode.getTextContent().trim());
} else if( childNode.getNodeName().equals( "southBoundLatitude" ) ) {
miny = getCoord(childNode.getTextContent().trim());
} else if( childNode.getNodeName().equals( "northBoundLatitude" ) ) {
maxy = getCoord(childNode.getTextContent().trim());
}
}
}
return new BoundingBox(srs, minx, miny, maxx, maxy);
} catch( Exception e ) {
throw new Exception( "Invalid bounding box element node: " + e.toString() );
}
}
// Coordinates in attributes minx, miny, maxx, maxy
public double getCoord(String name, NamedNodeMap nm) throws Exception {
return getCoord(nm.getNamedItem(name).getNodeValue());
}
// Coordinates in subelements westBoundLongitude, southBoundLongitude...
public double getCoord(String text) throws Exception {
if (text.equals("inf")) {
return Double.POSITIVE_INFINITY;
} else {
return Double.parseDouble(text);
}
}
abstract protected String getSRSName();
}