ingo@113: /*
ingo@113: * Copyright (c) 2011 by Intevation GmbH
ingo@113: *
ingo@113: * This program is free software under the LGPL (>=v2.1)
ingo@113: * Read the file LGPL.txt coming with the software for details
ingo@113: * or visit http://www.gnu.org/licenses/ if it does not exist.
ingo@113: */
ingo@113: package de.intevation.artifacts.common.utils;
ingo@113:
ingo@114: import javax.xml.xpath.XPathConstants;
ingo@114:
ingo@113: import org.w3c.dom.Document;
ingo@113: import org.w3c.dom.Element;
ingo@114: import org.w3c.dom.Node;
ingo@114: import org.w3c.dom.NodeList;
ingo@113:
ingo@113: import de.intevation.artifacts.common.ArtifactNamespaceContext;
ingo@113:
ingo@113:
ingo@113: /**
ingo@113: * This class provides methods that help creating the artifact protocol
ingo@113: * documents DESCRIBE, FEED, ADVANCE and OUT.
ingo@113: *
ingo@113: * @author Ingo Weinzierl
ingo@113: */
ingo@113: public class ClientProtocolUtils {
ingo@113:
ingo@114: /** The XPath to the current state in the DESCRIBE document.*/
ingo@114: public static final String XPATH_CURRENT_STATE = "/art:result/art:state";
ingo@114:
ingo@114: /** The XPath to the static UI part in the DESCRIBE document.*/
ingo@114: public static final String XPATH_STATIC = "/art:result/art:ui/art:static";
ingo@114:
ingo@114: /** The XPath to the dynamic UI part in the DESCRIBE document.*/
ingo@114: public static final String XPATH_DYNAMIC = "/art:result/art:ui/art:dynamic";
ingo@114:
ingo@114: /** The XPath to the reachable states part in the DESCRIBE document.*/
ingo@114: public static final String XPATH_STATES =
ingo@114: "/art:result/art:reachable-states";
ingo@114:
ingo@114: /** The XPath to the select node relative to the dynamic UI node in the
ingo@114: * DESCRIBE document.*/
ingo@114: public static final String XPATH_DATA_SELECT = "art:select";
ingo@114:
ingo@114: /** The XPath to the choices nodes relative to the select node in the
ingo@114: * DESCRIBE document.*/
ingo@114: public static final String XPATH_DATA_ITEMS = "art:choices/art:item";
ingo@114:
ingo@114: /** The XPath to a label in the artifact's DESCRIBE document.*/
ingo@114: public static final String XPATH_LABEL = "art:label/text()";
ingo@114:
ingo@114: /** The XPath to a value in the artifact's DESCRIBE document.*/
ingo@114: public static final String XPATH_VALUE = "art:value/text()";
ingo@114:
ingo@114:
ingo@113: /**
ingo@113: * It should not be necessary to create instances of this class.
ingo@113: */
ingo@113: private ClientProtocolUtils() {
ingo@113: }
ingo@113:
ingo@113:
ingo@113: /**
ingo@113: * This method creates a new CREATE document.
ingo@113: *
ingo@113: * @return the CREATE document.
ingo@113: */
ingo@113: public static Document newCreateDocument(String factory) {
ingo@113: Document doc = XMLUtils.newDocument();
ingo@113:
ingo@113: XMLUtils.ElementCreator cr = new XMLUtils.ElementCreator(
ingo@113: doc,
ingo@113: ArtifactNamespaceContext.NAMESPACE_URI,
ingo@113: ArtifactNamespaceContext.NAMESPACE_PREFIX);
ingo@113:
ingo@113: Element action = cr.create("action");
ingo@113: Element type = cr.create("type");
ingo@113: Element fac = cr.create("factory");
ingo@113:
ingo@113: type.setAttribute("name", "create");
ingo@113: fac.setAttribute("name", factory);
ingo@113:
ingo@113: action.appendChild(type);
ingo@113: action.appendChild(fac);
ingo@113:
ingo@113: doc.appendChild(action);
ingo@113:
ingo@113: return doc;
ingo@113: }
ingo@114:
ingo@114:
ingo@114: /**
ingo@114: * Returns string value found by {@link XPATH_LABEL} relative to
ingo@114: * node.
ingo@114: *
ingo@114: * @param node A node.
ingo@114: *
ingo@114: * @return the string value found by {@link XPATH_LABEL}.
ingo@114: */
ingo@114: public static String getLabel(Node node) {
ingo@114: return (String) XMLUtils.xpath(
ingo@114: node,
ingo@114: XPATH_LABEL,
ingo@114: XPathConstants.STRING,
ingo@114: ArtifactNamespaceContext.INSTANCE);
ingo@114: }
ingo@114:
ingo@114:
ingo@114: /**
ingo@114: * Returns string value found by {@link XPATH_VALUE} relative to
ingo@114: * node.
ingo@114: *
ingo@114: * @param node A node.
ingo@114: *
ingo@114: * @return the string value found by {@link XPATH_VALUE}.
ingo@114: */
ingo@114: public static String getValue(Node node) {
ingo@114: return (String) XMLUtils.xpath(
ingo@114: node,
ingo@114: XPATH_VALUE,
ingo@114: XPathConstants.STRING,
ingo@114: ArtifactNamespaceContext.INSTANCE);
ingo@114: }
ingo@114:
ingo@114:
ingo@114: /**
ingo@114: * This method returns the static UI part of the artifact's DESCRIBE
ingo@114: * document.
ingo@114: *
ingo@114: * @param description The document returned by the artifact server's
ingo@114: * DESCRIBE operation.
ingo@114: *
ingo@114: * @return the static UI node.
ingo@114: */
ingo@114: public static Node getStaticUI(Document description) {
ingo@114: return (Node) XMLUtils.xpath(
ingo@114: description,
ingo@114: XPATH_STATIC,
ingo@114: XPathConstants.NODE,
ingo@114: ArtifactNamespaceContext.INSTANCE);
ingo@114: }
ingo@114:
ingo@114:
ingo@114: /**
ingo@114: * This method returns the dynamic UI part of the artifact's DESCRIBE
ingo@114: * document.
ingo@114: *
ingo@114: * @param description The document returned by the artifact server's
ingo@114: * DESCRIBE operation.
ingo@114: *
ingo@114: * @return the dynamic UI node.
ingo@114: */
ingo@114: public static Node getDynamicUI(Document description) {
ingo@114: return (Node) XMLUtils.xpath(
ingo@114: description,
ingo@114: XPATH_DYNAMIC,
ingo@114: XPathConstants.NODE,
ingo@114: ArtifactNamespaceContext.INSTANCE);
ingo@114: }
ingo@114:
ingo@114:
ingo@114: /**
ingo@114: * This method returns the current state node contained in the DESCRIBE
ingo@114: * document.
ingo@114: *
ingo@114: * @param description The document returned by the artifact server's
ingo@114: * DESCRIBE operation.
ingo@114: *
ingo@114: * @return the node containing information about the current state.
ingo@114: */
ingo@114: public static Node getCurrentState(Document description) {
ingo@114: return (Node) XMLUtils.xpath(
ingo@114: description,
ingo@114: XPATH_CURRENT_STATE,
ingo@114: XPathConstants.NODE,
ingo@114: ArtifactNamespaceContext.INSTANCE);
ingo@114: }
ingo@114:
ingo@114:
ingo@114: /**
ingo@114: * This method returns the node that contains information about the
ingo@114: * reachable states of the artifact in the artifact's DESCRIBE document.
ingo@114: *
ingo@114: * @param description The document returned by the artifact server's
ingo@114: * DESCRIBE operation.
ingo@114: *
ingo@114: * @return the node that contains the reachable states.
ingo@114: */
ingo@114: public static Node getReachableStates(Document description) {
ingo@114: return (Node) XMLUtils.xpath(
ingo@114: description,
ingo@114: XPATH_STATES,
ingo@114: XPathConstants.NODE,
ingo@114: ArtifactNamespaceContext.INSTANCE);
ingo@114: }
ingo@114:
ingo@114:
ingo@114: /**
ingo@114: * Returns the node found by {@link XPATH_DATA_SELECT}.
ingo@114: *
ingo@114: * @param dynamicNode The dynamic UI node of the DESCRIBE document.
ingo@114: *
ingo@114: * @return the select node found in the dynamic UI node.
ingo@114: */
ingo@114: public static Node getSelectNode(Node dynamicNode) {
ingo@114: return (Node) XMLUtils.xpath(
ingo@114: dynamicNode,
ingo@114: XPATH_DATA_SELECT,
ingo@114: XPathConstants.NODE,
ingo@114: ArtifactNamespaceContext.INSTANCE);
ingo@114: }
ingo@114:
ingo@114:
ingo@114: /**
ingo@114: * Returns the items that could be found in the node.
ingo@114: *
ingo@114: * @param node A select node.
ingo@114: *
ingo@114: * @return the choices nodes as node list.
ingo@114: */
ingo@114: public static NodeList getItemNodes(Node node) {
ingo@114: return (NodeList) XMLUtils.xpath(
ingo@114: node,
ingo@114: XPATH_DATA_ITEMS,
ingo@114: XPathConstants.NODESET,
ingo@114: ArtifactNamespaceContext.INSTANCE);
ingo@114: }
ingo@113: }
ingo@113: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :