1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.validator.validators;
17
18 import java.math.BigDecimal;
19
20 import org.seasar.cubby.validator.MessageHelper;
21 import org.seasar.cubby.validator.ScalarFieldValidator;
22 import org.seasar.cubby.validator.ValidationContext;
23 import org.seasar.framework.util.StringUtil;
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class NumberValidator implements ScalarFieldValidator {
38
39
40
41
42 private final MessageHelper messageHelper;
43
44
45
46
47 public NumberValidator() {
48 this("valid.number");
49 }
50
51
52
53
54
55
56
57 public NumberValidator(final String messageKey) {
58 this.messageHelper = new MessageHelper(messageKey);
59 }
60
61 public void validate(final ValidationContext context, final Object value) {
62 if (value instanceof String) {
63 final String str = (String) value;
64 if (StringUtil.isEmpty(str)) {
65 return;
66 }
67 try {
68 new BigDecimal(str);
69 return;
70 } catch (final NumberFormatException e) {
71 }
72 } else if (value == null) {
73 return;
74 }
75 context.addMessageInfo(this.messageHelper.createMessageInfo());
76 }
77
78 }