freemarker.template
Class UnparsedTemplate

java.lang.Object
  extended by freemarker.template.AbstractTemplate
      extended by freemarker.template.UnparsedTemplate
All Implemented Interfaces:
Cacheable, Compileable, TemplateProcessor, java.io.Serializable, java.lang.Cloneable

public class UnparsedTemplate
extends AbstractTemplate
implements java.io.Serializable

An UnparsedTemplate consists only of text. No logic beyond simple outputting is performed.

You can pass the filename of the template to a UnparsedTemplateFactory object, in which case it is read immediately. Once read, the unparsed template is stored in an an character array for later use.

To process the unparsed template, call the TemplateProcessor.process(freemarker.template.TemplateProcessorParameters) method, which takes an optional tree of TemplateModel objects as its data model. The root node of the tree must be a TemplateWriteableHashModel.

Any error messages will be included as HTML comments in the output.

To facilitate multithreading, UnparsedTemplate objects are immutable; if you need to recompile a template, you must make a new UnparsedTemplate object. In most cases, it will be sufficient to let a TemplateCache do this for you.

Version:
$Id: UnparsedTemplate.java 1166 2013-04-21 08:21:15Z run2000 $
See Also:
TemplateCache, UnparsedTemplateFactory, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface freemarker.template.TemplateProcessor
TemplateProcessor.ExitStatus
 
Field Summary
protected  char[] templateText
          The unparsed template text.
 
Fields inherited from class freemarker.template.AbstractTemplate
cache
 
Constructor Summary
UnparsedTemplate(java.io.Reader reader)
          Constructs an unparsed template by compiling it from a Reader.
UnparsedTemplate(java.io.Reader reader, Cache cache)
          Constructs an unparsed template by compiling it from a Reader.
 
Method Summary
protected  void compile(java.io.Reader reader)
          Compiles the template from a Reader.
 boolean equals(java.lang.Object o)
          Tests this object for equality with the given object.
 int hashCode()
          Retrieve the hash code for this object
 TemplateProcessor.ExitStatus process(TemplateProcessorParameters p)
          Processes the contents of this UnparsedTemplate and outputs the resulting text to a Writer.
 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.
 TemplateProcessor.ExitStatus process(TemplateWriteableHashModel modelRoot, java.io.Writer out, TemplateRuntimeHandler eventHandler)
          Processes the contents of this UnparsedTemplate and outputs the resulting text to a Writer.
 void process(java.io.Writer out)
          Processes the template, using an empty data model, and outputs the resulting text to a Writer.
 java.lang.String toString()
          Return the String value of this object
 
Methods inherited from class freemarker.template.AbstractTemplate
clone, getCache, getTemplateText
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

templateText

protected char[] templateText
The unparsed template text.

Constructor Detail

UnparsedTemplate

public UnparsedTemplate(java.io.Reader reader)
                 throws java.io.IOException
Constructs an unparsed template by compiling it from a Reader. Calls compile().

Parameters:
reader - the source of the template file to be compiled.
Throws:
java.io.IOException

UnparsedTemplate

public UnparsedTemplate(java.io.Reader reader,
                        Cache cache)
                 throws java.io.IOException
Constructs an unparsed template by compiling it from a Reader. Calls compile().

Parameters:
reader - the source of the template file to be compiled.
Throws:
java.io.IOException
Method Detail

compile

protected void compile(java.io.Reader reader)
                throws java.io.IOException,
                       java.lang.IllegalArgumentException
Compiles the template from a Reader.

Specified by:
compile in class AbstractTemplate
Parameters:
reader - an Reader from which the template can be read.
Throws:
java.io.IOException
java.lang.IllegalArgumentException

process

public TemplateProcessor.ExitStatus process(TemplateWriteableHashModel modelRoot,
                                            java.io.Writer out,
                                            TemplateRuntimeHandler eventHandler)
                                     throws java.io.IOException
Processes the contents of this UnparsedTemplate and outputs the resulting text to a Writer.

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.
Throws:
java.io.IOException - an IO error occurred with the Writer during processing

process

public TemplateProcessor.ExitStatus process(TemplateProcessorParameters p)
                                     throws java.io.IOException
Processes the contents of this UnparsedTemplate and outputs the resulting text to a Writer.

Specified by:
process in interface TemplateProcessor
Specified by:
process in class AbstractTemplate
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 with the Writer during processing

process

public 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.

Specified by:
process in class AbstractTemplate
Parameters:
modelRoot - the root node of the data model.
out - a Writer to output the text to.
Throws:
java.io.IOException

process

public 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.

Specified by:
process in class AbstractTemplate
Parameters:
out - a Writer to output the text to.
Throws:
java.io.IOException

toString

public java.lang.String toString()
Return the String value of this object

Overrides:
toString in class java.lang.Object
Returns:
the template text, if any, of this object

equals

public boolean equals(java.lang.Object o)
Tests this object for equality with the given object.

Overrides:
equals in class java.lang.Object
Parameters:
o - the object to be compared against
Returns:
true if the two objects are equal, otherwise false

hashCode

public int hashCode()
Retrieve the hash code for this object

Overrides:
hashCode in class java.lang.Object
Returns:
a hash value corresponding to the value of this object