| 1 16 17 package org.apache.commons.beanutils.locale; 18 19 20 import org.apache.commons.beanutils.*; 21 import org.apache.commons.beanutils.ContextClassLoaderLocal; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import java.beans.IndexedPropertyDescriptor ; 26 import java.beans.PropertyDescriptor ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.util.Locale ; 29 30 31 43 44 public class LocaleBeanUtilsBean extends BeanUtilsBean { 45 46 49 private static final ContextClassLoaderLocal 50 localeBeansByClassLoader = new ContextClassLoaderLocal() { 51 protected Object initialValue() { 53 return new LocaleBeanUtilsBean(); 54 } 55 }; 56 57 58 public synchronized static LocaleBeanUtilsBean getLocaleBeanUtilsInstance() { 59 return (LocaleBeanUtilsBean)localeBeansByClassLoader.get(); 60 } 61 62 67 public synchronized static void setInstance(LocaleBeanUtilsBean newInstance) { 68 localeBeansByClassLoader.set(newInstance); 69 } 70 71 72 private static Log log = LogFactory.getLog(LocaleBeanUtilsBean.class); 73 74 76 77 private LocaleConvertUtilsBean localeConvertUtils; 78 79 81 82 public LocaleBeanUtilsBean() { 83 this.localeConvertUtils = new LocaleConvertUtilsBean(); 84 } 85 86 94 public LocaleBeanUtilsBean( 95 LocaleConvertUtilsBean localeConvertUtils, 96 ConvertUtilsBean convertUtilsBean, 97 PropertyUtilsBean propertyUtilsBean) { 98 super(convertUtilsBean, propertyUtilsBean); 99 this.localeConvertUtils = localeConvertUtils; 100 } 101 102 108 public LocaleBeanUtilsBean(LocaleConvertUtilsBean localeConvertUtils) { 109 this.localeConvertUtils = localeConvertUtils; 110 } 111 112 114 115 public LocaleConvertUtilsBean getLocaleConvertUtils() { 116 return localeConvertUtils; 117 } 118 119 122 public Locale getDefaultLocale() { 123 124 return getLocaleConvertUtils().getDefaultLocale(); 125 } 126 127 128 131 public void setDefaultLocale(Locale locale) { 132 133 getLocaleConvertUtils().setDefaultLocale(locale); 134 } 135 136 140 public boolean getApplyLocalized() { 141 142 return getLocaleConvertUtils().getApplyLocalized(); 143 } 144 145 149 public void setApplyLocalized(boolean newApplyLocalized) { 150 151 getLocaleConvertUtils().setApplyLocalized(newApplyLocalized); 152 } 153 154 155 157 176 public String getIndexedProperty( 177 Object bean, 178 String name, 179 String pattern) 180 throws 181 IllegalAccessException , 182 InvocationTargetException , 183 NoSuchMethodException { 184 185 Object value = getPropertyUtils().getIndexedProperty(bean, name); 186 return getLocaleConvertUtils().convert(value, pattern); 187 } 188 189 207 public String getIndexedProperty( 208 Object bean, 209 String name) 210 throws 211 IllegalAccessException , 212 InvocationTargetException , 213 NoSuchMethodException { 214 215 return getIndexedProperty(bean, name, null); 216 } 217 218 236 public String getIndexedProperty(Object bean, 237 String name, int index, String pattern) 238 throws IllegalAccessException , InvocationTargetException , 239 NoSuchMethodException { 240 241 Object value = getPropertyUtils().getIndexedProperty(bean, name, index); 242 return getLocaleConvertUtils().convert(value, pattern); 243 } 244 245 263 public String getIndexedProperty(Object bean, 264 String name, int index) 265 throws IllegalAccessException , InvocationTargetException , 266 NoSuchMethodException { 267 return getIndexedProperty(bean, name, index, null); 268 } 269 270 286 public String getSimpleProperty(Object bean, String name, String pattern) 287 throws IllegalAccessException , InvocationTargetException , 288 NoSuchMethodException { 289 290 Object value = getPropertyUtils().getSimpleProperty(bean, name); 291 return getLocaleConvertUtils().convert(value, pattern); 292 } 293 294 309 public String getSimpleProperty(Object bean, String name) 310 throws IllegalAccessException , InvocationTargetException , 311 NoSuchMethodException { 312 313 return getSimpleProperty(bean, name, null); 314 } 315 316 334 public String getMappedProperty( 335 Object bean, 336 String name, 337 String key, 338 String pattern) 339 throws 340 IllegalAccessException , 341 InvocationTargetException , 342 NoSuchMethodException { 343 344 Object value = getPropertyUtils().getMappedProperty(bean, name, key); 345 return getLocaleConvertUtils().convert(value, pattern); 346 } 347 348 365 public String getMappedProperty(Object bean, 366 String name, String key) 367 throws IllegalAccessException , InvocationTargetException , 368 NoSuchMethodException { 369 370 return getMappedProperty(bean, name, key, null); 371 } 372 373 374 394 public String getMappedPropertyLocale( 395 Object bean, 396 String name, 397 String pattern) 398 throws 399 IllegalAccessException , 400 InvocationTargetException , 401 NoSuchMethodException { 402 403 Object value = getPropertyUtils().getMappedProperty(bean, name); 404 return getLocaleConvertUtils().convert(value, pattern); 405 } 406 407 408 428 public String getMappedProperty(Object bean, String name) 429 throws 430 IllegalAccessException , 431 InvocationTargetException , 432 NoSuchMethodException { 433 434 return getMappedPropertyLocale(bean, name, null); 435 } 436 437 455 public String getNestedProperty( 456 Object bean, 457 String name, 458 String pattern) 459 throws 460 IllegalAccessException , 461 InvocationTargetException , 462 NoSuchMethodException { 463 464 Object value = getPropertyUtils().getNestedProperty(bean, name); 465 return getLocaleConvertUtils().convert(value, pattern); 466 } 467 468 485 public String getNestedProperty(Object bean, String name) 486 throws 487 IllegalAccessException , 488 InvocationTargetException , 489 NoSuchMethodException { 490 491 return getNestedProperty(bean, name, null); 492 } 493 494 511 public String getProperty(Object bean, String name, String pattern) 512 throws 513 IllegalAccessException , 514 InvocationTargetException , 515 NoSuchMethodException { 516 517 return getNestedProperty(bean, name, pattern); 518 } 519 520 537 public String getProperty(Object bean, String name) 538 throws 539 IllegalAccessException , 540 InvocationTargetException , 541 NoSuchMethodException { 542 543 return getNestedProperty(bean, name); 544 } 545 546 560 public void setProperty(Object bean, String name, Object value) 561 throws 562 IllegalAccessException , 563 InvocationTargetException { 564 565 setProperty(bean, name, value, null); 566 } 567 568 583 public void setProperty( 584 Object bean, 585 String name, 586 Object value, 587 String pattern) 588 throws 589 IllegalAccessException , 590 InvocationTargetException { 591 592 if (log.isTraceEnabled()) { 594 StringBuffer sb = new StringBuffer (" setProperty("); 595 sb.append(bean); 596 sb.append(", "); 597 sb.append(name); 598 sb.append(", "); 599 if (value == null) { 600 sb.append("<NULL>"); 601 } 602 else if (value instanceof String ) { 603 sb.append((String ) value); 604 } 605 else if (value instanceof String []) { 606 String values[] = (String []) value; 607 sb.append('['); 608 for (int i = 0; i < values.length; i++) { 609 if (i > 0) { 610 sb.append(','); 611 } 612 sb.append(values[i]); 613 } 614 sb.append(']'); 615 } 616 else { 617 sb.append(value.toString()); 618 } 619 sb.append(')'); 620 log.trace(sb.toString()); 621 } 622 623 Descriptor propInfo = calculate(bean, name); 624 625 if (propInfo != null) { 626 Class type = definePropertyType(propInfo.getTarget(), name, propInfo.getPropName()); 627 628 if (type != null) { 629 630 Object newValue = convert(type, propInfo.getIndex(), value, pattern); 631 invokeSetter(propInfo.getTarget(), propInfo.getPropName(), 632 propInfo.getKey(), propInfo.getIndex(), newValue); 633 } 634 } 635 } 636 637 649 protected Class definePropertyType(Object target, String name, String propName) 650 throws IllegalAccessException , InvocationTargetException { 651 652 Class type = null; 654 if (target instanceof DynaBean) { 655 DynaClass dynaClass = ((DynaBean) target).getDynaClass(); 656 DynaProperty dynaProperty = dynaClass.getDynaProperty(propName); 657 if (dynaProperty == null) { 658 return null; } 660 type = dynaProperty.getType(); 661 } 662 else { 663 PropertyDescriptor descriptor = null; 664 try { 665 descriptor = 666 getPropertyUtils().getPropertyDescriptor(target, name); 667 if (descriptor == null) { 668 return null; } 670 } 671 catch (NoSuchMethodException e) { 672 return null; } 674 if (descriptor instanceof MappedPropertyDescriptor) { 675 type = ((MappedPropertyDescriptor) descriptor). 676 getMappedPropertyType(); 677 } 678 else if (descriptor instanceof IndexedPropertyDescriptor ) { 679 type = ((IndexedPropertyDescriptor ) descriptor). 680 getIndexedPropertyType(); 681 } 682 else { 683 type = descriptor.getPropertyType(); 684 } 685 } 686 return type; 687 } 688 689 698 protected Object convert(Class type, int index, Object value, String pattern) { 699 700 if (log.isTraceEnabled()) { 701 log.trace("Converting value '" + value + "' to type:" + type); 702 } 703 704 Object newValue = null; 705 706 if (type.isArray() && (index < 0)) { if (value instanceof String ) { 708 String values[] = new String [1]; 709 values[0] = (String ) value; 710 newValue = getLocaleConvertUtils().convert((String []) values, type, pattern); 711 } 712 else if (value instanceof String []) { 713 newValue = getLocaleConvertUtils().convert((String []) value, type, pattern); 714 } 715 else { 716 newValue = value; 717 } 718 } 719 else if (type.isArray()) { if (value instanceof String ) { 721 newValue = getLocaleConvertUtils().convert((String ) value, 722 type.getComponentType(), pattern); 723 } 724 else if (value instanceof String []) { 725 newValue = getLocaleConvertUtils().convert(((String  |