Mercurial > dive4elements > framework
view artifacts-common/src/main/java/de/intevation/artifacts/common/utils/ClientProtocolUtils.java @ 239:ab196a861796
Enhanced the ClientProtocolUtils with a method that creates an XML document used for the Collection's out() operation.
artifacts/trunk@1653 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 07 Apr 2011 11:25:00 +0000 |
parents | 91333a41b234 |
children | c0bc172ec7b2 |
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.artifacts.common.utils; import javax.xml.xpath.XPathConstants; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import de.intevation.artifacts.common.ArtifactNamespaceContext; /** * 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 ClientProtocolUtils { /** The XPath to the current state in the DESCRIBE document.*/ public static final String XPATH_CURRENT_STATE = "/art:result/art:state"; /** The XPath to the static UI part in the DESCRIBE document.*/ public static final String XPATH_STATIC = "/art:result/art:ui/art:static"; /** The XPath to the dynamic UI part in the DESCRIBE document.*/ public static final String XPATH_DYNAMIC = "/art:result/art:ui/art:dynamic"; /** The XPath to the reachable states part in the DESCRIBE document.*/ public static final String XPATH_STATES = "/art:result/art:reachable-states"; /** The XPath to the output modes in the DESCRIBE document.*/ public static final String XPATH_OUTPUT_MODES = "/art:result/art:outputmodes/art:output"; /** The XPath to the select node relative to the dynamic UI node in the * DESCRIBE document.*/ public static final String XPATH_DATA_SELECT = "art:select"; /** The XPath to the choices nodes relative to the select node in the * DESCRIBE document.*/ public static final String XPATH_DATA_ITEMS = "art:choices/art:item"; /** The XPath to a label in the artifact's DESCRIBE document.*/ public static final String XPATH_LABEL = "art:label/text()"; /** The XPath to a value in the artifact's DESCRIBE document.*/ public static final String XPATH_VALUE = "art:value/text()"; /** * It should not be necessary to create instances of this class. */ private ClientProtocolUtils() { } /** * This method creates a new CREATE document. * * @return the CREATE document. */ public static Document newCreateDocument(String factory) { Document doc = XMLUtils.newDocument(); XMLUtils.ElementCreator cr = new XMLUtils.ElementCreator( doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); Element action = cr.create("action"); Element type = cr.create("type"); Element fac = cr.create("factory"); type.setAttribute("name", "create"); fac.setAttribute("name", factory); action.appendChild(type); action.appendChild(fac); doc.appendChild(action); return doc; } /** * This method creates a new FEED document. * * @param theUuid The identifier of the artifact. * @param theHash The hash of the artifact. * @param theData An array that contains key/value pairs that represent the * data that should be included in the FEED document. * * @return the FEED document. */ public static Document newFeedDocument( String theUuid, String theHash, String[][] theData) { Document doc = XMLUtils.newDocument(); XMLUtils.ElementCreator cr = new XMLUtils.ElementCreator( doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); Element action = cr.create("action"); Element type = cr.create("type"); Element uuid = cr.create("uuid"); Element hash = cr.create("hash"); Element data = cr.create("data"); // XXX It is not nice that the type has no attribute namespace, but to // be backward compatible, we don't change this now. cr.addAttr(type, "name", "feed", false); cr.addAttr(uuid, "value", theUuid, true); cr.addAttr(hash, "value", theHash, true); for (String[] kvp: theData) { Element input = cr.create("input"); cr.addAttr(input, "name", kvp[0], true); cr.addAttr(input, "value", kvp[1], true); data.appendChild(input); } action.appendChild(type); action.appendChild(uuid); action.appendChild(hash); action.appendChild(data); doc.appendChild(action); return doc; } /** * This method creates a new ADVANCE document. * * @param theUuid The identifier of the artifact. * @param theHash The hash of the artifact. * @param theTarget The target state identifier. * * @return the ADVANCE document. */ public static Document newAdvanceDocument( String theUuid, String theHash, String theTarget) { Document doc = XMLUtils.newDocument(); XMLUtils.ElementCreator cr = new XMLUtils.ElementCreator( doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); Element action = cr.create("action"); Element type = cr.create("type"); Element uuid = cr.create("uuid"); Element hash = cr.create("hash"); Element target = cr.create("target"); // XXX It is not nice that the type has no attribute namespace, but to // be backward compatible, we don't change this now. cr.addAttr(type, "name", "advance", false); cr.addAttr(uuid, "value", theUuid, true); cr.addAttr(hash, "value", theHash, true); cr.addAttr(target, "name", theTarget, true); action.appendChild(type); action.appendChild(uuid); action.appendChild(hash); action.appendChild(target); doc.appendChild(action); return doc; } /** * This method creates a new document that is used to create new artifact * collections in the artifact server. * * @param name <b>Optional</b> name of the collection. * * @return the document to create new collections. */ public static Document newCreateCollectionDocument(String name) { Document doc = XMLUtils.newDocument(); XMLUtils.ElementCreator cr = new XMLUtils.ElementCreator( doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); Element action = cr.create("action"); Element type = cr.create("type"); Element collection = cr.create("collection"); Element attribute = cr.create("attribute"); cr.addAttr(type, "name", "create"); cr.addAttr(collection, "name", name != null ? name : ""); action.appendChild(type); type.appendChild(collection); collection.appendChild(attribute); doc.appendChild(action); return doc; } /** * This method creates a new Document that is used to add an artifact to a * collection in the artifact server. * * @param artId The identifier of the artifact that should be added. * @param attr A document that contains attributes for the attribute's * life in the collection. * * @return the document to add an artifact into a collection. */ public static Document newAddArtifactDocument(String artId, Document attr) { Document doc = XMLUtils.newDocument(); XMLUtils.ElementCreator cr = new XMLUtils.ElementCreator( doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); Element action = cr.create("action"); Element type = cr.create("type"); Element artifact = cr.create("artifact"); Element attribute = cr.create("attribute"); cr.addAttr(artifact, "uuid", artId); cr.addAttr(type, "name", "addartifact"); if (attr != null) { attr.appendChild(attr); } action.appendChild(type); type.appendChild(artifact); artifact.appendChild(attribute); doc.appendChild(action); return doc; } /** * This method creates a new Document that is used to trigger the DESCRIBE * operation of a collection in the artifact server. * * @param uuid The identifier of the collection that should be described. * * @return the document to describe a collection. */ public static Document newDescribeCollectionDocument(String uuid) { Document doc = XMLUtils.newDocument(); XMLUtils.ElementCreator cr = new XMLUtils.ElementCreator( doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); Element action = cr.create("action"); Element type = cr.create("type"); cr.addAttr(type, "name", "describe"); action.appendChild(type); doc.appendChild(action); return doc; } /** * This function builds a document that is used as request document of the * out() operation of Collections. * * @param uuid The identifier of the collection. * @param type The name of the desired output type. * * @return the request document. */ public static Document newOutCollectionDocument(String uuid, String type) { Document doc = XMLUtils.newDocument(); XMLUtils.ElementCreator cr = new XMLUtils.ElementCreator( doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); Element action = cr.create("action"); cr.addAttr(action, "name", type, true); cr.addAttr(action, "type", type, true); doc.appendChild(action); return doc; } /** * Returns string value found by {@link XPATH_LABEL} relative to * <i>node</i>. * * @param node A node. * * @return the string value found by {@link XPATH_LABEL}. */ public static String getLabel(Node node) { return (String) XMLUtils.xpath( node, XPATH_LABEL, XPathConstants.STRING, ArtifactNamespaceContext.INSTANCE); } /** * Returns string value found by {@link XPATH_VALUE} relative to * <i>node</i>. * * @param node A node. * * @return the string value found by {@link XPATH_VALUE}. */ public static String getValue(Node node) { return (String) XMLUtils.xpath( node, XPATH_VALUE, XPathConstants.STRING, ArtifactNamespaceContext.INSTANCE); } /** * This method returns the static UI part of the artifact's DESCRIBE * document. * * @param description The document returned by the artifact server's * DESCRIBE operation. * * @return the static UI node. */ public static Node getStaticUI(Document description) { return (Node) XMLUtils.xpath( description, XPATH_STATIC, XPathConstants.NODE, ArtifactNamespaceContext.INSTANCE); } /** * This method returns the dynamic UI part of the artifact's DESCRIBE * document. * * @param description The document returned by the artifact server's * DESCRIBE operation. * * @return the dynamic UI node. */ public static Node getDynamicUI(Document description) { return (Node) XMLUtils.xpath( description, XPATH_DYNAMIC, XPathConstants.NODE, ArtifactNamespaceContext.INSTANCE); } /** * This method returns the current state node contained in the DESCRIBE * document. * * @param description The document returned by the artifact server's * DESCRIBE operation. * * @return the node containing information about the current state. */ public static Node getCurrentState(Document description) { return (Node) XMLUtils.xpath( description, XPATH_CURRENT_STATE, XPathConstants.NODE, ArtifactNamespaceContext.INSTANCE); } /** * This method returns the node that contains information about the * reachable states of the artifact in the artifact's DESCRIBE document. * * @param description The document returned by the artifact server's * DESCRIBE operation. * * @return the node that contains the reachable states. */ public static Node getReachableStates(Document description) { return (Node) XMLUtils.xpath( description, XPATH_STATES, XPathConstants.NODE, ArtifactNamespaceContext.INSTANCE); } /** * This method returns the output mode nodes of the DESCRIBE document. * * @param description The document returned by the artifact server's * DESCRIBE operation. * * @return the node that contains the output modes. */ public static NodeList getOutputModes(Document description) { return (NodeList) XMLUtils.xpath( description, XPATH_OUTPUT_MODES, XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE); } /** * Returns the node found by {@link XPATH_DATA_SELECT}. * * @param dynamicNode The dynamic UI node of the DESCRIBE document. * * @return the select node found in the dynamic UI node. */ public static NodeList getSelectNode(Node dynamicNode) { return (NodeList) XMLUtils.xpath( dynamicNode, XPATH_DATA_SELECT, XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE); } /** * Returns the items that could be found in the <i>node</i>. * * @param node A select node. * * @return the choices nodes as node list. */ public static NodeList getItemNodes(Node node) { return (NodeList) XMLUtils.xpath( node, XPATH_DATA_ITEMS, XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :