View Javadoc

    1   /*
    2    * Copyright 2002-2006 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package org.apache.commons.jexl;
    18
    19  import java.util.ArrayList;
    20  import java.util.List;
    21
    22  import org.apache.commons.jexl.parser.SimpleNode;
    23
    24  /***
    25   * Instances of ExpressionImpl are created by the {@link ExpressionFactory},
    26   * and this is the default implementation of the {@link Expression} interface.
    27   *
    28   * @since 1.0
    29   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
    30   * @version $Id: ExpressionImpl.java 397111 2006-04-26 06:47:52Z dion $
    31   */
    32  class ExpressionImpl implements Expression {
    33
    34      /*** resolvers called before expression evaluation. */
    35      protected List preResolvers;
    36
    37      /*** resolvers called after expression evaluation. */
    38      protected List postResolvers;
    39
    40      /***
    41       * Original expression. This is just a 'snippet', not a valid statement
    42       * (i.e. foo.bar() vs foo.bar();
    43       */
    44      protected String expression;
    45
    46      /***
    47       * The resulting AST we can call value() on.
    48       */
    49      protected SimpleNode node;
    50
    51      /***
    52       * do not let this be generally instantiated with a 'new'.
    53       *
    54       * @param expr the expression.
    55       * @param ref the parsed expression.
    56       */
    57      ExpressionImpl(String expr, SimpleNode ref) {
    58          expression = expr;
    59          node = ref;
    60      }
    61
    62      /***
    63       * {@inheritDoc}
    64       */
    65      public Object evaluate(JexlContext context) throws Exception {
    66          Object val = null;
    67
    68          /*
    69           * if we have pre resolvers, give them a wack
    70           */
    71          if (preResolvers != null) {
    72              val = tryResolver(preResolvers, context);
    73
    74              if (val != JexlExprResolver.NO_VALUE) {
    75                  return val;
    76              }
    77          }
    78
    79          val = node.value(context);
    80
    81          /*
    82           * if null, call post resolvers
    83           */
    84          if (val == null && postResolvers != null) {
    85              val = tryResolver(postResolvers, context);
    86
    87              if (val != JexlExprResolver.NO_VALUE) {
    88                  return val;
    89              }
    90          }
    91
    92          return val;
    93      }
    94
    95      /***
    96       * Tries the resolvers in the given resolverlist against the context.
    97       *
    98       * @param resolverList list of JexlExprResolvers
    99       * @param context JexlContext to use for evauluation
    100      * @return value (including null) or JexlExprResolver.NO_VALUE
    101      */
    102     protected Object tryResolver(List resolverList, JexlContext context) {
    103         Object val = JexlExprResolver.NO_VALUE;
    104         String expr = getExpression();
    105
    106         for (int i = 0; i < resolverList.size(); i++) {
    107             JexlExprResolver jer = (JexlExprResolver) resolverList.get(i);
    108
    109             val = jer.evaluate(context, expr);
    110
    111             /*
    112              * as long as it's not NO_VALUE, return it
    113              */
    114             if (val != JexlExprResolver.NO_VALUE) {
    115                 return val;
    116             }
    117         }
    118
    119         return val;
    120     }
    121
    122     /***
    123      * {@inheritDoc}
    124      */
    125     public String getExpression() {
    126         return expression;
    127     }
    128
    129     /***
    130      * {@inheritDoc}
    131      */
    132     public void addPreResolver(JexlExprResolver resolver) {
    133         if (preResolvers == null) {
    134             preResolvers = new ArrayList();
    135         }
    136         preResolvers.add(resolver);
    137     }
    138
    139     /***
    140      * {@inheritDoc}
    141      */
    142     public void addPostResolver(JexlExprResolver resolver) {
    143         if (postResolvers == null) {
    144             postResolvers = new ArrayList();
    145         }
    146         postResolvers.add(resolver);
    147     }
    148
    149     /***
    150      * Provide a string representation of the expression.
    151      *
    152      * @return the expression or blank if it's null.
    153      */
    154     public String toString() {
    155         String expr = getExpression();
    156         return expr == null ? "" : expr;
    157     }
    158
    159 }