public abstract class Ognl extends Object
This class provides static methods for parsing and interpreting OGNL expressions.
The simplest use of the Ognl class is to get the value of an expression from an object, without extra context or pre-parsing.
import org.apache.commons.ognl.Ognl; import org.apache.commons.ognl.OgnlException; try { result = Ognl.getValue(expression, root); } catch (OgnlException ex) { // Report error or recover }
This will parse the expression given and evaluate it against the root object given, returning the
result. If there is an error in the expression, such as the property is not found, the exception
is encapsulated into an OgnlException
.
Other more sophisticated uses of Ognl can pre-parse expressions. This provides two advantages: in
the case of user-supplied expressions it allows you to catch parse errors before evaluation and
it allows you to cache parsed expressions into an AST for better speed during repeated use. The
pre-parsed expression is always returned as an Object
to simplify use for programs
that just wish to store the value for repeated use and do not care that it is an AST. If it does
care it can always safely cast the value to an AST
type.
The Ognl class also takes a context map as one of the parameters to the set and get
methods. This allows you to put your own variables into the available namespace for OGNL
expressions. The default context contains only the #root
and #context
keys, which are required to be present. The addDefaultContext(Object, Map)
method
will alter an existing Map
to put the defaults in. Here is an example that shows
how to extract the documentName
property out of the root object and append a
string with the current user name in parens:
private Map context = new HashMap(); public void setUserName(String value) { context.put("userName", value); } try { // get value using our own custom context map result = Ognl.getValue("documentName + \" (\" + ((#userName == null) ? \"<nobody>\" : #userName) + \")\"", context, root); } catch (OgnlException ex) { // Report error or recover }
Modifier and Type | Method and Description |
---|---|
static Map |
addDefaultContext(Object root,
ClassResolver classResolver,
Map context)
Appends the standard naming context for evaluating an OGNL expression into the context given
so that cached maps can be used as a context.
|
static Map |
addDefaultContext(Object root,
ClassResolver classResolver,
TypeConverter converter,
Map context)
Appends the standard naming context for evaluating an OGNL expression into the context given
so that cached maps can be used as a context.
|
static Map |
addDefaultContext(Object root,
ClassResolver classResolver,
TypeConverter converter,
MemberAccess memberAccess,
Map context)
Appends the standard naming context for evaluating an OGNL expression into the context given
so that cached maps can be used as a context.
|
static Map |
addDefaultContext(Object root,
Map context)
Appends the standard naming context for evaluating an OGNL expression into the context given
so that cached maps can be used as a context.
|
static Node |
compileExpression(OgnlContext context,
Object root,
String expression)
Parses and compiles the given expression using the
ognl.enhance.OgnlExpressionCompiler returned
from ognl.OgnlRuntime#getCompiler() . |
static Map |
createDefaultContext(Object root)
Creates and returns a new standard naming context for evaluating an OGNL expression.
|
static Map |
createDefaultContext(Object root,
ClassResolver classResolver)
Creates and returns a new standard naming context for evaluating an OGNL expression.
|
static Map |
createDefaultContext(Object root,
ClassResolver classResolver,
TypeConverter converter)
Creates and returns a new standard naming context for evaluating an OGNL expression.
|
static Map |
createDefaultContext(Object root,
ClassResolver classResolver,
TypeConverter converter,
MemberAccess memberAccess)
Creates and returns a new standard naming context for evaluating an OGNL expression.
|
static ClassResolver |
getClassResolver(Map context)
Gets the previously stored
ClassResolver for the given context - if any. |
static Evaluation |
getLastEvaluation(Map context)
Gets the last
Evaluation executed on the given context. |
static MemberAccess |
getMemberAccess(Map context)
Gets the currently stored
MemberAccess object for the given context - if any. |
static Object |
getRoot(Map context)
Gets the stored root object for the given context - if any.
|
static TypeConverter |
getTypeConverter(Map context)
Gets the currently configured
TypeConverter for the given context - if any. |
static Object |
getValue(ExpressionAccessor expression,
OgnlContext context,
Object root)
Gets the value represented by the given pre-compiled expression on the specified root
object.
|
static Object |
getValue(ExpressionAccessor expression,
OgnlContext context,
Object root,
Class resultType)
Gets the value represented by the given pre-compiled expression on the specified root
object.
|
static Object |
getValue(Object tree,
Map context,
Object root)
Evaluates the given OGNL expression tree to extract a value from the given root object.
|
static Object |
getValue(Object tree,
Map context,
Object root,
Class resultType)
Evaluates the given OGNL expression tree to extract a value from the given root object.
|
static Object |
getValue(Object tree,
Object root)
Evaluates the given OGNL expression tree to extract a value from the given root object.
|
static Object |
getValue(Object tree,
Object root,
Class resultType)
Evaluates the given OGNL expression tree to extract a value from the given root object.
|
static Object |
getValue(String expression,
Map context,
Object root)
Evaluates the given OGNL expression to extract a value from the given root object in a given
context
|
static Object |
getValue(String expression,
Map context,
Object root,
Class resultType)
Evaluates the given OGNL expression to extract a value from the given root object in a given
context
|
static Object |
getValue(String expression,
Object root)
Convenience method that combines calls to
parseExpression and
getValue . |
static Object |
getValue(String expression,
Object root,
Class resultType)
Convenience method that combines calls to
parseExpression and
getValue . |
static boolean |
isConstant(Object tree)
Same as
isConstant(Object, java.util.Map) - only the Map context
is created for you. |
static boolean |
isConstant(Object tree,
Map context)
Checks if the specified
Node instance represents a constant
expression. |
static boolean |
isConstant(String expression)
Same as
isConstant(String, java.util.Map) - only the Map
instance is created for you. |
static boolean |
isConstant(String expression,
Map context)
Checks if the specified expression represents a constant expression.
|
static boolean |
isSimpleNavigationChain(Object tree) |
static boolean |
isSimpleNavigationChain(Object tree,
Map context) |
static boolean |
isSimpleNavigationChain(String expression) |
static boolean |
isSimpleNavigationChain(String expression,
Map context) |
static boolean |
isSimpleProperty(Object tree) |
static boolean |
isSimpleProperty(Object tree,
Map context) |
static boolean |
isSimpleProperty(String expression) |
static boolean |
isSimpleProperty(String expression,
Map context) |
static Object |
parseExpression(String expression)
Parses the given OGNL expression and returns a tree representation of the expression that can
be used by
Ognl static methods. |
static void |
setClassResolver(Map context,
ClassResolver classResolver)
Configures the
ClassResolver to use for the given context. |
static void |
setMemberAccess(Map context,
MemberAccess memberAccess)
Configures the specified context with a
MemberAccess instance for
handling field/method protection levels. |
static void |
setRoot(Map context,
Object root)
Sets the root object to use for all expressions in the given context - doesn't necessarily replace
root object instances explicitly passed in to other expression resolving methods on this class.
|
static void |
setTypeConverter(Map context,
TypeConverter converter)
Configures the type converter to use for a given context.
|
static void |
setValue(ExpressionAccessor expression,
OgnlContext context,
Object root,
Object value)
Sets the value given using the pre-compiled expression on the specified root
object.
|
static void |
setValue(Object tree,
Map context,
Object root,
Object value)
Evaluates the given OGNL expression tree to insert a value into the object graph rooted at
the given root object.
|
static void |
setValue(Object tree,
Object root,
Object value)
Evaluates the given OGNL expression tree to insert a value into the object graph rooted at
the given root object.
|
static void |
setValue(String expression,
Map context,
Object root,
Object value)
Evaluates the given OGNL expression to insert a value into the object graph rooted at the
given root object given the context.
|
static void |
setValue(String expression,
Object root,
Object value)
Convenience method that combines calls to
parseExpression and
setValue . |
public static Object parseExpression(String expression) throws OgnlException
Ognl
static methods.expression
- the OGNL expression to be parsedExpressionSyntaxException
- if the expression is malformedOgnlException
- if there is a pathological environmental problempublic static Node compileExpression(OgnlContext context, Object root, String expression) throws Exception
ognl.enhance.OgnlExpressionCompiler
returned
from ognl.OgnlRuntime#getCompiler()
.context
- The context to use.root
- The root object for the given expression.expression
- The expression to compile.ognl.Node#getAccessor()
if compilation
was successfull. In instances where compilation wasn't possible because of a partially null
expression the ExpressionAccessor
instance may be null and the compilation of this expression
still possible at some as yet indertermined point in the future.Exception
- If a compilation error occurs.public static Map createDefaultContext(Object root)
root
- the root of the object graphroot
and context
set
appropriatelypublic static Map createDefaultContext(Object root, ClassResolver classResolver)
root
- The root of the object graph.classResolver
- The resolver used to instantiate Class
instances referenced in the expression.root
and context
set
appropriatelypublic static Map createDefaultContext(Object root, ClassResolver classResolver, TypeConverter converter)
root
- The root of the object graph.classResolver
- The resolver used to instantiate Class
instances referenced in the expression.converter
- Converter used to convert return types of an expression in to their desired types.root
and context
set
appropriatelypublic static Map createDefaultContext(Object root, ClassResolver classResolver, TypeConverter converter, MemberAccess memberAccess)
root
- The root of the object graph.classResolver
- The resolver used to instantiate Class
instances referenced in the expression.converter
- Converter used to convert return types of an expression in to their desired types.memberAccess
- Java security handling object to determine semantics for accessing normally private/protected
methods / fields.root
and context
set
appropriatelypublic static Map addDefaultContext(Object root, Map context)
root
- the root of the object graphcontext
- the context to which OGNL context will be added.root
and context
set
appropriatelypublic static Map addDefaultContext(Object root, ClassResolver classResolver, Map context)
root
- The root of the object graph.classResolver
- The resolver used to instantiate Class
instances referenced in the expression.context
- The context to which OGNL context will be added.root
and context
set
appropriatelypublic static Map addDefaultContext(Object root, ClassResolver classResolver, TypeConverter converter, Map context)
root
- The root of the object graph.classResolver
- The resolver used to instantiate Class
instances referenced in the expression.converter
- Converter used to convert return types of an expression in to their desired types.context
- The context to which OGNL context will be added.root
and context
set
appropriatelypublic static Map addDefaultContext(Object root, ClassResolver classResolver, TypeConverter converter, MemberAccess memberAccess, Map context)
root
- the root of the object graphclassResolver
- The class loading resolver that should be used to resolve class references.converter
- The type converter to be used by default.memberAccess
- Definition for handling private/protected access.context
- Default context to use, if not an OgnlContext
will be dumped into
a new OgnlContext
object.root
and context
set
appropriatelypublic static void setClassResolver(Map context, ClassResolver classResolver)
ClassResolver
to use for the given context. Will be used during
expression parsing / execution to resolve class names.context
- The context to place the resolver.classResolver
- The resolver to use to resolve classes.public static ClassResolver getClassResolver(Map context)
ClassResolver
for the given context - if any.context
- The context to get the configured resolver from.public static void setTypeConverter(Map context, TypeConverter converter)
context
- The context to configure it for.converter
- The converter to use.public static TypeConverter getTypeConverter(Map context)
TypeConverter
for the given context - if any.context
- The context to get the converter from.public static void setMemberAccess(Map context, MemberAccess memberAccess)
MemberAccess
instance for
handling field/method protection levels.context
- The context to configure.memberAccess
- The access resolver to configure the context with.public static MemberAccess getMemberAccess(Map context)
MemberAccess
object for the given context - if any.context
- The context to get the object from.MemberAccess
instance in the specified context - or null if none found.public static void setRoot(Map context, Object root)
context
- The context to store the root object in.root
- The root object.public static Object getRoot(Map context)
context
- The context to get the root object from.public static Evaluation getLastEvaluation(Map context)
Evaluation
executed on the given context.context
- The context to get the evaluation from.Evaluation
- or null if none was found.public static Object getValue(Object tree, Map context, Object root) throws OgnlException
addDefaultContext()
.tree
- the OGNL expression tree to evaluate, as returned by parseExpression()context
- the naming context for the evaluationroot
- the root object for the OGNL expressionMethodFailedException
- if the expression called a method which failedNoSuchPropertyException
- if the expression referred to a nonexistent propertyInappropriateExpressionException
- if the expression can't be used in this contextOgnlException
- if there is a pathological environmental problempublic static Object getValue(Object tree, Map context, Object root, Class resultType) throws OgnlException
addDefaultContext()
.tree
- the OGNL expression tree to evaluate, as returned by parseExpression()context
- the naming context for the evaluationroot
- the root object for the OGNL expressionresultType
- the converted type of the resultant object, using the context's type converterMethodFailedException
- if the expression called a method which failedNoSuchPropertyException
- if the expression referred to a nonexistent propertyInappropriateExpressionException
- if the expression can't be used in this contextOgnlException
- if there is a pathological environmental problempublic static Object getValue(ExpressionAccessor expression, OgnlContext context, Object root)
expression
- The pre-compiled expression, as found in Node.getAccessor()
.context
- The ognl context.root
- The object to retrieve the expression value from.public static Object getValue(ExpressionAccessor expression, OgnlContext context, Object root, Class resultType)
expression
- The pre-compiled expression, as found in Node.getAccessor()
.context
- The ognl context.root
- The object to retrieve the expression value from.resultType
- The desired object type that the return value should be converted to using the getTypeConverter(java.util.Map)
}.public static Object getValue(String expression, Map context, Object root) throws OgnlException
expression
- the OGNL expression to be parsedcontext
- the naming context for the evaluationroot
- the root object for the OGNL expressionMethodFailedException
- if the expression called a method which failedNoSuchPropertyException
- if the expression referred to a nonexistent propertyInappropriateExpressionException
- if the expression can't be used in this contextOgnlException
- if there is a pathological environmental problemparseExpression(String)
,
getValue(Object,Object)
public static Object getValue(String expression, Map context, Object root, Class resultType) throws OgnlException
expression
- the OGNL expression to be parsedcontext
- the naming context for the evaluationroot
- the root object for the OGNL expressionresultType
- the converted type of the resultant object, using the context's type converterMethodFailedException
- if the expression called a method which failedNoSuchPropertyException
- if the expression referred to a nonexistent propertyInappropriateExpressionException
- if the expression can't be used in this contextOgnlException
- if there is a pathological environmental problemparseExpression(String)
,
getValue(Object,Object)
public static Object getValue(Object tree, Object root) throws OgnlException
tree
- the OGNL expression tree to evaluate, as returned by parseExpression()root
- the root object for the OGNL expressionMethodFailedException
- if the expression called a method which failedNoSuchPropertyException
- if the expression referred to a nonexistent propertyInappropriateExpressionException
- if the expression can't be used in this contextOgnlException
- if there is a pathological environmental problempublic static Object getValue(Object tree, Object root, Class resultType) throws OgnlException
tree
- the OGNL expression tree to evaluate, as returned by parseExpression()root
- the root object for the OGNL expressionresultType
- the converted type of the resultant object, using the context's type converterMethodFailedException
- if the expression called a method which failedNoSuchPropertyException
- if the expression referred to a nonexistent propertyInappropriateExpressionException
- if the expression can't be used in this contextOgnlException
- if there is a pathological environmental problempublic static Object getValue(String expression, Object root) throws OgnlException
parseExpression
and
getValue
.expression
- the OGNL expression to be parsedroot
- the root object for the OGNL expressionExpressionSyntaxException
- if the expression is malformedMethodFailedException
- if the expression called a method which failedNoSuchPropertyException
- if the expression referred to a nonexistent propertyInappropriateExpressionException
- if the expression can't be used in this contextOgnlException
- if there is a pathological environmental problemparseExpression(String)
,
getValue(Object,Object)
public static Object getValue(String expression, Object root, Class resultType) throws OgnlException
parseExpression
and
getValue
.expression
- the OGNL expression to be parsedroot
- the root object for the OGNL expressionresultType
- the converted type of the resultant object, using the context's type converterExpressionSyntaxException
- if the expression is malformedMethodFailedException
- if the expression called a method which failedNoSuchPropertyException
- if the expression referred to a nonexistent propertyInappropriateExpressionException
- if the expression can't be used in this contextOgnlException
- if there is a pathological environmental problemparseExpression(String)
,
getValue(Object,Object)
public static void setValue(Object tree, Map context, Object root, Object value) throws OgnlException
addDefaultContext()
.tree
- the OGNL expression tree to evaluate, as returned by parseExpression()context
- the naming context for the evaluationroot
- the root object for the OGNL expressionvalue
- the value to insert into the object graphMethodFailedException
- if the expression called a method which failedNoSuchPropertyException
- if the expression referred to a nonexistent propertyInappropriateExpressionException
- if the expression can't be used in this contextOgnlException
- if there is a pathological environmental problempublic static void setValue(ExpressionAccessor expression, OgnlContext context, Object root, Object value)
expression
- The pre-compiled expression, as found in Node.getAccessor()
.context
- The ognl context.root
- The object to set the expression value on.value
- The value to set.public static void setValue(String expression, Map context, Object root, Object value) throws OgnlException
expression
- the OGNL expression to be parsedroot
- the root object for the OGNL expressioncontext
- the naming context for the evaluationvalue
- the value to insert into the object graphMethodFailedException
- if the expression called a method which failedNoSuchPropertyException
- if the expression referred to a nonexistent propertyInappropriateExpressionException
- if the expression can't be used in this contextOgnlException
- if there is a pathological environmental problempublic static void setValue(Object tree, Object root, Object value) throws OgnlException
tree
- the OGNL expression tree to evaluate, as returned by parseExpression()root
- the root object for the OGNL expressionvalue
- the value to insert into the object graphMethodFailedException
- if the expression called a method which failedNoSuchPropertyException
- if the expression referred to a nonexistent propertyInappropriateExpressionException
- if the expression can't be used in this contextOgnlException
- if there is a pathological environmental problempublic static void setValue(String expression, Object root, Object value) throws OgnlException
parseExpression
and
setValue
.expression
- the OGNL expression to be parsedroot
- the root object for the OGNL expressionvalue
- the value to insert into the object graphExpressionSyntaxException
- if the expression is malformedMethodFailedException
- if the expression called a method which failedNoSuchPropertyException
- if the expression referred to a nonexistent propertyInappropriateExpressionException
- if the expression can't be used in this contextOgnlException
- if there is a pathological environmental problemparseExpression(String)
,
setValue(Object,Object,Object)
public static boolean isConstant(Object tree, Map context) throws OgnlException
Node
instance represents a constant
expression.tree
- The Node
to check.context
- The context to use.OgnlException
- If an error occurs checking the expression.public static boolean isConstant(String expression, Map context) throws OgnlException
expression
- The expression to check.context
- The context to use.OgnlException
- If an error occurs checking the expression.public static boolean isConstant(Object tree) throws OgnlException
isConstant(Object, java.util.Map)
- only the Map
context
is created for you.tree
- The Node
to check.OgnlException
- If an exception occurs.public static boolean isConstant(String expression) throws OgnlException
isConstant(String, java.util.Map)
- only the Map
instance is created for you.expression
- The expression to check.OgnlException
- If an exception occurs.public static boolean isSimpleProperty(Object tree, Map context) throws OgnlException
OgnlException
public static boolean isSimpleProperty(String expression, Map context) throws OgnlException
OgnlException
public static boolean isSimpleProperty(Object tree) throws OgnlException
OgnlException
public static boolean isSimpleProperty(String expression) throws OgnlException
OgnlException
public static boolean isSimpleNavigationChain(Object tree, Map context) throws OgnlException
OgnlException
public static boolean isSimpleNavigationChain(String expression, Map context) throws OgnlException
OgnlException
public static boolean isSimpleNavigationChain(Object tree) throws OgnlException
OgnlException
public static boolean isSimpleNavigationChain(String expression) throws OgnlException
OgnlException
Copyright © 1997–2021 The Apache Software Foundation. All rights reserved.