1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.form.validator; |
16 |
| |
17 |
| import java.util.ArrayList; |
18 |
| import java.util.Collections; |
19 |
| import java.util.HashMap; |
20 |
| import java.util.List; |
21 |
| import java.util.Map; |
22 |
| |
23 |
| import org.apache.hivemind.ApplicationRuntimeException; |
24 |
| import org.apache.hivemind.HiveMind; |
25 |
| import org.apache.hivemind.util.PropertyUtils; |
26 |
| import org.apache.tapestry.util.RegexpMatch; |
27 |
| import org.apache.tapestry.util.RegexpMatcher; |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| public class ValidatorFactoryImpl implements ValidatorFactory |
37 |
| { |
38 |
| private static final String PATTERN = "^\\s*(\\w+)\\s*(=\\s*(((?!,|\\[).)*))?"; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| private Map _masterCache = new HashMap(); |
45 |
| |
46 |
| private RegexpMatcher _matcher = new RegexpMatcher(); |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| private Map _validators; |
53 |
| |
54 |
12
| public synchronized List constructValidatorList(String specification)
|
55 |
| { |
56 |
12
| List result = (List) _masterCache.get(specification);
|
57 |
| |
58 |
12
| if (result == null)
|
59 |
| { |
60 |
11
| result = buildValidatorList(specification);
|
61 |
6
| _masterCache.put(specification, result);
|
62 |
| } |
63 |
| |
64 |
7
| return result;
|
65 |
| } |
66 |
| |
67 |
11
| private List buildValidatorList(String specification)
|
68 |
| { |
69 |
11
| if (HiveMind.isBlank(specification))
|
70 |
1
| return Collections.EMPTY_LIST;
|
71 |
| |
72 |
10
| List result = new ArrayList();
|
73 |
10
| String chopped = specification;
|
74 |
| |
75 |
10
| while (true)
|
76 |
| { |
77 |
16
| if (chopped.length() == 0)
|
78 |
3
| break;
|
79 |
| |
80 |
13
| if (!result.isEmpty())
|
81 |
| { |
82 |
3
| if (chopped.charAt(0) != ',')
|
83 |
0
| throw new ApplicationRuntimeException(ValidatorMessages
|
84 |
| .badSpecification(specification)); |
85 |
| |
86 |
3
| chopped = chopped.substring(1);
|
87 |
| } |
88 |
| |
89 |
13
| RegexpMatch[] matches = _matcher.getMatches(PATTERN, chopped);
|
90 |
| |
91 |
13
| if (matches.length != 1)
|
92 |
1
| throw new ApplicationRuntimeException(ValidatorMessages
|
93 |
| .badSpecification(specification)); |
94 |
| |
95 |
12
| RegexpMatch match = matches[0];
|
96 |
| |
97 |
12
| String name = match.getGroup(1);
|
98 |
12
| String value = match.getGroup(3);
|
99 |
12
| String message = null;
|
100 |
| |
101 |
12
| int length = match.getMatchLength();
|
102 |
| |
103 |
12
| if (chopped.length() > length)
|
104 |
| { |
105 |
6
| char lastChar = chopped.charAt(length);
|
106 |
6
| if (lastChar == ',')
|
107 |
2
| length--;
|
108 |
4
| else if (lastChar == '[')
|
109 |
| { |
110 |
4
| int messageClose = chopped.indexOf(']', length);
|
111 |
4
| message = chopped.substring(length + 1, messageClose);
|
112 |
4
| length = messageClose;
|
113 |
| } |
114 |
| } |
115 |
| |
116 |
12
| Validator validator = buildValidator(name, value, message);
|
117 |
| |
118 |
8
| result.add(validator);
|
119 |
| |
120 |
8
| if (length >= chopped.length())
|
121 |
2
| break;
|
122 |
| else |
123 |
6
| chopped = chopped.substring(length + 1);
|
124 |
| |
125 |
| } |
126 |
| |
127 |
5
| return Collections.unmodifiableList(result);
|
128 |
| } |
129 |
| |
130 |
12
| private Validator buildValidator(String name, String value, String message)
|
131 |
| { |
132 |
12
| ValidatorContribution vc = (ValidatorContribution) _validators.get(name);
|
133 |
| |
134 |
12
| if (vc == null)
|
135 |
1
| throw new ApplicationRuntimeException(ValidatorMessages.unknownValidator(name));
|
136 |
| |
137 |
11
| if (value == null && vc.isConfigurable())
|
138 |
1
| throw new ApplicationRuntimeException(ValidatorMessages.needsConfiguration("name"));
|
139 |
| |
140 |
10
| if (value != null && !vc.isConfigurable())
|
141 |
1
| throw new ApplicationRuntimeException(ValidatorMessages.notConfigurable(name, value));
|
142 |
| |
143 |
9
| try
|
144 |
| { |
145 |
9
| Object result = vc.getValidatorClass().newInstance();
|
146 |
| |
147 |
9
| if (vc.isConfigurable())
|
148 |
4
| PropertyUtils.smartWrite(result, name, value);
|
149 |
| |
150 |
9
| if (message != null)
|
151 |
4
| PropertyUtils.write(result, "message", message);
|
152 |
| |
153 |
9
| return (Validator) result;
|
154 |
| } |
155 |
| catch (Exception ex) |
156 |
| { |
157 |
1
| throw new ApplicationRuntimeException(ValidatorMessages.errorInitializingValidator(
|
158 |
| name, |
159 |
| vc.getValidatorClass(), |
160 |
| ex), ex); |
161 |
| } |
162 |
| } |
163 |
| |
164 |
10
| public void setValidators(Map validators)
|
165 |
| { |
166 |
10
| _validators = validators;
|
167 |
| } |
168 |
| } |