diff flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java @ 944:c256061287d7

Simplified the code to read all provided Outputs of an Artifact. flys-artifacts/trunk@2357 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 18 Jul 2011 17:09:00 +0000
parents 9ff7e06bcb77
children 59ae2a823e73
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java	Mon Jul 18 15:07:47 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java	Mon Jul 18 17:09:00 2011 +0000
@@ -1,27 +1,22 @@
 package de.intevation.flys.collections;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+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.ArtifactDatabase;
 import de.intevation.artifacts.ArtifactDatabaseException;
-import de.intevation.artifacts.ArtifactNamespaceContext;
+import de.intevation.artifacts.CallContext;
 import de.intevation.artifacts.CallMeta;
 
 import de.intevation.artifactdatabase.state.DefaultOutput;
+import de.intevation.artifactdatabase.state.Facet;
 import de.intevation.artifactdatabase.state.Output;
 
-import de.intevation.artifacts.common.utils.XMLUtils;
-
+import de.intevation.flys.artifacts.FLYSArtifact;
 import de.intevation.flys.artifacts.model.ManagedFacet;
 
 
@@ -36,14 +31,16 @@
 
     protected ArtifactDatabase db;
     protected CallMeta         meta;
+    protected CallContext      context;
 
     protected Map<String, Output> outs;
 
 
-    public OutputParser(ArtifactDatabase db, CallMeta meta) {
-        this.db   = db;
-        this.meta = meta;
-        this.outs = new HashMap<String, Output>();
+    public OutputParser(ArtifactDatabase db, CallContext context) {
+        this.db      = db;
+        this.meta    = context.getMeta();
+        this.context = context;
+        this.outs    = new HashMap<String, Output>();
     }
 
 
@@ -52,24 +49,32 @@
     {
         logger.debug("OutputParser.parse: " + uuid);
 
-        // XXX I am not sure if it works well every time with an empty
-        // document in the describe operation of an artifact.
-        Document description = db.describe(uuid, null, meta);
+        FLYSArtifact flys = (FLYSArtifact) db.getRawArtifact(uuid);
 
-        NodeList outs = (NodeList) XMLUtils.xpath(
-            description,
-            XPATH_ARTIFACT_OUTPUTMODES,
-            XPathConstants.NODESET,
-            ArtifactNamespaceContext.INSTANCE);
+        List<Output> outList = flys.getOutputs(context);
 
-        int num = outs != null ? outs.getLength() : 0;
-
-        logger.debug("Artifact has " + num + " outputs.");
+        for (Output out: outList) {
+            String name = out.getName();
 
-        for (int i = 0; i < num; i++) {
-            Element out = (Element)outs.item(i);
+            Output o = outs.get(name);
+            int  pos = 1;
 
-            parseOutput(uuid, out);
+            if (o == null) {
+                o = new DefaultOutput(
+                    out.getName(),
+                    out.getDescription(),
+                    out.getMimeType(),
+                    new ArrayList<Facet>(),
+                    out.getType());
+
+                outs.put(name, o);
+            }
+            else {
+                pos = o.getFacets().size() + 1;
+            }
+
+            List<Facet> facets = facet2ManagedFacet(uuid, out.getFacets(), pos);
+            o.addFacets(facets);
         }
     }
 
@@ -79,71 +84,22 @@
     }
 
 
-    protected void addItem(String out, ManagedFacet item) {
-        Output o = outs.get(out);
-
-        if (o != null) {
-            int num = o.getFacets().size();
-            item.setPosition(num+1);
+    protected List<Facet> facet2ManagedFacet(
+        String      uuid,
+        List<Facet> old,
+        int         pos)
+    {
+        List<Facet> newFacets = new ArrayList<Facet>(old.size());
 
-            o.addFacet(item);
-        }
-    }
-
-
-    protected void parseOutput(String uuid, Element out) {
-
-        String uri = ArtifactNamespaceContext.NAMESPACE_URI;
-
-        String name = out.getAttributeNS(uri, "name");
-
-        if (outs.get(name) == null) {
-            logger.debug("Create new output: " + name);
-            newOutput(out, name);
+        for (Facet f: old) {
+            newFacets.add(new ManagedFacet(
+                f.getName(),
+                f.getIndex(),
+                f.getDescription(),
+                uuid, pos++, 1));
         }
 
-        parseItems(uuid, out, name);
-    }
-
-
-    protected void newOutput(Element out, String name) {
-
-        String uri = ArtifactNamespaceContext.NAMESPACE_URI;
-
-        String desc     = out.getAttributeNS(uri, "description");
-        String mimetype = out.getAttributeNS(uri, "mime-type");
-        String type     = out.getAttributeNS(uri, "type");
-
-        Output o = new DefaultOutput(name, desc, mimetype, type);
-
-        outs.put(name, o);
-    }
-
-
-    protected void parseItems(String uuid, Node out, String outname) {
-        NodeList facets = (NodeList) XMLUtils.xpath(
-            out, "art:facets/art:facet",
-            XPathConstants.NODESET,
-            ArtifactNamespaceContext.INSTANCE);
-
-        int num = facets != null ? facets.getLength() : 0;
-
-        String uri = ArtifactNamespaceContext.NAMESPACE_URI;
-
-        logger.debug("Output has " + num + " facets.");
-
-        for (int i = 0; i < num; i++) {
-            Element facet = (Element) facets.item(i);
-
-            String name  = facet.getAttributeNS(uri, "name");
-            String desc  = facet.getAttributeNS(uri, "description");
-            String index = facet.getAttributeNS(uri, "index");
-
-            ManagedFacet item = new ManagedFacet(
-                name, Integer.parseInt(index), desc, uuid, 1, 1);
-
-            addItem(outname, item);
-        }
+        return newFacets;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org