1 | |
package org.seasar.cubby.validator.validators; |
2 | |
|
3 | |
import java.math.BigDecimal; |
4 | |
|
5 | |
import org.seasar.cubby.validator.BaseValidator; |
6 | |
import org.seasar.cubby.validator.ValidationContext; |
7 | |
import org.seasar.framework.util.StringUtil; |
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
public class NumberValidator extends BaseValidator { |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
public NumberValidator() { |
21 | 5 | this("valid.number"); |
22 | 5 | } |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 5 | public NumberValidator(final String messageKey) { |
29 | 5 | this.setMessageKey(messageKey); |
30 | 5 | } |
31 | |
|
32 | |
public String validate(final ValidationContext ctx) { |
33 | 14 | final Object value = ctx.getValue(); |
34 | 14 | if (value instanceof String) { |
35 | 14 | String str = (String)value; |
36 | 14 | if (StringUtil.isEmpty(str)) { |
37 | |
return null; |
38 | |
} |
39 | |
try { |
40 | 14 | new BigDecimal(str); |
41 | 8 | return null; |
42 | 6 | } catch (NumberFormatException e) {} |
43 | 6 | }else if(value == null){ |
44 | |
return null; |
45 | |
} |
46 | 6 | return getMessage(getPropertyMessage(ctx.getName())); |
47 | |
} |
48 | |
} |