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:
The keys and values in the supplied Map object must be String objects.
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)); } } }
VariableDereferencer
,
VariableSubstituter
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.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getVariableValue(java.lang.String varName,
java.lang.Object context)
Get the value associated with a given variable.
|
public MapVariableDereferencer(java.util.Map<java.lang.String,java.lang.String> map)
map
- The Map object from which to resolve
variable references.public MapVariableDereferencer(java.util.Properties properties)
properties
- The Properties object from which to resolve
variable references.public java.lang.String getVariableValue(java.lang.String varName, java.lang.Object context)
getVariableValue
in interface VariableDereferencer
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.