Mercurial > dive4elements > framework
view artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java @ 57:cf9848c85755 0.2
RELEASE 0.2: Summarized NEWS and Changes
artifacts/trunk@338 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Hans Plum <hans.plum@intevation.de> |
---|---|
date | Fri, 13 Nov 2009 18:37:51 +0000 |
parents | 4ae4dc99127d |
children | 76abf0c64d3b |
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: