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 []) value)[0], 726 type.getComponentType(), pattern); 727 } 728 else { 729 newValue = value; 730 } 731 } 732 else { if (value instanceof String ) { 734 newValue = getLocaleConvertUtils().convert((String ) value, type, pattern); 735 } 736 else if (value instanceof String []) { 737 newValue = getLocaleConvertUtils().convert(((String []) value)[0], 738 type, pattern); 739 } 740 else { 741 newValue = value; 742 } 743 } 744 return newValue; 745 } 746 747 754 protected Object convert(Class type, int index, Object value) { 755 756 Object newValue = null; 757 758 if (type.isArray() && (index < 0)) { if (value instanceof String ) { 760 String values[] = new String [1]; 761 values[0] = (String ) value; 762 newValue = ConvertUtils.convert((String []) values, type); 763 } 764 else if (value instanceof String []) { 765 newValue = ConvertUtils.convert((String []) value, type); 766 } 767 else { 768 newValue = value; 769 } 770 } 771 else if (type.isArray()) { if (value instanceof String ) { 773 newValue = ConvertUtils.convert((String ) value, 774 type.getComponentType()); 775 } 776 else if (value instanceof String []) { 777 newValue = ConvertUtils.convert(((String []) value)[0], 778 type.getComponentType()); 779 } 780 else { 781 newValue = value; 782 } 783 } 784 else { if (value instanceof String ) { 786 newValue = ConvertUtils.convert((String ) value, type); 787 } 788 else if (value instanceof String []) { 789 newValue = ConvertUtils.convert(((String []) value)[0], 790 type); 791 } 792 else { 793 newValue = value; 794 } 795 } 796 return newValue; 797 } 798 799 813 protected void invokeSetter(Object target, String propName, String key, int index, Object newValue) 814 throws IllegalAccessException , InvocationTargetException { 815 816 try { 817 if (index >= 0) { 818 getPropertyUtils().setIndexedProperty(target, propName, 819 index, newValue); 820 } 821 else if (key != null) { 822 getPropertyUtils().setMappedProperty(target, propName, 823 key, newValue); 824 } 825 else { 826 getPropertyUtils().setProperty(target, propName, newValue); 827 } 828 } 829 catch (NoSuchMethodException e) { 830 throw new InvocationTargetException 831 (e, "Cannot set " + propName); 832 } 833 } 834 835 846 protected Descriptor calculate(Object bean, String name) 847 throws IllegalAccessException , InvocationTargetException { 848 849 String propName = null; int index = -1; String key = null; 853 Object target = bean; 854 int delim = name.lastIndexOf(PropertyUtils.NESTED_DELIM); 855 if (delim >= 0) { 856 try { 857 target = 858 getPropertyUtils().getProperty(bean, name.substring(0, delim)); 859 } 860 catch (NoSuchMethodException e) { 861 return null; } 863 name = name.substring(delim + 1); 864 if (log.isTraceEnabled()) { 865 log.trace(" Target bean = " + target); 866 log.trace(" Target name = " + name); 867 } 868 } 869 870 propName = name; 872 int i = propName.indexOf(PropertyUtils.INDEXED_DELIM); 873 if (i >= 0) { 874 int k = propName.indexOf(PropertyUtils.INDEXED_DELIM2); 875 try { 876 index = 877 Integer.parseInt(propName.substring(i + 1, k)); 878 } 879 catch (NumberFormatException e) { 880 ; 881 } 882 propName = propName.substring(0, i); 883 } 884 int j = propName.indexOf(PropertyUtils.MAPPED_DELIM); 885 if (j >= 0) { 886 int k = propName.indexOf(PropertyUtils.MAPPED_DELIM2); 887 try { 888 key = propName.substring(j + 1, k); 889 } 890 catch (IndexOutOfBoundsException e) { 891 ; 892 } 893 propName = propName.substring(0, j); 894 } 895 return new Descriptor (target, name, propName, key, index); 896 } 897 898 protected class Descriptor { 899 900 private int index = -1; private String name; 902 private String propName; private String key; private Object target; 905 906 public Descriptor(Object target, String name, String propName, String key, int index) { 907 908 setTarget(target); 909 setName(name); 910 setPropName(propName); 911 setKey(key); 912 setIndex(index); 913 } 914 915 public Object getTarget() { 916 return target; 917 } 918 919 public void setTarget(Object target) { 920 this.target = target; 921 } 922 923 public String getKey() { 924 return key; 925 } 926 927 public void setKey(String key) { 928 this.key = key; 929 } 930 931 public int getIndex() { 932 return index; 933 } 934 935 public void setIndex(int index) { 936 this.index = index; 937 } 938 939 public String getName() { 940 return name; 941 } 942 943 public void setName(String name) { 944 this.name = name; 945 } 946 947 public String getPropName() { 948 return propName; 949 } 950 951 public void setPropName(String propName) { 952 this.propName = propName; 953 } 954 } 955 } 956 957 958 | Popular Tags |