view flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeParser.java @ 2286:89ca1e8572e4

Added PDF export for historical discharge curve calculations. flys-artifacts/trunk@3949 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Tue, 07 Feb 2012 10:29:39 +0000
parents 3e703d134bbe
children e57816cf41d5
line wrap: on
line source
package de.intevation.flys.collections;

import java.util.List;
import java.util.Map;

import javax.xml.xpath.XPathConstants;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import de.intevation.artifacts.ArtifactNamespaceContext;

import de.intevation.artifactdatabase.state.DefaultOutput;
import de.intevation.artifactdatabase.state.Facet;
import de.intevation.artifactdatabase.state.Output;
import de.intevation.artifactdatabase.state.Settings;

import de.intevation.artifacts.common.utils.XMLUtils;

import de.intevation.flys.artifacts.model.ManagedDomFacet;
import de.intevation.flys.exports.ChartSettings;

/**
 * Access parts of the Attribute parts of a FLYSCollections description
 * document.
 */
public class AttributeParser {

    /** Constant XPath that points to the outputmodes of an artifact. */
    public static final String XPATH_ARTIFACT_OUTPUTMODES =
        "/art:attribute/art:outputs/art:output";


    private static Logger logger = Logger.getLogger(AttributeParser.class);


    protected Document attributeDocument;

    protected CollectionAttribute attribute;


    public AttributeParser(Document attributeDocument) {
        this.attributeDocument = attributeDocument;
    }


    public void parse() {
        logger.debug("AttributeParser.parse");

        attribute = new CollectionAttribute();

        NodeList outs = (NodeList) XMLUtils.xpath(
            attributeDocument,
            XPATH_ARTIFACT_OUTPUTMODES,
            XPathConstants.NODESET,
            ArtifactNamespaceContext.INSTANCE);

        int num = outs != null ? outs.getLength() : 0;

        logger.debug("Attribute has " + num + " outputs.");

        for (int i = 0; i < num; i++) {
            Node out = outs.item(i);

            parseOutput(out);
        }
    }


    public CollectionAttribute getCollectionAttribute() {
        if (attribute == null) {
            parse();
        }

        return attribute;
    }


    public Document getAttributeDocument() {
        return attributeDocument;
    }


    public Map<String, Output> getOuts() {
        return attribute.getOutputs();
    }


    /**
     * Access all facets.
     * @return list of all facets.
     */
    public List<Facet> getFacets() {
        return attribute.getFacets();
    }


    protected void parseOutput(Node out) {
        String name = XMLUtils.xpathString(
            out, "@name", ArtifactNamespaceContext.INSTANCE);

        if (name == null || name.length() == 0) {
            logger.warn("No Output name specified. Cancel parsing!");
            return;
        }

        Output o = attribute.getOutput(name);

        if (o == null) {
            logger.debug("Create new output: " + name);

            o = new DefaultOutput(name, null, null);
            attribute.addOutput(name, o);
        }

        parseSettings(out, name);
        parseItems(out, name);
    }


    protected void parseSettings(Node out, String outname) {
        Node settingsNode = (Node) XMLUtils.xpath(
            out, "settings",
            XPathConstants.NODE,
            null);

        if (settingsNode == null) {
            logger.debug("No Settings found for Output '" + outname + "'");
            return;
        }

        Settings settings = ChartSettings.parse(settingsNode);
        attribute.setSettings(outname, settings);
    }


    protected void parseItems(Node out, String outname) {
        NodeList themes = (NodeList) XMLUtils.xpath(
            out, "art:facet",
            XPathConstants.NODESET,
            ArtifactNamespaceContext.INSTANCE);

        int num = themes != null ? themes.getLength() : 0;

        logger.debug("Output has " + num + " themes.");

        String uri = ArtifactNamespaceContext.NAMESPACE_URI;

        for (int i = 0; i < num; i++) {
            Element theme = (Element) themes.item(i);

            attribute.addFacet(outname, new ManagedDomFacet(theme));
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org