freemarker.template
Class AbstractTemplate

java.lang.Object
  |
  +--freemarker.template.AbstractTemplate
All Implemented Interfaces:
Cacheable, java.lang.Cloneable, Compileable, TemplateProcessor
Direct Known Subclasses:
Template, UnparsedTemplate

public abstract class AbstractTemplate
extends java.lang.Object
implements TemplateProcessor, Cacheable, Compileable, java.lang.Cloneable

A base class from which Template implementations are subclassed. This class implements all the interfaces required, and provides some of the basic machinery required to compile and cache a Template.

Version:
$Id: AbstractTemplate.java,v 1.14 2003/11/23 00:12:41 run2000 Exp $
See Also:
Template, UnparsedTemplate

Field Summary
protected  Cache cache
          The Cache to which this template belongs (if any).
 
Constructor Summary
AbstractTemplate()
          Constructs an empty unparsed template.
AbstractTemplate(java.io.File file)
          Constructs an unparsed template by compiling it from a file.
AbstractTemplate(java.io.InputStream stream)
          Constructs an unparsed template by compiling it from an InputStream.
AbstractTemplate(java.io.Reader stream)
          Constructs an unparsed template by compiling it from a Reader.
AbstractTemplate(java.lang.String filePath)
          Constructs an unparsed template by compiling it from a file.
 
Method Summary
 java.lang.Object clone()
          Clones the current template.
 void compileFromFile(java.io.File file)
          Reads an unparsed template from a file, by getting the file's FileInputStream and using it to call compileFromStream(java.io.InputStream), using the platform's default character encoding.
 void compileFromFile(java.lang.String filePath)
          Reads an unparsed template from a file, by getting the file's FileInputStream and using it to call compileFromStream(java.io.InputStream), using the platform's default character encoding.
 void compileFromStream(java.io.InputStream stream)
          Reads the unparsed template from an InputStream, using the platform's default character encoding.
 void compileFromStream(java.io.InputStream stream, java.lang.String encoding)
          Compiles the template from an InputStream, using the specified character encoding.
abstract  void compileFromStream(java.io.Reader stream)
          Compiles the template from a Reader.
 Cache getCache()
          Retrieve the Cache that this object is stored in.
abstract  void process(TemplateModelRoot modelRoot, java.io.Writer out)
          Processes the template, using data from a template model, and outputs the resulting text to a Writer.
abstract  void process(TemplateModelRoot modelRoot, java.io.Writer out, TemplateRuntimeHandler eventHandler)
          Processes the template, using data from the template model, writing any events to the TemplateEventAdapter, and outputs the resulting text to a Writer.
abstract  void process(java.io.Writer out)
          Processes the template, using an empty data model, and outputs the resulting text to a Writer.
 void setCache(Cache cache)
          Sets the Cache that this object is stored in.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cache

protected Cache cache
The Cache to which this template belongs (if any).
Constructor Detail

AbstractTemplate

public AbstractTemplate()
Constructs an empty unparsed template.

AbstractTemplate

public AbstractTemplate(java.lang.String filePath)
                 throws java.io.IOException
Constructs an unparsed template by compiling it from a file. Calls compileFromFile(String).
Parameters:
filePath - the absolute path of the template file to be compiled.

AbstractTemplate

public AbstractTemplate(java.io.File file)
                 throws java.io.IOException
Constructs an unparsed template by compiling it from a file. Calls compileFromFile(java.io.File).
Parameters:
file - a File representing the template file to be compiled.

AbstractTemplate

public AbstractTemplate(java.io.InputStream stream)
                 throws java.io.IOException
Constructs an unparsed template by compiling it from an InputStream. Calls compileFromStream(java.io.InputStream).
Parameters:
stream - an InputStream from which the template can be read.

AbstractTemplate

public AbstractTemplate(java.io.Reader stream)
                 throws java.io.IOException
Constructs an unparsed template by compiling it from a Reader. Calls compileFromStream(java.io.InputStream).
Parameters:
stream - a Reader from which the template can be read.
Method Detail

compileFromFile

public void compileFromFile(java.lang.String filePath)
                     throws java.io.IOException
Reads an unparsed template from a file, by getting the file's FileInputStream and using it to call compileFromStream(java.io.InputStream), using the platform's default character encoding.
Parameters:
filePath - the absolute path of the template file to be compiled.

compileFromFile

public void compileFromFile(java.io.File file)
                     throws java.io.IOException
Reads an unparsed template from a file, by getting the file's FileInputStream and using it to call compileFromStream(java.io.InputStream), using the platform's default character encoding.
Parameters:
file - a File representing the template file to be compiled.

compileFromStream

public void compileFromStream(java.io.InputStream stream)
                       throws java.io.IOException
Reads the unparsed template from an InputStream, using the platform's default character encoding. If the template has already been compiled, this method does nothing. Calls compileFromStream(java.io.Reader) to perform parsing.
Specified by:
compileFromStream in interface Compileable
Parameters:
stream - an InputStream from which the template can be read.

compileFromStream

public void compileFromStream(java.io.InputStream stream,
                              java.lang.String encoding)
                       throws java.io.IOException
Compiles the template from an InputStream, using the specified character encoding. If the template has already been compiled, this method does nothing. Calls compileFromStream(java.io.Reader) to perform parsing.
Specified by:
compileFromStream in interface Compileable
Parameters:
stream - an InputStream from which the template can be read.
encoding - the character encoding of the input stream

compileFromStream

public abstract void compileFromStream(java.io.Reader stream)
                                throws java.io.IOException
Compiles the template from a Reader. If the template has already been compiled, this method does nothing.
Parameters:
stream - an Reader from which the template can be read.

setCache

public void setCache(Cache cache)
Sets the Cache that this object is stored in.
Specified by:
setCache in interface Cacheable
Parameters:
cache - the Cache that this template belongs to.

getCache

public Cache getCache()
Retrieve the Cache that this object is stored in.
Specified by:
getCache in interface Cacheable
Returns:
the Cache that this template belongs to.

clone

public java.lang.Object clone()

Clones the current template.

Cloning is used in Caches, whenever we need to create a new template: rather than simply creating a new Template, we ask a TemplateRegistry to create one for us. TemplateRegistry uses the clone function to take an existing template, copy it, and return the copy to the cache, where it is then populated.

Specified by:
clone in interface Cacheable
Overrides:
clone in class java.lang.Object
Returns:
a copy of the current template.

process

public abstract void process(TemplateModelRoot modelRoot,
                             java.io.Writer out,
                             TemplateRuntimeHandler eventHandler)
                      throws java.io.IOException
Processes the template, using data from the template model, writing any events to the TemplateEventAdapter, and outputs the resulting text to a Writer.
Specified by:
process in interface TemplateProcessor
Parameters:
modelRoot - the root node of the data model.
out - a Writer to send the output to.
eventHandler - a TemplateEventAdapter for handling any events that occur during processing.

process

public abstract void process(TemplateModelRoot modelRoot,
                             java.io.Writer out)
                      throws java.io.IOException
Processes the template, using data from a template model, and outputs the resulting text to a Writer.
Parameters:
modelRoot - the root node of the data model. If null, an empty data model is used.
out - a Writer to output the text to.

process

public abstract void process(java.io.Writer out)
                      throws java.io.IOException
Processes the template, using an empty data model, and outputs the resulting text to a Writer.
Parameters:
out - a Writer to output the text to.