view artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java @ 77:48d1a9a082c2 0.5

Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>' to make the sources to be able to be formatted with jalopy (http://jalopy.sourceforge.net). artifacts/trunk@695 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 21 Feb 2010 23:05:32 +0000
parents ed03cc0e5800
children 730ff077a58c
line wrap: on
line source
package de.intevation.artifactdatabase;

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

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

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

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

import org.apache.log4j.Logger;

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

import org.xml.sax.SAXException;

/**
 *  @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
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;
    }

    public static boolean toStream(Document document, OutputStream out) {
        try {
            Transformer transformer =
                TransformerFactory.newInstance().newTransformer();
            DOMSource    source = new DOMSource(document);
            StreamResult result = new StreamResult(out);
            transformer.transform(source, result);
            return true;
        }
        catch (TransformerConfigurationException tce) {
            logger.error(tce.getLocalizedMessage(), tce);
        }
        catch (TransformerFactoryConfigurationError tfce) {
            logger.error(tfce.getLocalizedMessage(), tfce);
        }
        catch (TransformerException te) {
            logger.error(te.getLocalizedMessage(), te);
        }

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

http://dive4elements.wald.intevation.org