sdsu.util
Class Template

java.lang.Object
  |
  +--sdsu.util.Template

public class Template
extends java.lang.Object

A Template is an document containing variables. This class does the variable substitution and replaces with given values. If a variable is not given a value, it and the variable marker is removed from the template. Variables in denoted in the template by preceeding and following them by a special string, the variableMarker. The default value for the variableMarker is @@@. The creator of the template must insure this string does not occur as text in the template.

Version:
1.0.1 18 July 1997
Author:
Roger Whitney (whitney@cs.sdsu.edu)

Constructor Summary
Template(java.lang.String templateText)
          Creates a template object using the given text
 
Method Summary
 void clear()
          Clears all variables.
static Template fromFile(java.lang.String fileName)
          Reads the text for a template from the given file.
 void replace(java.util.Hashtable variablesAndValues)
          Replaces all occurances of the given variables with given values.
 void replace(java.lang.String variableName, java.lang.String variableValue)
          Replaces all occurances of the given variable with given string.
 void setVariableMarker(java.lang.String newMarker)
          Changes the value of the variable marker.
 java.lang.String toString()
          Returns the template with all variables subsituted for the given values.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Template

public Template(java.lang.String templateText)
Creates a template object using the given text
Method Detail

fromFile

public static Template fromFile(java.lang.String fileName)
                         throws java.io.FileNotFoundException,
                                java.io.IOException
Reads the text for a template from the given file.
Throws:
java.io.FileNotFoundException - if fileName can not be found
java.io.IOException - on read error when reading file

clear

public void clear()
Clears all variables. Any values given to them are removed.

replace

public void replace(java.lang.String variableName,
                    java.lang.String variableValue)
Replaces all occurances of the given variable with given string.

replace

public void replace(java.util.Hashtable variablesAndValues)
Replaces all occurances of the given variables with given values.
Parameters:
variablesAndValues - a hashtable whose keys are used as the variables and values are used as values of the variables. Keys and values are converted to strings before using.

setVariableMarker

public void setVariableMarker(java.lang.String newMarker)
Changes the value of the variable marker. Variables in the template are preceeded and followed by a special string, the variable marker. Default value is @@@.
Parameters:
newMarker - the new value for the variable marker

toString

public java.lang.String toString()
Returns the template with all variables subsituted for the given values. If a variable is not given a value, it is removed from the template string
Overrides:
toString in class java.lang.Object