net.sourceforge.sillyview
Class VelocityModel

java.lang.Object
  extended by net.sourceforge.sillyview.VelocityModel
All Implemented Interfaces:
WidgetModel

public class VelocityModel
extends Object
implements WidgetModel

Creates a WidgetModel backed by a Velocity Template.


Field Summary
protected  VelocityContext context
          The VelocityContext to which tokens are delegated.
protected  String template
          The raw velocity template.
 
Fields inherited from interface net.sourceforge.sillyview.WidgetModel
FALSE, TRUE
 
Constructor Summary
VelocityModel(String templateString)
          Creates a new VelocityModel instance with the given String as its template.
VelocityModel(URL templateURL)
          Initializes Velocity, creates a new context, and sets this model's template to the file at the given URL.
VelocityModel(VelocityContext context, URL templateURL)
          Allows several models to share a context.
 
Method Summary
 Object getCurrentModel()
          Returns the current model as a String.
static VelocityModel getModel(String url)
          Static factory method for creating a VelocityModel with the raw velocity template at the given URL.
 Object getRawModel()
          Returns the template without applying existing token values.
 Object getValue(Object key)
          Returns the value of the named token, taking it from the VelocityContext.
 void setRawModel(Object newTemplate)
          Updates the template without modifying the current tokens.
 void setToken(Object key, Object value)
          Sets the named token, delegating it to the VelocityContext.
 String toString()
          Uses Velocity.evaluate() to merge the current token values with the template string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

context

protected VelocityContext context
The VelocityContext to which tokens are delegated.


template

protected String template
The raw velocity template.

Constructor Detail

VelocityModel

public VelocityModel(URL templateURL)
              throws IOException,
                     Exception
Initializes Velocity, creates a new context, and sets this model's template to the file at the given URL.

Parameters:
templateURL - an URL value
Throws:
IOException - if an error occurs
Exception - if an error occurs

VelocityModel

public VelocityModel(VelocityContext context,
                     URL templateURL)
              throws IOException,
                     Exception
Allows several models to share a context. Assumes that Velocity.init() has already been called.

Parameters:
context - a VelocityContext value
templateURL - an URL value
Throws:
IOException - if an error occurs
Exception - if an error occurs

VelocityModel

public VelocityModel(String templateString)
              throws Exception
Creates a new VelocityModel instance with the given String as its template.

Parameters:
templateString - a String value
Throws:
Exception - if an error occurs
Method Detail

getCurrentModel

public Object getCurrentModel()
Returns the current model as a String.

Specified by:
getCurrentModel in interface WidgetModel
Returns:
an Object value

getRawModel

public Object getRawModel()
Returns the template without applying existing token values.

Specified by:
getRawModel in interface WidgetModel
Returns:
an Object value

setRawModel

public void setRawModel(Object newTemplate)
Updates the template without modifying the current tokens.

Parameters:
newTemplate - an Object value

toString

public String toString()
Uses Velocity.evaluate() to merge the current token values with the template string.

Specified by:
toString in interface WidgetModel
Overrides:
toString in class Object
Returns:
a String value

setToken

public void setToken(Object key,
                     Object value)
Sets the named token, delegating it to the VelocityContext.

Specified by:
setToken in interface WidgetModel
Parameters:
key - an Object value
value - an Object value

getValue

public Object getValue(Object key)
Returns the value of the named token, taking it from the VelocityContext.

Specified by:
getValue in interface WidgetModel
Parameters:
key - an Object value
Returns:
an Object value

getModel

public static VelocityModel getModel(String url)
Static factory method for creating a VelocityModel with the raw velocity template at the given URL. Calls Velocity.init() and creates a new VelocityContext.

Parameters:
url - a String value
Returns:
a VelocityModel value