net.sourceforge.sillyview
Class VelocityModel

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

public class VelocityModel
extends java.lang.Object
implements WidgetModel

Creates a WidgetModel backed by a Velocity Template.

Version:
1.0
Author:
T.J. Willis

Field Summary
private static Category cat
           
protected  VelocityContext context
          The VelocityContext to which tokens are delegated.
protected  java.lang.String template
          The raw velocity template.
 
Fields inherited from interface net.sourceforge.sillyview.WidgetModel
FALSE, TRUE
 
Constructor Summary
VelocityModel(java.lang.String templateString)
          Creates a new VelocityModel instance with the given String as its template.
VelocityModel(java.net.URL templateURL)
          Initializes Velocity, creates a new context, and sets this model's template to the file at the given URL.
VelocityModel(VelocityContext context, java.net.URL templateURL)
          Allows several models to share a context.
 
Method Summary
 java.lang.Object getCurrentModel()
          Returns the current model as a String.
static VelocityModel getModel(java.lang.String url)
          Static factory method for creating a VelocityModel with the raw velocity template at the given URL.
 java.lang.Object getRawModel()
          Returns the template without applying existing token values.
private  java.lang.String getTemplate(java.net.URL templateURL)
           
 java.lang.Object getValue(java.lang.Object key)
          Returns the value of the named token, taking it from the VelocityContext.
private  void init()
          Initializes velocity and creates a new context.
 void setRawModel(java.lang.Object newTemplate)
          Updates the template without modifying the current tokens.
 void setToken(java.lang.Object key, java.lang.Object value)
          Sets the named token, delegating it to the VelocityContext.
 java.lang.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

cat

private static Category cat

context

protected VelocityContext context
The VelocityContext to which tokens are delegated.


template

protected java.lang.String template
The raw velocity template.

Constructor Detail

VelocityModel

public VelocityModel(java.net.URL templateURL)
              throws java.io.IOException,
                     java.lang.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:
java.io.IOException - if an error occurs
java.lang.Exception - if an error occurs

VelocityModel

public VelocityModel(VelocityContext context,
                     java.net.URL templateURL)
              throws java.io.IOException,
                     java.lang.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:
java.io.IOException - if an error occurs
java.lang.Exception - if an error occurs

VelocityModel

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

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

init

private void init()
           throws java.lang.Exception
Initializes velocity and creates a new context.

Throws:
java.lang.Exception

getTemplate

private java.lang.String getTemplate(java.net.URL templateURL)
                              throws java.io.IOException,
                                     java.lang.Exception
Throws:
java.io.IOException
java.lang.Exception

getCurrentModel

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

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

getRawModel

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

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

setRawModel

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

Parameters:
newTemplate - an Object value

toString

public java.lang.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 java.lang.Object
Returns:
a String value

setToken

public void setToken(java.lang.Object key,
                     java.lang.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 java.lang.Object getValue(java.lang.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(java.lang.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