org.clapper.util.text
Class MapVariableDereferencer
- java.lang.Object
-
- org.clapper.util.text.MapVariableDereferencer
-
- All Implemented Interfaces:
- VariableDereferencer
public class MapVariableDereferencer extends java.lang.Object implements VariableDereferencer
The MapVariableDereferencer class implements the VariableDereferencer interface and resolves variable references by looking them up in a supplied Map object. By using a Map object, this class can support variable lookups from a variety of existing data structures, including:
- java.util.HashMap and java.util.TreeMap objects
- java.util.Hashtable objects
- java.util.Properties objects
The keys and values in the supplied Map object must be String objects.
Example
Perhaps the simplest example is one that uses the environment variables of the running Java VM. (Recall that java.lang.System.getenv() is no longer deprecated as of the 1.5 JDK.) The following sample program reads strings from the command line and substitutes Unix-style environment variable references.
import org.clapper.util.text.MapVariableDereferencer; import org.clapper.util.text.VariableDereferencer; import org.clapper.util.text.VariableSubstituter; import org.clapper.util.text.UnixShellVariableSubstituter; public class Test { public static void main (String[] args) throws Throwable { VariableDereference vars = new MapVariableDereferencer (System.getenv()); VariableSubstituter sub = new UnixShellVariableSubstituter(); for (int i = 0; i < args.length; i++) { System.out.println ("BEFORE: \"" + args[i] + "\""); System.out.println ("AFTER: \"" + sub.substitute (args[i], vars, null)); } } }- See Also:
VariableDereferencer,VariableSubstituter
-
-
Constructor Summary
Constructors Constructor and Description MapVariableDereferencer(java.util.Map<java.lang.String,java.lang.String> map)Create a new MapVariableDereferencer object that resolves its variable references from the specified Map object.MapVariableDereferencer(java.util.Properties properties)Create a new MapVariableDereferencer object that resolves its variable references from the specified Properties object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description java.lang.StringgetVariableValue(java.lang.String varName, java.lang.Object context)Get the value associated with a given variable.
-
-
-
Constructor Detail
-
MapVariableDereferencer
public MapVariableDereferencer(java.util.Map<java.lang.String,java.lang.String> map)
Create a new MapVariableDereferencer object that resolves its variable references from the specified Map object.- Parameters:
map- The Map object from which to resolve variable references.
-
MapVariableDereferencer
public MapVariableDereferencer(java.util.Properties properties)
Create a new MapVariableDereferencer object that resolves its variable references from the specified Properties object.- Parameters:
properties- The Properties object from which to resolve variable references.
-
-
Method Detail
-
getVariableValue
public java.lang.String getVariableValue(java.lang.String varName, java.lang.Object context)Get the value associated with a given variable.- Specified by:
getVariableValuein interfaceVariableDereferencer- Parameters:
varName- The name of the variable for which the value is desired.context- a context object, passed through from the caller to the dereferencer, or null if there isn't one. Ignored here.- Returns:
- The variable's value. If the variable has no value, this method must return null.
-
-
DMelt 3.0 © DataMelt by jWork.ORG