freemarker.engine
Class FMEngine

java.lang.Object
  extended by freemarker.engine.FMEngine

public final class FMEngine
extends java.lang.Object

This class provides services to an application developer, such as:

The methods in this class were developed to allow easy access to FM-Classic facilities without elaborate object construction.

This class provides can be used either as separate, new-able instances of this engine, or as a singleton using the singleton() static method.

Please ensure that you call one of the init() variants. This is critical for proper behavior.

Since:
1.9
Version:
$Id: FMEngine.java 1172 2013-04-21 10:00:22Z run2000 $
Author:
Nicholas Cull

Constructor Summary
FMEngine()
          Create a new FM-Classic engine.
 
Method Summary
 TemplateProcessor.ExitStatus apply(java.lang.String templateName, FMModel model, java.io.Writer writer)
          Apply the given model to the named template, writing the result to the given writer.
 TemplateProcessor.ExitStatus apply(java.lang.String templateName, TemplateWriteableHashModel model, java.io.Writer writer)
          Apply the given model to the named template, writing the result to the given writer.
 TemplateProcessor.ExitStatus apply(Template template, FMModel model, java.io.Writer writer)
          Apply the given model to the given template, writing the result to the given writer.
 TemplateProcessor.ExitStatus apply(Template template, TemplateWriteableHashModel model, java.io.Writer writer)
          Apply the given model to the given template, writing the result to the given writer.
 FMModel createModel(java.util.Properties properties)
          A simple way of creating objects and adding them to a new template model, using class named supplied from a properties object.
 Cache getCache()
          Get the template cache initialized by this engine.
 Cacheable getItem(java.lang.String name)
          Return the named cacheable item from the cache.
 Cacheable getItem(java.lang.String name, java.lang.String type)
          Return the named, typed cacheable item from the cache.
 java.lang.String getProperty(java.lang.String key)
          Get a property using one of the property keys from the PropertyConstants class.
 Template getTemplate(java.lang.String name)
          Return the named template from the cache.
 FMEngine init()
          Initialize this engine using properties set using the setProperty(String, String) method.
 FMEngine init(java.util.Properties properties)
          Initialize this engine using properties supplied from the given properties object.
static void main(java.lang.String[] args)
           
 java.lang.Object newInstanceFromClassName(java.lang.String c)
          Create a new instance of the named class using reflection.
 void setObject(java.lang.String propertyName, java.lang.Object objValue)
          Sets an object property using constants from the PropertyObjectConstants class.
 FMEngine setProperty(java.lang.String key, java.lang.String value)
          Set a property using one of the property keys from the PropertyConstants class.
static FMEngine singleton()
          Return a singleton instance of a FM-Classic engine.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FMEngine

public FMEngine()
Create a new FM-Classic engine.

Method Detail

singleton

public static FMEngine singleton()
Return a singleton instance of a FM-Classic engine. If this is the first time this method has been called, a new singleton instance is created.

Returns:
a singleton FMEngine instance

setProperty

public FMEngine setProperty(java.lang.String key,
                            java.lang.String value)
Set a property using one of the property keys from the PropertyConstants class.

Parameters:
key - the property name from PropertyConstants
value - the string value corresponding to the given key
Throws:
java.lang.IllegalStateException - the engine's init() method has already been called

getProperty

public java.lang.String getProperty(java.lang.String key)
Get a property using one of the property keys from the PropertyConstants class.

Parameters:
key - the property name from PropertyConstants
Returns:
a string value corresponding to the given key

init

public FMEngine init(java.util.Properties properties)
Initialize this engine using properties supplied from the given properties object. Only these properties will be used to initialize the engine.

Parameters:
properties - the properties to initialize this engine, using property keys from the PropertyConstants class
Throws:
java.lang.IllegalStateException - the engine's init() method has already been called

init

public FMEngine init()
Initialize this engine using properties set using the setProperty(String, String) method. Default properties are supplied from the PropertyConstants.getDefaultProperties() method.

Throws:
java.lang.IllegalStateException - the engine's init() method has already been called

setObject

public void setObject(java.lang.String propertyName,
                      java.lang.Object objValue)
Sets an object property using constants from the PropertyObjectConstants class.

Parameters:
propertyName - a property name from the PropertyObjectConstants class
objValue - the object value to be set

getCache

public Cache getCache()
Get the template cache initialized by this engine.

Returns:
the cache initialized for this engine

getTemplate

public Template getTemplate(java.lang.String name)
Return the named template from the cache.

Parameters:
name - the name of the template
Returns:
the named Template, or null if it is not found

getItem

public Cacheable getItem(java.lang.String name)
Return the named cacheable item from the cache.

Parameters:
name - the name of the cacheable item
Returns:
the named item, or null if it is not found

getItem

public Cacheable getItem(java.lang.String name,
                         java.lang.String type)
Return the named, typed cacheable item from the cache.

Parameters:
name - the name of the cacheable item
type - the type of the cacheable item
Returns:
the named Template, or null if it is not found

apply

public TemplateProcessor.ExitStatus apply(java.lang.String templateName,
                                          FMModel model,
                                          java.io.Writer writer)
                                   throws java.io.IOException
Apply the given model to the named template, writing the result to the given writer.

Parameters:
templateName - the name of the template to be loaded from the cache
model - the model to apply to the template
writer - the result of applying the model to the template
Returns:
the exit status
Throws:
java.io.IOException - there was a problem writing the result to the writer

apply

public TemplateProcessor.ExitStatus apply(Template template,
                                          FMModel model,
                                          java.io.Writer writer)
                                   throws java.io.IOException
Apply the given model to the given template, writing the result to the given writer.

Parameters:
template - the template applied against the model
model - the model to apply to the template
writer - the result of applying the model to the template
Returns:
the exit status
Throws:
java.io.IOException - there was a problem writing the result to the writer

apply

public TemplateProcessor.ExitStatus apply(java.lang.String templateName,
                                          TemplateWriteableHashModel model,
                                          java.io.Writer writer)
                                   throws java.io.IOException
Apply the given model to the named template, writing the result to the given writer.

Parameters:
templateName - the name of the template to be loaded from the cache
model - the model to apply to the template
writer - the result of applying the model to the template
Returns:
the exit status
Throws:
java.io.IOException - there was a problem writing the result to the writer

apply

public TemplateProcessor.ExitStatus apply(Template template,
                                          TemplateWriteableHashModel model,
                                          java.io.Writer writer)
                                   throws java.io.IOException
Apply the given model to the given template, writing the result to the given writer.

Parameters:
template - the template applied against the model
model - the model to apply to the template
writer - the result of applying the model to the template
Returns:
the exit status
Throws:
java.io.IOException - there was a problem writing the result to the writer

createModel

public FMModel createModel(java.util.Properties properties)
A simple way of creating objects and adding them to a new template model, using class named supplied from a properties object. Reflection is used to create the objects.

Parameters:
properties - a key-value pairing of names to class names to be loaded and added to the new template model
Returns:
the new template model

newInstanceFromClassName

public java.lang.Object newInstanceFromClassName(java.lang.String c)
Create a new instance of the named class using reflection. See the PropertyConstants class for details on how the reflection code creates the object.

Parameters:
c - the named class to be loaded
Returns:
the instantiated class, or null if it cannot be loaded

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception