Mercurial > dive4elements > framework
comparison 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 |
comparison
equal
deleted
inserted
replaced
4:13a12b607baf | 5:8f2de197bce2 |
---|---|
1 package de.intevation.artifactdatabase; | |
2 | |
3 import java.io.File; | |
4 import java.io.IOException; | |
5 | |
6 import org.w3c.dom.Document; | |
7 import org.w3c.dom.NodeList; | |
8 | |
9 import org.xml.sax.SAXException; | |
10 | |
11 import javax.xml.parsers.DocumentBuilderFactory; | |
12 | |
13 import javax.xml.parsers.ParserConfigurationException; | |
14 | |
15 import javax.xml.namespace.QName; | |
16 | |
17 import javax.xml.xpath.XPathFactory; | |
18 import javax.xml.xpath.XPath; | |
19 import javax.xml.xpath.XPathExpressionException; | |
20 import javax.xml.xpath.XPathConstants; | |
21 | |
22 public final class Config | |
23 { | |
24 public static final String CONFIG_PROPERTY = "artifact.database.config"; | |
25 | |
26 public static final String CONFIG_DEFAULT = "artifactdb-conf.xml"; | |
27 | |
28 private static Document config; | |
29 | |
30 private Config() { | |
31 } | |
32 | |
33 public static synchronized final Document getConfig() { | |
34 if (config == null) { | |
35 config = loadConfig(); | |
36 } | |
37 return config; | |
38 } | |
39 | |
40 private static Document loadConfig() { | |
41 File file = new File( | |
42 System.getProperty(CONFIG_PROPERTY, CONFIG_DEFAULT)); | |
43 | |
44 if (!file.canRead() && !file.isFile()) { | |
45 System.err.println("ERROR: cannot read config file '" | |
46 + file + "'."); | |
47 return null; | |
48 } | |
49 | |
50 try { | |
51 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
52 factory.setValidating(false); // XXX: This may change in future. | |
53 return factory.newDocumentBuilder().parse(file); | |
54 } | |
55 catch (SAXException se) { | |
56 System.err.println("ERROR: while processing XML file '" | |
57 + file + "'"); | |
58 se.printStackTrace(System.err); | |
59 } | |
60 catch (ParserConfigurationException pce) { | |
61 System.err.println("ERROR: with XML configuration"); | |
62 pce.printStackTrace(System.err); | |
63 } | |
64 catch (IOException ioe) { | |
65 System.err.println("ERROR: I/O while processing file '" | |
66 + file + "'"); | |
67 ioe.printStackTrace(System.err); | |
68 } | |
69 | |
70 return null; | |
71 } | |
72 | |
73 public static final String getStringXPath(String xpath) { | |
74 return getStringXPath(xpath, null); | |
75 } | |
76 | |
77 public static final String getStringXPath(String query, String def) { | |
78 String s = (String)getXPath(query, XPathConstants.STRING); | |
79 return s == null || s.length() == 0 | |
80 ? def | |
81 : s; | |
82 } | |
83 | |
84 public static final Object getXPath(String query, QName returnType) { | |
85 Document document = getConfig(); | |
86 if (document == null) { | |
87 return null; | |
88 } | |
89 | |
90 XPathFactory factory = XPathFactory.newInstance(); | |
91 XPath xpath = factory.newXPath(); | |
92 | |
93 try { | |
94 return xpath.evaluate(query, document, returnType); | |
95 } | |
96 catch (XPathExpressionException xpee) { | |
97 xpee.printStackTrace(System.err); | |
98 } | |
99 | |
100 return null; | |
101 } | |
102 | |
103 public static final NodeList getNodeSetXPath(String query) { | |
104 return (NodeList)getXPath(query, XPathConstants.NODESET); | |
105 } | |
106 } | |
107 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: | |
108 |