1 package org.apache.fulcrum.intake.validator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.text.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.commons.lang3.StringUtils;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class DateStringValidator
57 extends DefaultValidator<Date>
58 {
59 private static final String DEFAULT_DATE_MESSAGE =
60 "Date could not be parsed";
61
62
63 private List<String> dateFormats = null;
64
65
66 private String dateFormatMessage = null;
67
68
69 private boolean flexible = false;
70
71
72
73
74 public DateStringValidator()
75 {
76 super();
77 dateFormats = new ArrayList<String>(5);
78 }
79
80
81
82
83
84
85
86 @Override
87 public void init(Map<String, ? extends Constraint> paramMap)
88 throws InvalidMaskException
89 {
90 super.init(paramMap);
91
92 Constraint constraint = paramMap.get(FORMAT_RULE_NAME);
93
94 if (constraint != null)
95 {
96 dateFormats.add(constraint.getValue());
97 setDateFormatMessage(constraint.getMessage());
98 }
99
100 for(int i = 1 ;; i++)
101 {
102 constraint = paramMap.get(FORMAT_RULE_NAME + i);
103
104 if (constraint == null)
105 {
106 break;
107 }
108
109 dateFormats.add(constraint.getValue());
110 setDateFormatMessage(constraint.getMessage());
111 }
112
113 if (StringUtils.isEmpty(dateFormatMessage))
114 {
115 dateFormatMessage = DEFAULT_DATE_MESSAGE;
116 }
117
118 constraint = paramMap.get(FLEXIBLE_RULE_NAME);
119
120 if (constraint != null)
121 {
122 flexible = Boolean.valueOf(constraint.getValue()).booleanValue();
123 }
124 }
125
126
127
128
129
130
131
132
133
134 @Override
135 public void assertValidity(String testValue)
136 throws ValidationException
137 {
138 super.assertValidity(testValue);
139
140 if (required || StringUtils.isNotEmpty(testValue))
141 {
142 try
143 {
144 parse(testValue);
145 }
146 catch (ParseException e)
147 {
148 errorMessage = dateFormatMessage;
149 throw new ValidationException(dateFormatMessage);
150 }
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166 public Date parse(String s)
167 throws ParseException
168 {
169 Date date = null;
170
171 if (s == null)
172 {
173 throw new ParseException("Input string was null", -1);
174 }
175
176 SimpleDateFormat sdf = new SimpleDateFormat();
177 sdf.setLenient(flexible);
178
179 for (int i = 1; i < dateFormats.size() && date == null; i++)
180 {
181 sdf.applyPattern(dateFormats.get(i));
182
183 try
184 {
185 date = sdf.parse(s);
186 }
187 catch (ParseException e)
188 {
189
190 }
191 }
192
193 if (date == null)
194 {
195 sdf.applyPattern(dateFormats.get(0));
196
197 try
198 {
199 date = sdf.parse(s);
200 }
201 catch (ParseException e)
202 {
203
204 }
205 }
206
207 if (date == null)
208 {
209
210 date = SimpleDateFormat.getInstance().parse(s);
211 }
212
213
214
215 if (date == null)
216 {
217 throw new ParseException("Could not parse the date", 0);
218 }
219
220 return date;
221 }
222
223
224
225
226
227
228
229
230 public String format(Date date)
231 {
232 String s = null;
233
234 if (date != null && !dateFormats.isEmpty())
235 {
236 SimpleDateFormat sdf = new SimpleDateFormat(dateFormats.get(0));
237 s = sdf.format(date);
238 }
239
240 return s;
241 }
242
243
244
245
246
247
248
249
250
251
252
253 public String getDateFormatMessage()
254 {
255 return dateFormatMessage;
256 }
257
258
259
260
261
262
263
264
265 public void setDateFormatMessage(String message)
266 {
267 if (StringUtils.isNotEmpty(message))
268 {
269 dateFormatMessage = message;
270 }
271 }
272
273
274
275
276
277
278 public List<String> getDateFormats()
279 {
280 return dateFormats;
281 }
282
283
284
285
286
287
288 public void setDateFormats(List<String> formats)
289 {
290 this.dateFormats = formats;
291 }
292
293
294
295
296
297
298 public boolean isFlexible()
299 {
300 return flexible;
301 }
302
303
304
305
306
307
308 public void setFlexible(boolean flexible)
309 {
310 this.flexible = flexible;
311 }
312 }