sascha@20: package de.intevation.artifactdatabase; sascha@20: sascha@20: import org.w3c.dom.Document; sascha@20: import org.w3c.dom.Element; sascha@20: import org.w3c.dom.Attr; sascha@20: sascha@62: import org.xml.sax.SAXException; sascha@62: sascha@20: import javax.xml.parsers.DocumentBuilderFactory; sascha@20: sascha@20: import javax.xml.parsers.ParserConfigurationException; sascha@20: sascha@20: import org.apache.log4j.Logger; sascha@20: sascha@23: import javax.xml.xpath.XPathFactory; sascha@23: import javax.xml.xpath.XPath; sascha@25: import javax.xml.xpath.XPathExpressionException; sascha@26: import javax.xml.xpath.XPathConstants; sascha@23: sascha@23: import javax.xml.namespace.NamespaceContext; sascha@25: import javax.xml.namespace.QName; sascha@23: sascha@62: import java.io.File; sascha@62: import java.io.IOException; sascha@62: sascha@20: /** sascha@20: * @author Sascha L. Teichmann sascha@20: */ sascha@20: public final class XMLUtils sascha@20: { ingo@64: public static final String XFORM_URL = "http://www.w3.org/2002/xforms"; ingo@64: public static final String XFORM_PREFIX = "xform"; ingo@64: sascha@20: private static Logger logger = Logger.getLogger(XMLUtils.class); sascha@20: sascha@20: private XMLUtils() { sascha@20: } sascha@20: sascha@20: public static class ElementCreator sascha@20: { sascha@20: protected Document document; sascha@20: protected String ns; sascha@20: protected String prefix; sascha@20: sascha@20: public ElementCreator(Document document, String ns, String prefix) { sascha@20: this.document = document; sascha@20: this.ns = ns; sascha@20: this.prefix = prefix; sascha@20: } sascha@20: sascha@20: public Element create(String name) { sascha@20: Element element = document.createElementNS(ns, name); sascha@20: element.setPrefix(prefix); sascha@20: return element; sascha@20: } sascha@20: sascha@20: public void addAttr(Element element, String name, String value) { ingo@64: addAttr(element, name, value, false); ingo@64: } ingo@64: ingo@64: public void addAttr( ingo@64: Element element, ingo@64: String name, ingo@64: String value, ingo@64: boolean addPrefix ingo@64: ) { sascha@20: Attr attr = document.createAttributeNS(ns, name); sascha@20: attr.setValue(value); ingo@64: ingo@64: if (addPrefix) ingo@64: attr.setPrefix(prefix); ingo@64: sascha@20: element.setAttributeNode(attr); sascha@20: } sascha@20: } // class ElementCreator sascha@20: sascha@20: public static final Document newDocument() { ingo@64: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); ingo@64: factory.setNamespaceAware(true); ingo@64: sascha@20: try { ingo@64: return factory.newDocumentBuilder().newDocument(); sascha@20: } sascha@20: catch (ParserConfigurationException pce) { sascha@20: logger.error(pce.getLocalizedMessage(), pce); sascha@20: } sascha@20: return null; sascha@20: } sascha@23: sascha@62: public static final Document parseDocument(File file) { ingo@64: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); ingo@64: factory.setNamespaceAware(true); ingo@64: sascha@62: try { ingo@64: return factory.newDocumentBuilder().parse(file); sascha@62: } sascha@62: catch (ParserConfigurationException pce) { sascha@62: logger.error(pce.getLocalizedMessage(), pce); sascha@62: } sascha@62: catch (SAXException se) { sascha@62: logger.error(se.getLocalizedMessage(), se); sascha@62: } sascha@62: catch (IOException ioe) { sascha@62: logger.error(ioe.getLocalizedMessage(), ioe); sascha@62: } sascha@62: return null; sascha@62: } sascha@62: sascha@23: public static final XPath newXPath() { sascha@23: return newXPath(null); sascha@23: } sascha@23: sascha@23: public static final XPath newXPath(NamespaceContext namespaceContext) { sascha@23: XPathFactory factory = XPathFactory.newInstance(); sascha@23: XPath xpath = factory.newXPath(); sascha@23: if (namespaceContext != null) { sascha@23: xpath.setNamespaceContext(namespaceContext); sascha@23: } sascha@23: return xpath; sascha@23: } sascha@25: sascha@25: public static final Object xpath(Object root, String query, QName returnTyp) { sascha@25: return xpath(root, query, returnTyp, null); sascha@25: } sascha@25: sascha@26: public static final String xpathString( sascha@26: Object root, String query, NamespaceContext namespaceContext sascha@26: ) { sascha@26: return (String)xpath(root, query, XPathConstants.STRING, namespaceContext); sascha@26: } sascha@26: sascha@25: public static final Object xpath( sascha@25: Object root, sascha@25: String query, sascha@25: QName returnType, sascha@25: NamespaceContext namespaceContext sascha@25: ) { sascha@25: if (root == null) { sascha@25: return null; sascha@25: } sascha@25: sascha@25: try { sascha@25: XPath xpath = newXPath(namespaceContext); sascha@25: if (xpath != null) { sascha@25: return xpath.evaluate(query, root, returnType); sascha@25: } sascha@25: } sascha@25: catch (XPathExpressionException xpee) { sascha@25: logger.error(xpee.getLocalizedMessage(), xpee); sascha@47: } sascha@25: sascha@25: return null; sascha@25: } sascha@20: } sascha@20: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: