1 8 package org.codehaus.dna.impl; 9 10 import java.util.ArrayList ; 11 import java.util.HashMap ; 12 import java.util.List ; 13 import java.util.Map ; 14 import java.util.Set ; 15 16 import org.codehaus.dna.Configuration; 17 import org.codehaus.dna.ConfigurationException; 18 19 28 public class DefaultConfiguration 29 extends AbstractFreezable 30 implements Configuration 31 { 32 35 private static final String AUTOGEN_POSTFIX = "<autogen>"; 36 37 40 private static final String TRUE_STRING = "true"; 41 42 46 private static final String [] EMPTY_STRING_ARRAY = new String [ 0 ]; 47 48 52 private static final Configuration[] EMPTY_CONFIG_ARRAY = new Configuration[ 0 ]; 53 54 57 private final String m_name; 58 59 63 private final String m_location; 64 65 69 private final String m_path; 70 71 74 private Map m_attributes; 75 76 82 private List m_children; 83 84 89 private String m_value; 90 91 98 public DefaultConfiguration( final String name, 99 final String location, 100 final String path ) 101 { 102 if( null == name ) 103 { 104 throw new NullPointerException ( "name" ); 105 } 106 if( null == path ) 107 { 108 throw new NullPointerException ( "path" ); 109 } 110 if( null == location ) 111 { 112 throw new NullPointerException ( "location" ); 113 } 114 m_name = name; 115 m_path = path; 116 m_location = location; 117 } 118 119 124 public String getName() 125 { 126 return m_name; 127 } 128 129 136 public final String getPath() 137 { 138 return m_path; 139 } 140 141 149 public String getLocation() 150 { 151 return m_location; 152 } 153 154 159 public Configuration[] getChildren() 160 { 161 final List childList = getChildList(); 162 if( null == childList ) 163 { 164 return EMPTY_CONFIG_ARRAY; 165 } 166 else 167 { 168 return (Configuration[])childList.toArray( new Configuration[ childList.size() ] ); 169 } 170 } 171 172 178 public Configuration[] getChildren( final String name ) 179 { 180 if( null == name ) 181 { 182 throw new NullPointerException ( "name" ); 183 } 184 final List children = getChildList(); 185 if( null == children ) 186 { 187 return EMPTY_CONFIG_ARRAY; 188 } 189 else 190 { 191 final ArrayList results = new ArrayList (); 192 final int count = children.size(); 193 for( int i = 0; i < count; i++ ) 194 { 195 final Configuration child = (Configuration)children.get( i ); 196 if( child.getName().equals( name ) ) 197 { 198 results.add( child ); 199 } 200 } 201 return (Configuration[])results.toArray( new Configuration[ results.size() ] ); 202 } 203 } 204 205 212 public Configuration getChild( final String name ) 213 { 214 return getChild( name, true ); 215 } 216 217 226 public Configuration getChild( final String name, 227 final boolean createChild ) 228 { 229 if( null == name ) 230 { 231 throw new NullPointerException ( "name" ); 232 } 233 final List children = getChildList(); 234 if( null != children ) 235 { 236 final int count = children.size(); 237 for( int i = 0; i < count; i++ ) 238 { 239 final Configuration child = (Configuration)children.get( i ); 240 if( child.getName().equals( name ) ) 241 { 242 return child; 243 } 244 } 245 } 246 if( createChild ) 247 { 248 final String path = getPath() + ConfigurationUtil.PATH_SEPARATOR + getName(); 249 return new DefaultConfiguration( name, generateLocation(), path ); 250 } 251 else 252 { 253 return null; 254 } 255 256 } 257 258 264 public String getValue() 265 throws ConfigurationException 266 { 267 if( null != m_value ) 268 { 269 return m_value; 270 } 271 else 272 { 273 final String message = "No value specified"; 274 throw new ConfigurationException( message, getPath(), getLocation() ); 275 } 276 } 277 278 285 public String getValue( final String defaultValue ) 286 { 287 if( null != m_value ) 288 { 289 return m_value; 290 } 291 else 292 { 293 return defaultValue; 294 } 295 } 296 297 304 public boolean getValueAsBoolean() 305 throws ConfigurationException 306 { 307 return getValue().equals( "true" ); 308 } 309 310 318 public boolean getValueAsBoolean( final boolean defaultValue ) 319 { 320 if( null == m_value ) 321 { 322 return defaultValue; 323 } 324 else 325 { 326 return m_value.equals( TRUE_STRING ); 327 } 328 } 329 330 337 public int getValueAsInteger() 338 throws ConfigurationException 339 { 340 try 341 { 342 return Integer.parseInt( getValue() ); 343 } 344 catch( final NumberFormatException nfe ) 345 { 346 final String message = 347 "Unable to parse " + getValue() + " as an integer"; 348 throw new ConfigurationException( message, getPath(), getLocation(), nfe ); 349 } 350 } 351 352 360 public int getValueAsInteger( final int defaultValue ) 361 { 362 if( null == m_value ) 363 { 364 return defaultValue; 365 } 366 else 367 { 368 try 369 { 370 return Integer.parseInt( m_value ); 371 } 372 catch( final NumberFormatException nfe ) 373 { 374 return defaultValue; 375 } 376 } 377 } 378 379 386 public long getValueAsLong() 387 throws ConfigurationException 388 { 389 try 390 { 391 return Long.parseLong( getValue() ); 392 } 393 catch( final NumberFormatException nfe ) 394 { 395 final String message = 396 "Unable to parse " + getValue() + " as a Long"; 397 throw new ConfigurationException( message, getPath(), getLocation(), nfe ); 398 } 399 } 400 401 409 public long getValueAsLong( final long defaultValue ) 410 { 411 if( null == m_value ) 412 { 413 return defaultValue; 414 } 415 else 416 { 417 try 418 { 419 return Long.parseLong( m_value ); 420 } 421 catch( final NumberFormatException nfe ) 422 { 423 return defaultValue; 424 } 425 } 426 } 427 428 435 public float getValueAsFloat() 436 throws ConfigurationException 437 { 438 try 439 { 440 return Float.parseFloat( getValue() ); 441 } 442 catch( final NumberFormatException nfe ) 443 { 444 final String message = 445 "Unable to parse " + getValue() + " as a Long"; 446 throw new ConfigurationException( message, getPath(), getLocation(), nfe ); 447 } 448 } 449 450 458 public float getValueAsFloat( final float defaultValue ) 459 { 460 if( null == m_value ) 461 { 462 return defaultValue; 463 } 464 else 465 { 466 try 467 { 468 return Float.parseFloat( m_value ); 469 } 470 catch( final NumberFormatException nfe ) 471 { 472 return defaultValue; 473 } 474 } 475 } 476 477 482 public String [] getAttributeNames() 483 { 484 final Map attributeMap = getAttributeMap(); 485 if( null == attributeMap ) 486 { 487 return EMPTY_STRING_ARRAY; 488 } 489 else 490 { 491 final Set keys = attributeMap.keySet(); 492 return (String [])attributeMap.keySet().toArray( new String [ keys.size() ] ); 493 } 494 } 495 496 504 public String getAttribute( final String name ) 505 throws ConfigurationException 506 { 507 final String value = doGetAttribute( name ); 508 if( null != value ) 509 { 510 return value; 511 } 512 else 513 { 514 final String message = 515 "Attribute named " + name + " not specified."; 516 throw new ConfigurationException( message, getPath(), getLocation() ); 517 } 518 } 519 520 529 public String getAttribute( final String name, 530 final String defaultValue ) 531 { 532 final String value = doGetAttribute( name ); 533 if( null != value ) 534 { 535 return value; 536 } 537 else 538 { 539 return defaultValue; 540 } 541 } 542 543 550 private String doGetAttribute( final String name ) 551 { 552 if( null == name ) 553 { 554 throw new NullPointerException ( "name" ); 555 } 556 final Map attributeMap = getAttributeMap(); 557 if( null != attributeMap ) 558 { 559 final String value = (String )attributeMap.get( name ); 560 if( null != value ) 561 { 562 return value; 563 } 564 } 565 return null; 566 } 567 568 577 public boolean getAttributeAsBoolean( final String name ) 578 throws ConfigurationException 579 { 580 return getAttribute( name ).equals( TRUE_STRING ); 581 } 582 583 593 public boolean getAttributeAsBoolean( final String name, 594 final boolean defaultValue ) 595 { 596 final String value = getAttribute( name, null ); 597 if( null != value ) 598 { 599 return value.equals( TRUE_STRING ); 600 } 601 return defaultValue; 602 } 603 604 613 public int getAttributeAsInteger( final String name ) 614 throws ConfigurationException 615 { 616 final String value = getAttribute( name ); 617 try 618 { 619 return Integer.parseInt( value ); 620 } 621 catch( final NumberFormatException nfe ) 622 { 623 final String message = 624 "Unable to parse " + value + " as an Integer."; 625 throw new ConfigurationException( message, getPath(), getLocation() ); 626 } 627 } 628 629 639 public int getAttributeAsInteger( final String name, 640 final int defaultValue ) 641 { 642 final String value = getAttribute( name, null ); 643 if( null != value ) 644 { 645 try 646 { 647 return Integer.parseInt( value ); 648 } 649 catch( final NumberFormatException nfe ) 650 { 651 } 653 } 654 return defaultValue; 655 } 656 657 666 public long getAttributeAsLong( final String name ) 667 throws ConfigurationException 668 { 669 final String value = getAttribute( name ); 670 try 671 { 672 return Long.parseLong( value ); 673 } 674 catch( final NumberFormatException nfe ) 675 { 676 final String message = 677 "Unable to parse " + value + " as a Long."; 678 throw new ConfigurationException( message, getPath(), getLocation() ); 679 } 680 } 681 682 692 public long getAttributeAsLong( final String name, 693 final long defaultValue ) 694 { 695 final String value = getAttribute( name, null ); 696 if( null != value ) 697 { 698 try 699 { 700 return Long.parseLong( value ); 701 } 702 catch( final NumberFormatException nfe ) 703 { 704 } 706 } 707 return defaultValue; 708 } 709 710 719 public float getAttributeAsFloat( final String name ) 720 throws ConfigurationException 721 { 722 final String value = getAttribute( name ); 723 try 724 { 725 return Float.parseFloat( value ); 726 } 727 catch( final NumberFormatException nfe ) 728 { 729 final String message = 730 "Unable to parse " + value + " as a Float."; 731 throw new ConfigurationException( message, getPath(), getLocation() ); 732 } 733 } 734 735 745 public float getAttributeAsFloat( final String name, 746 final float defaultValue ) 747 { 748 final String value = getAttribute( name, null ); 749 if( null != value ) 750 { 751 try 752 { 753 return Float.parseFloat( value ); 754 } 755 catch( final NumberFormatException nfe ) 756 { 757 } 759 } 760 return defaultValue; 761 } 762 763 766 public void makeReadOnly() 767 { 768 super.makeReadOnly(); 769 final List children = getChildList(); 770 if( null != children ) 771 { 772 final int count = children.size(); 773 for( int i = 0; i < count; i++ ) 774 { 775 final Configuration configuration = (Configuration)children.get( i ); 776 if( configuration instanceof Freezable ) 777 { 778 ( (Freezable)configuration ).makeReadOnly(); 779 } 780 } 781 } 782 } 783 784 790 public void setAttribute( final String key, 791 final String value ) 792 { 793 if( null == key ) 794 { 795 throw new NullPointerException ( "key" ); 796 } 797 if( null == value ) 798 { 799 throw new NullPointerException ( "value" ); 800 } 801 checkWriteable(); 802 if( null == m_attributes ) 803 { 804 m_attributes = new HashMap (); 805 } 806 m_attributes.put( key, value ); 807 } 808 809 814 public void addChild( final Configuration configuration ) 815 { 816 if( null == configuration ) 817 { 818 throw new NullPointerException ( "configuration" ); 819 } 820 checkWriteable(); 821 if( null != m_value ) 822 { 823 throwMixedContentException(); 824 } 825 if( null == m_children ) 826 { 827 m_children = new ArrayList (); 828 } 829 m_children.add( configuration ); 830 } 831 832 837 public void setValue( final String value ) 838 { 839 if( null == value ) 840 { 841 throw new NullPointerException ( "value" ); 842 } 843 checkWriteable(); 844 final List children = getChildList(); 845 if( null != children && 0 != children.size() ) 846 { 847 throwMixedContentException(); 848 } 849 m_value = value; 850 } 851 852 857 public String toString() 858 { 859 final StringBuffer sb = new StringBuffer (); 860 sb.append( "[Configuration name='" ); 861 sb.append( getName() ); 862 sb.append( "'" ); 863 if( null != m_attributes ) 864 { 865 sb.append( " attributes=" ); 866 sb.append( m_attributes ); 867 } 868 sb.append( "]" ); 869 return sb.toString(); 870 } 871 872 877 protected final List getChildList() 878 { 879 return m_children; 880 } 881 882 887 protected final Map getAttributeMap() 888 { 889 return m_attributes; 890 } 891 892 898 protected final String generateLocation() 899 { 900 final String location = getLocation(); 901 if( !location.endsWith( AUTOGEN_POSTFIX ) ) 902 { 903 return location + AUTOGEN_POSTFIX; 904 } 905 else 906 { 907 return location; 908 } 909 } 910 911 915 protected final void throwMixedContentException() 916 { 917 final String message = 918 "Configuration objects do not support Mixed content. " + 919 "Configuration elements should not have both a value and " + 920 "child elements."; 921 throw new IllegalStateException ( message ); 922 } 923 } 924 | Popular Tags |