1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils.locale;
19
20 import java.lang.reflect.Array;
21 import java.math.BigDecimal;
22 import java.math.BigInteger;
23 import java.util.Collection;
24 import java.util.Locale;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.apache.commons.beanutils.BeanUtils;
29 import org.apache.commons.beanutils.locale.converters.BigDecimalLocaleConverter;
30 import org.apache.commons.beanutils.locale.converters.BigIntegerLocaleConverter;
31 import org.apache.commons.beanutils.locale.converters.ByteLocaleConverter;
32 import org.apache.commons.beanutils.locale.converters.DoubleLocaleConverter;
33 import org.apache.commons.beanutils.locale.converters.FloatLocaleConverter;
34 import org.apache.commons.beanutils.locale.converters.IntegerLocaleConverter;
35 import org.apache.commons.beanutils.locale.converters.LongLocaleConverter;
36 import org.apache.commons.beanutils.locale.converters.ShortLocaleConverter;
37 import org.apache.commons.beanutils.locale.converters.SqlDateLocaleConverter;
38 import org.apache.commons.beanutils.locale.converters.SqlTimeLocaleConverter;
39 import org.apache.commons.beanutils.locale.converters.SqlTimestampLocaleConverter;
40 import org.apache.commons.beanutils.locale.converters.StringLocaleConverter;
41 import org.apache.commons.collections.FastHashMap;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
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
83
84
85 public class LocaleConvertUtilsBean {
86
87
88
89
90
91
92 public static LocaleConvertUtilsBean getInstance() {
93 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getLocaleConvertUtils();
94 }
95
96
97
98
99 private Locale defaultLocale = Locale.getDefault();
100
101
102 private boolean applyLocalized = false;
103
104
105 private final Log log = LogFactory.getLog(LocaleConvertUtils.class);
106
107
108
109
110
111 private final FastHashMap mapConverters = new DelegateFastHashMap(BeanUtils.createCache());
112
113
114
115
116
117
118
119 public LocaleConvertUtilsBean() {
120 mapConverters.setFast(false);
121 deregister();
122 mapConverters.setFast(true);
123 }
124
125
126
127
128
129
130
131 public Locale getDefaultLocale() {
132
133 return defaultLocale;
134 }
135
136
137
138
139
140 public void setDefaultLocale(final Locale locale) {
141
142 if (locale == null) {
143 defaultLocale = Locale.getDefault();
144 }
145 else {
146 defaultLocale = locale;
147 }
148 }
149
150
151
152
153
154
155
156 public boolean getApplyLocalized() {
157 return applyLocalized;
158 }
159
160
161
162
163
164
165
166 public void setApplyLocalized(final boolean newApplyLocalized) {
167 applyLocalized = newApplyLocalized;
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181 public String convert(final Object value) {
182 return convert(value, defaultLocale, null);
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196 public String convert(final Object value, final String pattern) {
197 return convert(value, defaultLocale, pattern);
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212 public String convert(final Object value, final Locale locale, final String pattern) {
213
214 final LocaleConverter converter = lookup(String.class, locale);
215
216 return converter.convert(String.class, value, pattern);
217 }
218
219
220
221
222
223
224
225
226
227
228
229
230 public Object convert(final String value, final Class<?> clazz) {
231
232 return convert(value, clazz, defaultLocale, null);
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 public Object convert(final String value, final Class<?> clazz, final String pattern) {
249
250 return convert(value, clazz, defaultLocale, pattern);
251 }
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267 public Object convert(final String value, final Class<?> clazz, final Locale locale, final String pattern) {
268
269 if (log.isDebugEnabled()) {
270 log.debug("Convert string " + value + " to class " +
271 clazz.getName() + " using " + locale +
272 " locale and " + pattern + " pattern");
273 }
274
275 Class<?> targetClass = clazz;
276 LocaleConverter converter = lookup(clazz, locale);
277
278 if (converter == null) {
279 converter = lookup(String.class, locale);
280 targetClass = String.class;
281 }
282 if (log.isTraceEnabled()) {
283 log.trace(" Using converter " + converter);
284 }
285
286 return (converter.convert(targetClass, value, pattern));
287 }
288
289
290
291
292
293
294
295
296
297
298
299
300
301 public Object convert(final String[] values, final Class<?> clazz, final String pattern) {
302
303 return convert(values, clazz, getDefaultLocale(), pattern);
304 }
305
306
307
308
309
310
311
312
313
314
315
316
317 public Object convert(final String[] values, final Class<?> clazz) {
318
319 return convert(values, clazz, getDefaultLocale(), null);
320 }
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 public Object convert(final String[] values, final Class<?> clazz, final Locale locale, final String pattern) {
336
337 Class<?> type = clazz;
338 if (clazz.isArray()) {
339 type = clazz.getComponentType();
340 }
341 if (log.isDebugEnabled()) {
342 log.debug("Convert String[" + values.length + "] to class " +
343 type.getName() + "[] using " + locale +
344 " locale and " + pattern + " pattern");
345 }
346
347 final Object array = Array.newInstance(type, values.length);
348 for (int i = 0; i < values.length; i++) {
349 Array.set(array, i, convert(values[i], type, locale, pattern));
350 }
351
352 return (array);
353 }
354
355
356
357
358
359
360
361
362
363
364 public void register(final LocaleConverter converter, final Class<?> clazz, final Locale locale) {
365
366 lookup(locale).put(clazz, converter);
367 }
368
369
370
371
372 public void deregister() {
373
374 final FastHashMap defaultConverter = lookup(defaultLocale);
375
376 mapConverters.setFast(false);
377
378 mapConverters.clear();
379 mapConverters.put(defaultLocale, defaultConverter);
380
381 mapConverters.setFast(true);
382 }
383
384
385
386
387
388
389
390 public void deregister(final Locale locale) {
391
392 mapConverters.remove(locale);
393 }
394
395
396
397
398
399
400
401
402 public void deregister(final Class<?> clazz, final Locale locale) {
403
404 lookup(locale).remove(clazz);
405 }
406
407
408
409
410
411
412
413
414
415
416 public LocaleConverter lookup(final Class<?> clazz, final Locale locale) {
417
418 final LocaleConverter converter = (LocaleConverter) lookup(locale).get(clazz);
419
420 if (log.isTraceEnabled()) {
421 log.trace("LocaleConverter:" + converter);
422 }
423
424 return converter;
425 }
426
427
428
429
430
431
432
433
434
435
436 @Deprecated
437 protected FastHashMap lookup(final Locale locale) {
438 FastHashMap localeConverters;
439
440 if (locale == null) {
441 localeConverters = (FastHashMap) mapConverters.get(defaultLocale);
442 }
443 else {
444 localeConverters = (FastHashMap) mapConverters.get(locale);
445
446 if (localeConverters == null) {
447 localeConverters = create(locale);
448 mapConverters.put(locale, localeConverters);
449 }
450 }
451
452 return localeConverters;
453 }
454
455
456
457
458
459
460
461
462
463 @Deprecated
464 protected FastHashMap create(final Locale locale) {
465
466 final FastHashMap converter = new DelegateFastHashMap(BeanUtils.createCache());
467 converter.setFast(false);
468
469 converter.put(BigDecimal.class, new BigDecimalLocaleConverter(locale, applyLocalized));
470 converter.put(BigInteger.class, new BigIntegerLocaleConverter(locale, applyLocalized));
471
472 converter.put(Byte.class, new ByteLocaleConverter(locale, applyLocalized));
473 converter.put(Byte.TYPE, new ByteLocaleConverter(locale, applyLocalized));
474
475 converter.put(Double.class, new DoubleLocaleConverter(locale, applyLocalized));
476 converter.put(Double.TYPE, new DoubleLocaleConverter(locale, applyLocalized));
477
478 converter.put(Float.class, new FloatLocaleConverter(locale, applyLocalized));
479 converter.put(Float.TYPE, new FloatLocaleConverter(locale, applyLocalized));
480
481 converter.put(Integer.class, new IntegerLocaleConverter(locale, applyLocalized));
482 converter.put(Integer.TYPE, new IntegerLocaleConverter(locale, applyLocalized));
483
484 converter.put(Long.class, new LongLocaleConverter(locale, applyLocalized));
485 converter.put(Long.TYPE, new LongLocaleConverter(locale, applyLocalized));
486
487 converter.put(Short.class, new ShortLocaleConverter(locale, applyLocalized));
488 converter.put(Short.TYPE, new ShortLocaleConverter(locale, applyLocalized));
489
490 converter.put(String.class, new StringLocaleConverter(locale, applyLocalized));
491
492
493
494 converter.put(java.sql.Date.class, new SqlDateLocaleConverter(locale, "yyyy-MM-dd"));
495 converter.put(java.sql.Time.class, new SqlTimeLocaleConverter(locale, "HH:mm:ss"));
496 converter.put( java.sql.Timestamp.class,
497 new SqlTimestampLocaleConverter(locale, "yyyy-MM-dd HH:mm:ss.S")
498 );
499
500 converter.setFast(true);
501
502 return converter;
503 }
504
505
506
507
508
509
510
511
512
513 private static class DelegateFastHashMap extends FastHashMap {
514
515 private final Map<Object, Object> map;
516
517 private DelegateFastHashMap(final Map<Object, Object> map) {
518 this.map = map;
519 }
520 @Override
521 public void clear() {
522 map.clear();
523 }
524 @Override
525 public boolean containsKey(final Object key) {
526 return map.containsKey(key);
527 }
528 @Override
529 public boolean containsValue(final Object value) {
530 return map.containsValue(value);
531 }
532 @Override
533 public Set<Map.Entry<Object, Object>> entrySet() {
534 return map.entrySet();
535 }
536 @Override
537 public boolean equals(final Object o) {
538 return map.equals(o);
539 }
540 @Override
541 public Object get(final Object key) {
542 return map.get(key);
543 }
544 @Override
545 public int hashCode() {
546 return map.hashCode();
547 }
548 @Override
549 public boolean isEmpty() {
550 return map.isEmpty();
551 }
552 @Override
553 public Set<Object> keySet() {
554 return map.keySet();
555 }
556 @Override
557 public Object put(final Object key, final Object value) {
558 return map.put(key, value);
559 }
560 @SuppressWarnings({ "rawtypes", "unchecked" })
561
562
563 @Override
564 public void putAll(final Map m) {
565 map.putAll(m);
566 }
567 @Override
568 public Object remove(final Object key) {
569 return map.remove(key);
570 }
571 @Override
572 public int size() {
573 return map.size();
574 }
575 @Override
576 public Collection<Object> values() {
577 return map.values();
578 }
579 @Override
580 public boolean getFast() {
581 return BeanUtils.getCacheFast(map);
582 }
583 @Override
584 public void setFast(final boolean fast) {
585 BeanUtils.setCacheFast(map, fast);
586 }
587 }
588 }