1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils2.locale.converters;
19
20 import java.text.DateFormat;
21 import java.text.DateFormatSymbols;
22 import java.text.ParseException;
23 import java.text.ParsePosition;
24 import java.text.SimpleDateFormat;
25 import java.util.Calendar;
26 import java.util.Date;
27 import java.util.Locale;
28
29 import org.apache.commons.beanutils2.ConversionException;
30 import org.apache.commons.beanutils2.locale.BaseLocaleConverter;
31 import org.apache.commons.beanutils2.locale.LocaleConverter;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35
36
37
38
39
40
41
42 public class DateLocaleConverter<D extends Date> extends BaseLocaleConverter<D> {
43
44
45
46
47
48
49
50 public static class Builder<B extends Builder<B, D>, D extends Date> extends BaseLocaleConverter.Builder<B, D> {
51
52
53 private boolean lenient;
54
55
56
57
58 public Builder() {
59
60 }
61
62
63
64
65
66
67
68
69
70
71 @Override
72 public DateLocaleConverter<D> get() {
73 return new DateLocaleConverter<>(defaultValue, locale, pattern, useDefault || defaultValue != null, localizedPattern, lenient);
74 }
75
76
77
78
79
80
81
82 public boolean isLenient() {
83 return lenient;
84 }
85
86
87
88
89
90
91
92 public B setLenient(final boolean lenient) {
93 this.lenient = lenient;
94 return asThis();
95 }
96
97 }
98
99
100
101
102 private static final String DEFAULT_PATTERN_CHARS = DateLocaleConverter.initDefaultChars();
103
104
105 private static final Log LOG = LogFactory.getLog(DateLocaleConverter.class);
106
107
108
109
110
111
112
113
114 @SuppressWarnings("unchecked")
115 public static <B extends Builder<B, D>, D extends Date> B builder() {
116 return (B) new Builder<>();
117 }
118
119
120
121
122
123 private static String initDefaultChars() {
124 return new DateFormatSymbols(Locale.US).getLocalPatternChars();
125 }
126
127
128 private final boolean isLenient;
129
130
131
132
133
134
135
136
137
138
139
140 protected DateLocaleConverter(final D defaultValue, final Locale locale, final String pattern, final boolean useDefault, final boolean locPattern,
141 final boolean lenient) {
142 super(defaultValue, locale, pattern, useDefault, locPattern);
143 this.isLenient = lenient;
144 }
145
146
147
148
149
150
151
152
153 private String convertLocalizedPattern(final String localizedPattern, final Locale locale) {
154 if (localizedPattern == null) {
155 return null;
156 }
157
158
159
160
161
162
163 final DateFormatSymbols localizedSymbols = new DateFormatSymbols(locale);
164 final String localChars = localizedSymbols.getLocalPatternChars();
165
166 if (DEFAULT_PATTERN_CHARS.equals(localChars)) {
167 return localizedPattern;
168 }
169
170
171 String convertedPattern = null;
172 try {
173 convertedPattern = convertPattern(localizedPattern, localChars, DEFAULT_PATTERN_CHARS);
174 } catch (final Exception ex) {
175 if (LOG.isDebugEnabled()) {
176 LOG.debug("Converting pattern '" + localizedPattern + "' for " + locale, ex);
177 }
178 }
179 return convertedPattern;
180 }
181
182
183
184
185 private String convertPattern(final String pattern, final String fromChars, final String toChars) {
186 final StringBuilder converted = new StringBuilder();
187 boolean quoted = false;
188
189 for (int i = 0; i < pattern.length(); ++i) {
190 char thisChar = pattern.charAt(i);
191 if (quoted) {
192 if (thisChar == '\'') {
193 quoted = false;
194 }
195 } else if (thisChar == '\'') {
196 quoted = true;
197 } else if (thisChar >= 'a' && thisChar <= 'z' || thisChar >= 'A' && thisChar <= 'Z') {
198 final int index = fromChars.indexOf(thisChar);
199 if (index == -1) {
200 throw new IllegalArgumentException("Illegal pattern character '" + thisChar + "'");
201 }
202 thisChar = toChars.charAt(index);
203 }
204 converted.append(thisChar);
205 }
206
207 if (quoted) {
208 throw new IllegalArgumentException("Unfinished quote in pattern");
209 }
210
211 return converted.toString();
212 }
213
214
215
216
217
218
219
220 public boolean isLenient() {
221 return isLenient;
222 }
223
224
225
226
227
228
229
230
231
232
233 @Override
234 protected D parse(final Object value, String pattern) throws ParseException {
235
236 if (value instanceof Date) {
237 return (D) value;
238 }
239
240
241 if (value instanceof Calendar) {
242 return (D) ((Calendar) value).getTime();
243 }
244
245 if (localizedPattern) {
246 pattern = convertLocalizedPattern(pattern, locale);
247 }
248
249
250 final DateFormat formatter = pattern == null ? DateFormat.getDateInstance(DateFormat.SHORT, locale) : new SimpleDateFormat(pattern, locale);
251 formatter.setLenient(isLenient);
252
253
254 final ParsePosition pos = new ParsePosition(0);
255 final String strValue = value.toString();
256 final Object parsedValue = formatter.parseObject(strValue, pos);
257 if (pos.getErrorIndex() > -1) {
258 throw ConversionException.format("Error parsing date '%s' at position = %s", value, pos.getErrorIndex());
259 }
260 if (pos.getIndex() < strValue.length()) {
261 throw ConversionException.format("Date '%s' contains unparsed characters from position = %s", value, pos.getIndex());
262 }
263
264 return (D) parsedValue;
265 }
266
267 }