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 | 0 | public class ValidatorFactoryImpl implements ValidatorFactory |
37 | |
{ |
38 | |
private static final String PATTERN = "^\\s*(\\$?\\w+)\\s*(=\\s*(((?!,|\\[).)*))?"; |
39 | 0 | 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 | 0 | Defense.notNull(component, "component"); |
50 | |
|
51 | 0 | if (HiveMind.isBlank(specification)) |
52 | 0 | return Collections.EMPTY_LIST; |
53 | |
|
54 | 0 | List result = new ArrayList(); |
55 | 0 | String chopped = specification; |
56 | |
|
57 | |
while (true) |
58 | |
{ |
59 | 0 | if (chopped.length() == 0) |
60 | 0 | break; |
61 | |
|
62 | 0 | if (!result.isEmpty()) |
63 | |
{ |
64 | 0 | if (chopped.charAt(0) != ',') |
65 | 0 | failBadSpec(specification); |
66 | |
|
67 | 0 | chopped = chopped.substring(1); |
68 | |
} |
69 | |
|
70 | 0 | Matcher matcher = PATTERN_COMPILED.matcher(chopped); |
71 | |
|
72 | 0 | if (!matcher.find()) failBadSpec(specification); |
73 | |
|
74 | 0 | String name = matcher.group(1); |
75 | 0 | String value = matcher.group(3); |
76 | 0 | String message = null; |
77 | |
|
78 | 0 | int length = matcher.group().length(); |
79 | |
|
80 | 0 | if (matcher.find()) failBadSpec(specification); |
81 | |
|
82 | 0 | if (chopped.length() > length) |
83 | |
{ |
84 | 0 | char lastChar = chopped.charAt(length); |
85 | 0 | if (lastChar == ',') |
86 | 0 | length--; |
87 | 0 | else if (lastChar == '[') |
88 | |
{ |
89 | 0 | int messageClose = chopped.indexOf(']', length); |
90 | 0 | message = chopped.substring(length + 1, messageClose); |
91 | 0 | length = messageClose; |
92 | |
} |
93 | |
} |
94 | |
|
95 | 0 | Validator validator = buildValidator(component, name, value, message); |
96 | |
|
97 | 0 | result.add(validator); |
98 | |
|
99 | 0 | if (length >= chopped.length()) |
100 | 0 | break; |
101 | |
|
102 | 0 | chopped = chopped.substring(length + 1); |
103 | |
|
104 | 0 | } |
105 | |
|
106 | 0 | return Collections.unmodifiableList(result); |
107 | |
} |
108 | |
|
109 | |
private void failBadSpec(String specification) { |
110 | 0 | throw new ApplicationRuntimeException(ValidatorMessages.badSpecification(specification)); |
111 | |
} |
112 | |
|
113 | |
private Validator buildValidator(IComponent component, String name, String value, String message) |
114 | |
{ |
115 | 0 | if (name.startsWith("$")) |
116 | 0 | return extractValidatorBean(component, name, value, message); |
117 | |
|
118 | 0 | ValidatorContribution vc = (ValidatorContribution) _validators.get(name); |
119 | |
|
120 | 0 | if (vc == null) |
121 | 0 | throw new ApplicationRuntimeException(ValidatorMessages.unknownValidator(name)); |
122 | |
|
123 | 0 | if (value == null && vc.isConfigurable()) |
124 | 0 | throw new ApplicationRuntimeException(ValidatorMessages.needsConfiguration("name")); |
125 | |
|
126 | 0 | if (value != null && !vc.isConfigurable()) |
127 | 0 | throw new ApplicationRuntimeException(ValidatorMessages.notConfigurable(name, value)); |
128 | |
|
129 | |
try |
130 | |
{ |
131 | 0 | Object result = vc.getValidatorClass().newInstance(); |
132 | |
|
133 | 0 | if (vc.isConfigurable()) |
134 | 0 | PropertyUtils.smartWrite(result, name, value); |
135 | |
|
136 | 0 | if (message != null) |
137 | 0 | PropertyUtils.write(result, "message", message); |
138 | |
|
139 | 0 | return (Validator) result; |
140 | |
} |
141 | 0 | catch (Exception ex) |
142 | |
{ |
143 | 0 | 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 | 0 | String beanName = validatorName.substring(1); |
154 | |
|
155 | 0 | if (HiveMind.isNonBlank(value) || HiveMind.isNonBlank(message)) |
156 | 0 | throw new ApplicationRuntimeException(ValidatorMessages |
157 | |
.noValueOrMessageForBean(beanName)); |
158 | |
|
159 | 0 | return new BeanValidatorWrapper(component, beanName); |
160 | |
} |
161 | |
|
162 | |
public void setValidators(Map validators) |
163 | |
{ |
164 | 0 | _validators = validators; |
165 | 0 | } |
166 | |
} |