Class EnvironProperties

All Implemented Interfaces:
Serializable, Cloneable, Map<Object,Object>

public class EnvironProperties extends Properties
Properties with variable substitution.

With this variant of Properties, you can do things like the following:

  name=myname
  path=/path/to/${name}
 
See Also:
  • Constructor Details

    • EnvironProperties

      public EnvironProperties()
      Creates an empty environ list with no default values.
    • EnvironProperties

      public EnvironProperties(Properties defaults)
      Creates an empty environ list with the specified defaults.
      Parameters:
      defaults - the defaults.
  • Method Details

    • setRecursionLevel

      public void setRecursionLevel(int max)
      Sets the maximum recursion level in case there is a loop in property evaluations (for example a=${b}, b=${a}).

      The default value will work for most cases.

      Parameters:
      max - the maximum recursion level.
    • parse

      public String parse(String line) throws ParsingException
      Attempts to parse the contents of a line and perform a variable substitution when an environ-style entry (${property-key}) is found.

      Any occurrence of ${property-key} is replaced by the value of that property key.

      In order to avoid possible infinite loops, while property evaluation is recursive, loops are limited to what is set by setRecursionLevel. Default value should be appropriate for most uses.

      Parameters:
      line - the line to be parsed.
      Returns:
      the line after the variable substitution(s).
      Throws:
      ParsingException - if a syntax error is found.
    • parse

      protected String parse(String line, int indirection_level) throws ParsingException
      Attempts to parse the contents of a line and perform a variable substitution when an environ-style entry (${property-key}) is found.

      Any occurrence of ${property-key} is replaced by the value of that property key.

      In order to avoid possible infinite loops, while property evaluation is recursive, loops are limited to what is set by setRecursionLevel. Default value should be appropriate for most uses.

      Parameters:
      line - the line to be parsed.
      indirection_level - the current recursion level.
      Returns:
      the line after the variable substitution(s).
      Throws:
      ParsingException - if a syntax error is found.
    • setParse

      public void setParse(boolean doparsing)
      Turns on/off parsing when calling getProperty method. By default, does parsing.
      Parameters:
      doparsing - true to activate parsing, false to deactivate.
    • getProperty

      public String getProperty(String key)
      Gets a property attempting environment-variable style substitution. Any occurrence of ${property-key} in the key property value is, in turn, replaced by the value of that property-key.

      If a parsing error occurs, returns the unparsed value.

      Property evaluation is recursive.

      Overrides:
      getProperty in class Properties
      Parameters:
      key - the property key.
      Returns:
      the value in this property list with the specified key value, attempting a variable substitution.
    • getUnparsedProperty

      public String getUnparsedProperty(String key)
      Searches for the property with the specified key in this property list.

      Does not attempt to do any variable substitution.

      Parameters:
      key - the property key.
      Returns:
      the value in this property list with the specified key value.
    • setKeyPrefix

      public void setKeyPrefix(String prefix)
      Set a key prefix to be prepended to all keys used.

      For example, if a "myapp" prefix is set, and the property file looks like:

        myapp.key1=value1
        myapp.key2=value2
        myapp.otherkey=othervalue
       
      Then getting, for example, the "key1" property will return "value1".

      This is useful when you want to keep several property sets in a single file.

      Parameters:
      prefix - the prefix to be prepended (together with a dot). If null, no prefix will be used.