sascha@5: package de.intevation.artifactdatabase; sascha@5: sascha@5: import java.io.File; sascha@5: import java.io.IOException; sascha@5: sascha@5: import org.w3c.dom.Document; sascha@5: import org.w3c.dom.NodeList; sascha@9: import org.w3c.dom.Node; sascha@5: sascha@5: import org.xml.sax.SAXException; sascha@5: sascha@5: import javax.xml.parsers.DocumentBuilderFactory; sascha@5: sascha@5: import javax.xml.parsers.ParserConfigurationException; sascha@5: sascha@5: import javax.xml.namespace.QName; sascha@5: sascha@5: import javax.xml.xpath.XPathFactory; sascha@5: import javax.xml.xpath.XPath; sascha@5: import javax.xml.xpath.XPathExpressionException; sascha@5: import javax.xml.xpath.XPathConstants; sascha@5: sascha@17: import org.apache.log4j.Logger; sascha@17: sascha@10: /** sascha@10: * @author Sascha L. Teichmann sascha@10: */ sascha@5: public final class Config sascha@5: { sascha@17: private static Logger logger = Logger.getLogger(Config.class); sascha@17: sascha@5: public static final String CONFIG_PROPERTY = "artifact.database.config"; sascha@5: sascha@5: public static final String CONFIG_DEFAULT = "artifactdb-conf.xml"; sascha@5: sascha@5: private static Document config; sascha@5: sascha@5: private Config() { sascha@5: } sascha@5: sascha@5: public static synchronized final Document getConfig() { sascha@5: if (config == null) { sascha@5: config = loadConfig(); sascha@5: } sascha@5: return config; sascha@5: } sascha@5: sascha@5: private static Document loadConfig() { sascha@5: File file = new File( sascha@5: System.getProperty(CONFIG_PROPERTY, CONFIG_DEFAULT)); sascha@5: sascha@5: if (!file.canRead() && !file.isFile()) { sascha@17: logger.error("Cannot read config file '" sascha@5: + file + "'."); sascha@5: return null; sascha@5: } sascha@5: sascha@5: try { sascha@5: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); sascha@5: factory.setValidating(false); // XXX: This may change in future. sascha@5: return factory.newDocumentBuilder().parse(file); sascha@5: } sascha@5: catch (SAXException se) { sascha@17: logger.error(se.getLocalizedMessage(), se); sascha@5: } sascha@5: catch (ParserConfigurationException pce) { sascha@17: logger.error(pce.getLocalizedMessage(), pce); sascha@5: } sascha@5: catch (IOException ioe) { sascha@17: logger.error(ioe.getLocalizedMessage()); sascha@5: } sascha@5: sascha@5: return null; sascha@5: } sascha@5: sascha@10: public static final Object getXPath( sascha@10: Object root, String query, QName returnType sascha@10: ) { sascha@10: if (root == null) { sascha@10: return null; sascha@10: } sascha@10: sascha@10: XPathFactory factory = XPathFactory.newInstance(); sascha@10: XPath xpath = factory.newXPath(); sascha@10: sascha@10: try { sascha@10: return xpath.evaluate(query, root, returnType); sascha@10: } sascha@10: catch (XPathExpressionException xpee) { sascha@17: logger.error(xpee.getLocalizedMessage(), xpee); sascha@10: } sascha@10: sascha@10: return null; sascha@10: } sascha@10: sascha@10: public static final Object getXPath(String query, QName returnType) { sascha@10: return getXPath(getConfig(), query, returnType); sascha@10: } sascha@10: sascha@10: public static final NodeList getNodeSetXPath(String query) { sascha@10: return (NodeList)getXPath(query, XPathConstants.NODESET); sascha@10: } sascha@10: sascha@10: public static final Node getNodeXPath(String query) { sascha@10: return (Node)getXPath(query, XPathConstants.NODE); sascha@10: } sascha@10: sascha@5: public static final String getStringXPath(String xpath) { sascha@5: return getStringXPath(xpath, null); sascha@5: } sascha@5: sascha@5: public static final String getStringXPath(String query, String def) { sascha@5: String s = (String)getXPath(query, XPathConstants.STRING); sascha@5: return s == null || s.length() == 0 sascha@5: ? def sascha@5: : s; sascha@5: } sascha@5: sascha@10: public static final NodeList getNodeSetXPath(Object root, String query) { sascha@10: return (NodeList)getXPath(root, query, XPathConstants.NODESET); sascha@5: } sascha@5: sascha@10: public static final Node getNodeXPath(Object root, String query) { sascha@10: return (Node)getXPath(root, query, XPathConstants.NODE); sascha@5: } sascha@9: sascha@10: public static final String getStringXPath(Object root, String xpath) { sascha@10: return getStringXPath(root, xpath, null); sascha@10: } sascha@10: sascha@10: public static final String getStringXPath( sascha@10: Object root, String query, String def sascha@10: ) { sascha@10: String s = (String)getXPath(root, query, XPathConstants.STRING); sascha@10: return s == null || s.length() == 0 sascha@10: ? def sascha@10: : s; sascha@9: } sascha@5: } sascha@5: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: