view artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java @ 29:22b03d5c84c5

Added REST out handler for artifacts reachable via HTTP GET '/artifact/{uuid}/{type}'. artifacts/trunk@69 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 10 Sep 2009 15:49:17 +0000
parents 83a059c204f8
children 4ae4dc99127d
line wrap: on
line source
package de.intevation.artifactdatabase;

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

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;

import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathConstants;

import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;

/**
 *  @author Sascha L. Teichmann
 */
public final class XMLUtils
{
    private static Logger logger = Logger.getLogger(XMLUtils.class);

    private XMLUtils() {
    }

    public static class ElementCreator
    {
        protected Document document;
        protected String   ns;
        protected String   prefix;

        public ElementCreator(Document document, String ns, String prefix) {
            this.document = document;
            this.ns       = ns;
            this.prefix   = prefix;
        }

        public Element create(String name) {
            Element element = document.createElementNS(ns, name);
            element.setPrefix(prefix);
            return element;
        }

        public void addAttr(Element element, String name, String value) {
            Attr attr = document.createAttributeNS(ns, name);
            attr.setValue(value);
            attr.setPrefix(prefix);
            element.setAttributeNode(attr);
        }
    } // class ElementCreator

    public static final Document newDocument() {
        try {
            return DocumentBuilderFactory
                .newInstance()
                .newDocumentBuilder()
                .newDocument();
        }
        catch (ParserConfigurationException pce) {
            logger.error(pce.getLocalizedMessage(), pce);
        }
        return null;
    }

    public static final XPath newXPath() {
        return newXPath(null);
    }

    public static final XPath newXPath(NamespaceContext namespaceContext) {
        XPathFactory factory = XPathFactory.newInstance();
        XPath        xpath   = factory.newXPath();
        if (namespaceContext != null) {
            xpath.setNamespaceContext(namespaceContext);
        }
        return xpath;
    }

    public static final Object xpath(Object root, String query, QName returnTyp) {
        return xpath(root, query, returnTyp, null);
    }

    public static final String xpathString(
        Object root, String query, NamespaceContext namespaceContext
    ) {
        return (String)xpath(root, query, XPathConstants.STRING, namespaceContext);
    }

    public static final Object xpath(
        Object           root,
        String           query,
        QName            returnType,
        NamespaceContext namespaceContext
    ) {
        if (root == null) {
            return null;
        }

        try {
            XPath xpath = newXPath(namespaceContext);
            if (xpath != null) {
                return xpath.evaluate(query, root, returnType);
            }
        }
        catch (XPathExpressionException xpee) {
            logger.error(xpee.getLocalizedMessage(), xpee);
        }        

        return null;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org