org.writersforge.catalan.transform
Class VariableTextReplacer

java.lang.Object
  |
  +--org.writersforge.catalan.transform.BaseNodeProcessor
        |
        +--org.writersforge.catalan.transform.TextProcessor
              |
              +--org.writersforge.catalan.transform.VariableTextReplacer
All Implemented Interfaces:
org.writersforge.bellows.traverse.NodeProcessor

public class VariableTextReplacer
extends TextProcessor

A text processor which performs dynamic text replacement by looking up specially formatted variables and resolving them against a Map of variable names to text values. The variable markup defaults to UNIX shell-style "${variable}", but can be changed to other formats, for example Ant-style "@variable@".

The following processor will convert strings like "Hello, my name is %[firstname] %[lastname]." to "Hello, my name is Joe Smith."

 Map lookup = new HashMap ();
 lookup.put ("firstname", "Joe");
 lookup.put ("lastname", "Smith");
 
 NodeProcessor nameReplacer = new VariableTextReplacer (lookup);
 nameReplacer.setStartToken ("%[");
 nameReplacer.setEndToken ("]");

This processor accepts only String input, and produces only String output.

Author:
jsheets

Constructor Summary
VariableTextReplacer(java.util.Map lookup)
          Creates a new instance of VariableTextReplacer.
 
Method Summary
protected  java.util.List processText(java.lang.String text)
          Processes the text node.
 void setEndToken(java.lang.String endToken)
          Assigns the token used to end a variable.
 void setStartToken(java.lang.String startToken)
          Assigns the token used to start a variable.
 void setSwallowUndefined(boolean swallowUndefined)
          Determines the behavior for processing variables that aren't defined in lookup map.
 java.lang.String toString()
          Convert this object to a String value.
 
Methods inherited from class org.writersforge.catalan.transform.TextProcessor
processNode
 
Methods inherited from class org.writersforge.catalan.transform.BaseNodeProcessor
addLeftover, end, getLeftovers, getNodes, start
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

VariableTextReplacer

public VariableTextReplacer(java.util.Map lookup)
Creates a new instance of VariableTextReplacer.

Parameters:
lookup - the variable lookup mapping
Method Detail

setSwallowUndefined

public void setSwallowUndefined(boolean swallowUndefined)
Determines the behavior for processing variables that aren't defined in lookup map. A value of true will preserve the undefined variable untouched in the output; a value of false will delete the entire variable instance from the output. This is most useful for multiple pass variable resolvers. The first and middle passes should not swallow undefined variables so later passes can handle them; the final pass should swallow undefined variables so they don't show up in the final output.

By default, undefined variables are not swallowed.

Parameters:
swallowUndefined - false to pass undefs through untouched, or true to delete them

setStartToken

public void setStartToken(java.lang.String startToken)
Assigns the token used to start a variable. Defaults to "${".

Parameters:
startToken - starting token text

setEndToken

public void setEndToken(java.lang.String endToken)
Assigns the token used to end a variable. Defaults to "}".

Parameters:
endToken - ending token text

processText

protected java.util.List processText(java.lang.String text)
Processes the text node. Not called for non-String nodes.

Specified by:
processText in class TextProcessor
Parameters:
text - the text to process
Returns:
the processed text

toString

public java.lang.String toString()
Convert this object to a String value.

Overrides:
toString in class java.lang.Object
Returns:
stringified object