1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springmodules.validation.bean.rule.resolver;
18
19 import java.io.StringReader;
20 import java.util.Map;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springmodules.validation.valang.functions.Function;
25 import org.springmodules.validation.valang.functions.TargetBeanFunction;
26 import org.springmodules.validation.valang.parser.ParseException;
27 import org.springmodules.validation.valang.parser.ValangParser;
28
29
30
31
32
33
34 public class ValangErrorArgumentsResolver implements ErrorArgumentsResolver {
35
36 private final static Logger logger = LoggerFactory.getLogger(ValangErrorArgumentsResolver.class);
37
38 private Function[] functions;
39
40
41
42
43 public ValangErrorArgumentsResolver() {
44 this(new String[0]);
45 }
46
47
48
49
50
51
52
53 public ValangErrorArgumentsResolver(String[] expressions) {
54 this(expressions, null);
55 }
56
57
58
59
60
61
62
63
64
65 public ValangErrorArgumentsResolver(String[] expressions, Map functionsByName) {
66 if (expressions == null) {
67 expressions = new String[0];
68 }
69 functions = new Function[expressions.length];
70 for (int i = 0; i < expressions.length; i++) {
71 functions[i] = parseFunction(expressions[i], functionsByName);
72 }
73 }
74
75
76
77
78
79
80 public Object[] resolveArguments(Object obj) {
81 Object[] args = new Object[functions.length];
82 for (int i = 0; i < args.length; i++) {
83 args[i] = functions[i].getResult(obj);
84 }
85 return args;
86 }
87
88
89
90
91
92
93
94
95
96
97 protected Function parseFunction(String expression, Map functionsByName) {
98 ValangParser parser = new ValangParser(new StringReader(expression));
99 parser.setFunctionsByName(functionsByName);
100 try {
101 return parser.function(new TargetBeanFunction());
102 } catch (ParseException pe) {
103 logger.error("Could not parse valang expression '" + expression + "' to a function", pe);
104 throw new IllegalArgumentException("Could not parse valang expression '" + expression + "' to a function");
105 }
106 }
107
108 }