1 16 17 package org.apache.commons.beanutils.locale; 18 19 import org.apache.commons.beanutils.locale.converters.*; 20 import org.apache.commons.beanutils.Converter; 21 import org.apache.commons.collections.FastHashMap; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import java.lang.reflect.Array ; 26 import java.math.BigDecimal ; 27 import java.math.BigInteger ; 28 import java.sql.Date ; 29 import java.sql.Time ; 30 import java.sql.Timestamp ; 31 import java.util.Locale ; 32 33 73 public class LocaleConvertUtilsBean { 74 75 79 public static LocaleConvertUtilsBean getInstance() { 80 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getLocaleConvertUtils(); 81 } 82 83 85 86 private Locale defaultLocale = Locale.getDefault(); 87 88 89 private boolean applyLocalized = false; 90 91 92 private Log log = LogFactory.getLog(LocaleConvertUtils.class); 93 94 98 private FastHashMap mapConverters = new FastHashMap(); 99 100 102 106 public LocaleConvertUtilsBean() { 107 deregister(); 108 } 109 110 112 115 public Locale getDefaultLocale() { 116 117 return defaultLocale; 118 } 119 120 123 public void setDefaultLocale(Locale locale) { 124 125 if (locale == null) { 126 defaultLocale = Locale.getDefault(); 127 } 128 else { 129 defaultLocale = locale; 130 } 131 } 132 133 136 public boolean getApplyLocalized() { 137 return applyLocalized; 138 } 139 140 143 public void setApplyLocalized(boolean newApplyLocalized) { 144 applyLocalized = newApplyLocalized; 145 } 146 147 149 156 public String convert(Object value) { 157 return convert(value, defaultLocale, null); 158 } 159 160 169 public String convert(Object value, String pattern) { 170 return convert(value, defaultLocale, pattern); 171 } 172 173 183 public String convert(Object value, Locale locale, String pattern) { 184 185 LocaleConverter converter = lookup(String .class, locale); 186 187 return (String ) converter.convert(String .class, value, pattern); 188 } 189 190 199 public Object convert(String value, Class clazz) { 200 201 return convert(value, clazz, defaultLocale, null); 202 } 203 204 215 public Object convert(String value, Class clazz, String pattern) { 216 217 return convert(value, clazz, defaultLocale, pattern); 218 } 219 220 232 public Object convert(String value, Class clazz, Locale locale, String pattern) { 233 234 if (log.isDebugEnabled()) { 235 log.debug("Convert string " + value + " to class " + 236 clazz.getName() + " using " + locale.toString() + 237 " locale and " + pattern + " pattern"); 238 } 239 240 LocaleConverter converter = lookup(clazz, locale); 241 242 if (converter == null) { 243 converter = (LocaleConverter) lookup(String .class, locale); 244 } 245 if (log.isTraceEnabled()) { 246 log.trace(" Using converter " + converter); 247 } 248 249 return (converter.convert(clazz, value, pattern)); 250 } 251 252 262 public Object convert(String values[], Class clazz, String pattern) { 263 264 return convert(values, clazz, getDefaultLocale(), pattern); 265 } 266 267 276 public Object convert(String values[], Class clazz) { 277 278 return convert(values, clazz, getDefaultLocale(), null); 279 } 280 281 292 public Object convert(String values[], Class clazz, Locale locale, String pattern) { 293 294 Class type = clazz; 295 if (clazz.isArray()) { 296 type = clazz.getComponentType(); 297 } 298 if (log.isDebugEnabled()) { 299 log.debug("Convert String[" + values.length + "] to class " + 300 type.getName() + "[] using " + locale.toString() + 301 " locale and " + pattern + " pattern"); 302 } 303 304 Object array = Array.newInstance(type, values.length); 305 for (int i = 0; i < values.length; i++) { 306 Array.set(array, i, convert(values[i], type, locale, pattern)); 307 } 308 309 return (array); 310 } 311 312 321 public void register(LocaleConverter converter, Class clazz, Locale locale) { 322 323 lookup(locale).put(clazz, converter); 324 } 325 326 329 public void deregister() { 330 331 FastHashMap defaultConverter = lookup(defaultLocale); 332 333 mapConverters.setFast(false); 334 335 mapConverters.clear(); 336 mapConverters.put(defaultLocale, defaultConverter); 337 338 mapConverters.setFast(true); 339 } 340 341 342 347 public void deregister(Locale locale) { 348 349 mapConverters.remove(locale); 350 } 351 352 353 359 public void deregister(Class clazz, Locale locale) { 360 361 lookup(locale).remove(clazz); 362 } 363 364 372 public LocaleConverter lookup(Class clazz, Locale locale) { 373 374 LocaleConverter converter = (LocaleConverter) lookup(locale).get(clazz); 375 376 if (log.isTraceEnabled()) { 377 log.trace("LocaleConverter:" + converter); 378 } 379 380 return converter; 381 } 382 383 391 protected FastHashMap lookup(Locale locale) { 392 FastHashMap localeConverters; 393 394 if (locale == null) { 395 localeConverters = (FastHashMap) mapConverters.get(defaultLocale); 396 } 397 else { 398 localeConverters = (FastHashMap) mapConverters.get(locale); 399 400 if (localeConverters == null) { 401 localeConverters = create(locale); 402 mapConverters.put(locale, localeConverters); 403 } 404 } 405 406 return localeConverters; 407 } 408 409 416 protected FastHashMap create(Locale locale) { 417 418 FastHashMap converter = new FastHashMap(); 419 converter.setFast(false); 420 421 converter.put(BigDecimal .class, new BigDecimalLocaleConverter(locale, applyLocalized)); 422 converter.put(BigInteger .class, new BigIntegerLocaleConverter(locale, applyLocalized)); 423 424 converter.put(Byte .class, new ByteLocaleConverter(locale, applyLocalized)); 425 converter.put(Byte.TYPE, new ByteLocaleConverter(locale, applyLocalized)); 426 427 converter.put(Double .class, new DoubleLocaleConverter(locale, applyLocalized)); 428 converter.put(Double.TYPE, new DoubleLocaleConverter(locale, applyLocalized)); 429 430 converter.put(Float .class, new FloatLocaleConverter(locale, applyLocalized)); 431 converter.put(Float.TYPE, new FloatLocaleConverter(locale, applyLocalized)); 432 433 converter.put(Integer .class, new IntegerLocaleConverter(locale, applyLocalized)); 434 converter.put(Integer.TYPE, new IntegerLocaleConverter(locale, applyLocalized)); 435 436 converter.put(Long .class, new LongLocaleConverter(locale, applyLocalized)); 437 converter.put(Long.TYPE, new LongLocaleConverter(locale, applyLocalized)); 438 439 converter.put(Short .class, new ShortLocaleConverter(locale, applyLocalized)); 440 converter.put(Short.TYPE, new ShortLocaleConverter(locale, applyLocalized)); 441 442 converter.put(String .class, new StringLocaleConverter(locale, applyLocalized)); 443 444 converter.put(java.sql.Date .class, new SqlDateLocaleConverter(locale, "yyyy-MM-dd")); 447 converter.put(java.sql.Time .class, new SqlTimeLocaleConverter(locale, "HH:mm:ss")); 448 converter.put( java.sql.Timestamp .class, 449 new SqlTimestampLocaleConverter(locale, "yyyy-MM-dd HH:mm:ss.S") 450 ); 451 452 converter.setFast(true); 453 454 return converter; 455 } 456 } 457 | Popular Tags |