freemarker.template
Class RootModelWrapper

java.lang.Object
  extended by freemarker.template.RootModelWrapper
All Implemented Interfaces:
TemplateHashModel, TemplateModel, TemplateModelRoot, TemplateWriteableHashModel, java.io.Serializable

public class RootModelWrapper
extends java.lang.Object
implements TemplateModelRoot, java.io.Serializable

Wraps a template hash model with a template model root. The hash model can be accessed transparently from the root of this model. Any local variables are stored within a HashMap. Anything that doesn't exist within the HashMap is automatically forwarded to the wrapped hash model. Local variables are counted as being any variables added using the put() method.

Use this class when your template model (the hash model) is shared across several template calls, possibly multithreaded, and you want to be sure that the model isn't inadvertently changed between calls.

To do this for multithreaded calls, create a separate RootModelWrapper object for each template call, passing in the data model you want to be left unchanged. Then, call your templates with each RootModelWrapper object as your root data model.

To do this for synchronous calls, it is sufficient to create one RootModelWrapper object, passing in the data model you want to be left unchanged. Then, call your templates with the RootModelWrapper object as your root data model, and call the reset() method between each template call.

This class was previously in the freemarker.ext.misc package.

Since:
1.8
Version:
$Id: RootModelWrapper.java 1153 2013-04-15 10:59:37Z run2000 $
See Also:
LocalModelWrapper, Serialized Form

Field Summary
protected  TemplateHashModel hashModel
          The underlying template hash being wrapped.
protected  java.util.Map<java.lang.String,TemplateModel> rootModel
          The map containing temporary values for the root model.
 
Constructor Summary
RootModelWrapper(TemplateHashModel innerModel)
          Create a new RootModelWrapper with the given hash model as the model to be wrapped.
RootModelWrapper(TemplateHashModel innerModel, java.util.Map<java.lang.String,TemplateModel> modelMap)
          Create a new RootModelWrapper with the given hash model as the model to be wrapped.
 
Method Summary
 TemplateModel get(java.lang.String key)
          Retrieve a template model for the given key, if one exists.
 boolean isEmpty()
          Returns whether we have a completely empty model.
 void put(java.lang.String key, TemplateModel model)
          Put the given template model into local storage with the given key.
 void remove(java.lang.String key)
          Remove the named model from local storage.
 void reset()
          Clear all the local variables from the local storage, and just provide pass-through access to the wrapped hash model.
 java.lang.String toString()
          Returns a string representation of the object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

rootModel

protected java.util.Map<java.lang.String,TemplateModel> rootModel
The map containing temporary values for the root model.


hashModel

protected TemplateHashModel hashModel
The underlying template hash being wrapped.

Constructor Detail

RootModelWrapper

public RootModelWrapper(TemplateHashModel innerModel)
Create a new RootModelWrapper with the given hash model as the model to be wrapped.

Parameters:
innerModel - the hash model to be wrapped

RootModelWrapper

public RootModelWrapper(TemplateHashModel innerModel,
                        java.util.Map<java.lang.String,TemplateModel> modelMap)
Create a new RootModelWrapper with the given hash model as the model to be wrapped.

Parameters:
innerModel - the hash model to be wrapped
modelMap - the map containing the outer model
Method Detail

get

public TemplateModel get(java.lang.String key)
                  throws TemplateModelException
Retrieve a template model for the given key, if one exists. First, look at the local storage to see if we have an entry. If so, return the local entry. Otherwise, forward the call to the wrapped hash model.

Specified by:
get in interface TemplateHashModel
Parameters:
key - the name of the value to be returned
Returns:
a TemplateModel for the corresponding key, if one exists, otherwise null
Throws:
TemplateModelException - there was a problem with the underlying hash model

isEmpty

public boolean isEmpty()
                throws TemplateModelException
Returns whether we have a completely empty model. If the local storage is non-empty, return false. Otherwise, forward the call to the wrapped hash model.

Specified by:
isEmpty in interface TemplateModel
Returns:
true if the model is empty, otherwise false
Throws:
TemplateModelException - there was a problem with underlying hash model

put

public void put(java.lang.String key,
                TemplateModel model)
Put the given template model into local storage with the given key.

Specified by:
put in interface TemplateModelRoot
Specified by:
put in interface TemplateWriteableHashModel
Parameters:
key - the name of the model to be stored
model - the model being stored locally

remove

public void remove(java.lang.String key)

Remove the named model from local storage.

Note that we don't attempt to block access to the hash model when we remove a key. This is because the only time a "remove" call is performed is when the template engine is sure that there was no underlying data for that key to begin with.

Specified by:
remove in interface TemplateModelRoot
Parameters:
key - the name of the model to be removed

reset

public void reset()
Clear all the local variables from the local storage, and just provide pass-through access to the wrapped hash model.


toString

public java.lang.String toString()
Returns a string representation of the object.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the object.