1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springmodules.validation.valang.parser;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.springmodules.validation.util.context.BasicContextAware;
23 import org.springmodules.validation.valang.functions.FunctionDefinition;
24
25 /**
26 * A simple implementation of {@link ValangBased}.
27 *
28 * @author Uri Boness
29 */
30 public class SimpleValangBased extends BasicContextAware implements ValangBased {
31
32 private Map customFunctions;
33
34 private Map dateParsers;
35
36 public SimpleValangBased() {
37 customFunctions = new HashMap();
38 dateParsers = new HashMap();
39 }
40
41 /**
42 * @see ValangBased#setCustomFunctions(java.util.Map)
43 */
44 public void setCustomFunctions(Map functionByName) {
45 customFunctions = (functionByName != null) ? functionByName : new HashMap();
46 }
47
48 /**
49 * @see ValangBased#addCustomFunction(String, String)
50 */
51 public void addCustomFunction(String functionName, String functionClassName) {
52 customFunctions.put(functionName, functionClassName);
53 }
54
55 /**
56 * @see ValangBased#setDateParsers(java.util.Map)
57 */
58 public void setDateParsers(Map parserByRegexp) {
59 dateParsers = (parserByRegexp != null) ? parserByRegexp : new HashMap();
60 }
61
62 public Map getCustomFunctions() {
63 return customFunctions;
64 }
65
66 public Map getDateParsers() {
67 return dateParsers;
68 }
69
70 /**
71 * Creates a new {@link ValangParser} that is already configured with the proper custom functions and date
72 * parsers.
73 *
74 * @param expression Valang language validation expression.
75 *
76 * @return A new {@link ValangParser}.
77 */
78 public ValangParser createValangParser(String expression) {
79 return createValangParser(expression, null);
80 }
81
82 /**
83 * Creates a new {@link ValangParser} that is already configured with the proper custom functions and date
84 * parsers and uses the class name to perform bytecode generation to avoid reflection.
85 *
86 * @param expression Valang language validation expression.
87 * @param className Name of the <code>Class</code> to generated
88 * a <code>Function</code> to retrieve a property.
89 *
90 * @return A new {@link ValangParser}.
91 */
92 public ValangParser createValangParser(String expression, String className) {
93 ValangParser parser = new ValangParser(expression, className, getAllCustomFunctions(), dateParsers);
94 initLifecycle(parser.getVisitor());
95 return parser;
96 }
97
98 public void initValang(Object object) {
99 super.initLifecycle(object);
100 if (object instanceof ValangBased) {
101 ((ValangBased) object).setCustomFunctions(customFunctions);
102 ((ValangBased) object).setDateParsers(dateParsers);
103 }
104 }
105
106 /**
107 * Returns all the custom functions that can be found. This method returns all the custom functions
108 * that were explicitly registered with this instance and all custom functions that can be found in the
109 * application context (if one is set) by looking for {@link org.springmodules.validation.valang.functions.FunctionDefinition}
110 * beans.
111 *
112 * @return All the custom function that can be found.
113 */
114 public Map getAllCustomFunctions() {
115 Map functionByName = new HashMap();
116 functionByName.putAll(customFunctions);
117 functionByName.putAll(findAllCustomFunctionsInApplicationContext());
118 return functionByName;
119 }
120
121 protected Map findAllCustomFunctionsInApplicationContext() {
122 Map functionByName = new HashMap();
123 if (applicationContext == null) {
124 return functionByName;
125 }
126 String[] names = applicationContext.getBeanNamesForType(FunctionDefinition.class);
127 for (int i = 0; i < names.length; i++) {
128 FunctionDefinition def = (FunctionDefinition) applicationContext.getBean(names[i]);
129 functionByName.put(def.getName(), def.getClassName());
130 }
131 return functionByName;
132 }
133
134 }