view artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java @ 64:8b72676698b5

Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action. artifacts/trunk@523 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Sat, 09 Jan 2010 16:59:00 +0000
parents 76abf0c64d3b
children ed03cc0e5800
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 org.xml.sax.SAXException;

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;

import java.io.File;
import java.io.IOException;

/**
 *  @author Sascha L. Teichmann
 */
public final class XMLUtils
{
    public static final String XFORM_URL    = "http://www.w3.org/2002/xforms";
    public static final String XFORM_PREFIX = "xform";

    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) {
            addAttr(element, name, value, false);
        }

        public void addAttr(
            Element element,
            String  name,
            String  value,
            boolean addPrefix
        ) {
            Attr attr = document.createAttributeNS(ns, name);
            attr.setValue(value);

            if (addPrefix)
                attr.setPrefix(prefix);

            element.setAttributeNode(attr);
        }
    } // class ElementCreator

    public static final Document newDocument() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);

        try {
            return factory.newDocumentBuilder().newDocument();
        }
        catch (ParserConfigurationException pce) {
            logger.error(pce.getLocalizedMessage(), pce);
        }
        return null;
    }

    public static final Document parseDocument(File file) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);

        try {
            return factory.newDocumentBuilder().parse(file);
        }
        catch (ParserConfigurationException pce) {
            logger.error(pce.getLocalizedMessage(), pce);
        }
        catch (SAXException se) {
            logger.error(se.getLocalizedMessage(), se);
        }
        catch (IOException ioe) {
            logger.error(ioe.getLocalizedMessage(), ioe);
        }
        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