1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator;
18
19 import java.io.Serializable;
20 import java.text.DateFormat;
21 import java.text.NumberFormat;
22 import java.text.ParseException;
23 import java.text.ParsePosition;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.Locale;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31
32
33
34
35 public class GenericTypeValidator implements Serializable {
36
37 private static final long serialVersionUID = 5487162314134261703L;
38
39 private static final Log LOG = LogFactory.getLog(GenericTypeValidator.class);
40
41
42
43
44
45
46
47 public static Byte formatByte(final String value) {
48 if (value == null) {
49 return null;
50 }
51
52 try {
53 return Byte.valueOf(value);
54 } catch (final NumberFormatException e) {
55 return null;
56 }
57
58 }
59
60
61
62
63
64
65
66
67
68 public static Byte formatByte(final String value, final Locale locale) {
69 Byte result = null;
70
71 if (value != null) {
72 NumberFormat formatter = null;
73 if (locale != null) {
74 formatter = NumberFormat.getNumberInstance(locale);
75 } else {
76 formatter = NumberFormat.getNumberInstance(Locale.getDefault());
77 }
78 formatter.setParseIntegerOnly(true);
79 final ParsePosition pos = new ParsePosition(0);
80 final Number num = formatter.parse(value, pos);
81
82
83 if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
84 num.doubleValue() >= Byte.MIN_VALUE &&
85 num.doubleValue() <= Byte.MAX_VALUE) {
86 result = Byte.valueOf(num.byteValue());
87 }
88 }
89
90 return result;
91 }
92
93
94
95
96
97
98
99
100
101
102 public static Long formatCreditCard(final String value) {
103 return GenericValidator.isCreditCard(value) ? Long.valueOf(value) : null;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117 public static Date formatDate(final String value, final Locale locale) {
118 Date date = null;
119
120 if (value == null) {
121 return null;
122 }
123
124 try {
125
126 DateFormat formatterShort = null;
127 DateFormat formatterDefault = null;
128 if (locale != null) {
129 formatterShort =
130 DateFormat.getDateInstance(DateFormat.SHORT, locale);
131 formatterDefault =
132 DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
133 } else {
134 formatterShort =
135 DateFormat.getDateInstance(
136 DateFormat.SHORT,
137 Locale.getDefault());
138 formatterDefault =
139 DateFormat.getDateInstance(
140 DateFormat.DEFAULT,
141 Locale.getDefault());
142 }
143
144
145 formatterShort.setLenient(false);
146 formatterDefault.setLenient(false);
147
148
149 try {
150 date = formatterShort.parse(value);
151 } catch (final ParseException e) {
152
153 date = formatterDefault.parse(value);
154 }
155 } catch (final ParseException e) {
156
157 if (LOG.isDebugEnabled()) {
158 LOG.debug("Date parse failed value=[" + value + "], " +
159 "locale=[" + locale + "] " + e);
160 }
161 }
162
163 return date;
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public static Date formatDate(final String value, final String datePattern, final boolean strict) {
183 Date date = null;
184
185 if (value == null
186 || datePattern == null
187 || datePattern.isEmpty()) {
188 return null;
189 }
190
191 try {
192 final SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
193 formatter.setLenient(false);
194
195 date = formatter.parse(value);
196
197 if (strict && datePattern.length() != value.length()) {
198 date = null;
199 }
200 } catch (final ParseException e) {
201
202 if (LOG.isDebugEnabled()) {
203 LOG.debug("Date parse failed value=[" + value + "], " +
204 "pattern=[" + datePattern + "], " +
205 "strict=[" + strict + "] " + e);
206 }
207 }
208
209 return date;
210 }
211
212
213
214
215
216
217
218 public static Double formatDouble(final String value) {
219 if (value == null) {
220 return null;
221 }
222
223 try {
224 return Double.valueOf(value);
225 } catch (final NumberFormatException e) {
226 return null;
227 }
228
229 }
230
231
232
233
234
235
236
237
238
239 public static Double formatDouble(final String value, final Locale locale) {
240 Double result = null;
241
242 if (value != null) {
243 NumberFormat formatter = null;
244 if (locale != null) {
245 formatter = NumberFormat.getInstance(locale);
246 } else {
247 formatter = NumberFormat.getInstance(Locale.getDefault());
248 }
249 final ParsePosition pos = new ParsePosition(0);
250 final Number num = formatter.parse(value, pos);
251
252
253 if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
254 num.doubleValue() >= Double.MAX_VALUE * -1 &&
255 num.doubleValue() <= Double.MAX_VALUE) {
256 result = Double.valueOf(num.doubleValue());
257 }
258 }
259
260 return result;
261 }
262
263
264
265
266
267
268
269 public static Float formatFloat(final String value) {
270 if (value == null) {
271 return null;
272 }
273
274 try {
275 return Float.valueOf(value);
276 } catch (final NumberFormatException e) {
277 return null;
278 }
279
280 }
281
282
283
284
285
286
287
288
289
290 public static Float formatFloat(final String value, final Locale locale) {
291 Float result = null;
292
293 if (value != null) {
294 NumberFormat formatter = null;
295 if (locale != null) {
296 formatter = NumberFormat.getInstance(locale);
297 } else {
298 formatter = NumberFormat.getInstance(Locale.getDefault());
299 }
300 final ParsePosition pos = new ParsePosition(0);
301 final Number num = formatter.parse(value, pos);
302
303
304 if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
305 num.doubleValue() >= Float.MAX_VALUE * -1 &&
306 num.doubleValue() <= Float.MAX_VALUE) {
307 result = Float.valueOf(num.floatValue());
308 }
309 }
310
311 return result;
312 }
313
314
315
316
317
318
319
320 public static Integer formatInt(final String value) {
321 if (value == null) {
322 return null;
323 }
324
325 try {
326 return Integer.valueOf(value);
327 } catch (final NumberFormatException e) {
328 return null;
329 }
330
331 }
332
333
334
335
336
337
338
339
340
341 public static Integer formatInt(final String value, final Locale locale) {
342 Integer result = null;
343
344 if (value != null) {
345 NumberFormat formatter = null;
346 if (locale != null) {
347 formatter = NumberFormat.getNumberInstance(locale);
348 } else {
349 formatter = NumberFormat.getNumberInstance(Locale.getDefault());
350 }
351 formatter.setParseIntegerOnly(true);
352 final ParsePosition pos = new ParsePosition(0);
353 final Number num = formatter.parse(value, pos);
354
355
356 if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
357 num.doubleValue() >= Integer.MIN_VALUE &&
358 num.doubleValue() <= Integer.MAX_VALUE) {
359 result = Integer.valueOf(num.intValue());
360 }
361 }
362
363 return result;
364 }
365
366
367
368
369
370
371
372 public static Long formatLong(final String value) {
373 if (value == null) {
374 return null;
375 }
376
377 try {
378 return Long.valueOf(value);
379 } catch (final NumberFormatException e) {
380 return null;
381 }
382
383 }
384
385
386
387
388
389
390
391
392
393 public static Long formatLong(final String value, final Locale locale) {
394 Long result = null;
395
396 if (value != null) {
397 NumberFormat formatter = null;
398 if (locale != null) {
399 formatter = NumberFormat.getNumberInstance(locale);
400 } else {
401 formatter = NumberFormat.getNumberInstance(Locale.getDefault());
402 }
403 formatter.setParseIntegerOnly(true);
404 final ParsePosition pos = new ParsePosition(0);
405 final Number num = formatter.parse(value, pos);
406
407
408 if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
409 num.doubleValue() >= Long.MIN_VALUE &&
410 num.doubleValue() <= Long.MAX_VALUE) {
411 result = Long.valueOf(num.longValue());
412 }
413 }
414
415 return result;
416 }
417
418
419
420
421
422
423
424 public static Short formatShort(final String value) {
425 if (value == null) {
426 return null;
427 }
428
429 try {
430 return Short.valueOf(value);
431 } catch (final NumberFormatException e) {
432 return null;
433 }
434
435 }
436
437
438
439
440
441
442
443
444
445 public static Short formatShort(final String value, final Locale locale) {
446 Short result = null;
447
448 if (value != null) {
449 NumberFormat formatter = null;
450 if (locale != null) {
451 formatter = NumberFormat.getNumberInstance(locale);
452 } else {
453 formatter = NumberFormat.getNumberInstance(Locale.getDefault());
454 }
455 formatter.setParseIntegerOnly(true);
456 final ParsePosition pos = new ParsePosition(0);
457 final Number num = formatter.parse(value, pos);
458
459
460 if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
461 num.doubleValue() >= Short.MIN_VALUE &&
462 num.doubleValue() <= Short.MAX_VALUE) {
463 result = Short.valueOf(num.shortValue());
464 }
465 }
466
467 return result;
468 }
469
470 }
471