1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }