|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.tapestry.valid; |
|
16 |
| |
|
17 |
| import java.text.DateFormat; |
|
18 |
| import java.text.ParseException; |
|
19 |
| import java.text.SimpleDateFormat; |
|
20 |
| import java.util.Calendar; |
|
21 |
| import java.util.Date; |
|
22 |
| import java.util.GregorianCalendar; |
|
23 |
| import java.util.HashMap; |
|
24 |
| import java.util.Map; |
|
25 |
| |
|
26 |
| import org.apache.tapestry.IMarkupWriter; |
|
27 |
| import org.apache.tapestry.IRequestCycle; |
|
28 |
| import org.apache.tapestry.form.IFormComponent; |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| public class DateValidator extends BaseValidator |
|
39 |
| { |
|
40 |
| private DateFormat _format; |
|
41 |
| |
|
42 |
| private String _displayFormat; |
|
43 |
| |
|
44 |
| private Date _minimum; |
|
45 |
| |
|
46 |
| private Date _maximum; |
|
47 |
| |
|
48 |
| private Calendar _calendar; |
|
49 |
| |
|
50 |
| private String _scriptPath = "/org/apache/tapestry/valid/DateValidator.script"; |
|
51 |
| |
|
52 |
| private static DateFormat defaultDateFormat = new SimpleDateFormat("MM/dd/yyyy"); |
|
53 |
| |
|
54 |
| private static final String defaultDateDisplayFormat = "MM/DD/YYYY"; |
|
55 |
| |
|
56 |
| private String _dateTooEarlyMessage; |
|
57 |
| |
|
58 |
| private String _dateTooLateMessage; |
|
59 |
| |
|
60 |
| private String _invalidDateFormatMessage; |
|
61 |
| |
|
62 |
42
| public DateValidator()
|
|
63 |
| { |
|
64 |
| |
|
65 |
| } |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
0
| public DateValidator(String initializer)
|
|
74 |
| { |
|
75 |
0
| super(initializer);
|
|
76 |
| } |
|
77 |
| |
|
78 |
6
| public void setFormat(DateFormat value)
|
|
79 |
| { |
|
80 |
6
| _format = value;
|
|
81 |
| } |
|
82 |
| |
|
83 |
0
| public DateFormat getFormat()
|
|
84 |
| { |
|
85 |
0
| return _format;
|
|
86 |
| } |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
33
| public DateFormat getEffectiveFormat()
|
|
94 |
| { |
|
95 |
33
| if (_format == null)
|
|
96 |
27
| return defaultDateFormat;
|
|
97 |
| |
|
98 |
6
| return _format;
|
|
99 |
| } |
|
100 |
| |
|
101 |
0
| public String getDisplayFormat()
|
|
102 |
| { |
|
103 |
0
| return _displayFormat;
|
|
104 |
| } |
|
105 |
| |
|
106 |
0
| public void setDisplayFormat(String value)
|
|
107 |
| { |
|
108 |
0
| _displayFormat = value;
|
|
109 |
| } |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
6
| public String getEffectiveDisplayFormat()
|
|
119 |
| { |
|
120 |
6
| if (_displayFormat == null)
|
|
121 |
| { |
|
122 |
6
| DateFormat format = getEffectiveFormat();
|
|
123 |
6
| if (format instanceof SimpleDateFormat)
|
|
124 |
6
| return ((SimpleDateFormat) format).toPattern();
|
|
125 |
| |
|
126 |
0
| return defaultDateDisplayFormat;
|
|
127 |
| } |
|
128 |
| |
|
129 |
0
| return _displayFormat;
|
|
130 |
| } |
|
131 |
| |
|
132 |
9
| public String toString(IFormComponent file, Object value)
|
|
133 |
| { |
|
134 |
9
| if (value == null)
|
|
135 |
3
| return null;
|
|
136 |
| |
|
137 |
6
| Date date = (Date) value;
|
|
138 |
| |
|
139 |
6
| DateFormat format = getEffectiveFormat();
|
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
6
| synchronized (format)
|
|
144 |
| { |
|
145 |
6
| return format.format(date);
|
|
146 |
| } |
|
147 |
| } |
|
148 |
| |
|
149 |
33
| public Object toObject(IFormComponent field, String value) throws ValidatorException
|
|
150 |
| { |
|
151 |
33
| if (checkRequired(field, value))
|
|
152 |
12
| return null;
|
|
153 |
| |
|
154 |
21
| DateFormat format = getEffectiveFormat();
|
|
155 |
| |
|
156 |
21
| Date result;
|
|
157 |
| |
|
158 |
21
| try
|
|
159 |
| { |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
21
| synchronized (format)
|
|
164 |
| { |
|
165 |
21
| result = format.parse(value);
|
|
166 |
| } |
|
167 |
| |
|
168 |
15
| if (_calendar == null)
|
|
169 |
15
| _calendar = new GregorianCalendar();
|
|
170 |
| |
|
171 |
15
| _calendar.setTime(result);
|
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
15
| if (_calendar.get(Calendar.YEAR) < 1000)
|
|
179 |
0
| result = null;
|
|
180 |
| |
|
181 |
| } |
|
182 |
| catch (ParseException ex) |
|
183 |
| { |
|
184 |
| |
|
185 |
| |
|
186 |
6
| result = null;
|
|
187 |
| } |
|
188 |
| |
|
189 |
21
| if (result == null)
|
|
190 |
6
| throw new ValidatorException(buildInvalidDateFormatMessage(field),
|
|
191 |
| ValidationConstraint.DATE_FORMAT); |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
15
| if (_minimum != null && _minimum.compareTo(result) > 0)
|
|
196 |
6
| throw new ValidatorException(buildDateTooEarlyMessage(field, format.format(_minimum)),
|
|
197 |
| ValidationConstraint.TOO_SMALL); |
|
198 |
| |
|
199 |
9
| if (_maximum != null && _maximum.compareTo(result) < 0)
|
|
200 |
6
| throw new ValidatorException(buildDateTooLateMessage(field, format.format(_maximum)),
|
|
201 |
| ValidationConstraint.TOO_LARGE); |
|
202 |
| |
|
203 |
3
| return result;
|
|
204 |
| |
|
205 |
| } |
|
206 |
| |
|
207 |
0
| public Date getMaximum()
|
|
208 |
| { |
|
209 |
0
| return _maximum;
|
|
210 |
| } |
|
211 |
| |
|
212 |
9
| public void setMaximum(Date maximum)
|
|
213 |
| { |
|
214 |
9
| _maximum = maximum;
|
|
215 |
| } |
|
216 |
| |
|
217 |
0
| public Date getMinimum()
|
|
218 |
| { |
|
219 |
0
| return _minimum;
|
|
220 |
| } |
|
221 |
| |
|
222 |
9
| public void setMinimum(Date minimum)
|
|
223 |
| { |
|
224 |
9
| _minimum = minimum;
|
|
225 |
| } |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
0
| public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
|
|
232 |
| IRequestCycle cycle) |
|
233 |
| { |
|
234 |
0
| if (!(isClientScriptingEnabled() && isRequired()))
|
|
235 |
0
| return;
|
|
236 |
| |
|
237 |
0
| Map symbols = new HashMap();
|
|
238 |
| |
|
239 |
0
| symbols.put("requiredMessage", buildRequiredMessage(field));
|
|
240 |
| |
|
241 |
0
| processValidatorScript(_scriptPath, cycle, field, symbols);
|
|
242 |
| } |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
0
| public String getScriptPath()
|
|
249 |
| { |
|
250 |
0
| return _scriptPath;
|
|
251 |
| } |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
0
| public void setScriptPath(String scriptPath)
|
|
263 |
| { |
|
264 |
0
| _scriptPath = scriptPath;
|
|
265 |
| } |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
0
| public String getDateTooEarlyMessage()
|
|
270 |
| { |
|
271 |
0
| return _dateTooEarlyMessage;
|
|
272 |
| } |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
0
| public String getDateTooLateMessage()
|
|
277 |
| { |
|
278 |
0
| return _dateTooLateMessage;
|
|
279 |
| } |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
0
| public String getInvalidDateFormatMessage()
|
|
284 |
| { |
|
285 |
0
| return _invalidDateFormatMessage;
|
|
286 |
| } |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
6
| protected String buildInvalidDateFormatMessage(IFormComponent field)
|
|
291 |
| { |
|
292 |
6
| String pattern = getPattern(_invalidDateFormatMessage, "invalid-date-format", field
|
|
293 |
| .getPage().getLocale()); |
|
294 |
| |
|
295 |
6
| return formatString(pattern, field.getDisplayName(), getEffectiveDisplayFormat());
|
|
296 |
| } |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
6
| protected String buildDateTooEarlyMessage(IFormComponent field, String earliestDate)
|
|
301 |
| { |
|
302 |
6
| String pattern = getPattern(_dateTooEarlyMessage, "date-too-early", field.getPage()
|
|
303 |
| .getLocale()); |
|
304 |
| |
|
305 |
6
| return formatString(pattern, field.getDisplayName(), earliestDate);
|
|
306 |
| } |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
6
| protected String buildDateTooLateMessage(IFormComponent field, String latestDate)
|
|
311 |
| { |
|
312 |
6
| String pattern = getPattern(_dateTooLateMessage, "date-too-late", field.getPage()
|
|
313 |
| .getLocale()); |
|
314 |
| |
|
315 |
6
| return formatString(pattern, field.getDisplayName(), latestDate);
|
|
316 |
| } |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
3
| public void setDateTooEarlyMessage(String string)
|
|
326 |
| { |
|
327 |
3
| _dateTooEarlyMessage = string;
|
|
328 |
| } |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
3
| public void setDateTooLateMessage(String string)
|
|
338 |
| { |
|
339 |
3
| _dateTooLateMessage = string;
|
|
340 |
| } |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
3
| public void setInvalidDateFormatMessage(String string)
|
|
350 |
| { |
|
351 |
3
| _invalidDateFormatMessage = string;
|
|
352 |
| } |
|
353 |
| |
|
354 |
| } |