view flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java @ 1634:cc47828a1390

Added documentation. flys-artifacts/trunk@2813 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 23 Sep 2011 04:21:16 +0000
parents 16c74ca3586e
children 1b5204203e18
line wrap: on
line source
package de.intevation.flys.collections;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

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.model.ManagedFacet;

/**
 * Create attribute- element of describe document of an ArtifactCollection.
 * The attribute-element contains the merged output of all outputmodes and
 *  facets that are part of the collection.
 */
public class AttributeWriter {

    protected Map<String, Output> oldAttr;
    protected Map<String, Output> newAttr;

    private static Logger logger = Logger.getLogger(AttributeWriter.class);


    /**
     * Create a AttributeWriter.
     * Attributes not present in newAttr will not be included in the document.
     * @param oldAttr "Old" (possibly user-changed) outputs.
     * @param newAttr "New" (eventually re-read in its original, unchagnged
     *                form) outputs.
     */
    public AttributeWriter(
        Map<String, Output> oldAttr,
        Map<String, Output> newAttr)
    {
        this.oldAttr = oldAttr;
        this.newAttr = newAttr;
    }


    /**
     * Create document by merging outputs given in
     * constructor.
     * The "new" set rules about existance of attributes, so anything not
     * present in it will not be included in the resulting document.
     * The "old" set rules about the content of attributes (as user changes
     * are recorded here and not in the new set).
     * @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);

        for (String outName: newAttr.keySet()) {

            Output a = newAttr.get(outName);
            Output b = oldAttr.get(outName);

            writeOutput(doc, outs, cr, a, b);
        }

        return doc;
    }


    /**
     * @param doc  Document to add output nodes to
     * @param outs Node in Document to add output nodes to
     * @param a    the new output
     * @param b    the old output
     */
    protected void writeOutput(
        Document       doc,
        Node           outs,
        ElementCreator cr,
        Output         a, /* new output */
        Output         b) /* old output */
    {
        Element output = cr.create("output");
        cr.addAttr(output, "name", a.getName());

        outs.appendChild(output);

        List<Facet> facetsA = a.getFacets();
        List<Facet> facetsB = null;

        if (b != null) {
            facetsB = b.getFacets();
        }

        writeFacets(doc, cr, output, facetsA, facetsB);
    }


    /**
     * @param doc    Document to add facet nodes to
     * @param output Node in Document to add facet nodes to
     * @param a      the new facets
     * @param b      the old facets
     */
    protected void writeFacets(
        Document       doc,
        ElementCreator cr,
        Element        output,
        List<Facet>    a, /* new facets */
        List<Facet>    b) /* old facets */
    {
        int num = a.size();

        for (int i = 0; i < num; i++) {
            ManagedFacet fA = (ManagedFacet) a.get(i);

            if (!mergeFacets(doc, cr, output, fA, b)) {
                Node n = fA.toXML(doc);

                if (n != null) {
                    output.appendChild(n);
                }
            }
        }
    }


    /**
     * @param a    new facets
     * @param list old facets
     */
    protected boolean mergeFacets(
        Document       doc,
        ElementCreator cr,
        Element        output,
        ManagedFacet   a,    /* new facets */
        List<Facet>    list) /* old facets */
    {
        String nameA = a.getName() + a.getIndex();

        if (list == null) {
            logger.debug("No old facets found.");
            return false;
        }

        for (Facet facet: list) {
            String nameB = facet.getName() + facet.getIndex();

            if (nameA.equals(nameB)) {
                ManagedFacet b = (ManagedFacet) facet;

                if (!b.getArtifact().equals(a.getArtifact())) {
                    continue;
                }

                Node n = facet.toXML(doc);

                if (n != null) {
                    output.appendChild(n);
                }

                return true;
            }
        }

        return false;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org