fc.util
Class Template

java.lang.Object
  extended by fc.util.Template

public class Template
extends Object

Provides an ultra-simple template/merge type capability. Can be used standalone or as part of a servlet/cgi environment.

Template description:

Thread safety: This class is not thread safe and higher level synchronization should be used if shared by multiple threads.


Constructor Summary
Template(File templatefile)
          Constructs a new template object
Template(String template)
          Constructs a new template object from the given String.
 
Method Summary
 void fill(String name, boolean value)
          An alias for the set method.
 void fill(String name, int value)
          An alias for the set method.
 void fill(String name, long value)
          An alias for the set method.
 void fill(String name, Object value)
          An alias for the set method.
 void fill(String name, String value)
          An alias for the set method.
 Map getTemplateData()
          Returns the template data, which is a Map of template variables to values (which were all set, using the set(String, String) method.
static void main(String[] args)
          Unit Test History
 void reset()
          Resets the template data.
 void set(String name, String value)
          A template variable can be assigned data using this method.
 String toString()
           
 void write(File destfile)
          Merges and writes the template and data.
 void write(File destfile, boolean overwrite)
          Merges and writes the template and data.
 void write(PrintStream out)
          Merges and writes the template and data to the specified Writer
 void write(Writer out)
          Merges and writes the template and data to the specified Writer
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Template

public Template(File templatefile)
         throws IOException
Constructs a new template object

Parameters:
templatefile - the absolute path to the template file
Throws:
IOException

Template

public Template(String template)
         throws IOException
Constructs a new template object from the given String. Note, this is the the String to use as the template, not the name of a file. Various input streams can be converted into a template using methods from the IOUtil class.

Throws:
IOException
Method Detail

getTemplateData

public Map getTemplateData()
Returns the template data, which is a Map of template variables to values (which were all set, using the set(String, String) method. This map can be modified, as deemed necessary.


reset

public void reset()
Resets the template data. If reusing the template over and over again, this is faster, invoke this between each reuse. (rather than recreate it from scratch every time).


set

public void set(String name,
                String value)
A template variable can be assigned data using this method. This method should be called at least once for every unique template variable in the template.

Parameters:
name - the name of the template variable including the preceding$ sign. For example: $foo
value - the value to assign to the template variable.
Throws:
IllegalArgumentException - if the specified name of the template variable is not syntactically valid.

fill

public void fill(String name,
                 String value)
An alias for the set method.


fill

public void fill(String name,
                 int value)
An alias for the set method.


fill

public void fill(String name,
                 long value)
An alias for the set method.


fill

public void fill(String name,
                 boolean value)
An alias for the set method.


fill

public void fill(String name,
                 Object value)
An alias for the set method.


write

public void write(File destfile)
           throws IOException
Merges and writes the template and data. Always overwrites the specified destination (even if the destionation file already exists);

Parameters:
destfile - the destination file (to write to).
Throws:
IOException

write

public void write(File destfile,
                  boolean overwrite)
           throws IOException
Merges and writes the template and data. Overwrites the specified destination, only if the overwrite flag is specified as true, otherwise no output is written if the specified file already exists.

The check to see whether an existing file is the same as the output file for this template is inherently system dependent. For example, on Windows, say an an existing file "foo.html" exist in the file system. Also suppose that the output file for this template is set to "FOO.html". This template then will then overwrite the data in the existing "foo.html" file but the output filename will not change to "FOO.html". This is because the windows filesystem treats both files as the same and a new File with a different case ("FOO.html") is not created.

Parameters:
destfile - the destination file (to write to).
overwrite - true to overwrite the destination
Throws:
IOException - if an I/O error occurs or if the destination file cannot be written to.

write

public void write(Writer out)
           throws IOException
Merges and writes the template and data to the specified Writer

Throws:
IOException

write

public void write(PrintStream out)
           throws IOException
Merges and writes the template and data to the specified Writer

Throws:
IOException

toString

public String toString()
Overrides:
toString in class Object

main

public static void main(String[] args)
                 throws Exception
Unit Test History
Class Version Tester  Status    Notes
1.0       hj    passed    

Throws:
Exception