com.ls.util.text
Class ReplacementMap

java.lang.Object
  |
  +--java.util.AbstractMap
        |
        +--java.util.HashMap
              |
              +--com.ls.util.text.ReplacementMap
All Implemented Interfaces:
Cloneable, Map, Serializable

public class ReplacementMap
extends HashMap

Used to replace variables in strings with values. Variables are put in the ReplacementMap as "Map-keys", the corresponding values as "Map-values". Then the replace() method can be called, which returns a string, where each variable is replaced by its value (or throws a ParseException). '$' is the character, that denotes the start of a variable. If the variable name consists of more than one letter, it needs to be surrounded by '{' and '}'. Variable names are case sensitive.

Usage example:

 ReplacementMap replacementMap = new ReplacementMap();
 replacementMap.put("a", "value for 'a'");
 replacementMap.put("a2", "value for 'a2'");
 replacementMap.put("b", "unused value for 'b'");

 try {
     System.out.println(replacementMap.replace("this String contains the variables a=$a and a2=${a2}"));
 } catch (ParseException pex) {
     System.err.println(pex.toString());
 }
 
The output of this example would be
 this String contains the variables a=value for 'a' and a2=value for 'a2'
 
Note: The string argument of replace() is parsed only once, so '$' characters in the values of the ReplacementMap are not replaced!

Version:
$Revision: 1.3 $
Author:
Last modified by $Author: MFehrenbach $
See Also:
Serialized Form

Inner classes inherited from class java.util.Map
Map.Entry
 
Constructor Summary
ReplacementMap()
           
 
Method Summary
 List replace(List encodedList)
          Decodes all variables defined in this ReplacementMap in the given argument. Note, that variables are recognized by their leading '$' character.
 Map replace(Map encodedMap)
          Decodes all variables defined in this ReplacementMap in the given argument. Note, that variables are recognized by their leading '$' character.
 String replace(String encodedString)
          Decodes all variables defined in this ReplacementMap in the given argument. Note, that variables are recognized by their leading '$' character.
 
Methods inherited from class java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, get, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

ReplacementMap

public ReplacementMap()
Method Detail

replace

public String replace(String encodedString)
               throws ParseException
Decodes all variables defined in this ReplacementMap in the given argument.

Note, that variables are recognized by their leading '$' character.

Parameters:
encodedString - string to decode
Returns:
the decoded string
Throws:
ParseException - If no closing '}' for a variable embedded in '{' and '}' is found or if a variable if found in the endcoded string that is not stored in the ReplacementMap or if the character denoting the start of a variable is the last character in the encoded string

replace

public List replace(List encodedList)
             throws ParseException
Decodes all variables defined in this ReplacementMap in the given argument.

Note, that variables are recognized by their leading '$' character.

Parameters:
encodedList - list to decode
Returns:
the decoded list
Throws:
ParseException - If no closing '}' for a variable embedded in '{' and '}' is found or if a variable if found in the endcoded string that is not stored in the ReplacementMap or if the character denoting the start of a variable is the last character in the encoded string

replace

public Map replace(Map encodedMap)
            throws ParseException
Decodes all variables defined in this ReplacementMap in the given argument.

Note, that variables are recognized by their leading '$' character.

Parameters:
encodedMap - list to decode
Returns:
the decoded map
Throws:
ParseException - If no closing '}' for a variable embedded in '{' and '}' is found or if a variable is found in the endcoded string that is not stored in the ReplacementMap or if the character denoting the start of a variable is the last character in the encoded string