Generated Template Classes
Jamon templates are translated into Java classes, either by using the
Jamon ant task, or by executing the
TemplateProcessor
class. These generated classes have the following relationships to the
template sources:
-
The class name is the same as the name of the template source file
excluding any filename extension (i.e. a suffix starting with
'.').
-
The package of the class maps directly to the directory path from
the template source directory to the template.
-
The generated class has a
render
method whose
arguments are a writer, followed by the required arguments
declared in the template. The order of declaration in the
render
method is the same as the order of declaration
in the template.
-
The generated class has a
makeRenderer
method taking
the same parameters as render
, except for the writer.
-
Optional arguments in the template
generate
setXXX
methods in the class.
-
If a value for the
org.jamon.contextType
property has been set via
jamon.properties
then there is a setJamonContext
method in the class.
For example, suppose there is a template
MyTemplate
in the
directory
tmpls/org/foo
, the template source directory is
set to
tmpls
, and
MyTemplate
contains the
declarations
<%args>
int i;
String s = "yes";
java.math.BigDecimal someNumber = null;
boolean b;
</%args>
Suppose further that in
tmpls
is a
jamon.properties
file containing
org.jamon.contextType=org.foo.MyContext
Then a simplified sketch (showing only the pertinent public methods) of the
generated class would be:
package org.foo;
import org.jamon.TemplateManager;
import org.jamon.escaping.Escaping;
public class MyTemplate
extends org.jamon.AbstractTemplateProxy
{
public MyTemplate() { ... }
public MyTemplate(TemplateManager manager) { ... }
public void render(int i, boolean b)
throws java.io.IOException { ... }
public Renderer makeRenderer(java.io.Writer writer, int i, boolean b)
throws java.io.IOException { ... }
public MyTemplate setS(String s) { ... }
public MyTemplate setSomeNumber(java.math.BigDecimal someNumber) { ... }
public MyTemplate setJamonContext(org.foo.MyContext jamonContext() { ... }
}
If one template inherits from another, then:
-
The generated class of the parent template will be abstract, and
will not have
render
or makeRenderer
methods.
-
The generated class of the child template will extend the
generated class of the parent template.
-
Any setter methods for optional arguments declared in the parent
template will be declared to return the generated class for the
parent, not the child.
-
The generated class for the parent template will define a public
abstract inner class named
ParentRenderer
, which will
have:
-
A render method taking a writer, followed by the required
arguments for the parent template (including any required
arguments for the parent template's ancestors, if any).
-
setXXX
methods for each of the parent template's
optional arguments (including any optional arguments for the
parent template's ancestors, if any).
-
If a
jamon.properties
file has declared a value for org.jamon.contextType
, then there will be a
setJamonContext
method.
-
If the parent template itself extends another template (which
we'll call the grandparent template), a
makeParentRender
method taking as arguments the
required arguments declared by the parent template, and
returning an object whose type is the inner
ParentRenderer
class of the grandparent
template's generated class.
-
If the child template is a final template (i.e. does not contain a
<& *CHILD &>
call), then its generated
class will contain a makeParentRenderer
method which
takes as arguments the required arguments declared by the child
template, and returns an object whose type is the inner
ParentRenderer
class of the parent template's generated class.
For example, if
/org/foo/ParentTemplate
contained the
declarations
<%args>
int i;
String s = "yes";
Integer count;
</%args>
then the generated class
org.foo.ParentTemplate
would look
something like:
package org.foo;
import org.jamon.escaping.Escaping;
public abstract class ParentTemplate
extends org.jamon.AbstractTemplateProxy
{
public ParentTemplate setS(String s) { ... }
public abstract class ParentRenderer
{
public ParentRenderer setS(String s) { ... }
public ParentRenderer setJamonContext(org.foo.MyContext jamonContext) { ... }
public void render(java.io.Writer writer, int i, Integer count)
throws java.io.IOException { ... }
}
}
Moreover, if
/org/foo/ChildTemplate
contained the
declarations
<%extends ParentTemplate>
<%args>
long t;
java.math.BigDecimal amount = new BigDecimal(0);
double x;
</%args>
then the generated class
org.foo.ChildTemplate
would look
something like:
package org.foo;
import org.jamon.TemplateManager;
import org.jamon.escaping.Escaping;
public abstract class ChildTemplate
extends org.foo.ParentTemplate
{
public ChildTemplate() { ... }
public ChildTemplate(TemplateManager manager) { ... }
public void render(java.io.Writer writer, int i, Integer count, long t, double x)
throws java.io.IOException { ... }
public Renderer makeRenderer(int i, Integer count, long t, double x)
throws java.io.IOException { ... }
public ChildTemplate setAmount(java.math.BigDecimal amount) { ... }
public ChildTemplate setJamonContext(org.foo.MyContext jamonContext) { ... }
public ParentTemplate.ParentRenderer makeParentRenderer(long t, double x)
throws java.io.IOException { ... }
}
An instantiated template may be used at most once.