| 1 16 17 package org.apache.commons.configuration; 18 19 import java.awt.Color ; 20 import java.math.BigDecimal ; 21 import java.math.BigInteger ; 22 import java.net.URL ; 23 import java.util.ArrayList ; 24 import java.util.Calendar ; 25 import java.util.Collection ; 26 import java.util.Date ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Locale ; 30 31 import org.apache.commons.collections.CollectionUtils; 32 import org.apache.commons.lang.ArrayUtils; 33 import org.apache.commons.lang.StringUtils; 34 35 48 public class DataConfiguration extends AbstractConfiguration 49 { 50 51 public static final String DATE_FORMAT_KEY = "org.apache.commons.configuration.format.date"; 52 53 54 public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; 55 56 protected Configuration configuration; 57 58 public DataConfiguration(Configuration configuration) 59 { 60 this.configuration = configuration; 61 } 62 63 66 public Configuration getConfiguration() 67 { 68 return configuration; 69 } 70 71 public Object getProperty(String key) 72 { 73 return configuration.getProperty(key); 74 } 75 76 protected void addPropertyDirect(String key, Object obj) 77 { 78 configuration.addProperty(key, obj); 79 } 80 81 public boolean isEmpty() 82 { 83 return configuration.isEmpty(); 84 } 85 86 public boolean containsKey(String key) 87 { 88 return configuration.containsKey(key); 89 } 90 91 public void clearProperty(String key) 92 { 93 configuration.clearProperty(key); 94 } 95 96 public Iterator getKeys() 97 { 98 return configuration.getKeys(); 99 } 100 101 112 public List getBooleanList(String key) 113 { 114 return getBooleanList(key, new ArrayList ()); 115 } 116 117 129 public List getBooleanList(String key, List defaultValue) 130 { 131 Object value = getProperty(key); 132 133 List list = null; 134 135 if (value == null || (value instanceof String && StringUtils.isEmpty((String ) value))) 136 { 137 list = defaultValue; 138 } 139 else if (value instanceof boolean[]) 140 { 141 list = new ArrayList (); 142 CollectionUtils.addAll(list, ArrayUtils.toObject((boolean[]) value)); 143 } 144 else if (value instanceof Boolean []) 145 { 146 list = new ArrayList (); 147 CollectionUtils.addAll(list, (Boolean []) value); 148 } 149 else if (value instanceof Collection ) 150 { 151 Collection values = (Collection ) value; 152 list = new ArrayList (); 153 154 Iterator it = values.iterator(); 155 while (it.hasNext()) 156 { 157 list.add(PropertyConverter.toBoolean(it.next())); 158 } 159 } 160 else 161 { 162 try 163 { 164 list = new ArrayList (); 166 list.add(PropertyConverter.toBoolean(value)); 167 } 168 catch (ConversionException e) 169 { 170 throw new ConversionException('\'' + key + "' doesn't map to a list of booleans", e); 171 } 172 } 173 174 return list; 175 } 176 177 188 public boolean[] getBooleanArray(String key) 189 { 190 return getBooleanArray(key, new boolean[0]); 191 } 192 193 205 public boolean[] getBooleanArray(String key, boolean[] defaultValue) 206 { 207 Object value = getProperty(key); 208 209 boolean[] array; 210 211 if (value == null || (value instanceof String && StringUtils.isEmpty((String ) value))) 212 { 213 array = defaultValue; 214 } 215 else if (value instanceof boolean[]) 216 { 217 array = (boolean[]) value; 218 } 219 else if (value instanceof Boolean []) 220 { 221 array = ArrayUtils.toPrimitive((Boolean []) value); 222 } 223 else if (value instanceof Collection ) 224 { 225 Collection values = (Collection ) value; 226 array = new boolean[values.size()]; 227 228 int i = 0; 229 Iterator it = values.iterator(); 230 while (it.hasNext()) 231 { 232 array[i++] = PropertyConverter.toBoolean(it.next()).booleanValue(); 233 } 234 } 235 else 236 { 237 try 238 { 239 array = new boolean[1]; 241 array[0] = PropertyConverter.toBoolean(value).booleanValue(); 242 } 243 catch (ConversionException e) 244 { 245 throw new ConversionException('\'' + key + "' doesn't map to a list of booleans", e); 246 } 247 } 248 249 return array; 250 } 251 252 262 public List getByteList(String key) 263 { 264 return getByteList(key, new ArrayList ()); 265 } 266 267 279 public List getByteList(String key, List defaultValue) 280 { 281 Object value = getProperty(key); 282 283 List list = null; 284 285 if (value == null || (value instanceof String && StringUtils.isEmpty((String ) value))) 286 { 287 list = defaultValue; 288 } 289 else if (value instanceof byte[]) 290 { 291 list = new ArrayList (); 292 CollectionUtils.addAll(list, ArrayUtils.toObject((byte[]) value)); 293 } 294 else if (value instanceof Byte []) 295 { 296 list = new ArrayList (); 297 CollectionUtils.addAll(list, (Byte []) value); 298 } 299 else if (value instanceof Collection ) 300 { 301 Collection values = (Collection ) value; 302 list = new ArrayList (); 303 304 Iterator it = values.iterator(); 305 while (it.hasNext()) 306 { 307 list.add(PropertyConverter.toByte(it.next())); 308 } 309 } 310 else 311 { 312 try 313 { 314 list = new ArrayList (); 316 list.add(PropertyConverter.toByte(value)); 317 } 318 catch (ConversionException e) 319 { 320 throw new ConversionException('\'' + key + "' doesn't map to a list of bytes", e); 321 } 322 } 323 324 return list; 325 } 326 327 338 public byte[] getByteArray(String key) 339 { 340 return getByteArray(key, new byte[0]); 341 } 342 343 354 public byte[] getByteArray(String key, byte[] defaultValue) 355 { 356 Object value = getProperty(key); 357 358 byte[] array; 359 360 if (value == null || (value instanceof String && StringUtils.isEmpty((String ) value))) 361 { 362 array = defaultValue; 363 } 364 else if (value instanceof byte[]) 365 { 366 array = (byte[]) value; 367 } 368 else if (value instanceof Byte []) 369 { 370 array = ArrayUtils.toPrimitive((Byte []) value); 371 } 372 else if (value instanceof Collection ) 373 { 374 Collection values = (Collection ) value; 375 array = new byte[values.size()]; 376 377 int i = 0; 378 Iterator it = values.iterator(); 379 while (it.hasNext()) 380 { 381 array[i++] = PropertyConverter.toByte(it.next()).byteValue(); 382 } 383 } 384 else 385 { 386 try 387 { 388 array = new byte[1]; 390 array[0] = PropertyConverter.toByte(value).byteValue(); 391 } 392 catch (ConversionException e) 393 { 394 throw new ConversionException('\'' + key + "' doesn't map to a list of bytes", e); 395 } 396 } 397 398 return array; 399 } 400 401 411 public List getShortList(String key) 412 { 413 return getShortList(key, new ArrayList ()); 414 } 415 416 428 public List getShortList(String key, List defaultValue) 429 { 430 Object value = getProperty(key); 431 432 List list = null; 433 434 if (value == null || (value instanceof String && StringUtils.isEmpty((String ) value))) 435 { 436 list = defaultValue; 437 } 438 else if (value instanceof short[]) 439 { 440 list = new ArrayList (); 441 CollectionUtils.addAll(list, ArrayUtils.toObject((short[]) value)); 442 } 443 else if (value instanceof Short []) 444 { 445 list = new ArrayList (); 446 CollectionUtils.addAll(list, (Short []) value); 447 } 448 else if (value instanceof Collection ) 449 { 450 Collection values = (Collection ) value; 451 list = new ArrayList (); 452 453 Iterator it = values.iterator(); 454 while (it.hasNext()) 455 { 456 list.add(PropertyConverter.toShort(it.next())); 457 } 458 } 459 else 460 { 461 try 462 { 463 list = new ArrayList (); 465 list.add(PropertyConverter.toShort(value)); 466 } 467 catch (ConversionException e) 468 { 469 throw new ConversionException('\'' + key + "' doesn't map to a list of shorts", e); 470 } 471 } 472 473 return list; 474 } 475 476 487 public short[] getShortArray(String key) 488 { 489 return getShortArray(key, new short[0]); 490 } 491 492 503 public short[] getShortArray(String key, short[] defaultValue) 504 { 505 Object value = getProperty(key); 506 507 short[] array; 508 509 if (value == null || (value instanceof String && StringUtils.isEmpty((String ) value))) 510 { 511 array = defaultValue; 512 } 513 else if (value instanceof short[]) 514 { 515 array = (short[]) value; 516 } 517 else if (value instanceof Short []) 518 { 519 array = ArrayUtils.toPrimitive((Short []) value); 520 } 521 else if (value instanceof Collection ) 522 { 523 Collection values = (Collection ) value; 524 array = new short[values.size()]; 525 526 int i = 0; 527 Iterator it = values.iterator(); 528 while (it.hasNext()) 529 { 530 array[i++] = PropertyConverter.toShort(it.next()).shortValue(); 531 } 532 } 533 else 534 { 535 try 536 { 537 array = new short[1]; 539 array[0] = PropertyConverter.toShort(value).shortValue(); 540 } 541 catch (ConversionException e) 542 { 543 throw new ConversionException('\'' + key + "' doesn't map to a list of shorts", e); 544 } 545 } 546 547 return array; 548 } 549 550 561 public List getIntegerList(String key) 562 { 563 return getIntegerList(key, new ArrayList ()); 564 } 565 566 578 public List getIntegerList(String key, List defaultValue) 579 { 580 Object value = getProperty(key); 581 582 List list = null; 583 584 if (value == null || (value instanceof String && StringUtils.isEmpty((String ) value))) 585 { 586 list = defaultValue; 587 } 588 else if (value instanceof int[]) 589 { 590 list = new ArrayList (); 591 CollectionUtils.addAll(list, ArrayUtils.toObject((int[]) value)); 592 } 593 else if (value instanceof Integer []) 594 { 595 list = new ArrayList (); 596 CollectionUtils.addAll(list, (Integer []) value); 597 } 598 else if (value instanceof Collection ) 599 { 600 Collection values = (Collection ) value; 601 list = new ArrayList (); 602 603 Iterator it = values.iterator(); 604 while (it.hasNext()) 605 { 606 list.add(PropertyConverter.toInteger(it.next())); 607 } 608 } 609 else 610 { 611 try 612 { 613 list = new ArrayList (); 615 list.add(PropertyConverter.toInteger(value)); 616 } 617 catch (ConversionException e) 618 { 619 throw new ConversionException('\'' + key + "' doesn't map to a list of integers", e); 620 } 621 } 622 623 return list; 624 } 625 626 637 public int[] getIntArray(String key) 638 { 639 return getIntArray(key, new int[0]); 640 } 641 642 653 public int[] getIntArray(String key, int[] defaultValue) 654 { 655 Object value = getProperty(key); 656 657 int[] array; 658 659 if (value == null || (value instanceof String && StringUtils.isEmpty((String ) value))) 660 { 661 array = defaultValue; 662 } 663 else if (value instanceof int[]) 664 { 665 array = (int[]) value; 666 } 667 else if (value instanceof Integer []) 668 { 669 array = ArrayUtils.toPrimitive((Integer []) value); 670 } 671 else if (value instanceof Collection ) 672 { 673 Collection values = (Collection ) value; 674 array = new int[values.size()]; 675 676 int i = 0; 677 Iterator it = values.iterator(); 678 while (it.hasNext()) 679 { 680 array[i++] = PropertyConverter.toInteger(it.next()).intValue(); 681 } 682 } 683 else 684 { 685 try 686 { 687 array = new int[1]; 689 array[0] = PropertyConverter.toInteger(value).intValue(); 690 } 691 catch (ConversionException e) 692 { 693 throw new ConversionException('\'' + key + "' doesn't map to a list of integers", e); 694 } 695 } 696 697 return array; 698 } 699 700 710 public List getLongList(String key) 711 { 712 return getLongList(key, new ArrayList ()); 713 } 714 715 727 public List getLongList(String key, List defaultValue) 728 { 729 Object value = getProperty(key); 730 731 List list = null; 732 733 if (value == null || (value instanceof String && StringUtils.isEmpty((String ) value))) 734 { 735 list = defaultValue; 736 } 737 else if (value instanceof long[]) 738 { 739 list = new ArrayList (); 740 CollectionUtils.addAll(list, ArrayUtils.toObject((long[]) value)); 741 } 742 else if (value instanceof Long []) 743 { 744 list = new ArrayList (); 745 CollectionUtils.addAll(list, (Long []) value); 746 } 747 else if (value instanceof Collection ) 748 { 749 Collection values = (Collection ) value; 750 list = new ArrayList (); 751 752 Iterator it = values.iterator(); 753 while (it.hasNext()) 754 { 755 list.add(PropertyConverter.toLong(it.next())); 756 } 757 } 758 else 759 { 760 try 761 { 762 list = new ArrayList (); 764 list.add(PropertyConverter.toLong(value)); 765 } 766 catch (ConversionException e) 767 { 768 throw new ConversionException('\'' + key + "' doesn't map to a list of longs", e); 769 } 770 } 771 772 return list; 773 } 774 775 786 public long[] getLongArray(String key) 787 { 788 return getLongArray(key, new long[0]); 789 } 790 791 802 public long[] getLongArray(String key, long[] defaultValue) 803 { 804 Object value = getProperty(key); 805 806 long[] array; 807 808 if (value == null || (value instanceof String && StringUtils.isEmpty((String ) value)))
|