ariba.util.expr
Class Expr

java.lang.Object
  extended by ariba.util.expr.Expr

public abstract class Expr
extends java.lang.Object

This class provides static methods for parsing and interpreting AribaExpr expressions.

The simplest use of the Expr class is to get the value of an expression from an object, without extra context or pre-parsing.

 import ariba.util.expr.Expr;
 import ariba.util.expr.ExprException;

    try {
        result = Expr.getValue(expression, root);
    } catch (ExprException 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 ExprException.

Other more sophisticated uses of Expr 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 Expr 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 AribaExpr expressions. The default context contains only the THIS 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 = Expr.getValue("documentName + \" (\" + ((#userName == null) ? \"<nobody>\" : #userName) + \")\"", context, root);
     } catch (ExprException ex) {
         // Report error or recover
     }

 

Version:
27 June 1999
Author:
Luke Blanshard (blanshlu@netscape.net), Drew Davidson (drew@expr.org)

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 AribaExpr 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 AribaExpr 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 AribaExpr 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 AribaExpr 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 AribaExpr expression.
static java.util.Map createDefaultContext(java.lang.Object root, ClassResolver classResolver)
          Creates and returns a new standard naming context for evaluating an AribaExpr 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 AribaExpr 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 AribaExpr expression.
static java.util.Map createDefaultContext(java.lang.Object root, SymbolTable symbolTable)
          Creates and returns a new standard naming context for evaluating an AribaExpr 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 AribaExpr 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 AribaExpr 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 AribaExpr 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 AribaExpr 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 AribaExpr 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 AribaExpr 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 AribaExpr expression and returns a tree representation of the expression that can be used by Expr 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 AribaExpr 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 AribaExpr 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 AribaExpr 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
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

parseExpression

public static java.lang.Object parseExpression(java.lang.String expression)
                                        throws ExpressionSyntaxException
Parses the given AribaExpr expression and returns a tree representation of the expression that can be used by Expr static methods.

Parameters:
expression - the AribaExpr expression to be parsed
Returns:
a tree representation of the expression
Throws:
ExpressionSyntaxException - if the expression is malformed
ExpressionSyntaxException - if there is a pathological environmental problem

createDefaultContext

public static java.util.Map createDefaultContext(java.lang.Object root)
Creates and returns a new standard naming context for evaluating an AribaExpr expression.

Parameters:
root - the root of the object graph
Returns:
a new Map with the keys root and context set appropriately

createDefaultContext

public static java.util.Map createDefaultContext(java.lang.Object root,
                                                 SymbolTable symbolTable)
Creates and returns a new standard naming context for evaluating an AribaExpr expression.

Parameters:
root - the root of the object graph
Returns:
a new Map with the keys root and context set appropriately

createDefaultContext

public static java.util.Map createDefaultContext(java.lang.Object root,
                                                 ClassResolver classResolver)
Creates and returns a new standard naming context for evaluating an AribaExpr expression.

Parameters:
root - the root of the object graph
Returns:
a new ExprContext with the keys root and context set appropriately

createDefaultContext

public static java.util.Map createDefaultContext(java.lang.Object root,
                                                 ClassResolver classResolver,
                                                 TypeConverter converter)
Creates and returns a new standard naming context for evaluating an AribaExpr expression.

Parameters:
root - the root of the object graph
Returns:
a new Map with the keys root and context set appropriately

createDefaultContext

public 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 AribaExpr expression.

Parameters:
root - the root of the object graph
Returns:
a new Map with the keys root and context set appropriately

addDefaultContext

public static java.util.Map addDefaultContext(java.lang.Object root,
                                              java.util.Map context)
Appends the standard naming context for evaluating an AribaExpr expression into the context given so that cached maps can be used as a context.

Parameters:
root - the root of the object graph
context - the context to which AribaExpr context will be added.
Returns:
Context Map with the keys root and context set appropriately

addDefaultContext

public static java.util.Map addDefaultContext(java.lang.Object root,
                                              ClassResolver classResolver,
                                              java.util.Map context)
Appends the standard naming context for evaluating an AribaExpr expression into the context given so that cached maps can be used as a context.

Parameters:
root - the root of the object graph
context - the context to which AribaExpr context will be added.
Returns:
Context Map with the keys root and context set appropriately

addDefaultContext

public 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 AribaExpr expression into the context given so that cached maps can be used as a context.

Parameters:
root - the root of the object graph
context - the context to which AribaExpr context will be added.
Returns:
Context Map with the keys root and context set appropriately

addDefaultContext

public 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 AribaExpr expression into the context given so that cached maps can be used as a context.

Parameters:
root - the root of the object graph
context - the context to which AribaExpr context will be added.
Returns:
Context Map with the keys root and context set appropriately

setClassResolver

public static void setClassResolver(java.util.Map context,
                                    ClassResolver classResolver)

getClassResolver

public static ClassResolver getClassResolver(java.util.Map context)

setTypeConverter

public static void setTypeConverter(java.util.Map context,
                                    TypeConverter converter)

getTypeConverter

public static TypeConverter getTypeConverter(java.util.Map context)

setMemberAccess

public static void setMemberAccess(java.util.Map context,
                                   MemberAccess memberAccess)

getMemberAccess

public static MemberAccess getMemberAccess(java.util.Map context)

setRoot

public static void setRoot(java.util.Map context,
                           java.lang.Object root)

getRoot

public static java.lang.Object getRoot(java.util.Map context)

getLastEvaluation

public static Evaluation getLastEvaluation(java.util.Map context)

getValue

public static java.lang.Object getValue(java.lang.Object tree,
                                        java.util.Map context,
                                        java.lang.Object root)
                                 throws ExprException
Evaluates the given AribaExpr expression tree to extract a value from the given root object. The default context is set for the given context and root via addDefaultContext().

Parameters:
tree - the AribaExpr expression tree to evaluate, as returned by parseExpression()
context - the naming context for the evaluation
root - the root object for the AribaExpr expression
Returns:
the result of evaluating the expression
Throws:
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
ExprException - if there is a pathological environmental problem

getValue

public static java.lang.Object getValue(java.lang.Object tree,
                                        java.util.Map context,
                                        java.lang.Object root,
                                        java.lang.Class resultType)
                                 throws ExprException
Evaluates the given AribaExpr expression tree to extract a value from the given root object. The default context is set for the given context and root via addDefaultContext().

Parameters:
tree - the AribaExpr expression tree to evaluate, as returned by parseExpression()
context - the naming context for the evaluation
root - the root object for the AribaExpr expression
resultType - the converted type of the resultant object, using the context's type converter
Returns:
the result of evaluating the expression
Throws:
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
ExprException - if there is a pathological environmental problem

getValue

public static java.lang.Object getValue(java.lang.String expression,
                                        java.util.Map context,
                                        java.lang.Object root)
                                 throws ExprException
Evaluates the given AribaExpr expression to extract a value from the given root object in a given context

Parameters:
expression - the AribaExpr expression to be parsed
context - the naming context for the evaluation
root - the root object for the AribaExpr expression
Returns:
the result of evaluating the expression
Throws:
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
ExprException - if there is a pathological environmental problem
See Also:
parseExpression(String), getValue(Object,Object)

getValue

public static java.lang.Object getValue(java.lang.String expression,
                                        java.util.Map context,
                                        java.lang.Object root,
                                        java.lang.Class resultType)
                                 throws ExprException
Evaluates the given AribaExpr expression to extract a value from the given root object in a given context

Parameters:
expression - the AribaExpr expression to be parsed
context - the naming context for the evaluation
root - the root object for the AribaExpr expression
resultType - the converted type of the resultant object, using the context's type converter
Returns:
the result of evaluating the expression
Throws:
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
ExprException - if there is a pathological environmental problem
See Also:
parseExpression(String), getValue(Object,Object)

getValue

public static java.lang.Object getValue(java.lang.Object tree,
                                        java.lang.Object root)
                                 throws ExprException
Evaluates the given AribaExpr expression tree to extract a value from the given root object.

Parameters:
tree - the AribaExpr expression tree to evaluate, as returned by parseExpression()
root - the root object for the AribaExpr expression
Returns:
the result of evaluating the expression
Throws:
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
ExprException - if there is a pathological environmental problem

getValue

public static java.lang.Object getValue(java.lang.Object tree,
                                        java.lang.Object root,
                                        java.lang.Class resultType)
                                 throws ExprException
Evaluates the given AribaExpr expression tree to extract a value from the given root object.

Parameters:
tree - the AribaExpr expression tree to evaluate, as returned by parseExpression()
root - the root object for the AribaExpr expression
resultType - the converted type of the resultant object, using the context's type converter
Returns:
the result of evaluating the expression
Throws:
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
ExprException - if there is a pathological environmental problem

getValue

public static java.lang.Object getValue(java.lang.String expression,
                                        java.lang.Object root)
                                 throws ExprException
Convenience method that combines calls to parseExpression and getValue.

Parameters:
expression - the AribaExpr expression to be parsed
root - the root object for the AribaExpr expression
Returns:
the result of evaluating the expression
Throws:
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
ExprException - if there is a pathological environmental problem
See Also:
parseExpression(String), getValue(Object,Object)

getValue

public static java.lang.Object getValue(java.lang.String expression,
                                        java.lang.Object root,
                                        java.lang.Class resultType)
                                 throws ExprException
Convenience method that combines calls to parseExpression and getValue.

Parameters:
expression - the AribaExpr expression to be parsed
root - the root object for the AribaExpr expression
resultType - the converted type of the resultant object, using the context's type converter
Returns:
the result of evaluating the expression
Throws:
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
ExprException - if there is a pathological environmental problem
See Also:
parseExpression(String), getValue(Object,Object)

setValue

public static void setValue(java.lang.Object tree,
                            java.util.Map context,
                            java.lang.Object root,
                            java.lang.Object value)
                     throws ExprException
Evaluates the given AribaExpr expression tree to insert a value into the object graph rooted at the given root object. The default context is set for the given context and root via addDefaultContext().

Parameters:
tree - the AribaExpr expression tree to evaluate, as returned by parseExpression()
context - the naming context for the evaluation
root - the root object for the AribaExpr expression
value - the value to insert into the object graph
Throws:
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
ExprException - if there is a pathological environmental problem

setValue

public static void setValue(java.lang.String expression,
                            java.util.Map context,
                            java.lang.Object root,
                            java.lang.Object value)
                     throws ExprException
Evaluates the given AribaExpr expression to insert a value into the object graph rooted at the given root object given the context.

Parameters:
expression - the AribaExpr expression to be parsed
root - the root object for the AribaExpr expression
context - the naming context for the evaluation
value - the value to insert into the object graph
Throws:
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
ExprException - if there is a pathological environmental problem

setValue

public static void setValue(java.lang.Object tree,
                            java.lang.Object root,
                            java.lang.Object value)
                     throws ExprException
Evaluates the given AribaExpr expression tree to insert a value into the object graph rooted at the given root object.

Parameters:
tree - the AribaExpr expression tree to evaluate, as returned by parseExpression()
root - the root object for the AribaExpr expression
value - the value to insert into the object graph
Throws:
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
ExprException - if there is a pathological environmental problem

setValue

public static void setValue(java.lang.String expression,
                            java.lang.Object root,
                            java.lang.Object value)
                     throws ExprException
Convenience method that combines calls to parseExpression and setValue.

Parameters:
expression - the AribaExpr expression to be parsed
root - the root object for the AribaExpr expression
value - the value to insert into the object graph
Throws:
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
ExprException - if there is a pathological environmental problem
See Also:
parseExpression(String), setValue(Object,Object,Object)

isConstant

public static boolean isConstant(java.lang.Object tree,
                                 java.util.Map context)
                          throws ExprException
Throws:
ExprException

isConstant

public static boolean isConstant(java.lang.String expression,
                                 java.util.Map context)
                          throws ExprException
Throws:
ExprException

isConstant

public static boolean isConstant(java.lang.Object tree)
                          throws ExprException
Throws:
ExprException

isConstant

public static boolean isConstant(java.lang.String expression)
                          throws ExprException
Throws:
ExprException

isSimpleProperty

public static boolean isSimpleProperty(java.lang.Object tree,
                                       java.util.Map context)
                                throws ExprException
Throws:
ExprException

isSimpleProperty

public static boolean isSimpleProperty(java.lang.String expression,
                                       java.util.Map context)
                                throws ExprException
Throws:
ExprException

isSimpleProperty

public static boolean isSimpleProperty(java.lang.Object tree)
                                throws ExprException
Throws:
ExprException

isSimpleProperty

public static boolean isSimpleProperty(java.lang.String expression)
                                throws ExprException
Throws:
ExprException

isSimpleNavigationChain

public static boolean isSimpleNavigationChain(java.lang.Object tree,
                                              java.util.Map context)
                                       throws ExprException
Throws:
ExprException

isSimpleNavigationChain

public static boolean isSimpleNavigationChain(java.lang.String expression,
                                              java.util.Map context)
                                       throws ExprException
Throws:
ExprException

isSimpleNavigationChain

public static boolean isSimpleNavigationChain(java.lang.Object tree)
                                       throws ExprException
Throws:
ExprException

isSimpleNavigationChain

public static boolean isSimpleNavigationChain(java.lang.String expression)
                                       throws ExprException
Throws:
ExprException


AribaWeb User Interface Development Framework
Copyright © 2000-2014 Ariba, Inc. All Rights Reserved.