|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.ibatis.ognl.Ognl
public abstract class Ognl
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 ognl.Ognl; import 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 }
Method Summary | |
---|---|
static java.util.Map |
addDefaultContext(java.lang.Object root,
ClassResolver classResolver,
java.util.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 java.util.Map |
addDefaultContext(java.lang.Object root,
ClassResolver classResolver,
TypeConverter converter,
java.util.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 java.util.Map |
addDefaultContext(java.lang.Object root,
ClassResolver classResolver,
TypeConverter converter,
MemberAccess memberAccess,
java.util.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 java.util.Map |
addDefaultContext(java.lang.Object root,
java.util.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 java.util.Map |
createDefaultContext(java.lang.Object root)
Creates and returns a new standard naming context for evaluating an OGNL expression. |
static java.util.Map |
createDefaultContext(java.lang.Object root,
ClassResolver classResolver)
Creates and returns a new standard naming context for evaluating an OGNL expression. |
static java.util.Map |
createDefaultContext(java.lang.Object root,
ClassResolver classResolver,
TypeConverter converter)
Creates and returns a new standard naming context for evaluating an OGNL expression. |
static java.util.Map |
createDefaultContext(java.lang.Object root,
ClassResolver classResolver,
TypeConverter converter,
MemberAccess memberAccess)
Creates and returns a new standard naming context for evaluating an OGNL expression. |
static ClassResolver |
getClassResolver(java.util.Map context)
|
static Evaluation |
getLastEvaluation(java.util.Map context)
|
static MemberAccess |
getMemberAccess(java.util.Map context)
|
static java.lang.Object |
getRoot(java.util.Map context)
|
static TypeConverter |
getTypeConverter(java.util.Map context)
|
static java.lang.Object |
getValue(java.lang.Object tree,
java.util.Map context,
java.lang.Object root)
Evaluates the given OGNL expression tree to extract a value from the given root object. |
static java.lang.Object |
getValue(java.lang.Object tree,
java.util.Map context,
java.lang.Object root,
java.lang.Class resultType)
Evaluates the given OGNL expression tree to extract a value from the given root object. |
static java.lang.Object |
getValue(java.lang.Object tree,
java.lang.Object root)
Evaluates the given OGNL expression tree to extract a value from the given root object. |
static java.lang.Object |
getValue(java.lang.Object tree,
java.lang.Object root,
java.lang.Class resultType)
Evaluates the given OGNL expression tree to extract a value from the given root object. |
static java.lang.Object |
getValue(java.lang.String expression,
java.util.Map context,
java.lang.Object root)
Evaluates the given OGNL expression to extract a value from the given root object in a given context |
static java.lang.Object |
getValue(java.lang.String expression,
java.util.Map context,
java.lang.Object root,
java.lang.Class resultType)
Evaluates the given OGNL expression to extract a value from the given root object in a given context |
static java.lang.Object |
getValue(java.lang.String expression,
java.lang.Object root)
Convenience method that combines calls to parseExpression and
getValue . |
static java.lang.Object |
getValue(java.lang.String expression,
java.lang.Object root,
java.lang.Class resultType)
Convenience method that combines calls to parseExpression and
getValue . |
static boolean |
isConstant(java.lang.Object tree)
|
static boolean |
isConstant(java.lang.Object tree,
java.util.Map context)
|
static boolean |
isConstant(java.lang.String expression)
|
static boolean |
isConstant(java.lang.String expression,
java.util.Map context)
|
static boolean |
isSimpleNavigationChain(java.lang.Object tree)
|
static boolean |
isSimpleNavigationChain(java.lang.Object tree,
java.util.Map context)
|
static boolean |
isSimpleNavigationChain(java.lang.String expression)
|
static boolean |
isSimpleNavigationChain(java.lang.String expression,
java.util.Map context)
|
static boolean |
isSimpleProperty(java.lang.Object tree)
|
static boolean |
isSimpleProperty(java.lang.Object tree,
java.util.Map context)
|
static boolean |
isSimpleProperty(java.lang.String expression)
|
static boolean |
isSimpleProperty(java.lang.String expression,
java.util.Map context)
|
static java.lang.Object |
parseExpression(java.lang.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(java.util.Map context,
ClassResolver classResolver)
|
static void |
setMemberAccess(java.util.Map context,
MemberAccess memberAccess)
|
static void |
setRoot(java.util.Map context,
java.lang.Object root)
|
static void |
setTypeConverter(java.util.Map context,
TypeConverter converter)
|
static void |
setValue(java.lang.Object tree,
java.util.Map context,
java.lang.Object root,
java.lang.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(java.lang.Object tree,
java.lang.Object root,
java.lang.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(java.lang.String expression,
java.util.Map context,
java.lang.Object root,
java.lang.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(java.lang.String expression,
java.lang.Object root,
java.lang.Object value)
Convenience method that combines calls to parseExpression and
setValue . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static java.lang.Object parseExpression(java.lang.String expression) throws OgnlException
Ognl
static methods.
expression
- the OGNL expression to be parsed
ExpressionSyntaxException
- if the expression is malformed
OgnlException
- if there is a pathological environmental problempublic static java.util.Map createDefaultContext(java.lang.Object root)
root
- the root of the object graph
root
and context
set appropriatelypublic static java.util.Map createDefaultContext(java.lang.Object root, ClassResolver classResolver)
root
- the root of the object graph
root
and context
set appropriatelypublic static java.util.Map createDefaultContext(java.lang.Object root, ClassResolver classResolver, TypeConverter converter)
root
- the root of the object graph
root
and context
set appropriatelypublic static java.util.Map createDefaultContext(java.lang.Object root, ClassResolver classResolver, TypeConverter converter, MemberAccess memberAccess)
root
- the root of the object graph
root
and context
set appropriatelypublic static java.util.Map addDefaultContext(java.lang.Object root, java.util.Map context)
root
- the root of the object graphcontext
- the context to which OGNL context will be added.
root
and context
set appropriatelypublic static java.util.Map addDefaultContext(java.lang.Object root, ClassResolver classResolver, java.util.Map context)
root
- the root of the object graphcontext
- the context to which OGNL context will be added.
root
and context
set appropriatelypublic static java.util.Map addDefaultContext(java.lang.Object root, ClassResolver classResolver, TypeConverter converter, java.util.Map context)
root
- the root of the object graphcontext
- the context to which OGNL context will be added.
root
and context
set appropriatelypublic static java.util.Map addDefaultContext(java.lang.Object root, ClassResolver classResolver, TypeConverter converter, MemberAccess memberAccess, java.util.Map context)
root
- the root of the object graphcontext
- the context to which OGNL context will be added.
root
and context
set appropriatelypublic static void setClassResolver(java.util.Map context, ClassResolver classResolver)
public static ClassResolver getClassResolver(java.util.Map context)
public static void setTypeConverter(java.util.Map context, TypeConverter converter)
public static TypeConverter getTypeConverter(java.util.Map context)
public static void setMemberAccess(java.util.Map context, MemberAccess memberAccess)
public static MemberAccess getMemberAccess(java.util.Map context)
public static void setRoot(java.util.Map context, java.lang.Object root)
public static java.lang.Object getRoot(java.util.Map context)
public static Evaluation getLastEvaluation(java.util.Map context)
public static java.lang.Object getValue(java.lang.Object tree, java.util.Map context, java.lang.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 expression
MethodFailedException
- if the expression called a method which failed
NoSuchPropertyException
- if the expression referred to a nonexistent property
InappropriateExpressionException
- if the expression can't be used in this context
OgnlException
- if there is a pathological environmental problempublic static java.lang.Object getValue(java.lang.Object tree, java.util.Map context, java.lang.Object root, java.lang.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 converter
MethodFailedException
- if the expression called a method which failed
NoSuchPropertyException
- if the expression referred to a nonexistent property
InappropriateExpressionException
- if the expression can't be used in this context
OgnlException
- if there is a pathological environmental problempublic static java.lang.Object getValue(java.lang.String expression, java.util.Map context, java.lang.Object root) throws OgnlException
expression
- the OGNL expression to be parsedcontext
- the naming context for the evaluationroot
- the root object for the OGNL expression
MethodFailedException
- if the expression called a method which failed
NoSuchPropertyException
- if the expression referred to a nonexistent property
InappropriateExpressionException
- if the expression can't be used in this context
OgnlException
- if there is a pathological environmental problemparseExpression(String)
,
getValue(Object,Object)
public static java.lang.Object getValue(java.lang.String expression, java.util.Map context, java.lang.Object root, java.lang.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 converter
MethodFailedException
- if the expression called a method which failed
NoSuchPropertyException
- if the expression referred to a nonexistent property
InappropriateExpressionException
- if the expression can't be used in this context
OgnlException
- if there is a pathological environmental problemparseExpression(String)
,
getValue(Object,Object)
public static java.lang.Object getValue(java.lang.Object tree, java.lang.Object root) throws OgnlException
tree
- the OGNL expression tree to evaluate, as returned by parseExpression()root
- the root object for the OGNL expression
MethodFailedException
- if the expression called a method which failed
NoSuchPropertyException
- if the expression referred to a nonexistent property
InappropriateExpressionException
- if the expression can't be used in this context
OgnlException
- if there is a pathological environmental problempublic static java.lang.Object getValue(java.lang.Object tree, java.lang.Object root, java.lang.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 converter
MethodFailedException
- if the expression called a method which failed
NoSuchPropertyException
- if the expression referred to a nonexistent property
InappropriateExpressionException
- if the expression can't be used in this context
OgnlException
- if there is a pathological environmental problempublic static java.lang.Object getValue(java.lang.String expression, java.lang.Object root) throws OgnlException
parseExpression
and
getValue
.
expression
- the OGNL expression to be parsedroot
- the root object for the OGNL expression
ExpressionSyntaxException
- if the expression is malformed
MethodFailedException
- if the expression called a method which failed
NoSuchPropertyException
- if the expression referred to a nonexistent property
InappropriateExpressionException
- if the expression can't be used in this context
OgnlException
- if there is a pathological environmental problemparseExpression(String)
,
getValue(Object,Object)
public static java.lang.Object getValue(java.lang.String expression, java.lang.Object root, java.lang.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 converter
ExpressionSyntaxException
- if the expression is malformed
MethodFailedException
- if the expression called a method which failed
NoSuchPropertyException
- if the expression referred to a nonexistent property
InappropriateExpressionException
- if the expression can't be used in this context
OgnlException
- if there is a pathological environmental problemparseExpression(String)
,
getValue(Object,Object)
public static void setValue(java.lang.Object tree, java.util.Map context, java.lang.Object root, java.lang.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 graph
MethodFailedException
- if the expression called a method which failed
NoSuchPropertyException
- if the expression referred to a nonexistent property
InappropriateExpressionException
- if the expression can't be used in this context
OgnlException
- if there is a pathological environmental problempublic static void setValue(java.lang.String expression, java.util.Map context, java.lang.Object root, java.lang.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 graph
MethodFailedException
- if the expression called a method which failed
NoSuchPropertyException
- if the expression referred to a nonexistent property
InappropriateExpressionException
- if the expression can't be used in this context
OgnlException
- if there is a pathological environmental problempublic static void setValue(java.lang.Object tree, java.lang.Object root, java.lang.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 graph
MethodFailedException
- if the expression called a method which failed
NoSuchPropertyException
- if the expression referred to a nonexistent property
InappropriateExpressionException
- if the expression can't be used in this context
OgnlException
- if there is a pathological environmental problempublic static void setValue(java.lang.String expression, java.lang.Object root, java.lang.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 graph
ExpressionSyntaxException
- if the expression is malformed
MethodFailedException
- if the expression called a method which failed
NoSuchPropertyException
- if the expression referred to a nonexistent property
InappropriateExpressionException
- if the expression can't be used in this context
OgnlException
- if there is a pathological environmental problemparseExpression(String)
,
setValue(Object,Object,Object)
public static boolean isConstant(java.lang.Object tree, java.util.Map context) throws OgnlException
OgnlException
public static boolean isConstant(java.lang.String expression, java.util.Map context) throws OgnlException
OgnlException
public static boolean isConstant(java.lang.Object tree) throws OgnlException
OgnlException
public static boolean isConstant(java.lang.String expression) throws OgnlException
OgnlException
public static boolean isSimpleProperty(java.lang.Object tree, java.util.Map context) throws OgnlException
OgnlException
public static boolean isSimpleProperty(java.lang.String expression, java.util.Map context) throws OgnlException
OgnlException
public static boolean isSimpleProperty(java.lang.Object tree) throws OgnlException
OgnlException
public static boolean isSimpleProperty(java.lang.String expression) throws OgnlException
OgnlException
public static boolean isSimpleNavigationChain(java.lang.Object tree, java.util.Map context) throws OgnlException
OgnlException
public static boolean isSimpleNavigationChain(java.lang.String expression, java.util.Map context) throws OgnlException
OgnlException
public static boolean isSimpleNavigationChain(java.lang.Object tree) throws OgnlException
OgnlException
public static boolean isSimpleNavigationChain(java.lang.String expression) throws OgnlException
OgnlException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |