1 55 package org.apache.avalon.framework.configuration; 56 57 import java.io.Serializable ; 58 import java.util.ArrayList ; 59 import java.util.HashMap ; 60 61 67 public class DefaultConfiguration 68 extends AbstractConfiguration 69 implements Serializable 70 { 71 74 protected static final Configuration[] EMPTY_ARRAY = new Configuration[ 0 ]; 75 76 private final String m_name; 77 private final String m_location; 78 private final String m_namespace; 79 private final String m_prefix; 80 private HashMap m_attributes; 81 private ArrayList m_children; 82 private String m_value; 83 private boolean m_readOnly; 84 85 89 public DefaultConfiguration( final String name ) 90 { 91 this( name, null, "", "" ); 92 } 93 94 99 public DefaultConfiguration( final String name, final String location ) 100 { 101 this( name, location, "", "" ); 102 } 103 104 115 public DefaultConfiguration( final String name, 116 final String location, 117 final String ns, 118 final String prefix ) 119 { 120 m_name = name; 121 m_location = location; 122 m_namespace = ns; 123 m_prefix = prefix; } 125 126 130 public String getName() 131 { 132 return m_name; 133 } 134 135 141 public String getNamespace() throws ConfigurationException 142 { 143 if( null != m_namespace ) 144 { 145 return m_namespace; 146 } 147 else 148 { 149 throw new ConfigurationException 150 ( "No namespace (not even default \"\") is associated with the " 151 + "configuration element \"" + getName() 152 + "\" at " + getLocation() ); 153 } 154 } 155 156 162 protected String getPrefix() throws ConfigurationException 163 { 164 if( null != m_prefix ) 165 { 166 return m_prefix; 167 } 168 else 169 { 170 throw new ConfigurationException 171 ( "No prefix (not even default \"\") is associated with the " 172 + "configuration element \"" + getName() 173 + "\" at " + getLocation() ); 174 } 175 176 } 177 178 182 public String getLocation() 183 { 184 return m_location; 185 } 186 187 193 public String getValue( final String defaultValue ) 194 { 195 if( null != m_value ) 196 { 197 return m_value; 198 } 199 else 200 { 201 return defaultValue; 202 } 203 } 204 205 211 public String getValue() throws ConfigurationException 212 { 213 if( null != m_value ) 214 { 215 return m_value; 216 } 217 else 218 { 219 throw new ConfigurationException( "No value is associated with the " 220 + "configuration element \"" + getName() 221 + "\" at " + getLocation() ); 222 } 223 } 224 225 229 public String [] getAttributeNames() 230 { 231 if( null == m_attributes ) 232 { 233 return new String [ 0 ]; 234 } 235 else 236 { 237 return (String [])m_attributes.keySet().toArray( new String [ 0 ] ); 238 } 239 } 240 241 247 public Configuration[] getChildren() 248 { 249 if( null == m_children ) 250 { 251 return new Configuration[ 0 ]; 252 } 253 else 254 { 255 return (Configuration[])m_children.toArray( new Configuration[ 0 ] ); 256 } 257 } 258 259 267 public String getAttribute( final String name ) 268 throws ConfigurationException 269 { 270 final String value = 271 ( null != m_attributes ) ? (String )m_attributes.get( name ) : null; 272 273 if( null != value ) 274 { 275 return value; 276 } 277 else 278 { 279 throw new ConfigurationException( 280 "No attribute named \"" + name + "\" is " 281 + "associated with the configuration element \"" 282 + getName() + "\" at " + getLocation() ); 283 } 284 } 285 286 293 public Configuration getChild( final String name, final boolean createNew ) 294 { 295 if( null != m_children ) 296 { 297 final int size = m_children.size(); 298 for( int i = 0; i < size; i++ ) 299 { 300 final Configuration configuration = (Configuration)m_children.get( i ); 301 if( name.equals( configuration.getName() ) ) 302 { 303 return configuration; 304 } 305 } 306 } 307 308 if( createNew ) 309 { 310 return new DefaultConfiguration( name, "-" ); 311 } 312 else 313 { 314 return null; 315 } 316 } 317 318 327 public Configuration[] getChildren( final String name ) 328 { 329 if( null == m_children ) 330 { 331 return new Configuration[ 0 ]; 332 } 333 else 334 { 335 final ArrayList children = new ArrayList (); 336 final int size = m_children.size(); 337 338 for( int i = 0; i < size; i++ ) 339 { 340 final Configuration configuration = (Configuration)m_children.get( i ); 341 if( name.equals( configuration.getName() ) ) 342 { 343 children.add( configuration ); 344 } 345 } 346 347 return (Configuration[])children.toArray( new Configuration[ 0 ] ); 348 } 349 } 350 351 357 public void appendValueData( final String value ) 358 { 359 checkWriteable(); 360 361 if( null == m_value ) 362 { 363 m_value = value; 364 } 365 else 366 { 367 m_value += value; 368 } 369 } 370 371 376 public void setValue( final String value ) 377 { 378 checkWriteable(); 379 380 m_value = value; 381 } 382 383 389 public void setAttribute( final String name, final String value ) 390 { 391 checkWriteable(); 392 393 if( null == m_attributes ) 394 { 395 m_attributes = new HashMap (); 396 } 397 m_attributes.put( name, value ); 398 } 399 400 409 public String addAttribute( final String name, String value ) 410 { 411 checkWriteable(); 412 413 if( null == m_attributes ) 414 { 415 m_attributes = new HashMap (); 416 } 417 418 return (String )m_attributes.put( name, value ); 419 } 420 421 425 public void addChild( final Configuration configuration ) 426 { 427 checkWriteable(); 428 429 if( null == m_children ) 430 { 431 m_children = new ArrayList (); 432 } 433 434 m_children.add( configuration ); 435 } 436 437 444 public void addAll( final Configuration other ) 445 { 446 checkWriteable(); 447 448 setValue( other.getValue( null ) ); 449 addAllAttributes( other ); 450 addAllChildren( other ); 451 } 452 453 459 public void addAllAttributes( final Configuration other ) 460 { 461 checkWriteable(); 462 463 final String [] attributes = other.getAttributeNames(); 464 for( int i = 0; i < attributes.length; i++ ) 465 { 466 final String name = attributes[ i ]; 467 final String value = other.getAttribute( name, null ); 468 setAttribute( name, value ); 469 } 470 } 471 472 478 public void addAllChildren( final Configuration other ) 479 { 480 checkWriteable(); 481 482 final Configuration[] children = other.getChildren(); 483 for( int i = 0; i < children.length; i++ ) 484 { 485 addChild( children[ i ] ); 486 } 487 } 488 489 493 public void removeChild( final Configuration configuration ) 494 { 495 checkWriteable(); 496 497 if( null == m_children ) 498 { 499 return; 500 } 501 m_children.remove( configuration ); 502 } 503 504 508 public int getChildCount() 509 { 510 if( null == m_children ) 511 { 512 return 0; 513 } 514 515 return m_children.size(); 516 } 517 518 522 public void makeReadOnly() 523 { 524 m_readOnly = true; 525 } 526 527 532 protected final void checkWriteable() 533 throws IllegalStateException 534 { 535 if( m_readOnly ) 536 { 537 throw new IllegalStateException 538 ( "Configuration is read only and can not be modified" ); 539 } 540 } 541 } 542 | Popular Tags |