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