1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils2.locale;
19
20 import java.lang.reflect.Array;
21 import java.math.BigDecimal;
22 import java.math.BigInteger;
23 import java.util.Locale;
24 import java.util.Map;
25
26 import org.apache.commons.beanutils2.BeanUtils;
27 import org.apache.commons.beanutils2.ConversionException;
28 import org.apache.commons.beanutils2.locale.converters.BigDecimalLocaleConverter;
29 import org.apache.commons.beanutils2.locale.converters.BigIntegerLocaleConverter;
30 import org.apache.commons.beanutils2.locale.converters.ByteLocaleConverter;
31 import org.apache.commons.beanutils2.locale.converters.DoubleLocaleConverter;
32 import org.apache.commons.beanutils2.locale.converters.FloatLocaleConverter;
33 import org.apache.commons.beanutils2.locale.converters.IntegerLocaleConverter;
34 import org.apache.commons.beanutils2.locale.converters.LongLocaleConverter;
35 import org.apache.commons.beanutils2.locale.converters.ShortLocaleConverter;
36 import org.apache.commons.beanutils2.locale.converters.StringLocaleConverter;
37 import org.apache.commons.beanutils2.sql.converters.locale.SqlDateLocaleConverter;
38 import org.apache.commons.beanutils2.sql.converters.locale.SqlTimeLocaleConverter;
39 import org.apache.commons.beanutils2.sql.converters.locale.SqlTimestampLocaleConverter;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public class LocaleConvertUtilsBean {
83
84
85 private static final Log LOG = LogFactory.getLog(LocaleConvertUtilsBean.class);
86
87
88
89
90
91
92 public static LocaleConvertUtilsBean getInstance() {
93 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getLocaleConvertUtils();
94 }
95
96
97 private Locale defaultLocale = Locale.getDefault();
98
99
100 private boolean applyLocalized;
101
102
103
104
105
106
107
108
109 private final Map<Locale, Map<Class<?>, LocaleConverter<?>>> mapConverters;
110
111
112
113
114 public LocaleConvertUtilsBean() {
115 mapConverters = BeanUtils.createCache();
116 deregister();
117 }
118
119
120
121
122
123
124
125
126 public String convert(final Object value) {
127 return convert(value, defaultLocale, null);
128 }
129
130
131
132
133
134
135
136
137
138
139 public String convert(final Object value, final Locale locale, final String pattern) {
140 final LocaleConverter<String> converter = lookup(String.class, locale);
141 return converter.convert(String.class, value, pattern);
142 }
143
144
145
146
147
148
149
150
151
152 public String convert(final Object value, final String pattern) {
153 return convert(value, defaultLocale, pattern);
154 }
155
156
157
158
159
160
161
162
163
164 public Object convert(final String value, final Class<?> clazz) {
165 return convert(value, clazz, defaultLocale, null);
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179 public Object convert(final String value, final Class<?> clazz, final Locale locale, final String pattern) {
180 if (LOG.isDebugEnabled()) {
181 LOG.debug("Convert string " + value + " to class " + clazz.getName() + " using " + locale + " locale and " + pattern + " pattern");
182 }
183
184 Class<?> targetClass = clazz;
185 LocaleConverter converter = lookup(clazz, locale);
186
187 if (converter == null) {
188 converter = lookup(String.class, locale);
189 targetClass = String.class;
190 }
191 if (LOG.isTraceEnabled()) {
192 LOG.trace(" Using converter " + converter);
193 }
194
195 return converter.convert(targetClass, value, pattern);
196 }
197
198
199
200
201
202
203
204
205
206
207
208 public Object convert(final String value, final Class<?> clazz, final String pattern) {
209 return convert(value, clazz, defaultLocale, pattern);
210 }
211
212
213
214
215
216
217
218
219
220 public Object convert(final String[] values, final Class<?> clazz) {
221 return convert(values, clazz, getDefaultLocale(), null);
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235 public <T> T[] convert(final String[] values, final Class<T> clazz, final Locale locale, final String pattern) {
236 Class<?> type = clazz;
237 if (clazz.isArray()) {
238 type = clazz.getComponentType();
239 }
240 if (LOG.isDebugEnabled()) {
241 LOG.debug("Convert String[" + values.length + "] to class " + type.getName() + "[] using " + locale + " locale and " + pattern + " pattern");
242 }
243
244 final T[] array = (T[]) Array.newInstance(type, values.length);
245 for (int i = 0; i < values.length; i++) {
246 Array.set(array, i, convert(values[i], type, locale, pattern));
247 }
248 return array;
249 }
250
251
252
253
254
255
256
257
258
259
260
261 public <T> T[] convert(final String[] values, final Class<T> clazz, final String pattern) {
262 return convert(values, clazz, getDefaultLocale(), pattern);
263 }
264
265
266
267
268
269
270
271 protected Map<Class<?>, LocaleConverter<?>> create(final Locale locale) {
272 final Map<Class<?>, LocaleConverter<?>> converter = BeanUtils.createCache();
273
274 converter.put(BigDecimal.class, BigDecimalLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
275 converter.put(BigInteger.class, BigIntegerLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
276
277 converter.put(Byte.class, ByteLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
278 converter.put(Byte.TYPE, ByteLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
279
280 converter.put(Double.class, DoubleLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
281 converter.put(Double.TYPE, DoubleLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
282
283 converter.put(Float.class, FloatLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
284 converter.put(Float.TYPE, FloatLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
285
286 converter.put(Integer.class, IntegerLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
287 converter.put(Integer.TYPE, IntegerLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
288
289 converter.put(Long.class, LongLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
290 converter.put(Long.TYPE, LongLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
291
292 converter.put(Short.class, ShortLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
293 converter.put(Short.TYPE, ShortLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
294
295 converter.put(String.class, StringLocaleConverter.builder().setLocale(locale).setLocalizedPattern(applyLocalized).get());
296
297
298
299 converter.put(java.sql.Date.class, SqlDateLocaleConverter.builder().setLocale(locale).setPattern("yyyy-MM-dd").get());
300 converter.put(java.sql.Time.class, SqlTimeLocaleConverter.builder().setLocale(locale).setPattern("HH:mm:ss").get());
301 converter.put(java.sql.Timestamp.class, SqlTimestampLocaleConverter.builder().setLocale(locale).setPattern("yyyy-MM-dd HH:mm:ss.S").get());
302
303 return converter;
304 }
305
306
307
308
309 public void deregister() {
310 final Map<Class<?>, LocaleConverter<?>> defaultConverter = lookup(defaultLocale);
311 mapConverters.clear();
312 mapConverters.put(defaultLocale, defaultConverter);
313 }
314
315
316
317
318
319
320
321 public void deregister(final Class<?> clazz, final Locale locale) {
322 lookup(locale).remove(clazz);
323 }
324
325
326
327
328
329
330 public void deregister(final Locale locale) {
331 mapConverters.remove(locale);
332 }
333
334
335
336
337
338
339 public boolean getApplyLocalized() {
340 return applyLocalized;
341 }
342
343
344
345
346
347
348 public Locale getDefaultLocale() {
349 return defaultLocale;
350 }
351
352
353
354
355
356
357
358
359
360
361 public <T> LocaleConverter<T> lookup(final Class<T> clazz, final Locale locale) {
362 final LocaleConverter<T> converter = (LocaleConverter<T>) lookup(locale).get(clazz);
363
364 if (LOG.isTraceEnabled()) {
365 LOG.trace("LocaleConverter:" + converter);
366 }
367
368 return converter;
369 }
370
371
372
373
374
375
376
377 protected Map<Class<?>, LocaleConverter<?>> lookup(final Locale locale) {
378 return mapConverters.computeIfAbsent(locale == null ? defaultLocale : locale, this::create);
379 }
380
381
382
383
384
385
386
387
388
389 public <T> void register(final LocaleConverter<T> converter, final Class<T> clazz, final Locale locale) {
390 lookup(locale).put(clazz, converter);
391 }
392
393
394
395
396
397
398 public void setApplyLocalized(final boolean newApplyLocalized) {
399 applyLocalized = newApplyLocalized;
400 }
401
402
403
404
405
406
407 public void setDefaultLocale(final Locale locale) {
408 if (locale == null) {
409 defaultLocale = Locale.getDefault();
410 } else {
411 defaultLocale = locale;
412 }
413 }
414 }