1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.valid; |
16 |
| |
17 |
| import java.util.HashMap; |
18 |
| import java.util.Map; |
19 |
| |
20 |
| import org.apache.hivemind.ApplicationRuntimeException; |
21 |
| import org.apache.tapestry.IMarkupWriter; |
22 |
| import org.apache.tapestry.IRequestCycle; |
23 |
| import org.apache.tapestry.Tapestry; |
24 |
| import org.apache.tapestry.form.IFormComponent; |
25 |
| import org.apache.tapestry.util.RegexpMatcher; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| public class PatternValidator extends BaseValidator |
46 |
| { |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| private String _patternString = ""; |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| private String _patternNotMatchedMessage; |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| private PatternDelegate _patternDelegate; |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| private String _scriptPath = "/org/apache/tapestry/valid/PatternValidator.script"; |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
0
| public String getPatternNotMatchedMessage()
|
74 |
| { |
75 |
0
| return _patternNotMatchedMessage;
|
76 |
| } |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
5
| public String getPatternString()
|
82 |
| { |
83 |
5
| return _patternString;
|
84 |
| } |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
1
| public void setPatternNotMatchedMessage(String message)
|
90 |
| { |
91 |
1
| _patternNotMatchedMessage = message;
|
92 |
| } |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
8
| public void setPatternString(String pattern)
|
98 |
| { |
99 |
8
| _patternString = pattern;
|
100 |
| } |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| private static class RegExpDelegate implements PatternDelegate |
107 |
| { |
108 |
| private RegexpMatcher _matcher; |
109 |
| |
110 |
9
| private RegexpMatcher getPatternMatcher()
|
111 |
| { |
112 |
9
| if (_matcher == null)
|
113 |
5
| _matcher = new RegexpMatcher();
|
114 |
| |
115 |
9
| return _matcher;
|
116 |
| } |
117 |
| |
118 |
8
| public boolean contains(String patternString, String input)
|
119 |
| { |
120 |
8
| return getPatternMatcher().contains(patternString, input);
|
121 |
| } |
122 |
| |
123 |
1
| public String getEscapedPatternString(String patternString)
|
124 |
| { |
125 |
1
| return getPatternMatcher().getEscapedPatternString(patternString);
|
126 |
| } |
127 |
| } |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
1
| public void setPatternDelegate(PatternDelegate patternDelegate)
|
134 |
| { |
135 |
1
| _patternDelegate = patternDelegate;
|
136 |
| } |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
11
| public PatternDelegate getPatternDelegate()
|
143 |
| { |
144 |
11
| if (_patternDelegate == null)
|
145 |
5
| _patternDelegate = new RegExpDelegate();
|
146 |
| |
147 |
11
| return _patternDelegate;
|
148 |
| } |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
0
| public String toString(IFormComponent field, Object value)
|
154 |
| { |
155 |
0
| if (value == null)
|
156 |
0
| return null;
|
157 |
| |
158 |
0
| return value.toString();
|
159 |
| } |
160 |
| |
161 |
5
| private String buildPatternNotMatchedMessage(IFormComponent field, String patternString)
|
162 |
| { |
163 |
5
| String templateMessage =
|
164 |
| getPattern( |
165 |
| _patternNotMatchedMessage, |
166 |
| "pattern-not-matched", |
167 |
| field.getPage().getLocale()); |
168 |
| |
169 |
5
| return formatString(templateMessage, field.getDisplayName(), patternString);
|
170 |
| } |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
9
| public Object toObject(IFormComponent field, String input) throws ValidatorException
|
176 |
| { |
177 |
9
| if (checkRequired(field, input))
|
178 |
0
| return null;
|
179 |
| |
180 |
9
| boolean matched = false;
|
181 |
| |
182 |
9
| try
|
183 |
| { |
184 |
9
| matched = getPatternDelegate().contains(_patternString, input);
|
185 |
| } |
186 |
| catch (Throwable t) |
187 |
| { |
188 |
1
| throw new ApplicationRuntimeException(
|
189 |
| Tapestry.format( |
190 |
| "PatternValidator.pattern-match-error", |
191 |
| _patternString, |
192 |
| field.getDisplayName()), |
193 |
| field, |
194 |
| field.getLocation(), |
195 |
| t); |
196 |
| } |
197 |
| |
198 |
8
| if (!matched)
|
199 |
5
| throw new ValidatorException(
|
200 |
| buildPatternNotMatchedMessage(field, _patternString), |
201 |
| ValidationConstraint.PATTERN_MISMATCH); |
202 |
| |
203 |
3
| return input;
|
204 |
| } |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
0
| public void setScriptPath(String scriptPath)
|
210 |
| { |
211 |
0
| _scriptPath = scriptPath;
|
212 |
| } |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
0
| public void renderValidatorContribution(
|
218 |
| IFormComponent field, |
219 |
| IMarkupWriter writer, |
220 |
| IRequestCycle cycle) |
221 |
| { |
222 |
0
| if (!isClientScriptingEnabled())
|
223 |
0
| return;
|
224 |
| |
225 |
0
| Map symbols = new HashMap();
|
226 |
| |
227 |
0
| if (isRequired())
|
228 |
0
| symbols.put("requiredMessage", buildRequiredMessage(field));
|
229 |
| |
230 |
0
| symbols.put(
|
231 |
| "patternNotMatchedMessage", |
232 |
| buildPatternNotMatchedMessage(field, getEscapedPatternString())); |
233 |
| |
234 |
0
| processValidatorScript(_scriptPath, cycle, field, symbols);
|
235 |
| } |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
2
| public String getEscapedPatternString()
|
241 |
| { |
242 |
2
| return getPatternDelegate().getEscapedPatternString(_patternString);
|
243 |
| } |
244 |
| |
245 |
0
| public String toString()
|
246 |
| { |
247 |
0
| return "Pattern: "
|
248 |
| + _patternString |
249 |
| + "; Script Path: " |
250 |
| + _scriptPath |
251 |
| + "; Pattern Delegate: " |
252 |
| + _patternDelegate; |
253 |
| } |
254 |
| } |