Class TemplateScript
- java.lang.Object
-
- org.apache.commons.jexl3.internal.TemplateScript
-
- All Implemented Interfaces:
JxltEngine.Template
public final class TemplateScript extends java.lang.Object implements JxltEngine.Template
A Template instance.
-
-
Field Summary
Fields Modifier and Type Field Description private TemplateEngine.TemplateExpression[]exprsThe TemplateEngine expressions called by the script.private TemplateEnginejxltThe engine.private java.lang.StringprefixThe prefix marker.private ASTJexlScriptscriptThe resulting script.private TemplateEngine.Block[]sourceThe array of source blocks.
-
Constructor Summary
Constructors Constructor Description TemplateScript(TemplateEngine engine, java.lang.String thePrefix, TemplateEngine.Block[] theSource, ASTJexlScript theScript, TemplateEngine.TemplateExpression[] theExprs)Private ctor used to expand deferred expressions during prepare.TemplateScript(TemplateEngine engine, JexlInfo jexlInfo, java.lang.String directive, java.io.Reader reader, java.lang.String... parms)Creates a new template from an character input.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringasString()Recreate the template source from its inner components.private TemplateEngine.TemplateExpression[]calleeScripts(Scope scope, TemplateEngine.Block[] blocks, JexlNode.Info[] callSites)Creates the expression array from the list of blocks.private static java.lang.StringcallerScript(TemplateEngine.Block[] blocks)Creates the script calling the list of blocks.private static voidcollectPrintScope(JexlNode node, JexlNode.Info[] callSites)Collects the call-site surrounding a call to jexl:print(i).voidevaluate(JexlContext context, java.io.Writer writer)Evaluates this template.voidevaluate(JexlContext context, java.io.Writer writer, java.lang.Object... args)Evaluates this template.(package private) TemplateEngine.TemplateExpression[]getExpressions()java.lang.String[]getParameters()Gets the list of parameters expected by this template.java.util.Map<java.lang.String,java.lang.Object>getPragmas()Gets this script pragmas.(package private) ASTJexlScriptgetScript()java.util.Set<java.util.List<java.lang.String>>getVariables()Gets the list of variables accessed by this template.TemplateScriptprepare(JexlContext context)Prepares this template by expanding any contained deferred TemplateExpression.TemplateScriptprepare(JexlContext context, java.lang.Object... args)Prepares this template by expanding any contained deferred TemplateExpression with optional arguments.private static ScopescopeOf(JexlNode.Info info, Scope scope)Gets the scope from a node info.java.lang.StringtoString()
-
-
-
Field Detail
-
prefix
private final java.lang.String prefix
The prefix marker.
-
source
private final TemplateEngine.Block[] source
The array of source blocks.
-
script
private final ASTJexlScript script
The resulting script.
-
exprs
private final TemplateEngine.TemplateExpression[] exprs
The TemplateEngine expressions called by the script.
-
jxlt
private final TemplateEngine jxlt
The engine.
-
-
Constructor Detail
-
TemplateScript
public TemplateScript(TemplateEngine engine, JexlInfo jexlInfo, java.lang.String directive, java.io.Reader reader, java.lang.String... parms)
Creates a new template from an character input.- Parameters:
engine- the template enginejexlInfo- the source infodirective- the prefix for lines of code; cannot be "$", "${", "#" or "#{" since this would preclude being able to differentiate directives and jxlt expressionsreader- the input readerparms- the parameter names- Throws:
java.lang.NullPointerException- if either the directive prefix or input is nulljava.lang.IllegalArgumentException- if the directive prefix is invalid
-
TemplateScript
TemplateScript(TemplateEngine engine, java.lang.String thePrefix, TemplateEngine.Block[] theSource, ASTJexlScript theScript, TemplateEngine.TemplateExpression[] theExprs)
Private ctor used to expand deferred expressions during prepare.- Parameters:
engine- the template enginethePrefix- the directive prefixtheSource- the sourcetheScript- the scripttheExprs- the expressions
-
-
Method Detail
-
collectPrintScope
private static void collectPrintScope(JexlNode node, JexlNode.Info[] callSites)
Collects the call-site surrounding a call to jexl:print(i).This allows parsing the blocks with the known symbols in the frame visible to the parser.
- Parameters:
node- the visited nodecallSites- the map of printed expression number to node info
-
scopeOf
private static Scope scopeOf(JexlNode.Info info, Scope scope)
Gets the scope from a node info.- Parameters:
info- the node infoscope- the outer scope- Returns:
- the scope
-
calleeScripts
private TemplateEngine.TemplateExpression[] calleeScripts(Scope scope, TemplateEngine.Block[] blocks, JexlNode.Info[] callSites)
Creates the expression array from the list of blocks.- Parameters:
scope- the outer scopeblocks- the list of blocks- Returns:
- the array of expressions
-
callerScript
private static java.lang.String callerScript(TemplateEngine.Block[] blocks)
Creates the script calling the list of blocks.This is used to create a script from a list of blocks that were parsed from a template.
- Parameters:
blocks- the list of blocks- Returns:
- the script source
-
asString
public java.lang.String asString()
Description copied from interface:JxltEngine.TemplateRecreate the template source from its inner components.- Specified by:
asStringin interfaceJxltEngine.Template- Returns:
- the template source rewritten
-
evaluate
public void evaluate(JexlContext context, java.io.Writer writer)
Description copied from interface:JxltEngine.TemplateEvaluates this template.- Specified by:
evaluatein interfaceJxltEngine.Template- Parameters:
context- the context to use during evaluationwriter- the writer to use for output
-
evaluate
public void evaluate(JexlContext context, java.io.Writer writer, java.lang.Object... args)
Description copied from interface:JxltEngine.TemplateEvaluates this template.- Specified by:
evaluatein interfaceJxltEngine.Template- Parameters:
context- the context to use during evaluationwriter- the writer to use for outputargs- the arguments
-
getExpressions
TemplateEngine.TemplateExpression[] getExpressions()
- Returns:
- exprs
-
getParameters
public java.lang.String[] getParameters()
Description copied from interface:JxltEngine.TemplateGets the list of parameters expected by this template.- Specified by:
getParametersin interfaceJxltEngine.Template- Returns:
- the parameter names array
-
getPragmas
public java.util.Map<java.lang.String,java.lang.Object> getPragmas()
Description copied from interface:JxltEngine.TemplateGets this script pragmas.- Specified by:
getPragmasin interfaceJxltEngine.Template- Returns:
- the (non-null, possibly empty) pragmas map
-
getScript
ASTJexlScript getScript()
- Returns:
- script
-
getVariables
public java.util.Set<java.util.List<java.lang.String>> getVariables()
Description copied from interface:JxltEngine.TemplateGets the list of variables accessed by this template.This method will visit all nodes of the sub-expressions and extract all variables whether they are written in 'dot' or 'bracketed' notation. (a.b is equivalent to a['b']).
- Specified by:
getVariablesin interfaceJxltEngine.Template- Returns:
- the set of variables, each as a list of strings (ant-ish variables use more than 1 string) or the empty set if no variables are used
-
prepare
public TemplateScript prepare(JexlContext context)
Description copied from interface:JxltEngine.TemplatePrepares this template by expanding any contained deferred TemplateExpression.- Specified by:
preparein interfaceJxltEngine.Template- Parameters:
context- the context to prepare against- Returns:
- the prepared version of the template
-
prepare
public TemplateScript prepare(JexlContext context, java.lang.Object... args)
Description copied from interface:JxltEngine.TemplatePrepares this template by expanding any contained deferred TemplateExpression with optional arguments.This binds arguments to template parameters for immediate expressions when the template also uses deferred/nested expressions.
- Specified by:
preparein interfaceJxltEngine.Template- Parameters:
context- the context to prepare againstargs- the arguments to bind (optional)- Returns:
- the prepared version of the template
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-