Class TemplateEngine
- java.lang.Object
-
- org.apache.commons.jexl3.JxltEngine
-
- org.apache.commons.jexl3.internal.TemplateEngine
-
public final class TemplateEngine extends JxltEngine
A JxltEngine implementation.- Since:
- 3.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classTemplateEngine.BlockAbstract the source fragments, verbatim or immediate typed text blocks.(package private) static classTemplateEngine.BlockTypeThe enum capturing the difference between verbatim and code source fragments.(package private) classTemplateEngine.CompositeExpressionA composite unified expression: "...(package private) classTemplateEngine.ConstantExpressionA constant unified expression.(package private) classTemplateEngine.DeferredExpressionA deferred unified expression: #{jexl}.(package private) static classTemplateEngine.ExpressionBuilderA helper class to build expressions.(package private) static classTemplateEngine.ExpressionTypeTypes of expressions.(package private) classTemplateEngine.ImmediateExpressionAn immediate unified expression: ${jexl}.(package private) classTemplateEngine.JexlBasedExpressionThe base for JEXL based unified expressions.(package private) classTemplateEngine.NestedExpressionAn immediate unified expression nested into a deferred unified expression.private static classTemplateEngine.ParseStateThe different parsing states.(package private) classTemplateEngine.TemplateExpressionThe abstract base class for all unified expressions, immediate '${...}' and deferred '#{...}'.-
Nested classes/interfaces inherited from class org.apache.commons.jexl3.JxltEngine
JxltEngine.Exception, JxltEngine.Expression, JxltEngine.Template
-
-
Field Summary
Fields Modifier and Type Field Description (package private) JexlCache<Source,java.lang.Object>cacheThe TemplateExpression cache.(package private) chardeferredCharThe first character for deferred expressions.(package private) charimmediateCharThe first character for immediate expressions.(package private) EnginejexlThe JEXL engine instance.(package private) org.apache.commons.logging.LogloggerThe logger.(package private) booleannoscriptWhether expressions can use JEXL script or only expressions (i.e., no for, var, etc).
-
Constructor Summary
Constructors Constructor Description TemplateEngine(Engine jexl, boolean noScript, int cacheSize, char immediate, char deferred)Creates a new instance ofJxltEnginecreating a local cache.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static intappend(java.lang.StringBuilder strb, java.lang.CharSequence expr, int position, char c)Helper for expression dealing with embedded strings.voidclearCache()Clears the cache.(package private) static JxltEngine.ExceptioncreateException(JexlInfo info, java.lang.String action, TemplateEngine.TemplateExpression expr, java.lang.Exception xany)Creates a JxltEngine.Exception from a JexlException.JxltEngine.ExpressioncreateExpression(JexlInfo jexlInfo, java.lang.String expression)Creates aJxltEngine.Expressionfrom an expression string.JxltEngine.ExpressioncreateExpression(JexlInfo jexlInfo, java.lang.String expression, Scope scope)TemplateScriptcreateTemplate(JexlInfo info, java.lang.String prefix, java.io.Reader source, java.lang.String... parms)Creates a new template.private java.lang.StringescapeString(java.lang.CharSequence str)(package private) chargetDeferredChar()EnginegetEngine()Gets the JexlEngine underlying this JxltEngine.(package private) chargetImmediateChar()private static booleanisIgnorable(char c)Determines whether the given character is considered ignorable whitespace in template expressions.(package private) TemplateEngine.TemplateExpressionparseExpression(JexlInfo info, java.lang.String expr, Scope scope)Parses a unified expression.(package private) static java.util.Iterator<java.lang.CharSequence>readLines(java.io.Reader reader)Reads lines from a (buffered / mark-able) reader keeping all new-lines and line-feeds.(package private) java.util.List<TemplateEngine.Block>readTemplate(java.lang.String prefix, java.io.Reader source)Reads lines of a template grouping them by typed blocks.(package private) intstartsWith(java.lang.CharSequence sequence, java.lang.CharSequence pattern)Tests whether a sequence starts with a given set of characters (following spaces).-
Methods inherited from class org.apache.commons.jexl3.JxltEngine
createExpression, createTemplate, createTemplate, createTemplate, createTemplate, createTemplate
-
-
-
-
Field Detail
-
jexl
final Engine jexl
The JEXL engine instance.
-
logger
final org.apache.commons.logging.Log logger
The logger.
-
immediateChar
final char immediateChar
The first character for immediate expressions.
-
deferredChar
final char deferredChar
The first character for deferred expressions.
-
noscript
final boolean noscript
Whether expressions can use JEXL script or only expressions (i.e., no for, var, etc).
-
-
Constructor Detail
-
TemplateEngine
public TemplateEngine(Engine jexl, boolean noScript, int cacheSize, char immediate, char deferred)
Creates a new instance ofJxltEnginecreating a local cache.- Parameters:
jexl- the JexlEngine to use.noScript- whether this engine only allows JEXL expressions or scriptscacheSize- the number of expressions in this cache, default is 256immediate- the immediate template expression character, default is '$'deferred- the deferred template expression character, default is '#'
-
-
Method Detail
-
append
private static int append(java.lang.StringBuilder strb, java.lang.CharSequence expr, int position, char c)Helper for expression dealing with embedded strings.- Parameters:
strb- the expression buffer to copy characters intoexpr- the sourceposition- the offset into the sourcec- the separator character- Returns:
- the new position to read the source from
-
createException
static JxltEngine.Exception createException(JexlInfo info, java.lang.String action, TemplateEngine.TemplateExpression expr, java.lang.Exception xany)
Creates a JxltEngine.Exception from a JexlException.- Parameters:
info- the source infoaction- createExpression, prepare, evaluateexpr- the template expressionxany- the exception- Returns:
- an exception containing an explicit error message
-
readLines
static java.util.Iterator<java.lang.CharSequence> readLines(java.io.Reader reader)
Reads lines from a (buffered / mark-able) reader keeping all new-lines and line-feeds.- Parameters:
reader- the reader- Returns:
- the line iterator
-
clearCache
public void clearCache()
Clears the cache.- Specified by:
clearCachein classJxltEngine
-
createExpression
public JxltEngine.Expression createExpression(JexlInfo jexlInfo, java.lang.String expression)
Description copied from class:JxltEngineCreates aJxltEngine.Expressionfrom an expression string. Uses and fills up the expression cache if any.If the underlying JEXL engine is silent, errors will be logged through its logger as warnings.
- Specified by:
createExpressionin classJxltEngine- Parameters:
jexlInfo- theJexlInfosource informationexpression- theJxltEngine.Templatestring expression- Returns:
- the
JxltEngine.Expression, null if silent and an error occurred
-
createExpression
public JxltEngine.Expression createExpression(JexlInfo jexlInfo, java.lang.String expression, Scope scope)
-
createTemplate
public TemplateScript createTemplate(JexlInfo info, java.lang.String prefix, java.io.Reader source, java.lang.String... parms)
Description copied from class:JxltEngineCreates a new template.- Specified by:
createTemplatein classJxltEngine- Parameters:
info- the jexl info (file, line, column)prefix- the directive prefixsource- the sourceparms- the parameter names- Returns:
- the template
-
getDeferredChar
char getDeferredChar()
- Returns:
- the deferred character
-
getEngine
public Engine getEngine()
Gets the JexlEngine underlying this JxltEngine.- Specified by:
getEnginein classJxltEngine- Returns:
- the JexlEngine
-
getImmediateChar
char getImmediateChar()
- Returns:
- the immediate character
-
parseExpression
TemplateEngine.TemplateExpression parseExpression(JexlInfo info, java.lang.String expr, Scope scope)
Parses a unified expression.- Parameters:
info- the source infoexpr- the string expressionscope- the template scope- Returns:
- the unified expression instance
- Throws:
JexlException- if an error occurs during parsing
-
escapeString
private java.lang.String escapeString(java.lang.CharSequence str)
-
isIgnorable
private static boolean isIgnorable(char c)
Determines whether the given character is considered ignorable whitespace in template expressions.The characters newline (
'\n'), carriage return ('\r'), tab ('\t') and form feed ('\f') are treated as ignorable within template expressions and are skipped by the parser. These characters are not ignored between the expression prefix ('$'or'#') and the opening brace {@code '{'}; in that position they influence parsing instead of being discarded.- Parameters:
c- the character to test- Returns:
trueif the character is ignorable,falseotherwise
-
readTemplate
java.util.List<TemplateEngine.Block> readTemplate(java.lang.String prefix, java.io.Reader source)
Reads lines of a template grouping them by typed blocks.- Parameters:
prefix- the directive prefixsource- the source reader- Returns:
- the list of blocks
-
startsWith
int startsWith(java.lang.CharSequence sequence, java.lang.CharSequence pattern)Tests whether a sequence starts with a given set of characters (following spaces).Space characters at beginning of line before the pattern are discarded.
- Parameters:
sequence- the sequencepattern- the pattern to match at start of sequence- Returns:
- the first position after end of pattern if it matches, -1 otherwise
-
-