ingo@1127: /* ingo@1127: * Copyright (c) 2010 by Intevation GmbH ingo@1127: * ingo@1127: * This program is free software under the LGPL (>=v2.1) ingo@1127: * Read the file LGPL.txt coming with the software for details ingo@1127: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@1127: */ ingo@1127: ingo@1121: package de.intevation.gnv.geobackend.util; ingo@1121: ingo@1121: import javax.xml.namespace.NamespaceContext; ingo@1121: import javax.xml.namespace.QName; ingo@1121: ingo@1121: import javax.xml.xpath.XPath; ingo@1121: import javax.xml.xpath.XPathExpressionException; ingo@1121: import javax.xml.xpath.XPathFactory; ingo@1121: ingo@1121: import org.apache.log4j.Logger; ingo@1121: ingo@1121: ingo@1121: /** ingo@1121: * @author Ingo Weinzierl ingo@1121: */ ingo@1121: public final class XMLUtils { ingo@1121: ingo@1121: private static Logger logger = Logger.getLogger(XMLUtils.class); ingo@1121: ingo@1121: ingo@1121: private XMLUtils() { ingo@1121: } ingo@1121: ingo@1121: ingo@1121: public static Object xpath( ingo@1121: Object root, ingo@1121: String query, ingo@1121: QName returnType, ingo@1121: NamespaceContext namespaceContext ingo@1121: ) { ingo@1121: if (root == null) { ingo@1121: return null; ingo@1121: } ingo@1121: ingo@1121: try { ingo@1121: XPath xpath = newXPath(namespaceContext); ingo@1121: ingo@1121: if (xpath != null) { ingo@1121: return xpath.evaluate(query, root, returnType); ingo@1121: } ingo@1121: } ingo@1121: catch (XPathExpressionException xpee) { ingo@1121: logger.error(xpee.getLocalizedMessage(), xpee); ingo@1121: } ingo@1121: ingo@1121: return null; ingo@1121: } ingo@1121: ingo@1121: ingo@1121: public static XPath newXPath(NamespaceContext namespaceContext) { ingo@1121: XPathFactory factory = XPathFactory.newInstance(); ingo@1121: XPath xpath = factory.newXPath(); ingo@1121: ingo@1121: if (namespaceContext != null) { ingo@1121: xpath.setNamespaceContext(namespaceContext); ingo@1121: } ingo@1121: ingo@1121: return xpath; ingo@1121: } ingo@1121: } ingo@1121: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :