Class ExprLookup
java.lang.Object
org.apache.commons.configuration2.interpol.ExprLookup
- All Implemented Interfaces:
Lookup
Lookup that allows expressions to be evaluated.
ExprLookup.Variables vars = new ExprLookup.Variables(); vars.add(new ExprLookup.Variable("String", org.apache.commons.lang.StringUtils.class)); vars.add(new ExprLookup.Variable("Util", new Utility("Hello"))); vars.add(new ExprLookup.Variable("System", "Class:java.lang.System")); XMLConfiguration config = new XMLConfiguration(TEST_FILE); config.setLogger(log); ExprLookup lookup = new ExprLookup(vars); lookup.setConfiguration(config); String str = lookup.lookup("'$[element] ' + String.trimToEmpty('$[space.description]')");In the example above TEST_FILE contains xml that looks like:
<configuration> <element>value</element> <space xml:space="preserve"> <description xml:space="default"> Some text </description> </space> </configuration>The result will be "value Some text". This lookup uses Apache Commons Jexl and requires that the dependency be added to any projects which use this.
- Since:
- 1.7
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
The key and corresponding object that will be made available to the JexlContext for use in expressions.static class
List wrapper used to allow the Variables list to be created as beans in DefaultConfigurationBuilder. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
Prefix to identify a Java Class objectprivate static final String
The default prefix for subordinate lookup expressionsprivate static final String
The default suffix for subordinate lookup expressionsprivate final org.apache.commons.jexl3.JexlEngine
The engine.private ConfigurationInterpolator
The ConfigurationInterpolator used by this object.private ConfigurationLogger
The logger used by this instance.private String
The String to use to start subordinate lookup expressionsprivate org.apache.commons.text.StringSubstitutor
The StringSubstitutor for performing replace operations.private String
The String to use to terminate subordinate lookup expressionsprivate ExprLookup.Variables
The variables maintained by this object. -
Constructor Summary
ConstructorsConstructorDescriptionThe default constructor.Constructor for use by applications.ExprLookup
(ExprLookup.Variables list, String prefix, String suffix) Constructor for use by applications. -
Method Summary
Modifier and TypeMethodDescriptionprivate org.apache.commons.jexl3.JexlContext
Creates a newJexlContext
and initializes it with the variables managed by this Lookup object.Gets theConfigurationInterpolator
used by this object.Gets the logger used by this object.Gets the list of Variables that are accessible within expressions.private void
initializeContext
(org.apache.commons.jexl3.JexlContext ctx) Initializes the specified context with the variables managed by this Lookup object.private void
Creates aStringSubstitutor
object which uses the passed inConfigurationInterpolator
as lookup object.Evaluates the expression.void
setInterpolator
(ConfigurationInterpolator interpolator) Sets theConfigurationInterpolator
to be used by this object.void
setLogger
(ConfigurationLogger logger) Sets the logger to be used by this object.void
setVariablePrefixMatcher
(String prefix) Set the prefix to use to identify subordinate expressions.void
Add the Variables that will be accessible within expressions.void
setVariableSuffixMatcher
(String suffix) Set the suffix to use to identify subordinate expressions.
-
Field Details
-
CLASS
Prefix to identify a Java Class object- See Also:
-
DEFAULT_PREFIX
The default prefix for subordinate lookup expressions- See Also:
-
DEFAULT_SUFFIX
The default suffix for subordinate lookup expressions- See Also:
-
interpolator
The ConfigurationInterpolator used by this object. -
substitutor
private org.apache.commons.text.StringSubstitutor substitutorThe StringSubstitutor for performing replace operations. -
logger
The logger used by this instance. -
engine
private final org.apache.commons.jexl3.JexlEngine engineThe engine. -
variables
The variables maintained by this object. -
prefixMatcher
The String to use to start subordinate lookup expressions -
suffixMatcher
The String to use to terminate subordinate lookup expressions
-
-
Constructor Details
-
ExprLookup
public ExprLookup()The default constructor. Will get used when the Lookup is constructed via configuration. -
ExprLookup
Constructor for use by applications.- Parameters:
list
- The list of objects to be accessible in expressions.
-
ExprLookup
Constructor for use by applications.- Parameters:
list
- The list of objects to be accessible in expressions.prefix
- The prefix to use for subordinate lookups.suffix
- The suffix to use for subordinate lookups.
-
-
Method Details
-
setVariablePrefixMatcher
Set the prefix to use to identify subordinate expressions. This cannot be the same as the prefix used for the primary expression.- Parameters:
prefix
- The String identifying the beginning of the expression.
-
setVariableSuffixMatcher
Set the suffix to use to identify subordinate expressions. This cannot be the same as the suffix used for the primary expression.- Parameters:
suffix
- The String identifying the end of the expression.
-
setVariables
Add the Variables that will be accessible within expressions.- Parameters:
list
- The list of Variables.
-
getVariables
Gets the list of Variables that are accessible within expressions. This method returns a copy of the variables managed by this lookup; so modifying this object has no impact on this lookup.- Returns:
- the List of Variables that are accessible within expressions.
-
getLogger
Gets the logger used by this object.- Returns:
- the
Log
- Since:
- 2.0
-
setLogger
Sets the logger to be used by this object. If no logger is passed in, no log output is generated.- Parameters:
logger
- theLog
- Since:
- 2.0
-
getInterpolator
Gets theConfigurationInterpolator
used by this object.- Returns:
- the
ConfigurationInterpolator
- Since:
- 2.0
-
setInterpolator
Sets theConfigurationInterpolator
to be used by this object.- Parameters:
interpolator
- theConfigurationInterpolator
(may be null)- Since:
- 2.0
-
lookup
Evaluates the expression. -
installSubstitutor
Creates aStringSubstitutor
object which uses the passed inConfigurationInterpolator
as lookup object.- Parameters:
ip
- theConfigurationInterpolator
to be used
-
createContext
private org.apache.commons.jexl3.JexlContext createContext()Creates a newJexlContext
and initializes it with the variables managed by this Lookup object.- Returns:
- the newly created context
-
initializeContext
private void initializeContext(org.apache.commons.jexl3.JexlContext ctx) Initializes the specified context with the variables managed by this Lookup object.- Parameters:
ctx
- the context to be initialized
-