org.clapper.util.text
Class MapVariableDereferencer

java.lang.Object
  extended by 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:

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));
         }
     }
 }
 

Version:
$Revision: 6735 $
Author:
Copyright © 2004-2007 Brian M. Clapper
See Also:
VariableDereferencer, VariableSubstituter

Constructor Summary
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
 java.lang.String getVariableValue(java.lang.String varName, java.lang.Object context)
          Get the value associated with a given variable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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:
getVariableValue in interface VariableDereferencer
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.


Copyright © 2004-2007 Brian M. Clapper. All Rights Reserved.