1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package nl.openedge.baritus;
33
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.regex.Pattern;
37
38 import nl.openedge.baritus.population.FieldPopulator;
39 import nl.openedge.baritus.population.OgnlFieldPopulator;
40
41 /***
42 * Registry for populators. Each instance of FormBeanBase has its own instance.
43 *
44 * @author Eelco Hillenius
45 */
46 public final class PopulatorRegistry
47 {
48
49 private Map fieldPopulators = null;
50
51 private Map regexFieldPopulators = null;
52
53 private FieldPopulator defaultFieldPopulator = null;
54
55 /***
56 * construct registry with the current instance of FormBeanBase
57 * @param formBeanCtrl
58 */
59 public PopulatorRegistry(FormBeanCtrlBase formBeanCtrl)
60 {
61 defaultFieldPopulator = new OgnlFieldPopulator(formBeanCtrl);
62 }
63
64 /***
65 * Register a field populator for the given fieldName.
66 * Field populators override the default population of a property on the current form
67 * @param fieldName name of field
68 * @param populator populator instance
69 */
70 public void addPopulator(String fieldName, FieldPopulator populator)
71 {
72 if(fieldPopulators == null)
73 {
74 fieldPopulators = new HashMap();
75 }
76 fieldPopulators.put(fieldName, populator);
77 }
78
79
80 /***
81 * de-register the field populator that was registered with the given fieldName
82 * @param fieldName name of field
83 */
84 public void removePopulator(String fieldName)
85 {
86 if(fieldPopulators != null)
87 {
88 fieldPopulators.remove(fieldName);
89 if(fieldPopulators.isEmpty()) fieldPopulators = null;
90 }
91 }
92
93 /***
94 * Register a custom populator that overrides the default population
95 * process for all request parameters that match the regular expression stored in
96 * the provided pattern.
97 *
98 * @param pattern regex pattern
99 * @param populator populator instance
100 */
101 public void addPopulator(Pattern pattern, FieldPopulator populator)
102 {
103 if(regexFieldPopulators == null)
104 {
105 regexFieldPopulators = new HashMap();
106 }
107 regexFieldPopulators.put(pattern, populator);
108 }
109
110
111 /***
112 * Remove a populator that was registered for the provided pattern
113 * @param pattern regex pattern
114 */
115 public void removePopulator(Pattern pattern)
116 {
117 if(regexFieldPopulators != null)
118 {
119 regexFieldPopulators.remove(pattern);
120 }
121 }
122
123 /***
124 * get the populators that were registered with regex patterns
125 * @return Map the populators that were registered with regex patterns
126 */
127 public Map getRegexFieldPopulators()
128 {
129 return regexFieldPopulators;
130 }
131
132 /***
133 * get the field populator for the provided fieldName, null if none registered
134 * @param fieldName name of the field
135 * @return the field populator for the provided fieldName, null if none registered
136 */
137 public FieldPopulator getFieldPopulator(String fieldName)
138 {
139 return (fieldPopulators != null) ?
140 (FieldPopulator)fieldPopulators.get(fieldName) : null;
141 }
142
143 /***
144 * get the field populators
145 * @return Map the field populators
146 */
147 public Map getFieldPopulators()
148 {
149 return fieldPopulators;
150 }
151
152 /***
153 * get the default field populator
154 * @return FieldPopulator the default field populator
155 */
156 public FieldPopulator getDefaultFieldPopulator()
157 {
158 return defaultFieldPopulator;
159 }
160
161 /***
162 * set the default field populator
163 * @param populator the default field populator
164 */
165 public void setDefaultFieldPopulator(FieldPopulator populator)
166 {
167 defaultFieldPopulator = populator;
168 }
169
170 }