org.apache.commons.jexl2
Class UnifiedJEXL.Template

java.lang.Object
  extended by org.apache.commons.jexl2.UnifiedJEXL.Template
Enclosing class:
UnifiedJEXL

public final class UnifiedJEXL.Template
extends Object

A Template is a script that evaluates by writing its content through a Writer. This is a simplified replacement for Velocity that uses JEXL (instead of OGNL/VTL) as the scripting language.

The source text is parsed considering each line beginning with '$$' (as default pattern) as JEXL script code and all others as Unified JEXL expressions; those expressions will be invoked from the script during evaluation and their output gathered through a writer. It is thus possible to use looping or conditional construct "around" expressions generating output.

For instance:

 $$ for(var x : [1, 3, 5, 42, 169]) {
 $$   if (x == 42) {
 Life, the universe, and everything
 $$   } else if (x > 42) {
 The value $(x} is over fourty-two
 $$   } else {
 The value ${x} is under fourty-two
 $$   }
 $$ }
 
Will evaluate as:

 The value 1 is under fourty-two
 The value 3 is under fourty-two
 The value 5 is under fourty-two
 Life, the universe, and everything
 The value 169 is over fourty-two
 

During evaluation, the template context exposes its writer as '$jexl' which is safe to use in this case. This allows writing directly through the writer without adding new-lines as in:

 $$ for(var cell : cells) { $jexl.print(cell); $jexl.print(';') }
 

A template is expanded as one JEXL script and a list of UnifiedJEXL expressions; each UnifiedJEXL expression being replace in the script by a call to jexl:print(expr) (the expr is in fact the expr number in the template). This integration uses a specialized JexlContext (TemplateContext) that serves as a namespace (for jexl:) and stores the expression array and the writer (java.io.Writer) that the 'jexl:print(...)' delegates the output generation to.

Since:
2.1

Constructor Summary
UnifiedJEXL.Template(String directive, Reader reader, String... parms)
          Creates a new template from an input.
 
Method Summary
 String asString()
          Recreate the template source from its inner components.
 void evaluate(JexlContext context, Writer writer)
          Evaluates this template.
 void evaluate(JexlContext context, Writer writer, Object... args)
          Evaluates this template.
 UnifiedJEXL.Template prepare(JexlContext context)
          Prepares this template by expanding any contained deferred expression.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UnifiedJEXL.Template

public UnifiedJEXL.Template(String directive,
                            Reader reader,
                            String... parms)
Creates a new template from an input.

Parameters:
directive - the prefix for lines of code; can not be "$", "${", "#" or "#{" since this would preclude being able to differentiate directives and UnifiedJEXL expressions
reader - the input reader
parms - the parameter names
Throws:
NullPointerException - if either the directive prefix or input is null
IllegalArgumentException - if the directive prefix is invalid
Method Detail

toString

public String toString()
Overrides:
toString in class Object

asString

public String asString()
Recreate the template source from its inner components.

Returns:
the template source rewritten

prepare

public UnifiedJEXL.Template prepare(JexlContext context)
Prepares this template by expanding any contained deferred expression.

Parameters:
context - the context to prepare against
Returns:
the prepared version of the template

evaluate

public void evaluate(JexlContext context,
                     Writer writer)
Evaluates this template.

Parameters:
context - the context to use during evaluation
writer - the writer to use for output

evaluate

public void evaluate(JexlContext context,
                     Writer writer,
                     Object... args)
Evaluates this template.

Parameters:
context - the context to use during evaluation
writer - the writer to use for output
args - the arguments


Copyright © 2001-2011 The Apache Software Foundation. All Rights Reserved.