| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.jexl.ExpressionImpl | 
 | 
 | 
| 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 | 271 |      ExpressionImpl(String expr, SimpleNode ref) { | 
| 58 | 271 | expression = expr; | 
| 59 | 271 | node = ref; | 
| 60 | 271 | } | 
| 61 | ||
| 62 |      /** | |
| 63 |       * {@inheritDoc} | |
| 64 |       */ | |
| 65 |      public Object evaluate(JexlContext context) throws Exception { | |
| 66 | 272 |          Object val = null; | 
| 67 | ||
| 68 |          /* | |
| 69 |           * if we have pre resolvers, give them a wack | |
| 70 |           */ | |
| 71 | 272 | if (preResolvers != null) { | 
| 72 | 3 | val = tryResolver(preResolvers, context); | 
| 73 | ||
| 74 | 3 |              if (val != JexlExprResolver.NO_VALUE) { | 
| 75 | 2 |                  return val; | 
| 76 | } | |
| 77 | } | |
| 78 | ||
| 79 | 270 | val = node.value(context); | 
| 80 | ||
| 81 |          /* | |
| 82 |           * if null, call post resolvers | |
| 83 |           */ | |
| 84 | 269 | if (val == null && postResolvers != class="keyword">null) { | 
| 85 | 0 |              val = tryResolver(postResolvers, context); | 
| 86 | ||
| 87 | 0 |              if (val != JexlExprResolver.NO_VALUE) { | 
| 88 | 0 |                  return val; | 
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | 269 |          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 | 3 | Object val = JexlExprResolver.NO_VALUE; | 
| 104 | 3 | String expr = getExpression(); | 
| 105 | ||
| 106 | 4 | for (int i = 0; i < resolverList.size(); i++) { | 
| 107 | 3 | JexlExprResolver jer = (JexlExprResolver) resolverList.get(i); | 
| 108 | ||
| 109 | 3 | val = jer.evaluate(context, expr); | 
| 110 | ||
| 111 |              /* | |
| 112 |               * as long as it's not NO_VALUE, return it | |
| 113 |               */ | |
| 114 | 3 |              if (val != JexlExprResolver.NO_VALUE) { | 
| 115 | 2 |                  return val; | 
| 116 | } | |
| 117 | } | |
| 118 | ||
| 119 | 1 |          return val; | 
| 120 | } | |
| 121 | ||
| 122 |      /** | |
| 123 |       * {@inheritDoc} | |
| 124 |       */ | |
| 125 |      public String getExpression() { | |
| 126 | 4 |          return expression; | 
| 127 | } | |
| 128 | ||
| 129 |      /** | |
| 130 |       * {@inheritDoc}      | |
| 131 |       */ | |
| 132 | public void addPreResolver(JexlExprResolver resolver) { | |
| 133 | 3 | if (preResolvers == null) { | 
| 134 | 3 |              preResolvers = new ArrayList(); | 
| 135 | } | |
| 136 | 3 | preResolvers.add(resolver); | 
| 137 | 3 | } | 
| 138 | ||
| 139 |      /** | |
| 140 |       * {@inheritDoc} | |
| 141 |       */ | |
| 142 | public void addPostResolver(JexlExprResolver resolver) { | |
| 143 | 1 | if (postResolvers == null) { | 
| 144 | 1 |              postResolvers = new ArrayList(); | 
| 145 | } | |
| 146 | 1 | postResolvers.add(resolver); | 
| 147 | 1 | } | 
| 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 | 1 | String expr = getExpression(); | 
| 156 | 1 | return expr == null ? "" : expr; | 
| 157 | } | |
| 158 | ||
| 159 | } | 
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |