Class ConfigurationUtilities
- java.lang.Object
-
- de.julielab.java.utilities.ConfigurationUtilities
-
public class ConfigurationUtilities extends Object
Utilities for the work with commons configuration 2. Note that the dependency for the configuration 2 project is set with scope provided. Thus, the dependency will not automatically be introduced into a project depending on this project. This is done to keep transitive dependencies at a minimum.
-
-
Constructor Summary
Constructors Constructor Description ConfigurationUtilities()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringattrEq(String attribute, String value)Creates the string attr='value' to be used in a predicate.static StringattrEqMultiPred(String operator, String... attributesAndValues)Creates expressions in the form [attr='value' AND attr2='value2'] to be used in predicate XPath expressions.static StringattrEqPred(String attribute, String value)Creates expressions in the form [attr='value'] to be used in predicate XPath expressions.static voidcheckFilesExist(org.apache.commons.configuration2.HierarchicalConfiguration<org.apache.commons.configuration2.tree.ImmutableNode> configuration, String... parameters)Meant for configuration values denoting files.static voidcheckParameters(org.apache.commons.configuration2.HierarchicalConfiguration<org.apache.commons.configuration2.tree.ImmutableNode> configuration, String... parameters)Checks if the configuration keys given with parameters are defined in the passed configuration.static org.apache.commons.configuration2.HierarchicalConfiguration<org.apache.commons.configuration2.tree.ImmutableNode>createEmptyConfiguration()This default implementation constructs an emptyXMLConfigurationwith anXPathExpressionEngineand a UTF-8 encoding.static Stringdot(String... keys)Convenience method for quick concatenation of hierarchical configuration keys.static Stringlast(String path)Appends the [last()] predicate to the given XPath.static org.apache.commons.configuration2.XMLConfigurationloadXmlConfiguration(File configurationFile)Loads the Apache Commons Configuration2XMLConfigurationfrom the given file.static <T> TrequirePresent(String key, Function<String,T> f)This method is meant to check if a configuration has a value for the given key It throws an Exception if not..static Stringslash(String... keys)Convenience method for quick concatenation of hierarchical configuration keys into an XPath expression.static voidwriteConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration<org.apache.commons.configuration2.tree.ImmutableNode> configuration, File destination)Generates the configuration template by callingcreateEmptyConfiguration()and stores the template to destination.static Stringws(String baseElement, String newElement)Concatenates the input elements with a whitespace.
-
-
-
Field Detail
-
LS
public static final String LS
-
-
Method Detail
-
requirePresent
public static <T> T requirePresent(String key, Function<String,T> f) throws org.apache.commons.configuration2.ex.ConfigurationException
This method is meant to check if a configuration has a value for the given key It throws an Exception if not..
To this end, the function f should have the form key -> configuration.getString(key). Getters for other types can also be used.
- Type Parameters:
T- The datatype of the expected configuration value.- Parameters:
key- The configuration key to check for a value.f- The function that does the value checking.- Returns:
- The configuration value if it exists.
- Throws:
org.apache.commons.configuration2.ex.ConfigurationException- If the value does not exist.
-
checkParameters
public static void checkParameters(org.apache.commons.configuration2.HierarchicalConfiguration<org.apache.commons.configuration2.tree.ImmutableNode> configuration, String... parameters) throws org.apache.commons.configuration2.ex.ConfigurationExceptionChecks if the configuration keys given with parameters are defined in the passed configuration.
- Parameters:
configuration- The configuration to check for existing keys.parameters- The keys to check.- Throws:
org.apache.commons.configuration2.ex.ConfigurationException- If a key was not found, i.e. does not have a value (the empty string is a value).
-
checkFilesExist
public static void checkFilesExist(org.apache.commons.configuration2.HierarchicalConfiguration<org.apache.commons.configuration2.tree.ImmutableNode> configuration, String... parameters) throws org.apache.commons.configuration2.ex.ConfigurationExceptionMeant for configuration values denoting files. Checks if the configuration keys given with parameters exist in the passed configuration and makes sure that their values point to existing files.
If a parameter is not defined our its value does not point to an existing file, an Exception with a explaining message will be raised.
- Parameters:
configuration- The config to check for keys and file-path-values.parameters- The configuration keys whose values should point to files.- Throws:
org.apache.commons.configuration2.ex.ConfigurationException- If a key does not exist in configuration or its value does not point to an existing file.
-
loadXmlConfiguration
public static org.apache.commons.configuration2.XMLConfiguration loadXmlConfiguration(File configurationFile) throws org.apache.commons.configuration2.ex.ConfigurationException
Loads the Apache Commons Configuration2XMLConfigurationfrom the given file. By default, theXPathExpressionEngineis set.- Parameters:
configurationFile-- Returns:
- Throws:
org.apache.commons.configuration2.ex.ConfigurationException
-
createEmptyConfiguration
public static org.apache.commons.configuration2.HierarchicalConfiguration<org.apache.commons.configuration2.tree.ImmutableNode> createEmptyConfiguration() throws org.apache.commons.configuration2.ex.ConfigurationExceptionThis default implementation constructs an emptyXMLConfigurationwith anXPathExpressionEngineand a UTF-8 encoding. Note that when using this template generation method, the keys of the configuration must be given in XPath form, i.e. 'key/subkey' instead of the default dotted notation 'key.subkey'.- Returns:
- An empty XMLConfiguration template.
- Throws:
org.apache.commons.configuration2.ex.ConfigurationException- If the template generation fails.
-
writeConfiguration
public static void writeConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration<org.apache.commons.configuration2.tree.ImmutableNode> configuration, File destination) throws org.apache.commons.configuration2.ex.ConfigurationExceptionGenerates the configuration template by callingcreateEmptyConfiguration()and stores the template to destination.- Parameters:
destination- The file path where the template should be written to.- Throws:
org.apache.commons.configuration2.ex.ConfigurationException- It template generation of population fails.
-
dot
public static String dot(String... keys)
Convenience method for quick concatenation of hierarchical configuration keys. When using Apache Commons Configuration, theDefaultExpressionEngineuses dots as configuration path key separation values by default.- Parameters:
keys- Configuration keys to concatenate into a single hierarchical key.- Returns:
- The input keys joined with dots.
-
slash
public static String slash(String... keys)
Convenience method for quick concatenation of hierarchical configuration keys into an XPath expression.- Parameters:
keys- Configuration keys to concatenate into a single hierarchical key.- Returns:
- The input keys joined with slashes for xpath expressions.
-
ws
public static String ws(String baseElement, String newElement)
Concatenates the input elements with a whitespace.- Parameters:
baseElement- The left element.newElement- The right element.- Returns:
- The whitespace-concatenated elements.
-
last
public static String last(String path)
Appends the [last()] predicate to the given XPath.- Parameters:
path-- Returns:
-
attrEqPred
public static String attrEqPred(String attribute, String value)
Creates expressions in the form [attr='value'] to be used in predicate XPath expressions.
- Parameters:
attribute- The attribute name.value- The value to check the attribute for.- Returns:
- The XPath predicate expression.
- See Also:
attrEqMultiPred(String, String...)
-
attrEqMultiPred
public static String attrEqMultiPred(String operator, String... attributesAndValues)
Creates expressions in the form [attr='value' AND attr2='value2'] to be used in predicate XPath expressions.
- Parameters:
operator- 'and' or 'or', case sensitive.attributesAndValues- A sequence of attribute names and the values they are matched against, beginning with an attribute name.- Returns:
- The XPath predicate expression.
-
attrEq
public static String attrEq(String attribute, String value)
Creates the string attr='value' to be used in a predicate. Not that this is not yet an XPath predicate expression due to the missing parenthesis.- Parameters:
attribute- The attribute.value- The value.- Returns:
- The attribute-equals-value XPath expression.
- See Also:
attrEq(String, String),attrEqMultiPred(String, String...)
-
-