diff artifact-database/src/main/java/de/intevation/artifactdatabase/Config.java @ 5:8f2de197bce2

Added config to artifact database and modelled bootstap of artifact factories on top of this. artifacts/trunk@15 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 04 Sep 2009 12:04:12 +0000
parents
children a5a279a0ee35
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Config.java	Fri Sep 04 12:04:12 2009 +0000
@@ -0,0 +1,108 @@
+package de.intevation.artifactdatabase;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+
+import org.xml.sax.SAXException;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+import javax.xml.namespace.QName;
+
+import javax.xml.xpath.XPathFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathConstants;
+
+public final class Config
+{
+    public static final String CONFIG_PROPERTY = "artifact.database.config";
+
+    public static final String CONFIG_DEFAULT  = "artifactdb-conf.xml";
+
+    private static Document config;
+
+    private Config() {
+    }
+
+    public static synchronized final Document getConfig() {
+        if (config == null) {
+            config = loadConfig();
+        }
+        return config;
+    }
+
+    private static Document loadConfig() {
+        File file = new File(
+            System.getProperty(CONFIG_PROPERTY, CONFIG_DEFAULT));
+
+        if (!file.canRead() && !file.isFile()) {
+            System.err.println("ERROR: cannot read config file '"
+                + file + "'.");
+            return null;
+        }
+
+        try {
+            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+            factory.setValidating(false); // XXX: This may change in future.
+            return factory.newDocumentBuilder().parse(file);
+        }
+        catch (SAXException se) {
+            System.err.println("ERROR: while processing XML file '"
+                + file + "'");
+            se.printStackTrace(System.err);
+        }
+        catch (ParserConfigurationException pce) {
+            System.err.println("ERROR: with XML configuration");
+            pce.printStackTrace(System.err);
+        }
+        catch (IOException ioe) {
+            System.err.println("ERROR: I/O while processing file '"
+                + file + "'");
+            ioe.printStackTrace(System.err);
+        }
+
+        return null;
+    }
+
+    public static final String getStringXPath(String xpath) {
+        return getStringXPath(xpath, null);
+    }
+
+    public static final String getStringXPath(String query, String def) {
+        String s = (String)getXPath(query, XPathConstants.STRING);
+        return s == null || s.length() == 0
+            ? def
+            : s;
+    }
+
+    public static final Object getXPath(String query, QName returnType) {
+        Document document = getConfig();
+        if (document == null) {
+            return null;
+        }
+
+        XPathFactory factory = XPathFactory.newInstance();
+        XPath        xpath   = factory.newXPath();
+
+        try {
+            return xpath.evaluate(query, document, returnType);
+        }
+        catch (XPathExpressionException xpee) {
+            xpee.printStackTrace(System.err);
+        }
+
+        return null;
+    }
+
+    public static final NodeList getNodeSetXPath(String query) {
+        return (NodeList)getXPath(query, XPathConstants.NODESET);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
+

http://dive4elements.wald.intevation.org