1 55 package org.apache.avalon.framework.configuration; 56 57 64 public abstract class AbstractConfiguration 65 implements Configuration 66 { 67 76 protected abstract String getPrefix() throws ConfigurationException; 77 78 87 public int getValueAsInteger() 88 throws ConfigurationException 89 { 90 final String value = getValue().trim(); 91 try 92 { 93 if( value.startsWith( "0x" ) ) 94 { 95 return Integer.parseInt( value.substring( 2 ), 16 ); 96 } 97 else if( value.startsWith( "0o" ) ) 98 { 99 return Integer.parseInt( value.substring( 2 ), 8 ); 100 } 101 else if( value.startsWith( "0b" ) ) 102 { 103 return Integer.parseInt( value.substring( 2 ), 2 ); 104 } 105 else 106 { 107 return Integer.parseInt( value ); 108 } 109 } 110 catch( final Exception nfe ) 111 { 112 final String message = 113 "Cannot parse the value \"" + value 114 + "\" as an integer in the configuration element \"" 115 + getName() + "\" at " + getLocation(); 116 throw new ConfigurationException( message ); 117 } 118 } 119 120 129 public int getValueAsInteger( final int defaultValue ) 130 { 131 try 132 { 133 return getValueAsInteger(); 134 } 135 catch( final ConfigurationException ce ) 136 { 137 return defaultValue; 138 } 139 } 140 141 150 public long getValueAsLong() 151 throws ConfigurationException 152 { 153 final String value = getValue().trim(); 154 try 155 { 156 if( value.startsWith( "0x" ) ) 157 { 158 return Long.parseLong( value.substring( 2 ), 16 ); 159 } 160 else if( value.startsWith( "0o" ) ) 161 { 162 return Long.parseLong( value.substring( 2 ), 8 ); 163 } 164 else if( value.startsWith( "0b" ) ) 165 { 166 return Long.parseLong( value.substring( 2 ), 2 ); 167 } 168 else 169 { 170 return Long.parseLong( value ); 171 } 172 } 173 catch( final Exception nfe ) 174 { 175 final String message = 176 "Cannot parse the value \"" + value 177 + "\" as a long in the configuration element \"" 178 + getName() + "\" at " + getLocation(); 179 throw new ConfigurationException( message ); 180 } 181 } 182 183 192 public long getValueAsLong( final long defaultValue ) 193 { 194 try 195 { 196 return getValueAsLong(); 197 } 198 catch( final ConfigurationException ce ) 199 { 200 return defaultValue; 201 } 202 } 203 204 210 public float getValueAsFloat() 211 throws ConfigurationException 212 { 213 final String value = getValue().trim(); 214 try 215 { 216 return Float.parseFloat( value ); 217 } 218 catch( final Exception nfe ) 219 { 220 final String message = 221 "Cannot parse the value \"" + value 222 + "\" as a float in the configuration element \"" 223 + getName() + "\" at " + getLocation(); 224 throw new ConfigurationException( message ); 225 } 226 } 227 228 234 public float getValueAsFloat( final float defaultValue ) 235 { 236 try 237 { 238 return getValueAsFloat(); 239 } 240 catch( final ConfigurationException ce ) 241 { 242 return ( defaultValue ); 243 } 244 } 245 246 252 public boolean getValueAsBoolean() 253 throws ConfigurationException 254 { 255 final String value = getValue().trim(); 256 257 if( isTrue( value ) ) 258 { 259 return true; 260 } 261 else if( isFalse( value ) ) 262 { 263 return false; 264 } 265 else 266 { 267 final String message = 268 "Cannot parse the value \"" + value 269 + "\" as a boolean in the configuration element \"" 270 + getName() + "\" at " + getLocation(); 271 throw new ConfigurationException( message ); 272 } 273 } 274 275 281 public boolean getValueAsBoolean( final boolean defaultValue ) 282 { 283 try 284 { 285 return getValueAsBoolean(); 286 } 287 catch( final ConfigurationException ce ) 288 { 289 return defaultValue; 290 } 291 } 292 293 299 public String getValue( final String defaultValue ) 300 { 301 try 302 { 303 return getValue(); 304 } 305 catch( final ConfigurationException ce ) 306 { 307 return defaultValue; 308 } 309 } 310 311 322 public int getAttributeAsInteger( final String name ) 323 throws ConfigurationException 324 { 325 final String value = getAttribute( name ).trim(); 326 try 327 { 328 if( value.startsWith( "0x" ) ) 329 { 330 return Integer.parseInt( value.substring( 2 ), 16 ); 331 } 332 else if( value.startsWith( "0o" ) ) 333 { 334 return Integer.parseInt( value.substring( 2 ), 8 ); 335 } 336 else if( value.startsWith( "0b" ) ) 337 { 338 return Integer.parseInt( value.substring( 2 ), 2 ); 339 } 340 else 341 { 342 return Integer.parseInt( value ); 343 } 344 } 345 catch( final Exception nfe ) 346 { 347 final String message = 348 "Cannot parse the value \"" + value 349 + "\" as an integer in the attribute \"" 350 + name + "\" at " + getLocation(); 351 throw new ConfigurationException( message ); 352 } 353 } 354 355 366 public int getAttributeAsInteger( final String name, final int defaultValue ) 367 { 368 try 369 { 370 return getAttributeAsInteger( name ); 371 } 372 catch( final ConfigurationException ce ) 373 { 374 return defaultValue; 375 } 376 } 377 378 389 public long getAttributeAsLong( final String name ) 390 throws ConfigurationException 391 { 392 final String value = getAttribute( name ); 393 394 try 395 { 396 if( value.startsWith( "0x" ) ) 397 { 398 return Long.parseLong( value.substring( 2 ), 16 ); 399 } 400 else if( value.startsWith( "0o" ) ) 401 { 402 return Long.parseLong( value.substring( 2 ), 8 ); 403 } 404 else if( value.startsWith( "0b" ) ) 405 { 406 return Long.parseLong( value.substring( 2 ), 2 ); 407 } 408 else 409 { 410 return Long.parseLong( value ); 411 } 412 } 413 catch( final Exception nfe ) 414 { 415 final String message = 416 "Cannot parse the value \"" + value 417 + "\" as a long in the attribute \"" 418 + name + "\" at " + getLocation(); 419 throw new ConfigurationException( message ); 420 } 421 } 422 423 434 public long getAttributeAsLong( final String name, final long defaultValue ) 435 { 436 try 437 { 438 return getAttributeAsLong( name ); 439 } 440 catch( final ConfigurationException ce ) 441 { 442 return defaultValue; 443 } 444 } 445 446 454 public float getAttributeAsFloat( final String name ) 455 throws ConfigurationException 456 { 457 final String value = getAttribute( name ); 458 try 459 { 460 return Float.parseFloat( value ); 461 } 462 catch( final Exception e ) 463 { 464 final String message = 465 "Cannot parse the value \"" + value 466 + "\" as a float in the attribute \"" 467 + name + "\" at " + getLocation(); 468 throw new ConfigurationException( message ); 469 } 470 } 471 472 480 public float getAttributeAsFloat( final String name, final float defaultValue ) 481 { 482 try 483 { 484 return getAttributeAsFloat( name ); 485 } 486 catch( final ConfigurationException ce ) 487 { 488 return defaultValue; 489 } 490 } 491 492 500 public boolean getAttributeAsBoolean( final String name ) 501 throws ConfigurationException 502 { 503 final String value = getAttribute( name ); 504 505 if( isTrue( value ) ) 506 { 507 return true; 508 } 509 else if( isFalse( value ) ) 510 { 511 return false; 512 } 513 else 514 { 515 final String message = 516 "Cannot parse the value \"" + value 517 + "\" as a boolean in the attribute \"" 518 + name + "\" at " + getLocation(); 519 throw new ConfigurationException( message ); 520 } 521 } 522 523 private boolean isTrue( final String value ) 524 { 525 return value.equalsIgnoreCase( "true" ) 526 || value.equalsIgnoreCase( "yes" ) 527 || value.equalsIgnoreCase( "on" ) 528 || value.equalsIgnoreCase( "1" ); 529 } 530 531 private boolean isFalse( final String value ) 532 { 533 return value.equalsIgnoreCase( "false" ) 534 || value.equalsIgnoreCase( "no" ) 535 || value.equalsIgnoreCase( "off" ) 536 || value.equalsIgnoreCase( "0" ); 537 } 538 539 547 public boolean getAttributeAsBoolean( final String name, final boolean defaultValue ) 548 { 549 try 550 { 551 return getAttributeAsBoolean( name ); 552 } 553 catch( final ConfigurationException ce ) 554 { 555 return defaultValue; 556 } 557 } 558 559 567 public String getAttribute( final String name, final String defaultValue ) 568 { 569 try 570 { 571 return getAttribute( name ); 572 } 573 catch( final ConfigurationException ce ) 574 { 575 return defaultValue; 576 } 577 } 578 579 587 public Configuration getChild( final String name ) 588 { 589 return getChild( name, true ); 590 } 591 592 600 public Configuration getChild( final String name, final boolean createNew ) 601 { 602 final Configuration[] children = getChildren( name ); 603 if( children.length > 0 ) 604 { 605 return children[ 0 ]; 606 } 607 else 608 { 609 if( createNew ) 610 { 611 return new DefaultConfiguration( name, "-" ); 612 } 613 else 614 { 615 return null; 616 } 617 } 618 } 619 } 620 | Popular Tags |