view gnv/src/main/java/de/intevation/gnv/action/mapviewer/parser/XMLExternalCallParser.java @ 1022:28a0628b11b0

Added license file and license header. gnv/trunk@1258 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:15:08 +0000
parents 4b64b4d1f0cf
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

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;

/**
 * This class provides an XMl-Parser which try's to create an DOM from
 * an given Inputstream. Is this is possible it try to read the required
 * informations e.g. Mapservices, Layer, SRID and Geometry from the 
 * document.
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 */
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";

    /**
     * The geometry which was parsed from the document.
     */
    private String geometry = null;
    
    /**
     * The srid which was parsed from the document.
     */
    private String srs = null;

    /**
     * The mapservices that were parsed from the document.
     */
    private Collection<MapService> mapServices = null;

    /**
     * The inputstream where the data should be read from.
     */
    private InputStream inputStream = null;

    /**
     * Constructor
     * @param inputStream The stream where the data should be read from
     */
    public XMLExternalCallParser(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getGeometry() {
        return this.geometry;
    }

    public Collection<MapService> getMapServices() {
        return this.mapServices;
    }

    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 the collection where the layer should be add to.
     * @param groupId the id of the group of the layers
     * @param layerNodes the Nodes which should contain the intormations 
     *                   about layers
     * @return the layer
     */
    private Collection<Layer> extractLayer(Collection<Layer> layer, 
                                           String groupId, 
                                           NodeList layerNodes){
        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;
    }

    public String getSRS() {
        return this.srs;
    }

}

http://dive4elements.wald.intevation.org