Top Description Inners Fields Constructors Methods
com.google.escapevelocity

public Class Template

extends Object
Class Inheritance
Imports
com.google.common.collect.ImmutableMap, com.google.escapevelocity.EvaluationContext.PlainEvaluationContext, java.io.IOException, .Reader, java.util.LinkedHashMap, .Map, .TreeMap

A template expressed in EscapeVelocity, a subset of the Velocity Template Language (VTL) from Apache. The intent of this implementation is that if a template is accepted and successfully produces output, that output will be identical to what Velocity would have produced for the same template and input variables.
Author
[email protected] (Éamonn McManus)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
Template.ResourceOpener

Used to resolve references to resources in the template, through #parse directives.

Field Summary

Modifier and TypeField and Description
private final ImmutableMap<String, Macro>
macros

Macros that are defined in this template (this exact VTL file).

private final MethodFinder
methodFinder

Caches Method objects for public methods accessed through references.

private final Node

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the string result of evaluating the template.
evaluate
(Map<String, ?>
a map where the keys are variable names and the values are the corresponding variable values. For example, if "x" maps to 23, then $x in the template will expand to 23.
vars
)

Evaluate the given template with the given initial set of variables.

pack-priv ImmutableMap<String, Macro>
public static Template

Returns:

an object representing the parsed template.
parseFrom
(Reader
a Reader that will supply the text of the template. It will be closed on return from this method. The Reader will be buffered internally by this method (via BufferedReader), so the passed-in Reader doesn't need to perform its own buffering.
reader
)

Parses a VTL template from the given Reader.

public static Template

Returns:

an object representing the parsed template.
parseFrom
(String
name of the resource. May be null.
resourceName
,
Template.ResourceOpener
used to open the initial resource and resources referenced by #parse directives in the template.
resourceOpener
)

Parses a VTL template of the given name using the given ResourceOpener.

pack-priv static Template
parseFrom(String resourceName, Template.ResourceOpener resourceOpener, Map<String, Template> parseCache)

pack-priv void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

macrosback to summary
private final ImmutableMap<String, Macro> macros

Macros that are defined in this template (this exact VTL file). If the template includes #parse directives, those might end up defining other macros when a #parse is evaluated. The #parse produces a separate Template object with its own macros map. When the root Template is evaluated, the EvaluationContext starts off with the macros here, and each #parse that is executed may add macros to the map in the EvaluationContext.

methodFinderback to summary
private final MethodFinder methodFinder

Caches Method objects for public methods accessed through references. The first time we evaluate $var.property or $var.method(...) for a $var of a given class and for a given property or method signature, we'll store the resultant Method object. Every subsequent time we'll reuse that Method. The method lookup is quite slow so caching is useful. The main downside is that we may potentially hold on to Method objects that will never be used with this Template again. But in practice templates tend to be used repeatedly with the same classes.

rootback to summary
private final Node root

Constructor Detail

Templateback to summary
pack-priv Template(Node root, ImmutableMap<String, Macro> macros)

Method Detail

evaluateback to summary
public String evaluate(Map<String, ?> vars)

Evaluate the given template with the given initial set of variables.

Parameters
vars:Map<String, ?>

a map where the keys are variable names and the values are the corresponding variable values. For example, if "x" maps to 23, then $x in the template will expand to 23.

Returns:String

the string result of evaluating the template.

Exceptions
EvaluationException:
if the evaluation failed, for example because of an undefined reference. If the template contains a #parse directive, there may be an exception such as ParseException or IOException when the nested template is read and parsed. That exception will then be the cause of an EvaluationException.
getMacrosback to summary
pack-priv ImmutableMap<String, Macro> getMacros()
parseFromback to summary
public static Template parseFrom(Reader reader) throws IOException

Parses a VTL template from the given Reader. The template cannot reference other templates (for example with #parse). For that, use parseFrom(String, ResourceOpener).

Parameters
reader:Reader

a Reader that will supply the text of the template. It will be closed on return from this method. The Reader will be buffered internally by this method (via BufferedReader), so the passed-in Reader doesn't need to perform its own buffering.

Returns:Template

an object representing the parsed template.

Exceptions
IOException:
if there is an exception reading from reader, or if the template references another template via #parse.
ParseException:
if the text of the template could not be parsed.
parseFromback to summary
public static Template parseFrom(String resourceName, Template.ResourceOpener resourceOpener) throws IOException

Parses a VTL template of the given name using the given ResourceOpener.

Parameters
resourceName:String

name of the resource. May be null.

resourceOpener:Template.ResourceOpener

used to open the initial resource and resources referenced by #parse directives in the template.

Returns:Template

an object representing the parsed template.

Exceptions
IOException:
if there is an exception opening or reading from any resource.
ParseException:
if the text of the template could not be parsed.
parseFromback to summary
pack-priv static Template parseFrom(String resourceName, Template.ResourceOpener resourceOpener, Map<String, Template> parseCache) throws IOException
renderback to summary
pack-priv void render(EvaluationContext context, StringBuilder output)
com.google.escapevelocity back to summary

public Interface Template.ResourceOpener

Annotations
@FunctionalInterface

Used to resolve references to resources in the template, through #parse directives.

Here is an example that opens nested templates as resources relative to the calling class:

ResourceOpener resourceOpener = resourceName -> {
    InputStream inputStream = getClass().getResource(resourceName);
    if (inputStream == null) {
      throw new IOException("Unknown resource: " + resourceName);
    }
    return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
  };
  Template template = Template.parseFrom("foo.vm", resourceOpener);

Method Summary

Modifier and TypeMethod and Description
public Reader

Returns:

a Reader for the resource.
openResource
(String
the name of the resource to be read. This can be null if Template.parseFrom is called with a null resourceName.
resourceName
)

Returns a Reader that will be used to read the given resource, then closed.

Method Detail

openResourceback to summary
public Reader openResource(String resourceName) throws IOException

Returns a Reader that will be used to read the given resource, then closed. The caller of this method will perform its own buffering (via BufferedReader), so the returned Reader doesn't need to be buffered.

Parameters
resourceName:String

the name of the resource to be read. This can be null if Template.parseFrom is called with a null resourceName.

Returns:Reader

a Reader for the resource.

Exceptions
IOException:
if the resource cannot be opened.