Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Config.java @ 91:730ff077a58c
More javadoc, some code formatting.
artifacts/trunk@847 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 28 Mar 2010 10:01:03 +0000 |
parents | 8447467cef86 |
children | e27cf9c84eb8 |
comparison
equal
deleted
inserted
replaced
90:68285f7bc476 | 91:730ff077a58c |
---|---|
17 import javax.xml.xpath.XPathConstants; | 17 import javax.xml.xpath.XPathConstants; |
18 | 18 |
19 import org.apache.log4j.Logger; | 19 import org.apache.log4j.Logger; |
20 | 20 |
21 /** | 21 /** |
22 * The central access to the configuration of the artifact database. | |
23 * This class provides some static methods to access the central | |
24 * configuration XML file via XPath. | |
25 * | |
22 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | 26 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> |
23 */ | 27 */ |
24 public final class Config | 28 public final class Config |
25 { | 29 { |
26 private static Logger logger = Logger.getLogger(Config.class); | 30 private static Logger logger = Logger.getLogger(Config.class); |
27 | 31 |
32 /** | |
33 * System property name where to find the configuration directory. | |
34 */ | |
28 public static final String CONFIG_DIR = "artifact.database.dir"; | 35 public static final String CONFIG_DIR = "artifact.database.dir"; |
29 | 36 |
37 /** | |
38 * Default path to the configuration directory if none | |
39 * was specified by the CONFIG_DIR system property. | |
40 */ | |
30 public static final File CONFIG_DIR_DEFAULT = | 41 public static final File CONFIG_DIR_DEFAULT = |
31 new File(new File(System.getProperty("user.home", | 42 new File(new File(System.getProperty("user.home", |
32 System.getProperty("user.dir", "."))), ".artifactdb"); | 43 System.getProperty("user.dir", "."))), ".artifactdb"); |
33 | 44 |
45 /** | |
46 * Name of the central configuration XML file. | |
47 */ | |
34 public static final String CONFIG_FILE = "conf.xml"; | 48 public static final String CONFIG_FILE = "conf.xml"; |
35 | 49 |
36 public static final String CONFIG_DIR_PLACEHOLDER = "${artifacts.config.dir}"; | 50 /** |
51 * Name of the configuration filename alias to be use | |
52 * within the configuration. This alias is replaced by | |
53 * the real path. | |
54 */ | |
55 public static final String CONFIG_DIR_PLACEHOLDER = | |
56 "${artifacts.config.dir}"; | |
37 | 57 |
38 private static Document config; | 58 private static Document config; |
39 | 59 |
40 private Config() { | 60 private Config() { |
41 } | 61 } |
42 | 62 |
63 /** | |
64 * Singleton access to the central XML configuration document. | |
65 * @return The central XML configuration document. | |
66 */ | |
43 public static synchronized final Document getConfig() { | 67 public static synchronized final Document getConfig() { |
44 if (config == null) { | 68 if (config == null) { |
45 config = loadConfig(); | 69 config = loadConfig(); |
46 } | 70 } |
47 return config; | 71 return config; |
48 } | 72 } |
49 | 73 |
74 /** | |
75 * Returns the path to the configuartion directory. If a path | |
76 * was specified via the CONFIG_DIR system property this one | |
77 * is used. Else it falls back to default configuration path. | |
78 * @return The path to the configuartion directory. | |
79 */ | |
50 public static File getConfigDirectory() { | 80 public static File getConfigDirectory() { |
51 String configDirString = System.getProperty(CONFIG_DIR); | 81 String configDirString = System.getProperty(CONFIG_DIR); |
52 | 82 |
53 File configDir = configDirString != null | 83 File configDir = configDirString != null |
54 ? new File(configDirString) | 84 ? new File(configDirString) |
60 } | 90 } |
61 | 91 |
62 return configDir; | 92 return configDir; |
63 } | 93 } |
64 | 94 |
95 /** | |
96 * Replaces the CONFIG_DIR_PLACEHOLDER alias with the real path | |
97 * of the configuration directory. | |
98 * @param path The path containing the CONFIG_DIR_PLACEHOLDER placeholder. | |
99 * @return The path where the CONFIG_DIR_PLACEHOLDER placeholders are | |
100 * replaced by the real path name. | |
101 */ | |
65 public static String replaceConfigDir(String path) { | 102 public static String replaceConfigDir(String path) { |
66 String configDir = getConfigDirectory().getAbsolutePath(); | 103 String configDir = getConfigDirectory().getAbsolutePath(); |
67 return path.replace(CONFIG_DIR_PLACEHOLDER, configDir); | 104 return path.replace(CONFIG_DIR_PLACEHOLDER, configDir); |
68 } | 105 } |
69 | 106 |
78 + file + "'."); | 115 + file + "'."); |
79 return null; | 116 return null; |
80 } | 117 } |
81 | 118 |
82 try { | 119 try { |
83 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | 120 DocumentBuilderFactory factory = |
121 DocumentBuilderFactory.newInstance(); | |
84 factory.setValidating(false); // XXX: This may change in future. | 122 factory.setValidating(false); // XXX: This may change in future. |
85 return factory.newDocumentBuilder().parse(file); | 123 return factory.newDocumentBuilder().parse(file); |
86 } | 124 } |
87 catch (SAXException se) { | 125 catch (SAXException se) { |
88 logger.error(se.getLocalizedMessage(), se); | 126 logger.error(se.getLocalizedMessage(), se); |
95 } | 133 } |
96 | 134 |
97 return null; | 135 return null; |
98 } | 136 } |
99 | 137 |
138 /** | |
139 * Convenience method to search within a given document tree via XPath. | |
140 * See {@link XMLUtils#xpath(Object, String, QName) } for details. | |
141 * @param root The object which is used as the root of the tree to | |
142 * be searched in. | |
143 * @param query The XPath query. | |
144 * @param returnType The type of the result. | |
145 * @return The result of type 'returnTyp' or null if something went | |
146 * wrong during XPath evaluation. | |
147 */ | |
100 public static final Object getXPath( | 148 public static final Object getXPath( |
101 Object root, String query, QName returnType | 149 Object root, String query, QName returnType |
102 ) { | 150 ) { |
103 return XMLUtils.xpath(root, query, returnType); | 151 return XMLUtils.xpath(root, query, returnType); |
104 } | 152 } |
105 | 153 |
154 /** | |
155 * Convenience method to search within the central configuration via XPath. | |
156 * See {@link XMLUtils#xpath(Object, String, QName) } for details. | |
157 * @param query The XPath query. | |
158 * @param returnType The type of the result. | |
159 * @return The result of type 'returnTyp' or null if something went | |
160 * wrong during XPath evaluation. | |
161 */ | |
106 public static final Object getXPath(String query, QName returnType) { | 162 public static final Object getXPath(String query, QName returnType) { |
107 return XMLUtils.xpath(getConfig(), query, returnType); | 163 return XMLUtils.xpath(getConfig(), query, returnType); |
108 } | 164 } |
109 | 165 |
166 /** | |
167 * Convenience method to search for a node list within the central | |
168 * configuation document via XPath. | |
169 * @param query The XPath query. | |
170 * @return The queried node list or null if something went | |
171 * wrong during XPath evaluation. | |
172 */ | |
110 public static final NodeList getNodeSetXPath(String query) { | 173 public static final NodeList getNodeSetXPath(String query) { |
111 return (NodeList)getXPath(query, XPathConstants.NODESET); | 174 return (NodeList)getXPath(query, XPathConstants.NODESET); |
112 } | 175 } |
113 | 176 |
177 /** | |
178 * Convenience method to search for a node within the central | |
179 * configuation document via XPath. | |
180 * @param query The XPath query. | |
181 * @return The queried node or null if something went | |
182 * wrong during XPath evaluation. | |
183 */ | |
114 public static final Node getNodeXPath(String query) { | 184 public static final Node getNodeXPath(String query) { |
115 return (Node)getXPath(query, XPathConstants.NODE); | 185 return (Node)getXPath(query, XPathConstants.NODE); |
116 } | 186 } |
117 | 187 |
188 /** | |
189 * Convenience method to search for a string within the central | |
190 * configuation document via XPath. | |
191 * @param xpath The XPath query. | |
192 * @return The queried string or null if something went | |
193 * wrong during XPath evaluation. | |
194 */ | |
118 public static final String getStringXPath(String xpath) { | 195 public static final String getStringXPath(String xpath) { |
119 return getStringXPath(xpath, null); | 196 return getStringXPath(xpath, null); |
120 } | 197 } |
121 | 198 |
199 /** | |
200 * Convenience method to search for a string within the central | |
201 * configuation document via XPath. | |
202 * @param query The XPath query. | |
203 * @param def The string to be returned if the search has no results. | |
204 * @return The queried string or the default value if something went | |
205 * wrong during XPath evaluation. | |
206 */ | |
122 public static final String getStringXPath(String query, String def) { | 207 public static final String getStringXPath(String query, String def) { |
123 String s = (String)getXPath(query, XPathConstants.STRING); | 208 String s = (String)getXPath(query, XPathConstants.STRING); |
124 return s == null || s.length() == 0 | 209 return s == null || s.length() == 0 |
125 ? def | 210 ? def |
126 : s; | 211 : s; |
127 } | 212 } |
128 | 213 |
214 /** | |
215 * Convenience method to search for a node list within a given tree | |
216 * via XPath. | |
217 * @param root The root of the tree to be searched in. | |
218 * @param query The XPath query. | |
219 * @return The queried node list or null if something went | |
220 * wrong during XPath evaluation. | |
221 */ | |
129 public static final NodeList getNodeSetXPath(Object root, String query) { | 222 public static final NodeList getNodeSetXPath(Object root, String query) { |
130 return (NodeList)getXPath(root, query, XPathConstants.NODESET); | 223 return (NodeList)getXPath(root, query, XPathConstants.NODESET); |
131 } | 224 } |
132 | 225 |
226 /** | |
227 * Convenience method to search for a node within a given tree | |
228 * via XPath. | |
229 * @param root The root of the tree to be searched in. | |
230 * @param query The XPath query. | |
231 * @return The queried node or null if something went | |
232 * wrong during XPath evaluation. | |
233 */ | |
133 public static final Node getNodeXPath(Object root, String query) { | 234 public static final Node getNodeXPath(Object root, String query) { |
134 return (Node)getXPath(root, query, XPathConstants.NODE); | 235 return (Node)getXPath(root, query, XPathConstants.NODE); |
135 } | 236 } |
136 | 237 |
238 /** | |
239 * Convenience method to search for a string within a given tree | |
240 * via XPath. | |
241 * @param root The root of the tree to be searched in. | |
242 * @param xpath The XPath query. | |
243 * @return The queried string or null if something went | |
244 * wrong during XPath evaluation. | |
245 */ | |
137 public static final String getStringXPath(Object root, String xpath) { | 246 public static final String getStringXPath(Object root, String xpath) { |
138 return getStringXPath(root, xpath, null); | 247 return getStringXPath(root, xpath, null); |
139 } | 248 } |
140 | 249 |
250 /** | |
251 * Convenience method to search for a string within a given tree | |
252 * via XPath. | |
253 * @param root The root of the tree to be searched in. | |
254 * @param query xpath The XPath query. | |
255 * @param def The string to be returned if the search has no results. | |
256 * @return The queried string or the default value if something went | |
257 * wrong during XPath evaluation. | |
258 */ | |
141 public static final String getStringXPath( | 259 public static final String getStringXPath( |
142 Object root, String query, String def | 260 Object root, String query, String def |
143 ) { | 261 ) { |
144 String s = (String)getXPath(root, query, XPathConstants.STRING); | 262 String s = (String)getXPath(root, query, XPathConstants.STRING); |
145 return s == null || s.length() == 0 | 263 return s == null || s.length() == 0 |
146 ? def | 264 ? def |
147 : s; | 265 : s; |
148 } | 266 } |
149 } | 267 } |
150 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: | 268 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |