View Javadoc

1   /*
2    * Copyright 2004-2009 the original author or authors.
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.springmodules.validation.util.fel.parser;
18  
19  import org.springmodules.validation.util.fel.FelEvaluationException;
20  import org.springmodules.validation.util.fel.FelParseException;
21  import org.springmodules.validation.util.fel.Function;
22  import org.springmodules.validation.util.fel.FunctionExpressionParser;
23  import org.springmodules.validation.valang.functions.TargetBeanFunction;
24  import org.springmodules.validation.valang.parser.ParseException;
25  import org.springmodules.validation.valang.parser.SimpleValangBased;
26  import org.springmodules.validation.valang.parser.ValangParser;
27  
28  /**
29   * A {@link FunctionExpressionParser} implementation that knows how to parse valang function expressions.
30   *
31   * @author Uri Boness
32   */
33  public class ValangFunctionExpressionParser extends SimpleValangBased implements FunctionExpressionParser {
34  
35      public Function parse(String expression) {
36          return new ValangFunction(expression);
37      }
38  
39      /**
40       * A function that is associated with a valang function expression. This function evaluates the function on
41       * the given object and returns the result.
42       */
43      protected class ValangFunction implements Function {
44  
45          private String valangExpression;
46  
47          private org.springmodules.validation.valang.functions.Function valangFunction;
48  
49          public ValangFunction(String valangExpression) {
50              this.valangExpression = valangExpression;
51              ValangParser parser = createValangParser(valangExpression);
52              try {
53                  this.valangFunction = parser.function(new TargetBeanFunction());
54              } catch (ParseException pe) {
55                  throw new FelParseException("Could not parse valang function expression '" +
56                      valangExpression + "'", pe);
57              }
58          }
59  
60          public Object evaluate(Object argument) {
61              try {
62                  return valangFunction.getResult(argument);
63              } catch (Throwable t) {
64                  throw new FelEvaluationException("Could not evaluate valang expression '" +
65                      valangExpression + "' on bean '" + String.valueOf(argument) + "'", t);
66              }
67          }
68      }
69  
70  }