diff flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java @ 1976:0b466bd4ab24

Introduced a CollectionAttribute class that stores the information provided by the Collection's attribute document. flys-artifacts/trunk@3400 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 13 Dec 2011 11:55:47 +0000
parents 490ab097f58c
children 9c0acaa369ea
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java	Tue Dec 13 09:10:48 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java	Tue Dec 13 11:55:47 2011 +0000
@@ -13,14 +13,10 @@
 
 import de.intevation.artifacts.ArtifactDatabase;
 import de.intevation.artifacts.ArtifactDatabaseException;
-import de.intevation.artifacts.ArtifactNamespaceContext;
 
 import de.intevation.artifactdatabase.state.Facet;
 import de.intevation.artifactdatabase.state.Output;
 
-import de.intevation.artifacts.common.utils.XMLUtils;
-import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
-
 import de.intevation.flys.artifacts.FLYSArtifact;
 import de.intevation.flys.artifacts.model.ManagedFacet;
 
@@ -51,6 +47,11 @@
      */
     protected Map<String, List<String>> compatibilities;
 
+
+    /** The result of the <i>write()</i> operation.*/
+    protected CollectionAttribute attribute;
+
+
     private static Logger logger = Logger.getLogger(AttributeWriter.class);
 
 
@@ -90,60 +91,34 @@
      *
      * @return document with merged outputs as described.
      */
-    protected Document write() {
-        Document doc = XMLUtils.newDocument();
-
-        ElementCreator cr = new ElementCreator(
-            doc,
-            ArtifactNamespaceContext.NAMESPACE_URI,
-            ArtifactNamespaceContext.NAMESPACE_PREFIX);
-
-        Element attribute = cr.create("attribute");
-        Element outs      = cr.create("outputs");
-
-        attribute.appendChild(outs);
-        doc.appendChild(attribute);
+    protected CollectionAttribute write() {
+        attribute = new CollectionAttribute();
 
         for (String outName: newAttr.keySet()) {
             Output a = newAttr.get(outName);
-            Output b = oldAttr.get(outName);
 
-            writeOutput(doc, outs, cr, a.getName(), newFacets, oldFacets);
+            attribute.addOutput(a.getName(), a);
+
+            writeOutput(a.getName(), newFacets, oldFacets);
         }
 
-        return doc;
+        return attribute;
     }
 
 
     /**
-     * @param doc  Document to add output nodes to
-     * @param outs Node in Document to add output nodes to
-     * @param cr   ElementCreator in use to modify doc/outs
      * @param outputName the "new" outputs name
      * @param newOutFacets Facets of the new outputs
      * @param oldOutFacets Facets of the old outputs (can be null)
      */
     protected void writeOutput(
-        Document       doc,
-        Node           outs,
-        ElementCreator cr,
-        String         outputName,
-        List<Facet>    newOutFacets,
-        List<Facet>    oldOutFacets)
-    {
-        Element output = cr.create("output");
-        cr.addAttr(output, "name", outputName);
-
-
-        List<String> compatibleFacets = this.compatibilities.get(outputName);
+        String      outputName,
+        List<Facet> newOutFacets,
+        List<Facet> oldOutFacets
+    ) {
+        List<String> compatFacets = this.compatibilities.get(outputName);
         try {
-            if (writeFacets(
-                    doc, cr, output, newOutFacets, oldOutFacets,
-                    compatibleFacets))
-            {
-                // Add output element only if it contains facets.
-                outs.appendChild(output);
-            }
+            writeFacets(outputName, newOutFacets, oldOutFacets, compatFacets);
         }
         catch (ArtifactDatabaseException ade) {
             logger.error(ade, ade);
@@ -152,18 +127,13 @@
 
 
     /**
-     * @param doc    Document to add facet nodes to
-     * @param cr     ElementCreator to use with output/doc
-     * @param output Node in Document to add facet nodes to
      * @param newFacets the new facets
      * @param oldFacets the old facets
      * @param compatibleFacets List of facets to accept
      * @return true if any facets are written to the out.
      */
     protected boolean writeFacets(
-        Document       doc,
-        ElementCreator cr,
-        Element        output,
+        String         outputName,
         List<Facet>    newFacets,
         List<Facet>    oldFacets,
         List<String>   compatibleFacets)
@@ -204,11 +174,11 @@
         for (ManagedFacet newMF: genuinelyNewFacets) {
             FLYSArtifact flys = (FLYSArtifact) db.getRawArtifact(newMF.getArtifact());
             newMF.setActive(flys.getInitialFacetActivity(
-                output.getAttribute("name"),
+                outputName,
                 newMF.getName(),
                 newMF.getIndex()));
         }
-        
+
         // For each genuinely new Facet check positional conflicts.
         for (ManagedFacet newMF: genuinelyNewFacets) {
             boolean conflicts = true;
@@ -254,10 +224,7 @@
 
         // Now add all facets.
         for (ManagedFacet oldMF: currentFacets) {
-            Node node = oldMF.toXML(doc);
-            if (node != null) {
-                output.appendChild(node);
-            }
+            attribute.addFacet(outputName, oldMF);
         }
 
         return currentFacets.size() > 0;
@@ -274,14 +241,13 @@
      *                  these.
      * @return facet if genuinely new, matching old facet otherwise.
      */
-    protected ManagedFacet pickFacet(ManagedFacet facet,
-        List<Facet> oldFacets)
+    protected ManagedFacet pickFacet(ManagedFacet facet, List<Facet> oldFacets)
     {
         if (oldFacets == null) {
             logger.debug("No old facets to compare a new to found.");
             return facet;
         }
-       
+
         String hash = facet.getName() + facet.getIndex() + facet.getArtifact();
 
         // Compare "new" facet with all old facets.

http://dive4elements.wald.intevation.org