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