diff artifact-database/src/main/java/de/intevation/artifactdatabase/state/AbstractState.java @ 226:41404961c804

Added support for facets - facets of output modes are read from configuration now. artifacts/trunk@1626 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 30 Mar 2011 14:45:15 +0000
parents 1a3fb29b8b2e
children 3d14fe6e05f7
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/state/AbstractState.java	Wed Mar 30 13:35:37 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/state/AbstractState.java	Wed Mar 30 14:45:15 2011 +0000
@@ -14,6 +14,8 @@
 
 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;
@@ -46,6 +48,14 @@
     /** The XPath to the output nodes of the state configuration.*/
     public static final String XPATH_OUTPUT_MODES = "outputmodes/outputmode";
 
+    /** The XPath to the list of facets relative to the output mode it belongs
+     * to.*/
+    public static final String XPATH_FACETS = "facets/facet";
+
+
+    /** The logger that is used in this class.*/
+    private static Logger logger = Logger.getLogger(AbstractState.class);
+
 
     /** The ID of the state. */
     protected String id;
@@ -163,6 +173,8 @@
      * @param config The state configuration node.
      */
     public void setup(Node config) {
+        logger.info("AbstractState.setup");
+
         id = (String) XMLUtils.xpath(config, XPATH_ID, XPathConstants.STRING);
 
         description = (String) XMLUtils.xpath(
@@ -215,7 +227,52 @@
         String mimetype = XMLUtils.xpathString(
             out, "@mime-type", ArtifactNamespaceContext.INSTANCE);
 
-        return name != null ? new DefaultOutput(name, desc, mimetype) : null;
+        if (name == null) {
+            return null;
+        }
+
+        NodeList facets = (NodeList) XMLUtils.xpath(
+            out,
+            XPATH_FACETS,
+            XPathConstants.NODESET,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (facets == null || facets.getLength() == 0) {
+            return new DefaultOutput(name, desc, mimetype);
+        }
+
+        int num = facets.getLength();
+
+        List<Facet> facetList = new ArrayList<Facet>(num);
+
+        for (int i = 0; i < num; i++) {
+            Facet facet = buildFacet(facets.item(i));
+
+            if (facet != null) {
+                facetList.add(facet);
+            }
+        }
+
+        return new DefaultOutput(name, desc, mimetype, facetList);
+    }
+
+
+    /**
+     * A helper method that creates a Facet object based on the <i>facet</i>
+     * node.
+     *
+     * @param facet The facet node.
+     *
+     * @return a Facet object or null if no valid Facet was found.
+     */
+    protected Facet buildFacet(Node facet) {
+        String name = XMLUtils.xpathString(
+            facet, "@name", ArtifactNamespaceContext.INSTANCE);
+
+        String desc = XMLUtils.xpathString(
+            facet, "@description", ArtifactNamespaceContext.INSTANCE);
+
+        return name != null ? new DefaultFacet(name, desc) : null;
     }
 
 

http://dive4elements.wald.intevation.org