Class XmlConfiguration

java.lang.Object
com.github.hypfvieh.config.xml.XmlConfiguration

public class XmlConfiguration extends Object
Slim replacement for commons-configuration2 XMLConfiguration. This class handles configurations saved as XML. It does not do DTD/XSD validation.
Since:
v1.0.1 - 2018-01-15
Author:
hypfvieh
  • Method Details

    • getKeyDelimiter

      public String getKeyDelimiter()
      Get the delimiting key (default is '/').
      Returns:
      current delimiting key
    • setKeyDelimiter

      public void setKeyDelimiter(String _keyDelimiter)
      Set the delimiting key.
      If null is given, default ('/') will be used.
      Parameters:
      _keyDelimiter - delimiter to use
    • keyCount

      public int keyCount()
      Get the amount of nodes in config.
      Returns:
      nodes in document
    • getString

      public String getString(String _key)
      Get the String value behind the given key. Shortcut of getString(String, null).
      Parameters:
      _key - key to look for
      Returns:
      value stored in config or null if value not found
    • getString

      public String getString(String _key, String _default)
      Get the string value behind the given key or return default.
      Will first try to pick up key from environment variables (if enabled),
      if that fails, config will be used.
      If this also fails, default is returned.
      Parameters:
      _key - key to look for
      _default - default to return if key not found
      Returns:
      value stored in environment/config or default if key not found
    • getInt

      public int getInt(String _key, int _default)
      Get a configuration value as int. If _key not found, or not an integer, default is returned.
      Parameters:
      _key - key to read
      _default - default to return if key not found/invalid
      Returns:
      value as int or default
    • getDouble

      public double getDouble(String _key, double _default)
      Retrieve a configuration value as double. If _key could not be found, or value was not of type 'double', default is returned.
      Parameters:
      _key - key to read
      _default - default to return if key not found/invalid
      Returns:
      value as double or default
    • getBoolean

      public boolean getBoolean(String _key, boolean _default)
      Retrieve a configuration value as boolean. If _key could not be found, default is returned.
      All other values will be tried to read as boolean.

      Considered true values are:
      • 1
      • y
      • j
      • ja
      • yes
      • true
      • enabled
      • enable
      • active
      All other values are considered to be false.
      Parameters:
      _key - key to read
      _default - default to return if key not found/invalid
      Returns:
      boolean or default value
    • setString

      public void setString(String _key, boolean _asAttribute, String _value)
      Set string value of given key
      Parameters:
      _key - key to write
      _asAttribute - set this as attribute instead of node
      _value - value to write
    • getStringList

      public List<String> getStringList(String _key)
      Returns the results of key as ArrayList.
      Parameters:
      _key - key to read
      Returns:
      never null, maybe empty list
    • getStringSet

      public Set<String> getStringSet(String _key, Class<? extends Set> _setClass)
      Returns a result as Set of the given Set-Subclass. If given Set-Class is null or could not be instantiated, TreeSet is used.
      Parameters:
      _key - key to read
      _setClass - Set class to use
      Returns:
      set of string maybe empty
    • setValues

      public void setValues(Map<String,String> _values, boolean _asAttributes)
      Set keys found in map to value found in map.
      Parameters:
      _values - values to write
      _asAttributes - write values as attributes instead of nodes
    • save

      public void save() throws IOException
      Save config file. Will replace input file!
      Throws:
      IOException - if file writing fails
    • save

      public void save(BiConsumer<Document,OutputStream> _outputGenerator) throws IOException
      Save config using BiConsumer. This allows transforming/formatting of output before it is saved to the configured output stream.
      Parameters:
      _outputGenerator - lambda to modify output with before writing
      Throws:
      IOException - if writing fails
    • getInputFile

      public File getInputFile()
      Get file which is used as input
      Returns:
      file, maybe null if input is read from stream