Mercurial > dive4elements > framework
annotate artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/Config.java @ 556:ba31ca213c88
loadConfig static method
author | gernotbelger |
---|---|
date | Tue, 26 Jun 2018 20:25:46 +0200 |
parents | 415df0fc4fa1 |
children | 609ced80bcf0 |
rev | line source |
---|---|
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 /* |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 * Copyright (c) 2010 by Intevation GmbH |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 * |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 * This program is free software under the LGPL (>=v2.1) |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 * Read the file LGPL.txt coming with the software for details |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 * or visit http://www.gnu.org/licenses/ if it does not exist. |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 */ |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 |
475
415df0fc4fa1
Fixed maven group ids
Sascha L. Teichmann <teichmann@intevation.de>
parents:
472
diff
changeset
|
9 package org.dive4elements.artifacts.common.utils; |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 import java.io.File; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 import java.io.IOException; |
556 | 13 import java.io.InputStreamReader; |
14 import java.nio.charset.StandardCharsets; | |
15 import java.nio.file.Files; | |
16 import java.util.Properties; | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 import javax.xml.namespace.QName; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 import javax.xml.parsers.DocumentBuilderFactory; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 import javax.xml.parsers.ParserConfigurationException; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 import javax.xml.xpath.XPathConstants; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 import org.apache.log4j.Logger; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 import org.w3c.dom.Document; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 import org.w3c.dom.Node; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 import org.w3c.dom.NodeList; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 import org.xml.sax.SAXException; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 * The central access to the configuration of the artifact database. |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 * This class provides some static methods to access the central |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 * configuration XML file via XPath. |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 * |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 */ |
556 | 36 public final class Config { |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 private static Logger logger = Logger.getLogger(Config.class); |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 * System property name where to find the configuration directory. |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 */ |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 public static final String CONFIG_DIR = "artifact.database.dir"; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 * Default path to the configuration directory if none |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 * was specified by the CONFIG_DIR system property. |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 */ |
556 | 48 public static final File CONFIG_DIR_DEFAULT = new File(new File(System.getProperty("user.home", System.getProperty("user.dir", "."))), ".artifactdb"); |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 * Name of the central configuration XML file. |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 */ |
556 | 53 public static final String CONFIG_FILE = "conf.xml"; |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 * Name of the configuration filename alias to be use |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 * within the configuration. This alias is replaced by |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 * the real path. |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 */ |
556 | 60 public static final String CONFIG_DIR_PLACEHOLDER = "${artifacts.config.dir}"; |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 private static Document config; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 private Config() { |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 * Singleton access to the central XML configuration document. |
556 | 69 * |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 * @return The central XML configuration document. |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 */ |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 public static synchronized final Document getConfig() { |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 if (config == null) { |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 config = loadConfig(); |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 return config; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 |
556 | 79 public static Properties loadProperties(final String CONFIG_FILE_LOCAL) { |
80 final File configDir = getConfigDirectory(); | |
81 final File configFile = new File(configDir, CONFIG_FILE_LOCAL); | |
82 | |
83 final Properties properties = new Properties(); | |
84 InputStreamReader reader; | |
85 try { | |
86 reader = new InputStreamReader(Files.newInputStream(configFile.toPath()), StandardCharsets.ISO_8859_1); | |
87 properties.load(reader); | |
88 } | |
89 catch (final IOException e) { | |
90 e.printStackTrace(); | |
91 } | |
92 | |
93 return properties; | |
94 } | |
95 | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 * Returns the path to the configuartion directory. If a path |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 * was specified via the CONFIG_DIR system property this one |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 * is used. Else it falls back to default configuration path. |
556 | 100 * |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 * @return The path to the configuartion directory. |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 */ |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 public static File getConfigDirectory() { |
556 | 104 final String configDirString = System.getProperty(CONFIG_DIR); |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 |
556 | 106 File configDir = configDirString != null ? new File(configDirString) : CONFIG_DIR_DEFAULT; |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
108 if (!configDir.isDirectory()) { |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
109 logger.warn("'" + configDir + "' is not a directory."); |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
110 configDir = CONFIG_DIR_DEFAULT; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
111 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
112 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
113 return configDir; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
116 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
117 * Replaces the CONFIG_DIR_PLACEHOLDER alias with the real path |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
118 * of the configuration directory. |
556 | 119 * |
120 * @param path | |
121 * The path containing the CONFIG_DIR_PLACEHOLDER placeholder. | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 * @return The path where the CONFIG_DIR_PLACEHOLDER placeholders are |
556 | 123 * replaced by the real path name. |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 */ |
556 | 125 public static String replaceConfigDir(final String path) { |
126 final String configDir = getConfigDirectory().getAbsolutePath(); | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
127 return path.replace(CONFIG_DIR_PLACEHOLDER, configDir); |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
128 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
129 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
130 private static Document loadConfig() { |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
131 |
556 | 132 final File configDir = getConfigDirectory(); |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
133 |
556 | 134 final File file = new File(configDir, CONFIG_FILE); |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
135 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
136 if (!file.canRead() && !file.isFile()) { |
556 | 137 logger.error("Cannot read config file '" + file + "'."); |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
138 return null; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
139 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
140 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
141 try { |
556 | 142 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
143 factory.setValidating(false); // XXX: This may change in future. |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
144 return factory.newDocumentBuilder().parse(file); |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
145 } |
556 | 146 catch (final SAXException se) { |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
147 logger.error(se.getLocalizedMessage(), se); |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
148 } |
556 | 149 catch (final ParserConfigurationException pce) { |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 logger.error(pce.getLocalizedMessage(), pce); |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 } |
556 | 152 catch (final IOException ioe) { |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
153 logger.error(ioe.getLocalizedMessage()); |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
154 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
155 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
156 return null; |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
157 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
158 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
159 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
160 * Convenience method to search within a given document tree via XPath. |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
161 * See {@link XMLUtils#xpath(Object, String, QName) } for details. |
556 | 162 * |
163 * @param root | |
164 * The object which is used as the root of the tree to | |
165 * be searched in. | |
166 * @param query | |
167 * The XPath query. | |
168 * @param returnType | |
169 * The type of the result. | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
170 * @return The result of type 'returnTyp' or null if something went |
556 | 171 * wrong during XPath evaluation. |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
172 */ |
556 | 173 public static final Object getXPath(final Object root, final String query, final QName returnType) { |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
174 return XMLUtils.xpath(root, query, returnType); |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
175 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
176 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
177 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
178 * Convenience method to search within the central configuration via XPath. |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
179 * See {@link XMLUtils#xpath(Object, String, QName) } for details. |
556 | 180 * |
181 * @param query | |
182 * The XPath query. | |
183 * @param returnType | |
184 * The type of the result. | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
185 * @return The result of type 'returnTyp' or null if something went |
556 | 186 * wrong during XPath evaluation. |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
187 */ |
556 | 188 public static final Object getXPath(final String query, final QName returnType) { |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
189 return XMLUtils.xpath(getConfig(), query, returnType); |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
190 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
191 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
192 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
193 * Convenience method to search for a node list within the central |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
194 * configuation document via XPath. |
556 | 195 * |
196 * @param query | |
197 * The XPath query. | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
198 * @return The queried node list or null if something went |
556 | 199 * wrong during XPath evaluation. |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
200 */ |
556 | 201 public static final NodeList getNodeSetXPath(final String query) { |
202 return (NodeList) getXPath(query, XPathConstants.NODESET); | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
203 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
204 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
205 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
206 * Convenience method to search for a node within the central |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
207 * configuation document via XPath. |
556 | 208 * |
209 * @param query | |
210 * The XPath query. | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
211 * @return The queried node or null if something went |
556 | 212 * wrong during XPath evaluation. |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
213 */ |
556 | 214 public static final Node getNodeXPath(final String query) { |
215 return (Node) getXPath(query, XPathConstants.NODE); | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
216 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
217 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
218 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
219 * Convenience method to search for a string within the central |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
220 * configuation document via XPath. |
556 | 221 * |
222 * @param xpath | |
223 * The XPath query. | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
224 * @return The queried string or null if something went |
556 | 225 * wrong during XPath evaluation. |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
226 */ |
556 | 227 public static final String getStringXPath(final String xpath) { |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
228 return getStringXPath(xpath, null); |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
229 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
230 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
231 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
232 * Convenience method to search for a string within the central |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
233 * configuation document via XPath. |
556 | 234 * |
235 * @param query | |
236 * The XPath query. | |
237 * @param def | |
238 * The string to be returned if the search has no results. | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
239 * @return The queried string or the default value if something went |
556 | 240 * wrong during XPath evaluation. |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
241 */ |
556 | 242 public static final String getStringXPath(final String query, final String def) { |
243 final String s = (String) getXPath(query, XPathConstants.STRING); | |
244 return s == null || s.length() == 0 ? def : s; | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
245 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
246 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
247 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
248 * Convenience method to search for a node list within a given tree |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
249 * via XPath. |
556 | 250 * |
251 * @param root | |
252 * The root of the tree to be searched in. | |
253 * @param query | |
254 * The XPath query. | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
255 * @return The queried node list or null if something went |
556 | 256 * wrong during XPath evaluation. |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
257 */ |
556 | 258 public static final NodeList getNodeSetXPath(final Object root, final String query) { |
259 return (NodeList) getXPath(root, query, XPathConstants.NODESET); | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
260 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
261 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
262 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
263 * Convenience method to search for a node within a given tree |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
264 * via XPath. |
556 | 265 * |
266 * @param root | |
267 * The root of the tree to be searched in. | |
268 * @param query | |
269 * The XPath query. | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
270 * @return The queried node or null if something went |
556 | 271 * wrong during XPath evaluation. |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
272 */ |
556 | 273 public static final Node getNodeXPath(final Object root, final String query) { |
274 return (Node) getXPath(root, query, XPathConstants.NODE); | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
275 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
276 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
277 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
278 * Convenience method to search for a string within a given tree |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
279 * via XPath. |
556 | 280 * |
281 * @param root | |
282 * The root of the tree to be searched in. | |
283 * @param xpath | |
284 * The XPath query. | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
285 * @return The queried string or null if something went |
556 | 286 * wrong during XPath evaluation. |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
287 */ |
556 | 288 public static final String getStringXPath(final Object root, final String xpath) { |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
289 return getStringXPath(root, xpath, null); |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
290 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
291 |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
292 /** |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
293 * Convenience method to search for a string within a given tree |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
294 * via XPath. |
556 | 295 * |
296 * @param root | |
297 * The root of the tree to be searched in. | |
298 * @param query | |
299 * xpath The XPath query. | |
300 * @param def | |
301 * The string to be returned if the search has no results. | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
302 * @return The queried string or the default value if something went |
556 | 303 * wrong during XPath evaluation. |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
304 */ |
556 | 305 public static final String getStringXPath(final Object root, final String query, final String def) { |
306 final String s = (String) getXPath(root, query, XPathConstants.STRING); | |
307 return s == null || s.length() == 0 ? def : s; | |
206
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
308 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
309 } |
33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
310 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |