info.informatica.util
Class EnvironProperties

java.lang.Object
  extended by java.util.Dictionary<K,V>
      extended by java.util.Hashtable<Object,Object>
          extended by java.util.Properties
              extended by info.informatica.util.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}
 

Version:
1.02
Author:
amengual at informatica dot info
See Also:
Serialized Form

Constructor Summary
EnvironProperties()
          Creates an empty environ list with no default values.
EnvironProperties(Properties defaults)
          Creates an empty environ list with the specified defaults.
 
Method Summary
 String getProperty(String key)
          Gets a property attempting environment-variable style substitution.
 String getUnparsedProperty(String key)
          Searches for the property with the specified key in this property list.
 String parse(String line)
          Attempts to parse the contents of a line and perform a variable substitution when an environ-style entry (${property-key}) is found.
 void setKeyPrefix(String prefix)
          Set a key prefix to be prepended to all keys used.
 void setParse(boolean doparsing)
          Turns on/off parsing when calling getProperty method.
 void setRecursionLevel(int max)
          Sets the maximum recursion level in case there is a loop in property evaluations (for example a=${b}, b=${a}).
 
Methods inherited from class java.util.Properties
getProperty, list, list, load, load, loadFromXML, propertyNames, save, setProperty, store, store, storeToXML, storeToXML, stringPropertyNames
 
Methods inherited from class java.util.Hashtable
clear, clone, contains, containsKey, containsValue, elements, entrySet, equals, get, hashCode, isEmpty, keys, keySet, put, putAll, remove, size, toString, values
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

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 Detail

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.

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.