1 16 17 package org.apache.commons.beanutils.locale; 18 19 20 import org.apache.commons.beanutils.*; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import java.beans.IndexedPropertyDescriptor ; 25 import java.beans.PropertyDescriptor ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.util.Locale ; 28 29 30 44 45 public class LocaleBeanUtils extends BeanUtils { 46 47 48 50 51 private static Log log = LogFactory.getLog(LocaleBeanUtils.class); 52 53 60 public static Locale getDefaultLocale() { 61 62 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getDefaultLocale(); 63 } 64 65 66 73 public static void setDefaultLocale(Locale locale) { 74 75 LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().setDefaultLocale(locale); 76 } 77 78 85 public static boolean getApplyLocalized() { 86 87 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getApplyLocalized(); 88 } 89 90 97 public static void setApplyLocalized(boolean newApplyLocalized) { 98 99 LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().setApplyLocalized(newApplyLocalized); 100 } 101 102 103 105 113 public static String getIndexedProperty(Object bean, String name, String pattern) 114 throws IllegalAccessException , InvocationTargetException , 115 NoSuchMethodException { 116 117 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getIndexedProperty(bean, name, pattern); 118 } 119 120 129 public static String getIndexedProperty(Object bean, String name) 130 throws IllegalAccessException , InvocationTargetException , 131 NoSuchMethodException { 132 133 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getIndexedProperty(bean, name); 134 } 135 136 144 public static String getIndexedProperty(Object bean, 145 String name, int index, String pattern) 146 throws IllegalAccessException , InvocationTargetException , 147 NoSuchMethodException { 148 149 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getIndexedProperty(bean, name, index, pattern); 150 } 151 152 161 public static String getIndexedProperty(Object bean, 162 String name, int index) 163 throws IllegalAccessException , InvocationTargetException , 164 NoSuchMethodException { 165 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getIndexedProperty(bean, name, index); 166 } 167 168 177 public static String getSimpleProperty(Object bean, String name, String pattern) 178 throws IllegalAccessException , InvocationTargetException , 179 NoSuchMethodException { 180 181 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getSimpleProperty(bean, name, pattern); 182 } 183 184 193 public static String getSimpleProperty(Object bean, String name) 194 throws IllegalAccessException , InvocationTargetException , 195 NoSuchMethodException { 196 197 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getSimpleProperty(bean, name); 198 } 199 200 208 public static String getMappedProperty(Object bean, 209 String name, String key, String pattern) 210 throws IllegalAccessException , InvocationTargetException , 211 NoSuchMethodException { 212 213 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getMappedProperty(bean, name, key, pattern); 214 } 215 216 226 public static String getMappedProperty(Object bean, 227 String name, String key) 228 throws IllegalAccessException , InvocationTargetException , 229 NoSuchMethodException { 230 231 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getMappedProperty(bean, name, key); 232 } 233 234 235 243 public static String getMappedPropertyLocale(Object bean, String name, String pattern) 244 throws IllegalAccessException , InvocationTargetException , 245 NoSuchMethodException { 246 247 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getMappedPropertyLocale(bean, name, pattern); 248 } 249 250 251 260 public static String getMappedProperty(Object bean, String name) 261 throws IllegalAccessException , InvocationTargetException , 262 NoSuchMethodException { 263 264 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getMappedProperty(bean, name); 265 } 266 267 276 public static String getNestedProperty(Object bean, String name, String pattern) 277 throws IllegalAccessException , InvocationTargetException , 278 NoSuchMethodException { 279 280 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getNestedProperty(bean, name, pattern); 281 } 282 283 291 public static String getNestedProperty(Object bean, String name) 292 throws IllegalAccessException , InvocationTargetException , 293 NoSuchMethodException { 294 295 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getNestedProperty(bean, name); 296 } 297 298 306 public static String getProperty(Object bean, String name, String pattern) 307 throws IllegalAccessException , InvocationTargetException , 308 NoSuchMethodException { 309 310 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getProperty(bean, name, pattern); 311 } 312 313 321 public static String getProperty(Object bean, String name) 322 throws IllegalAccessException , InvocationTargetException , 323 NoSuchMethodException { 324 325 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getProperty(bean, name); 326 } 327 328 337 public static void setProperty(Object bean, String name, Object value) 338 throws IllegalAccessException , InvocationTargetException { 339 340 LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().setProperty(bean, name, value); 341 } 342 343 352 public static void setProperty(Object bean, String name, Object value, String pattern) 353 throws IllegalAccessException , InvocationTargetException { 354 355 LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().setProperty(bean, name, value, pattern); 356 } 357 358 365 protected static Class definePropertyType(Object target, String name, String propName) 366 throws IllegalAccessException , InvocationTargetException { 367 368 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().definePropertyType(target, name, propName); 369 } 370 371 379 protected static Object convert(Class type, int index, Object value, String pattern) { 380 381 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().convert(type, index, value, pattern); 382 } 383 384 391 protected static Object convert(Class type, int index, Object value) { 392 393 return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().convert(type, index, value); 394 } 395 396 403 protected static void invokeSetter(Object target, String propName, String key, int index, Object newValue) 404 throws IllegalAccessException , InvocationTargetException { 405 406 LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().invokeSetter(target, propName, key, index, newValue); 407 } 408 409 421 protected static Descriptor calculate(Object bean, String name) 422 throws IllegalAccessException , InvocationTargetException { 423 424 String propName = null; int index = -1; String key = null; 428 Object target = bean; 429 int delim = name.lastIndexOf(PropertyUtils.NESTED_DELIM); 430 if (delim >= 0) { 431 try { 432 target = 433 PropertyUtils.getProperty(bean, name.substring(0, delim)); 434 } 435 catch (NoSuchMethodException e) { 436 return null; } 438 name = name.substring(delim + 1); 439 if (log.isTraceEnabled()) { 440 log.trace(" Target bean = " + target); 441 log.trace(" Target name = " + name); 442 } 443 } 444 445 propName = name; 447 int i = propName.indexOf(PropertyUtils.INDEXED_DELIM); 448 if (i >= 0) { 449 int k = propName.indexOf(PropertyUtils.INDEXED_DELIM2); 450 try { 451 index = 452 Integer.parseInt(propName.substring(i + 1, k)); 453 } 454 catch (NumberFormatException e) { 455 ; 456 } 457 propName = propName.substring(0, i); 458 } 459 int j = propName.indexOf(PropertyUtils.MAPPED_DELIM); 460 if (j >= 0) { 461 int k = propName.indexOf(PropertyUtils.MAPPED_DELIM2); 462 try { 463 key = propName.substring(j + 1, k); 464 } 465 catch (IndexOutOfBoundsException e) { 466 ; 467 } 468 propName = propName.substring(0, j); 469 } 470 return new Descriptor (target, name, propName, key, index); 471 } 472 473 474 protected static class Descriptor { 475 476 private int index = -1; private String name; 478 private String propName; private String key; private Object target; 481 482 public Descriptor(Object target, String name, String propName, String key, int index) { 483 484 setTarget(target); 485 setName(name); 486 setPropName(propName); 487 setKey(key); 488 setIndex(index); 489 } 490 491 public Object getTarget() { 492 return target; 493 } 494 495 public void setTarget(Object target) { 496 this.target = target; 497 } 498 499 public String getKey() { 500 return key; 501 } 502 503 public void setKey(String key) { 504 this.key = key; 505 } 506 507 public int getIndex() { 508 return index; 509 } 510 511 public void setIndex(int index) { 512 this.index = index; 513 } 514 515 public String getName() { 516 return name; 517 } 518 519 public void setName(String name) { 520 this.name = name; 521 } 522 523 public String getPropName() { 524 return propName; 525 } 526 527 public void setPropName(String propName) { 528 this.propName = propName; 529 } 530 } 531 } 532 533 534 | Popular Tags |