view artifact-database/src/main/java/de/intevation/artifactdatabase/ProtocolUtils.java @ 107:39d9391059bd

Added a ProtocolUtils class that provides functions that help creating the artifact protocol documents. artifacts/trunk@1295 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 04 Feb 2011 16:52:39 +0000
parents
children ab646e0f5569
line wrap: on
line source
/*
 * Copyright (c) 2011 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */
package de.intevation.artifactdatabase;

import org.apache.log4j.Logger;

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

import de.intevation.artifacts.common.utils.XMLUtils;


/**
 * This class provides methods that help creating the artifact protocol
 * documents describe, feed, advance and out.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ProtocolUtils {

    /**
     * It should not be necessary to create instances of this class.
     */
    private ProtocolUtils() {}


    /**
     * This method creates a node that might be used for the artifact protocol.
     *
     * @param creator The ElementCreator that is used to create the node.
     * @param nodeName The node name.
     * @param attrName The names of optional attributes.
     * @param value The values for the optional attributes.
     *
     * @return the created node.
     */
    public static Element createArtNode(
        XMLUtils.ElementCreator creator,
        String nodeName, String[] attrName, String[] value)
    {
        Element typeNode = creator.create(nodeName);

        if (attrName != null && value != null) {
            for (int i = 0; i < attrName.length; i++) {
                if (i < value.length) {
                    creator.addAttr(typeNode, attrName[i], value[i], true);
                }
                else {
                    break;
                }
            }
        }

        return typeNode;
    }


    /**
     * This method creates the root node for all artifact protocol documents.
     *
     * @param creator The ElementCreator used to create new elements.
     *
     * @return the root node for the artifact protocol document.
     */
    public static Element createRootNode(XMLUtils.ElementCreator creator) {
        return createArtNode(creator, "result", null, null);
    }


    /**
     * This method appends the three necessary nodes <i>type</i>, <i>uuid</i>
     * and <i>hash</i> of the describe document to <i>root</i> node.
     *
     * @param creator The ElementCreator that is used to create new nodes.
     * @param root The root node of the describe document.
     * @param uuid The UUID of the artifact.
     * @param hash The hash if the artifact.
     */
    public static void appendDescribeHeader(
        XMLUtils.ElementCreator creator, Element root, String uuid, String hash)
    {
        root.appendChild(createArtNode(
            creator,
            "type",
            new String[] {"name"},
            new String[] {"describe"}));

        root.appendChild(createArtNode(
            creator,
            "uuid",
            new String[] {"value"},
            new String[] {uuid}));

        root.appendChild(createArtNode(
            creator,
            "hash",
            new String[] {"value"},
            new String[] {hash}));
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org