diff artifacts/src/main/java/org/dive4elements/river/collections/AttributeParser.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/collections/AttributeParser.java@bd047b71ab37
children 4897a58c8746
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/collections/AttributeParser.java	Thu Apr 25 15:23:37 2013 +0200
@@ -0,0 +1,167 @@
+package org.dive4elements.river.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 org.dive4elements.artifacts.ArtifactNamespaceContext;
+
+import org.dive4elements.artifactdatabase.state.DefaultOutput;
+import org.dive4elements.artifactdatabase.state.Facet;
+import org.dive4elements.artifactdatabase.state.Output;
+import org.dive4elements.artifactdatabase.state.Settings;
+
+import org.dive4elements.artifacts.common.utils.XMLUtils;
+
+import org.dive4elements.river.artifacts.model.ManagedDomFacet;
+import org.dive4elements.river.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