# HG changeset patch # User gernotbelger # Date 1530037546 -7200 # Node ID ba31ca213c8804ebfea17ea123c2459ef794a42d # Parent 8096dc2afb945683b81db2148d716b9b040c1893 loadConfig static method diff -r 8096dc2afb94 -r ba31ca213c88 artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/Config.java --- a/artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/Config.java Thu Mar 01 12:11:07 2018 +0100 +++ b/artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/Config.java Tue Jun 26 20:25:46 2018 +0200 @@ -10,20 +10,20 @@ import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Properties; import javax.xml.namespace.QName; - import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; - import javax.xml.xpath.XPathConstants; import org.apache.log4j.Logger; - import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; - import org.xml.sax.SAXException; /** @@ -33,8 +33,7 @@ * * @author Sascha L. Teichmann */ -public final class Config -{ +public final class Config { private static Logger logger = Logger.getLogger(Config.class); /** @@ -46,22 +45,19 @@ * Default path to the configuration directory if none * was specified by the CONFIG_DIR system property. */ - public static final File CONFIG_DIR_DEFAULT = - new File(new File(System.getProperty("user.home", - System.getProperty("user.dir", "."))), ".artifactdb"); + public static final File CONFIG_DIR_DEFAULT = new File(new File(System.getProperty("user.home", System.getProperty("user.dir", "."))), ".artifactdb"); /** * Name of the central configuration XML file. */ - public static final String CONFIG_FILE = "conf.xml"; + public static final String CONFIG_FILE = "conf.xml"; /** * Name of the configuration filename alias to be use * within the configuration. This alias is replaced by * the real path. */ - public static final String CONFIG_DIR_PLACEHOLDER = - "${artifacts.config.dir}"; + public static final String CONFIG_DIR_PLACEHOLDER = "${artifacts.config.dir}"; private static Document config; @@ -70,6 +66,7 @@ /** * Singleton access to the central XML configuration document. + * * @return The central XML configuration document. */ public static synchronized final Document getConfig() { @@ -79,18 +76,34 @@ return config; } + public static Properties loadProperties(final String CONFIG_FILE_LOCAL) { + final File configDir = getConfigDirectory(); + final File configFile = new File(configDir, CONFIG_FILE_LOCAL); + + final Properties properties = new Properties(); + InputStreamReader reader; + try { + reader = new InputStreamReader(Files.newInputStream(configFile.toPath()), StandardCharsets.ISO_8859_1); + properties.load(reader); + } + catch (final IOException e) { + e.printStackTrace(); + } + + return properties; + } + /** * Returns the path to the configuartion directory. If a path * was specified via the CONFIG_DIR system property this one * is used. Else it falls back to default configuration path. + * * @return The path to the configuartion directory. */ public static File getConfigDirectory() { - String configDirString = System.getProperty(CONFIG_DIR); + final String configDirString = System.getProperty(CONFIG_DIR); - File configDir = configDirString != null - ? new File(configDirString) - : CONFIG_DIR_DEFAULT; + File configDir = configDirString != null ? new File(configDirString) : CONFIG_DIR_DEFAULT; if (!configDir.isDirectory()) { logger.warn("'" + configDir + "' is not a directory."); @@ -103,40 +116,40 @@ /** * Replaces the CONFIG_DIR_PLACEHOLDER alias with the real path * of the configuration directory. - * @param path The path containing the CONFIG_DIR_PLACEHOLDER placeholder. + * + * @param path + * The path containing the CONFIG_DIR_PLACEHOLDER placeholder. * @return The path where the CONFIG_DIR_PLACEHOLDER placeholders are - * replaced by the real path name. + * replaced by the real path name. */ - public static String replaceConfigDir(String path) { - String configDir = getConfigDirectory().getAbsolutePath(); + public static String replaceConfigDir(final String path) { + final String configDir = getConfigDirectory().getAbsolutePath(); return path.replace(CONFIG_DIR_PLACEHOLDER, configDir); } private static Document loadConfig() { - File configDir = getConfigDirectory(); + final File configDir = getConfigDirectory(); - File file = new File(configDir, CONFIG_FILE); + final File file = new File(configDir, CONFIG_FILE); if (!file.canRead() && !file.isFile()) { - logger.error("Cannot read config file '" - + file + "'."); + logger.error("Cannot read config file '" + file + "'."); return null; } try { - DocumentBuilderFactory factory = - DocumentBuilderFactory.newInstance(); + final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); // XXX: This may change in future. return factory.newDocumentBuilder().parse(file); } - catch (SAXException se) { + catch (final SAXException se) { logger.error(se.getLocalizedMessage(), se); } - catch (ParserConfigurationException pce) { + catch (final ParserConfigurationException pce) { logger.error(pce.getLocalizedMessage(), pce); } - catch (IOException ioe) { + catch (final IOException ioe) { logger.error(ioe.getLocalizedMessage()); } @@ -146,131 +159,152 @@ /** * Convenience method to search within a given document tree via XPath. * See {@link XMLUtils#xpath(Object, String, QName) } for details. - * @param root The object which is used as the root of the tree to - * be searched in. - * @param query The XPath query. - * @param returnType The type of the result. + * + * @param root + * The object which is used as the root of the tree to + * be searched in. + * @param query + * The XPath query. + * @param returnType + * The type of the result. * @return The result of type 'returnTyp' or null if something went - * wrong during XPath evaluation. + * wrong during XPath evaluation. */ - public static final Object getXPath( - Object root, String query, QName returnType - ) { + public static final Object getXPath(final Object root, final String query, final QName returnType) { return XMLUtils.xpath(root, query, returnType); } /** * Convenience method to search within the central configuration via XPath. * See {@link XMLUtils#xpath(Object, String, QName) } for details. - * @param query The XPath query. - * @param returnType The type of the result. + * + * @param query + * The XPath query. + * @param returnType + * The type of the result. * @return The result of type 'returnTyp' or null if something went - * wrong during XPath evaluation. + * wrong during XPath evaluation. */ - public static final Object getXPath(String query, QName returnType) { + public static final Object getXPath(final String query, final QName returnType) { return XMLUtils.xpath(getConfig(), query, returnType); } /** * Convenience method to search for a node list within the central * configuation document via XPath. - * @param query The XPath query. + * + * @param query + * The XPath query. * @return The queried node list or null if something went - * wrong during XPath evaluation. + * wrong during XPath evaluation. */ - public static final NodeList getNodeSetXPath(String query) { - return (NodeList)getXPath(query, XPathConstants.NODESET); + public static final NodeList getNodeSetXPath(final String query) { + return (NodeList) getXPath(query, XPathConstants.NODESET); } /** * Convenience method to search for a node within the central * configuation document via XPath. - * @param query The XPath query. + * + * @param query + * The XPath query. * @return The queried node or null if something went - * wrong during XPath evaluation. + * wrong during XPath evaluation. */ - public static final Node getNodeXPath(String query) { - return (Node)getXPath(query, XPathConstants.NODE); + public static final Node getNodeXPath(final String query) { + return (Node) getXPath(query, XPathConstants.NODE); } /** * Convenience method to search for a string within the central * configuation document via XPath. - * @param xpath The XPath query. + * + * @param xpath + * The XPath query. * @return The queried string or null if something went - * wrong during XPath evaluation. + * wrong during XPath evaluation. */ - public static final String getStringXPath(String xpath) { + public static final String getStringXPath(final String xpath) { return getStringXPath(xpath, null); } /** * Convenience method to search for a string within the central * configuation document via XPath. - * @param query The XPath query. - * @param def The string to be returned if the search has no results. + * + * @param query + * The XPath query. + * @param def + * The string to be returned if the search has no results. * @return The queried string or the default value if something went - * wrong during XPath evaluation. + * wrong during XPath evaluation. */ - 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 String getStringXPath(final String query, final String def) { + final String s = (String) getXPath(query, XPathConstants.STRING); + return s == null || s.length() == 0 ? def : s; } /** * Convenience method to search for a node list within a given tree * via XPath. - * @param root The root of the tree to be searched in. - * @param query The XPath query. + * + * @param root + * The root of the tree to be searched in. + * @param query + * The XPath query. * @return The queried node list or null if something went - * wrong during XPath evaluation. + * wrong during XPath evaluation. */ - public static final NodeList getNodeSetXPath(Object root, String query) { - return (NodeList)getXPath(root, query, XPathConstants.NODESET); + public static final NodeList getNodeSetXPath(final Object root, final String query) { + return (NodeList) getXPath(root, query, XPathConstants.NODESET); } /** * Convenience method to search for a node within a given tree * via XPath. - * @param root The root of the tree to be searched in. - * @param query The XPath query. + * + * @param root + * The root of the tree to be searched in. + * @param query + * The XPath query. * @return The queried node or null if something went - * wrong during XPath evaluation. + * wrong during XPath evaluation. */ - public static final Node getNodeXPath(Object root, String query) { - return (Node)getXPath(root, query, XPathConstants.NODE); + public static final Node getNodeXPath(final Object root, final String query) { + return (Node) getXPath(root, query, XPathConstants.NODE); } /** * Convenience method to search for a string within a given tree * via XPath. - * @param root The root of the tree to be searched in. - * @param xpath The XPath query. + * + * @param root + * The root of the tree to be searched in. + * @param xpath + * The XPath query. * @return The queried string or null if something went - * wrong during XPath evaluation. + * wrong during XPath evaluation. */ - public static final String getStringXPath(Object root, String xpath) { + public static final String getStringXPath(final Object root, final String xpath) { return getStringXPath(root, xpath, null); } /** * Convenience method to search for a string within a given tree * via XPath. - * @param root The root of the tree to be searched in. - * @param query xpath The XPath query. - * @param def The string to be returned if the search has no results. + * + * @param root + * The root of the tree to be searched in. + * @param query + * xpath The XPath query. + * @param def + * The string to be returned if the search has no results. * @return The queried string or the default value if something went - * wrong during XPath evaluation. + * wrong during XPath evaluation. */ - public static final String getStringXPath( - Object root, String query, String def - ) { - String s = (String)getXPath(root, query, XPathConstants.STRING); - return s == null || s.length() == 0 - ? def - : s; + public static final String getStringXPath(final Object root, final String query, final String def) { + final String s = (String) getXPath(root, query, XPathConstants.STRING); + return s == null || s.length() == 0 ? def : s; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :