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 static org.seasar.cubby.internal.util.LogMessages.format; |
19 | |
|
20 | |
import java.text.DateFormat; |
21 | |
import java.text.ParsePosition; |
22 | |
import java.text.SimpleDateFormat; |
23 | |
import java.util.Date; |
24 | |
|
25 | |
import org.seasar.cubby.controller.FormatPattern; |
26 | |
import org.seasar.cubby.internal.util.StringUtils; |
27 | |
import org.seasar.cubby.spi.ContainerProvider; |
28 | |
import org.seasar.cubby.spi.ProviderFactory; |
29 | |
import org.seasar.cubby.spi.container.Container; |
30 | |
import org.seasar.cubby.validator.MessageHelper; |
31 | |
import org.seasar.cubby.validator.ScalarFieldValidator; |
32 | |
import org.seasar.cubby.validator.ValidationContext; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public class DateFormatValidator implements ScalarFieldValidator { |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
private final MessageHelper messageHelper; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private final String pattern; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public DateFormatValidator() { |
64 | 0 | this(null); |
65 | 0 | } |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public DateFormatValidator(final String pattern) { |
74 | 1 | this(pattern, "valid.dateFormat"); |
75 | 1 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | 1 | public DateFormatValidator(final String pattern, final String messageKey) { |
86 | 1 | this.pattern = pattern; |
87 | 1 | this.messageHelper = new MessageHelper(messageKey); |
88 | 1 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public void validate(final ValidationContext context, final Object value) { |
94 | 4 | if (value == null) { |
95 | 0 | return; |
96 | |
} |
97 | 4 | if (value instanceof String) { |
98 | 4 | final String stringValue = (String) value; |
99 | 4 | if (StringUtils.isEmpty((String) value)) { |
100 | 0 | return; |
101 | |
} |
102 | |
try { |
103 | 4 | final DateFormat dateFormat = createDateFormat(context, value); |
104 | 4 | final ParsePosition parsePosition = new ParsePosition(0); |
105 | 4 | final Date date = dateFormat.parse(stringValue, parsePosition); |
106 | 4 | if (date != null |
107 | |
&& parsePosition.getIndex() == stringValue.length()) { |
108 | 1 | return; |
109 | |
} |
110 | 0 | } catch (final Exception e) { |
111 | 3 | } |
112 | |
} |
113 | |
|
114 | 3 | context.addMessageInfo(this.messageHelper.createMessageInfo()); |
115 | 3 | } |
116 | |
|
117 | |
private DateFormat createDateFormat(final ValidationContext context, |
118 | |
final Object value) { |
119 | 4 | final SimpleDateFormat dateFormat = new SimpleDateFormat(); |
120 | |
final String pattern; |
121 | 4 | if (StringUtils.isEmpty(this.pattern)) { |
122 | 0 | final Container container = ProviderFactory.get( |
123 | |
ContainerProvider.class).getContainer(); |
124 | 0 | final FormatPattern formatPattern = container |
125 | |
.lookup(FormatPattern.class); |
126 | 0 | if (formatPattern == null) { |
127 | 0 | throw new IllegalStateException(format("ECUB0301", this, value)); |
128 | |
} |
129 | 0 | pattern = formatPattern.getDatePattern(); |
130 | 0 | } else { |
131 | 4 | pattern = this.pattern; |
132 | |
} |
133 | 4 | dateFormat.applyPattern(pattern); |
134 | 4 | dateFormat.setLenient(false); |
135 | 4 | return dateFormat; |
136 | |
} |
137 | |
|
138 | |
} |