ingo@100: /* ingo@100: * Copyright (c) 2010 by Intevation GmbH ingo@100: * ingo@100: * This program is free software under the LGPL (>=v2.1) ingo@100: * Read the file LGPL.txt coming with the software for details ingo@100: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@100: */ ingo@100: sascha@5: package de.intevation.artifactdatabase; sascha@5: sascha@5: import java.io.File; sascha@5: import java.io.IOException; sascha@5: sascha@93: import javax.xml.namespace.QName; sascha@5: sascha@5: import javax.xml.parsers.DocumentBuilderFactory; sascha@5: import javax.xml.parsers.ParserConfigurationException; sascha@5: sascha@5: import javax.xml.xpath.XPathConstants; sascha@5: sascha@17: import org.apache.log4j.Logger; sascha@17: sascha@93: import org.w3c.dom.Document; sascha@93: import org.w3c.dom.Node; sascha@93: import org.w3c.dom.NodeList; sascha@93: sascha@93: import org.xml.sax.SAXException; sascha@93: sascha@10: /** sascha@91: * The central access to the configuration of the artifact database. sascha@91: * This class provides some static methods to access the central sascha@91: * configuration XML file via XPath. sascha@91: * ingo@80: * @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@91: /** sascha@91: * System property name where to find the configuration directory. sascha@91: */ sascha@19: public static final String CONFIG_DIR = "artifact.database.dir"; sascha@5: sascha@91: /** sascha@91: * Default path to the configuration directory if none sascha@91: * was specified by the CONFIG_DIR system property. sascha@91: */ sascha@19: public static final File CONFIG_DIR_DEFAULT = sascha@19: new File(new File(System.getProperty("user.home", sascha@19: System.getProperty("user.dir", "."))), ".artifactdb"); sascha@19: sascha@91: /** sascha@91: * Name of the central configuration XML file. sascha@91: */ sascha@19: public static final String CONFIG_FILE = "conf.xml"; sascha@5: sascha@91: /** sascha@91: * Name of the configuration filename alias to be use sascha@91: * within the configuration. This alias is replaced by sascha@91: * the real path. sascha@91: */ sascha@91: public static final String CONFIG_DIR_PLACEHOLDER = sascha@91: "${artifacts.config.dir}"; tim@54: sascha@5: private static Document config; sascha@5: sascha@5: private Config() { sascha@5: } sascha@5: sascha@91: /** sascha@91: * Singleton access to the central XML configuration document. sascha@91: * @return The central XML configuration document. sascha@91: */ sascha@5: public static synchronized final Document getConfig() { sascha@5: if (config == null) { ingo@61: config = loadConfig(); sascha@5: } sascha@5: return config; sascha@5: } sascha@5: sascha@91: /** sascha@91: * Returns the path to the configuartion directory. If a path sascha@91: * was specified via the CONFIG_DIR system property this one sascha@91: * is used. Else it falls back to default configuration path. sascha@91: * @return The path to the configuartion directory. sascha@91: */ sascha@19: public static File getConfigDirectory() { sascha@19: String configDirString = System.getProperty(CONFIG_DIR); sascha@19: sascha@19: File configDir = configDirString != null sascha@19: ? new File(configDirString) sascha@19: : CONFIG_DIR_DEFAULT; sascha@19: sascha@19: if (!configDir.isDirectory()) { sascha@19: logger.warn("'" + configDir + "' is not a directory."); sascha@19: configDir = CONFIG_DIR_DEFAULT; sascha@19: } sascha@19: sascha@19: return configDir; sascha@19: } sascha@19: sascha@91: /** sascha@91: * Replaces the CONFIG_DIR_PLACEHOLDER alias with the real path sascha@91: * of the configuration directory. sascha@91: * @param path The path containing the CONFIG_DIR_PLACEHOLDER placeholder. sascha@91: * @return The path where the CONFIG_DIR_PLACEHOLDER placeholders are sascha@91: * replaced by the real path name. sascha@91: */ tim@54: public static String replaceConfigDir(String path) { tim@54: String configDir = getConfigDirectory().getAbsolutePath(); tim@54: return path.replace(CONFIG_DIR_PLACEHOLDER, configDir); tim@54: } tim@54: ingo@61: private static Document loadConfig() { sascha@19: sascha@19: File configDir = getConfigDirectory(); sascha@19: ingo@61: File file = new File(configDir, CONFIG_FILE); 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@91: DocumentBuilderFactory factory = sascha@91: 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@91: /** sascha@91: * Convenience method to search within a given document tree via XPath. sascha@91: * See {@link XMLUtils#xpath(Object, String, QName) } for details. sascha@91: * @param root The object which is used as the root of the tree to sascha@91: * be searched in. sascha@91: * @param query The XPath query. sascha@91: * @param returnType The type of the result. sascha@91: * @return The result of type 'returnTyp' or null if something went sascha@91: * wrong during XPath evaluation. sascha@91: */ sascha@10: public static final Object getXPath( sascha@10: Object root, String query, QName returnType sascha@10: ) { sascha@25: return XMLUtils.xpath(root, query, returnType); sascha@10: } sascha@10: sascha@91: /** sascha@91: * Convenience method to search within the central configuration via XPath. sascha@91: * See {@link XMLUtils#xpath(Object, String, QName) } for details. sascha@91: * @param query The XPath query. sascha@91: * @param returnType The type of the result. sascha@91: * @return The result of type 'returnTyp' or null if something went sascha@91: * wrong during XPath evaluation. sascha@91: */ sascha@10: public static final Object getXPath(String query, QName returnType) { sascha@25: return XMLUtils.xpath(getConfig(), query, returnType); sascha@10: } sascha@10: sascha@91: /** sascha@91: * Convenience method to search for a node list within the central sascha@91: * configuation document via XPath. sascha@91: * @param query The XPath query. sascha@91: * @return The queried node list or null if something went sascha@91: * wrong during XPath evaluation. sascha@91: */ sascha@10: public static final NodeList getNodeSetXPath(String query) { sascha@10: return (NodeList)getXPath(query, XPathConstants.NODESET); sascha@10: } sascha@10: sascha@91: /** sascha@91: * Convenience method to search for a node within the central sascha@91: * configuation document via XPath. sascha@91: * @param query The XPath query. sascha@91: * @return The queried node or null if something went sascha@91: * wrong during XPath evaluation. sascha@91: */ sascha@10: public static final Node getNodeXPath(String query) { sascha@10: return (Node)getXPath(query, XPathConstants.NODE); sascha@10: } sascha@10: sascha@91: /** sascha@91: * Convenience method to search for a string within the central sascha@91: * configuation document via XPath. sascha@91: * @param xpath The XPath query. sascha@91: * @return The queried string or null if something went sascha@91: * wrong during XPath evaluation. sascha@91: */ sascha@5: public static final String getStringXPath(String xpath) { sascha@5: return getStringXPath(xpath, null); sascha@5: } sascha@5: sascha@91: /** sascha@91: * Convenience method to search for a string within the central sascha@91: * configuation document via XPath. sascha@91: * @param query The XPath query. sascha@91: * @param def The string to be returned if the search has no results. sascha@91: * @return The queried string or the default value if something went sascha@91: * wrong during XPath evaluation. sascha@91: */ 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@91: /** sascha@91: * Convenience method to search for a node list within a given tree sascha@91: * via XPath. sascha@91: * @param root The root of the tree to be searched in. sascha@91: * @param query The XPath query. sascha@91: * @return The queried node list or null if something went sascha@91: * wrong during XPath evaluation. sascha@91: */ 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@91: /** sascha@91: * Convenience method to search for a node within a given tree sascha@91: * via XPath. sascha@91: * @param root The root of the tree to be searched in. sascha@91: * @param query The XPath query. sascha@91: * @return The queried node or null if something went sascha@91: * wrong during XPath evaluation. sascha@91: */ 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@91: /** sascha@91: * Convenience method to search for a string within a given tree sascha@91: * via XPath. sascha@91: * @param root The root of the tree to be searched in. sascha@91: * @param xpath The XPath query. sascha@91: * @return The queried string or null if something went sascha@91: * wrong during XPath evaluation. sascha@91: */ sascha@10: public static final String getStringXPath(Object root, String xpath) { sascha@10: return getStringXPath(root, xpath, null); sascha@10: } sascha@10: sascha@91: /** sascha@91: * Convenience method to search for a string within a given tree sascha@91: * via XPath. sascha@91: * @param root The root of the tree to be searched in. sascha@91: * @param query xpath The XPath query. sascha@91: * @param def The string to be returned if the search has no results. sascha@91: * @return The queried string or the default value if something went sascha@91: * wrong during XPath evaluation. sascha@91: */ 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@93: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :