Class 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.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String LS  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String attrEq​(String attribute, String value)
      Creates the string attr='value' to be used in a predicate.
      static String attrEqMultiPred​(String operator, String... attributesAndValues)
      Creates expressions in the form [attr='value' AND attr2='value2'] to be used in predicate XPath expressions.
      static String attrEqPred​(String attribute, String value)
      Creates expressions in the form [attr='value'] to be used in predicate XPath expressions.
      static void checkFilesExist​(org.apache.commons.configuration2.HierarchicalConfiguration<org.apache.commons.configuration2.tree.ImmutableNode> configuration, String... parameters)
      Meant for configuration values denoting files.
      static void checkParameters​(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 empty XMLConfiguration with an XPathExpressionEngine and a UTF-8 encoding.
      static String dot​(String... keys)
      Convenience method for quick concatenation of hierarchical configuration keys.
      static String last​(String path)
      Appends the [last()] predicate to the given XPath.
      static org.apache.commons.configuration2.XMLConfiguration loadXmlConfiguration​(File configurationFile)
      Loads the Apache Commons Configuration2 XMLConfiguration from the given file.
      static <T> T requirePresent​(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 String slash​(String... keys)
      Convenience method for quick concatenation of hierarchical configuration keys into an XPath expression.
      static void writeConfiguration​(org.apache.commons.configuration2.HierarchicalConfiguration<org.apache.commons.configuration2.tree.ImmutableNode> configuration, File destination)
      Generates the configuration template by calling createEmptyConfiguration() and stores the template to destination.
      static String ws​(String baseElement, String newElement)
      Concatenates the input elements with a whitespace.
    • Field Detail

      • LS

        public static final String LS
    • Constructor Detail

      • ConfigurationUtilities

        public ConfigurationUtilities()
    • 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.ConfigurationException

        Checks 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.ConfigurationException

        Meant 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 Configuration2 XMLConfiguration from the given file. By default, the XPathExpressionEngine is 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.ConfigurationException
        This default implementation constructs an empty XMLConfiguration with an XPathExpressionEngine and 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.ConfigurationException
        Generates the configuration template by calling createEmptyConfiguration() 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, the DefaultExpressionEngine uses 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...)