public final class UnifiedJEXL
extends java.lang.Object
An expression can mix immediate, deferred and nested sub-expressions as well as string constants;
"...${jexl-expr}..."
"...#{jexl-expr}..."
"...#{...${jexl-expr0}...}..."
"...${jexl-expr0}... #{jexl-expr1}..."
Deferred & immediate expression carry different intentions:
For instance: "Hello ${name}, now is #{time}"
is a composite "deferred" expression since one
of its subexpressions is deferred. Furthermore, this (composite) expression intent is
to perform two evaluations; one close to its definition and another one in a later
phase.
The API reflects this feature in 2 methods, prepare and evaluate. The prepare method will evaluate the immediate subexpression and return an expression that contains only the deferred subexpressions (& constants), a prepared expression. Such a prepared expression is suitable for a later phase evaluation that may occur with a different JexlContext. Note that it is valid to call evaluate without prepare in which case the same JexlContext is used for the 2 evaluation phases.
In the most common use-case where deferred expressions are to be kept around as properties of objects, one should parse & prepare an expression before storing it and evaluate it each time the property storing it is accessed.
Note that nested expression use the JEXL syntax as in:
"#{${bar}+'.charAt(2)'}"
The most common mistake leading to an invalid expression being the following:
"#{${bar}charAt(2)}"
Also note that methods that parse evaluate expressions may throw unchecked exceptions;
The UnifiedJEXL.Exception
are thrown when the engine instance is in "non-silent" mode
but since these are RuntimeException, user-code should catch them where appropriate.
Modifier and Type | Class and Description |
---|---|
private static class |
UnifiedJEXL.BlockType
The enum capturing the difference between verbatim and code source fragments.
|
private class |
UnifiedJEXL.CompositeExpression
A composite expression: "...
|
private class |
UnifiedJEXL.ConstantExpression
A constant expression.
|
private class |
UnifiedJEXL.DeferredExpression
A deferred expression: #{jexl}.
|
static class |
UnifiedJEXL.Exception
The sole type of (runtime) exception the UnifiedJEXL can throw.
|
class |
UnifiedJEXL.Expression
The abstract base class for all expressions, immediate '${...}' and deferred '#{...}'.
|
private static class |
UnifiedJEXL.ExpressionBuilder
A helper class to build expressions.
|
private static class |
UnifiedJEXL.ExpressionType
Types of expressions.
|
private class |
UnifiedJEXL.ImmediateExpression
An immediate expression: ${jexl}.
|
private class |
UnifiedJEXL.JexlBasedExpression
The base for Jexl based expressions.
|
private class |
UnifiedJEXL.NestedExpression
An immediate expression nested into a deferred expression.
|
private static class |
UnifiedJEXL.ParseState
The different parsing states.
|
class |
UnifiedJEXL.Template
A Template is a script that evaluates by writing its content through a Writer.
|
private static class |
UnifiedJEXL.TemplateBlock
Abstract the source fragments, verbatim or immediate typed text blocks.
|
class |
UnifiedJEXL.TemplateContext
The type of context to use during evaluation of templates.
|
Modifier and Type | Field and Description |
---|---|
private JexlEngine.SoftCache<java.lang.String,UnifiedJEXL.Expression> |
cache
The expression cache.
|
private static int |
CACHE_SIZE
The default cache size.
|
private static char |
DEF_CHAR
The first character for deferred expressions.
|
private static char |
IMM_CHAR
The first character for immediate expressions.
|
private JexlEngine |
jexl
The JEXL engine instance.
|
Constructor and Description |
---|
UnifiedJEXL(JexlEngine aJexl)
Creates a new instance of UnifiedJEXL with a default size cache.
|
UnifiedJEXL(JexlEngine aJexl,
int cacheSize)
Creates a new instance of UnifiedJEXL creating a local cache.
|
Modifier and Type | Method and Description |
---|---|
void |
clearCache()
Clears the cache.
|
private UnifiedJEXL.Exception |
createException(java.lang.String action,
UnifiedJEXL.Expression expr,
java.lang.Exception xany)
Creates a UnifiedJEXL.Exception from a JexlException.
|
UnifiedJEXL.Template |
createTemplate(java.lang.String source)
Creates a new template.
|
UnifiedJEXL.Template |
createTemplate(java.lang.String prefix,
java.io.Reader source,
java.lang.String... parms)
Creates a new template.
|
UnifiedJEXL.Template |
createTemplate(java.lang.String source,
java.lang.String... parms)
Creates a new template.
|
JexlEngine |
getEngine()
Gets the JexlEngine underlying the UnifiedJEXL.
|
UnifiedJEXL.Expression |
parse(java.lang.String expression)
Creates a a
UnifiedJEXL.Expression from an expression string. |
private UnifiedJEXL.Expression |
parseExpression(java.lang.String expr,
JexlEngine.Scope scope)
Parses a unified expression.
|
protected java.util.List<UnifiedJEXL.TemplateBlock> |
readTemplate(java.lang.String prefix,
java.io.Reader source)
Reads lines of a template grouping them by typed blocks.
|
protected int |
startsWith(java.lang.CharSequence sequence,
java.lang.CharSequence pattern)
Whether a sequence starts with a given set of characters (following spaces).
|
private final JexlEngine jexl
private final JexlEngine.SoftCache<java.lang.String,UnifiedJEXL.Expression> cache
private static final int CACHE_SIZE
private static final char IMM_CHAR
private static final char DEF_CHAR
public UnifiedJEXL(JexlEngine aJexl)
aJexl
- the JexlEngine to use.public UnifiedJEXL(JexlEngine aJexl, int cacheSize)
aJexl
- the JexlEngine to use.cacheSize
- the number of expressions in this cachepublic JexlEngine getEngine()
public void clearCache()
public UnifiedJEXL.Expression parse(java.lang.String expression)
UnifiedJEXL.Expression
from an expression string.
Uses & fills up the expression cache if any.
If the underlying JEXL engine is silent, errors will be logged through its logger as warnings.
expression
- the UnifiedJEXL string expressionUnifiedJEXL.Exception
- if an error occurs and the JexlEngine
is not silentprivate UnifiedJEXL.Exception createException(java.lang.String action, UnifiedJEXL.Expression expr, java.lang.Exception xany)
action
- parse, prepare, evaluateexpr
- the expressionxany
- the exceptionprivate UnifiedJEXL.Expression parseExpression(java.lang.String expr, JexlEngine.Scope scope)
expr
- the string expressionscope
- the expression scopeJexlException
- if an error occur during parsingprotected int startsWith(java.lang.CharSequence sequence, java.lang.CharSequence pattern)
Space characters at beginning of line before the pattern are discarded.
sequence
- the sequencepattern
- the pattern to match at start of sequenceprotected java.util.List<UnifiedJEXL.TemplateBlock> readTemplate(java.lang.String prefix, java.io.Reader source)
prefix
- the directive prefixsource
- the source readerpublic UnifiedJEXL.Template createTemplate(java.lang.String prefix, java.io.Reader source, java.lang.String... parms)
prefix
- the directive prefixsource
- the sourceparms
- the parameter namespublic UnifiedJEXL.Template createTemplate(java.lang.String source, java.lang.String... parms)
source
- the sourceparms
- the parameter namespublic UnifiedJEXL.Template createTemplate(java.lang.String source)
source
- the source