Mercurial > dive4elements > gnv-client
diff gnv/src/main/java/de/intevation/gnv/action/mapviewer/parser/XMLExternalCallParser.java @ 402:b88e881e8e94
Added the first Implementation (not complete and not ready to use) for the Interface from the MapViewer to the GNV
gnv/trunk@573 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Tue, 19 Jan 2010 14:47:36 +0000 |
parents | |
children | 15ac78a91d1b |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/action/mapviewer/parser/XMLExternalCallParser.java Tue Jan 19 14:47:36 2010 +0000 @@ -0,0 +1,155 @@ +/** + * + */ +package de.intevation.gnv.action.mapviewer.parser; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; + + +import org.apache.log4j.Logger; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import de.intevation.gnv.artifactdatabase.objects.map.DefaultLayer; +import de.intevation.gnv.artifactdatabase.objects.map.DefaultMapService; +import de.intevation.gnv.artifactdatabase.objects.map.Layer; +import de.intevation.gnv.artifactdatabase.objects.map.MapService; +import de.intevation.gnv.util.XMLUtils; + +/** + * @author Tim Englich <tim.englich@intevation.de> + * + */ +public class XMLExternalCallParser implements ExternalCallParser { + + /** + * the logger, used to log exceptions and additonaly information + */ + private static Logger log = Logger.getLogger(XMLExternalCallParser.class); + + private static String XPATH_GEOMETRY = "/gnviewer/location/data"; + private static String XPATH_SRS = "/gnviewer/location/srs"; + private static String XPATH_MAPSERVICES_NODESET = "/gnviewer/mapservices/mapservice"; + private static String XPATH_LAYER = "layer"; + private static String ATTRIBUTE_ID = "id"; + private static String ATTRIBUTE_NAME = "name"; + private static String ATTRIBUTE_TYPE = "type"; + private static String ATTRIBUTE_URL = "url"; + + private String geometry = null; + private String srs = null; + + private Collection<MapService> mapServices = null; + + private InputStream inputStream = null; + + /** + * Constructor + */ + public XMLExternalCallParser(InputStream inputStream) { + this.inputStream = inputStream; + } + + /** + * @see de.intevation.gnv.action.mapviewer.parser.ExternalCallParser#getGeometry() + */ + public String getGeometry() { + return this.geometry; + } + + /** + * @see de.intevation.gnv.action.mapviewer.parser.ExternalCallParser#getMapServices() + */ + public Collection<MapService> getMapServices() { + return this.mapServices; + } + + /** + * @see de.intevation.gnv.action.mapviewer.parser.ExternalCallParser#parse() + */ + public void parse() throws ExternalCallParserException { + if (inputStream != null){ + XMLUtils xmlUtils = new XMLUtils(); + Document document = xmlUtils.readDocument(this.inputStream); + if (document != null){ + + this.geometry = xmlUtils.getStringXPath(document, XPATH_GEOMETRY); + this.srs = xmlUtils.getStringXPath(document, XPATH_SRS); + NodeList mapservicesNodes = + xmlUtils.getNodeSetXPath(document, XPATH_MAPSERVICES_NODESET); + if (mapservicesNodes != null){ + this.mapServices = new ArrayList<MapService>(mapservicesNodes.getLength()); + for (int i = 0; i < mapservicesNodes.getLength(); i++){ + Element mapserviceNode = (Element)mapservicesNodes.item(i); + String mapserviceID = mapserviceNode.getAttribute(ATTRIBUTE_ID); + String mapserviceType = mapserviceNode.getAttribute(ATTRIBUTE_TYPE); + String mapserviceUrl = mapserviceNode.getAttribute(ATTRIBUTE_URL); + NodeList layerNodes = xmlUtils.getNodeSetXPath(mapserviceNode, XPATH_LAYER); + Collection<Layer> layer = null; + if (layerNodes != null && layerNodes.getLength() > 0){ + layer = new ArrayList<Layer>(layerNodes.getLength()); + layer = this.extractLayer(layer, null, layerNodes); + }else{ + log.debug("No Layer given for this Mapservice"); + } + MapService mapService = + new DefaultMapService(mapserviceID, layer, + mapserviceType, mapserviceUrl); + this.mapServices.add(mapService); + } + + }else{ + String errMsg = "XML-Document does not contain any Mapservices which are required."; + log.error(errMsg); + throw new ExternalCallParserException(errMsg); + } + + }else{ + String errMsg = "XML-Document could not be read from InputStream."; + log.error(errMsg); + throw new ExternalCallParserException(errMsg); + } + }else{ + String errMsg = "No InputStream given for parsing the Call."; + log.error(errMsg); + throw new ExternalCallParserException(errMsg); + } + } + + /** + * This Method extracts all Layers and put them into the Collection. + * @param layer + * @param groupId + * @param layerNodes + * @return + */ + private Collection<Layer> extractLayer(Collection<Layer> layer, String groupId, NodeList layerNodes){ + XMLUtils xmlUtils = new XMLUtils(); + for (int i = 0; i < layerNodes.getLength(); i++){ + Element layerNode = (Element)layerNodes.item(i); + String id = layerNode.getAttribute(ATTRIBUTE_ID); + String name = layerNode.getAttribute(ATTRIBUTE_NAME); + NodeList localLayerNodes = xmlUtils.getNodeSetXPath(layerNode, XPATH_LAYER); + Layer tmpLayer = new DefaultLayer(id, name, + (localLayerNodes != null && + localLayerNodes.getLength() > 0), + groupId); + layer.add(tmpLayer); + if (localLayerNodes != null && localLayerNodes.getLength() > 0){ + layer = this.extractLayer(layer, id, localLayerNodes); + } + } + return layer; + } + + /** + * @see de.intevation.gnv.action.mapviewer.parser.ExternalCallParser#getSRS() + */ + public String getSRS() { + return this.srs; + } + +}