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: import javax.xml.parsers.ParserConfigurationException; sascha@5: sascha@5: import javax.xml.namespace.QName; sascha@5: 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@19: public static final String CONFIG_DIR = "artifact.database.dir"; sascha@5: 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@19: public static final String CONFIG_FILE = "conf.xml"; sascha@5: tim@54: public static final String CONFIG_DIR_PLACEHOLDER = "${artifacts.config.dir}"; tim@54: 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@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: 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: sascha@5: private static Document loadConfig() { sascha@19: sascha@19: File configDir = getConfigDirectory(); sascha@19: sascha@19: 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@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@25: return XMLUtils.xpath(root, query, returnType); sascha@10: } sascha@10: sascha@10: public static final Object getXPath(String query, QName returnType) { sascha@25: return XMLUtils.xpath(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: