freemarker.template
Class LocalModelWrapper

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

public class LocalModelWrapper
extends java.lang.Object
implements TemplateWriteableHashModel, 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 the supplied Map. Anything that doesn't exist within the Map is automatically forwarded to the wrapped global model. Local variables are counted as being all variables contained in the localModel parameter of the constructor method.

Use this class when you want a well-defined distinction between global and local variables.

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

Field Summary
protected  TemplateWriteableHashModel globalModel
          The underlying root model being wrapped.
protected  java.util.Map<java.lang.String,TemplateModel> localModel
          The map containing local values for the root model.
 
Constructor Summary
LocalModelWrapper(TemplateWriteableHashModel globalModel, java.util.Map<java.lang.String,TemplateModel> localModel)
          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 storage with the given key.
 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

localModel

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


globalModel

protected TemplateWriteableHashModel globalModel
The underlying root model being wrapped.

Constructor Detail

LocalModelWrapper

public LocalModelWrapper(TemplateWriteableHashModel globalModel,
                         java.util.Map<java.lang.String,TemplateModel> localModel)
Create a new RootModelWrapper with the given hash model as the model to be wrapped.

Parameters:
globalModel - the root model to be wrapped
localModel - a Map containing the pre-populated local variables
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 global 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)
         throws TemplateModelException
Put the given template model into storage with the given key. The storage may be either the local or global model, depending on what was declared as local at construction time.

Specified by:
put in interface TemplateWriteableHashModel
Parameters:
key - the name of the model to be stored
model - the model being stored
Throws:
TemplateModelException - there was a problem setting the value for the given key

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.