freemarker.template
Class AbstractTemplate

java.lang.Object
  extended by freemarker.template.AbstractTemplate
All Implemented Interfaces:
Cacheable, Compileable, TemplateProcessor, java.lang.Cloneable
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 1162 2013-04-20 12:04:32Z run2000 $
See Also:
Template, UnparsedTemplate

Nested Class Summary
 
Nested classes/interfaces inherited from interface freemarker.template.TemplateProcessor
TemplateProcessor.ExitStatus
 
Field Summary
protected  Cache cache
          The Cache to which this template belongs (if any).
 
Constructor Summary
protected AbstractTemplate()
           
  AbstractTemplate(java.io.Reader reader)
          Constructs a template by compiling it from a Reader.
  AbstractTemplate(java.io.Reader reader, Cache cache)
          Constructs a template by compiling it from a Reader.
 
Method Summary
 java.lang.Object clone()
          Clones the current template.
protected abstract  void compile(java.io.Reader reader)
          Compiles the template from a Reader.
 Cache getCache()
          Retrieve the Cache that this object is stored in.
protected static java.lang.String getTemplateText(java.io.Reader reader)
          Takes the given Reader, reads it until the end of the stream, and accumulates the contents in a String.
abstract  TemplateProcessor.ExitStatus process(TemplateProcessorParameters p)
          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(TemplateWriteableHashModel 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(java.io.Writer out)
          Processes the template, using an empty data model, and outputs the resulting text to a Writer.
 
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

protected AbstractTemplate()

AbstractTemplate

public AbstractTemplate(java.io.Reader reader)
                 throws java.io.IOException,
                        java.lang.IllegalArgumentException
Constructs a template by compiling it from a Reader.

Parameters:
reader - a Reader from which the template can be read.
Throws:
java.io.IOException
java.lang.IllegalArgumentException

AbstractTemplate

public AbstractTemplate(java.io.Reader reader,
                        Cache cache)
                 throws java.io.IOException,
                        java.lang.IllegalArgumentException
Constructs a template by compiling it from a Reader.

Parameters:
reader - a Reader from which the template can be read.
Throws:
java.io.IOException
java.lang.IllegalArgumentException
Method Detail

compile

protected abstract void compile(java.io.Reader reader)
                         throws java.io.IOException,
                                java.lang.IllegalArgumentException
Compiles the template from a Reader. If the template has already been compiled, this method does nothing.

Parameters:
reader - an Reader from which the template can be read.
Throws:
java.io.IOException
java.lang.IllegalArgumentException

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.

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

process

public abstract TemplateProcessor.ExitStatus process(TemplateProcessorParameters p)
                                              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:
p - template processing parameters
Returns:
an exit code indicating how the process terminated, typically used for short-circuiting template processing
Throws:
java.io.IOException - an IO error occurred during processing

process

public abstract void process(TemplateWriteableHashModel 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.
Throws:
java.io.IOException

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.
Throws:
java.io.IOException

getTemplateText

protected static java.lang.String getTemplateText(java.io.Reader reader)
                                           throws java.io.IOException
Takes the given Reader, reads it until the end of the stream, and accumulates the contents in a String. The string is returned when the stream is exhausted.

Parameters:
reader - the reader to be turned into a String
Returns:
a String representation of the given stream
Throws:
java.io.IOException - something went wrong while processing the stream