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  package org.apache.commons.jexl.resolver;
    17
    18  import org.apache.commons.jexl.JexlExprResolver;
    19  import org.apache.commons.jexl.JexlContext;
    20
    21  /***
    22   *  Simple resolver to try the expression as-is from the context.
    23   *
    24   *  For example, you could resolve ant-ish properties (foo.bar.woogie)
    25   *  using this...
    26   *
    27   *  hint, hint...
    28   *
    29   *  @since 1.0
    30   *  @author <a href="mailto:geirm@adeptra.com">Geir Magnusson Jr.</a>
    31   *  @version $Id: FlatResolver.java 397542 2006-04-27 13:43:47Z dion $
    32   */
    33  public class FlatResolver implements JexlExprResolver {
    34      /***
    35       *  Flag to return NO_VALUE on null from context.
    36       *  this allows jexl to try to evaluate
    37       */
    38      protected boolean noValOnNull = true;
    39
    40      /***
    41       * Default CTOR.
    42       */
    43      public FlatResolver() {
    44      }
    45
    46      /***
    47       *  CTOR that lets you override the default behavior of
    48       *  noValOnNull, which is true. (jexl gets a shot after if null)
    49       *
    50       *  @param valOnNull Whether NO_VALUE will be returned instead of null.
    51       */
    52      public FlatResolver(boolean valOnNull) {
    53          noValOnNull = valOnNull;
    54      }
    55
    56      /***
    57       *  Try to resolve expression as-is.
    58       *
    59       *  @param context The context for resolution.
    60       *  @param expression The flat expression.
    61       *  @return The resolved value.
    62       */
    63      public Object evaluate(JexlContext context, String expression) {
    64          Object val = context.getVars().get(expression);
    65
    66          if (val == null && noValOnNull) {
    67              return JexlExprResolver.NO_VALUE;
    68          }
    69
    70          return val;
    71      }
    72  }