view flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeParser.java @ 5243:2e519b08cff3

Added tag 2.9.12 for changeset 88e3473a3846
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 12 Mar 2013 11:32:42 +0100
parents ef0db530c341
children
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 = ((Element)out).getAttribute("name");

        if (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);
    }

    private static final Node getChild(Element element, String name) {
        NodeList children = element.getChildNodes();
        for (int i = 0, N = children.getLength(); i < N; ++i) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE
            && child.getLocalName().equals(name)) {
                return child;
            }
        }
        return null;
    }


    protected void parseSettings(Node out, String outname) {
        Node settingsNode = getChild((Element)out, "settings");

        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) {
        String uri = ArtifactNamespaceContext.NAMESPACE_URI;
        Element element = (Element)out;

        NodeList themes = element.getElementsByTagNameNS(uri, "facet");

        int num = themes.getLength();

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

        for (int i = 0; i < num; i++) {
            Element theme = (Element) themes.item(i);
            if (theme.getParentNode() == out) {
                attribute.addFacet(outname, new ManagedDomFacet(theme));
            }
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org