commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jexl/src/java/org/apache/commons/jexl Expression.java ExpressionImpl.java
Date Thu, 13 Jun 2002 16:09:54 GMT
geirm       2002/06/13 09:09:54

  Modified:    jexl/src/java/org/apache/commons/jexl Expression.java
                        ExpressionImpl.java
  Log:
  Added resolver support
  
  Revision  Changes    Path
  1.2       +17 -1     jakarta-commons-sandbox/jexl/src/java/org/apache/commons/jexl/Expression.java
  
  Index: Expression.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jexl/src/java/org/apache/commons/jexl/Expression.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Expression.java	26 Apr 2002 04:23:14 -0000	1.1
  +++ Expression.java	13 Jun 2002 16:09:53 -0000	1.2
  @@ -72,4 +72,20 @@
        *  returns the expression used
        */
       public String getExpression();
  +
  +    /**
  +     *  allows addition of a resolver to allow custom interdiction of
  +     *  expression evaluation
  +     *
  +     *  @param resolver resolver to be called before Jexl expression evaluated
  +     */
  +    public void addPreResolver(JexlExprResolver resolver);
  +
  +    /**
  +     *  allows addition of a resolver to allow custom interdiction of
  +     *  expression evaluation
  +     *
  +     *  @param resolver resolver to be called if Jexl expression evaluated to null
  +     */
  +    public void addPostResolver(JexlExprResolver resolver);
   }
  
  
  
  1.2       +84 -2     jakarta-commons-sandbox/jexl/src/java/org/apache/commons/jexl/ExpressionImpl.java
  
  Index: ExpressionImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jexl/src/java/org/apache/commons/jexl/ExpressionImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ExpressionImpl.java	26 Apr 2002 04:23:14 -0000	1.1
  +++ ExpressionImpl.java	13 Jun 2002 16:09:53 -0000	1.2
  @@ -59,6 +59,8 @@
   import org.apache.commons.jexl.parser.ASTReference;
   
   import java.io.StringReader;
  +import java.util.ArrayList;
  +import java.util.List;
   
   /**
    *  Implelmentation of an Expression.  Created by the ExpressionFactory.
  @@ -71,6 +73,9 @@
    */
   class ExpressionImpl implements Expression
   {
  +    List preResolvers = new ArrayList();
  +    List postResolvers = new ArrayList();
  +
       /**
        *  Original expression - this is just a 'snippet', not a valid
        *  statement (i.e.  foo.bar() vs foo.bar();
  @@ -100,7 +105,67 @@
       public Object evaluate(JexlContext context)
           throws Exception
       {
  -        return node.value(context);
  +        Object val = null;
  +
  +        /*
  +         * if we have pre resolvers, give them a wack
  +         */
  +        if (preResolvers.size() != 0)
  +        {
  +            val = tryResolver(preResolvers, context);
  +
  +            if (val != JexlExprResolver.NO_VALUE)
  +            {
  +                return val;
  +            }
  +        }
  +
  +        val = node.value(context);
  +
  +        /*
  +         * if null, call post resolvers
  +         */
  +        if (val == null && postResolvers.size() != 0)
  +        {
  +            val = tryResolver(postResolvers, context);
  +
  +            if (val != JexlExprResolver.NO_VALUE)
  +            {
  +                return val;
  +            }
  +        }
  +
  +        return val;
  +    }
  +
  +    /**
  +     *  Tries the resolvers in the given resolverlist against the context
  +     *
  +     *  @param resolverList list of JexlExprResolvers
  +     *  @param context JexlContext to use for evauluation
  +     *  @return value (including null) or JexlExprResolver.NO_VALUE
  +     */
  +    protected Object tryResolver(List resolverList, JexlContext context)
  +    {
  +        Object val = JexlExprResolver.NO_VALUE;
  +        String expr = getExpression();
  +
  +        for (int i = 0; i < resolverList.size(); i++)
  +        {
  +            JexlExprResolver jer = (JexlExprResolver) resolverList.get(i);
  +
  +            val = jer.evaluate(context, expr);
  +
  +            /*
  +            * as long as it's not NO_VALUE, return it
  +            */
  +            if (val != JexlExprResolver.NO_VALUE)
  +            {
  +               return val;
  +            }
  +        }
  +
  +        return val;
       }
   
       /**
  @@ -110,4 +175,21 @@
       {
           return expression;
       }
  +
  +    public void addPreResolver(JexlExprResolver resolver)
  +    {
  +        preResolvers.add(resolver);
  +    }
  +
  +    /**
  +     *  allows addition of a resolver to allow custom interdiction of
  +     *  expression evaluation
  +     *
  +     *  @param resolver resolver to be called if Jexl expression evaluated to null
  +     */
  +    public void addPostResolver(JexlExprResolver resolver)
  +    {
  +        preResolvers.add(resolver);
  +    }
  +
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message