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