1 55 package org.apache.avalon.framework.parameters; 56 57 import java.io.Serializable ; 58 import java.util.Enumeration ; 59 import java.util.HashMap ; 60 import java.util.Iterator ; 61 import java.util.Map ; 62 import java.util.Properties ; 63 import org.apache.avalon.framework.configuration.Configuration; 64 import org.apache.avalon.framework.configuration.ConfigurationException; 65 66 95 public class Parameters 96 implements Serializable 97 { 98 103 public static final Parameters EMPTY_PARAMETERS; 104 105 106 static 107 { 108 EMPTY_PARAMETERS = new Parameters(); 109 EMPTY_PARAMETERS.makeReadOnly(); 110 } 111 112 private Map m_parameters = new HashMap (); 114 115 private boolean m_readOnly; 116 117 127 public String setParameter( final String name, final String value ) 128 throws IllegalStateException 129 { 130 checkWriteable(); 131 132 if( null == name ) 133 { 134 return null; 135 } 136 137 if( null == value ) 138 { 139 return (String )m_parameters.remove( name ); 140 } 141 142 return (String )m_parameters.put( name, value ); 143 } 144 145 150 public void removeParameter( final String name ) 151 { 152 setParameter( name, null ); 153 } 154 155 161 public Iterator getParameterNames() 162 { 163 return m_parameters.keySet().iterator(); 164 } 165 166 171 public String [] getNames() 172 { 173 return (String [])m_parameters.keySet().toArray( new String [ 0 ] ); 174 } 175 176 182 public boolean isParameter( final String name ) 183 { 184 return m_parameters.containsKey( name ); 185 } 186 187 196 public String getParameter( final String name ) 197 throws ParameterException 198 { 199 if( null == name ) 200 { 201 throw new ParameterException( "You cannot lookup a null parameter" ); 202 } 203 204 final String test = (String )m_parameters.get( name ); 205 206 if( null == test ) 207 { 208 throw new ParameterException( "The parameter '" + name 209 + "' does not contain a value" ); 210 } 211 else 212 { 213 return test; 214 } 215 } 216 217 228 public String getParameter( final String name, final String defaultValue ) 229 { 230 if( name == null ) 231 { 232 return defaultValue; 233 } 234 235 final String test = (String )m_parameters.get( name ); 236 237 if( test == null ) 238 { 239 return defaultValue; 240 } 241 else 242 { 243 return test; 244 } 245 } 246 247 257 private int parseInt( final String value ) 258 throws NumberFormatException 259 { 260 if( value.startsWith( "0x" ) ) 261 { 262 return Integer.parseInt( value.substring( 2 ), 16 ); 263 } 264 else if( value.startsWith( "0o" ) ) 265 { 266 return Integer.parseInt( value.substring( 2 ), 8 ); 267 } 268 else if( value.startsWith( "0b" ) ) 269 { 270 return Integer.parseInt( value.substring( 2 ), 2 ); 271 } 272 else 273 { 274 return Integer.parseInt( value ); 275 } 276 } 277 278 291 public int getParameterAsInteger( final String name ) 292 throws ParameterException 293 { 294 try 295 { 296 return parseInt( getParameter( name ) ); 297 } 298 catch( final NumberFormatException e ) 299 { 300 throw new ParameterException( "Could not return an integer value", e ); 301 } 302 } 303 304 317 public int getParameterAsInteger( final String name, final int defaultValue ) 318 { 319 try 320 { 321 final String value = getParameter( name, null ); 322 if( value == null ) 323 { 324 return defaultValue; 325 } 326 327 return parseInt( value ); 328 } 329 catch( final NumberFormatException e ) 330 { 331 return defaultValue; 332 } 333 } 334 335 345 private long parseLong( final String value ) 346 throws NumberFormatException 347 { 348 if( value.startsWith( "0x" ) ) 349 { 350 return Long.parseLong( value.substring( 2 ), 16 ); 351 } 352 else if( value.startsWith( "0o" ) ) 353 { 354 return Long.parseLong( value.substring( 2 ), 8 ); 355 } 356 else if( value.startsWith( "0b" ) ) 357 { 358 return Long.parseLong( value.substring( 2 ), 2 ); 359 } 360 else 361 { 362 return Long.parseLong( value ); 363 } 364 } 365 366 379 public long getParameterAsLong( final String name ) 380 throws ParameterException 381 { 382 try 383 { 384 return parseLong( getParameter( name ) ); 385 } 386 catch( final NumberFormatException e ) 387 { 388 throw new ParameterException( "Could not return a long value", e ); 389 } 390 } 391 392 405 public long getParameterAsLong( final String name, final long defaultValue ) 406 { 407 try 408 { 409 final String value = getParameter( name, null ); 410 if( value == null ) 411 { 412 return defaultValue; 413 } 414 415 return parseLong( value ); 416 } 417 catch( final NumberFormatException e ) 418 { 419 return defaultValue; 420 } 421 } 422 423 433 public float getParameterAsFloat( final String name ) 434 throws ParameterException 435 { 436 try 437 { 438 return Float.parseFloat( getParameter( name ) ); 439 } 440 catch( final NumberFormatException e ) 441 { 442 throw new ParameterException( "Could not return a float value", e ); 443 } 444 } 445 446 456 public float getParameterAsFloat( final String name, final float defaultValue ) 457 { 458 try 459 { 460 final String value = getParameter( name, null ); 461 if( value == null ) 462 { 463 return defaultValue; 464 } 465 466 return Float.parseFloat( value ); 467 } 468 catch( final NumberFormatException pe ) 469 { 470 return defaultValue; 471 } 472 } 473 474 484 public boolean getParameterAsBoolean( final String name ) 485 throws ParameterException 486 { 487 final String value = getParameter( name ); 488 489 if( value.equalsIgnoreCase( "true" ) ) 490 { 491 return true; 492 } 493 else if( value.equalsIgnoreCase( "false" ) ) 494 { 495 return false; 496 } 497 else 498 { 499 throw new ParameterException( "Could not return a boolean value" ); 500 } 501 } 502 503 513 public boolean getParameterAsBoolean( final String name, final boolean defaultValue ) 514 { 515 final String value = getParameter( name, null ); 516 if( value == null ) 517 { 518 return defaultValue; 519 } 520 521 if( value.equalsIgnoreCase( "true" ) ) 522 { 523 return true; 524 } 525 else if( value.equalsIgnoreCase( "false" ) ) 526 { 527 return false; 528 } 529 else 530 { 531 return defaultValue; 532 } 533 } 534 535 542 public Parameters merge( final Parameters other ) 543 { 544 checkWriteable(); 545 546 final String [] names = other.getNames(); 547 548 for( int i = 0; i < names.length; i++ ) 549 { 550 final String name = names[ i ]; 551 String value = null; 552 try 553 { 554 value = other.getParameter( name ); 555 } 556 catch( final ParameterException pe ) 557 { 558 value = null; 559 } 560 561 setParameter( name, value ); 562 } 563 564 return this; 565 } 566 567 572 public void makeReadOnly() 573 { 574 m_readOnly = true; 575 } 576 577 582 protected final void checkWriteable() 583 throws IllegalStateException 584 { 585 if( m_readOnly ) 586 { 587 throw new IllegalStateException ( "Context is read only and can not be modified" ); 588 } 589 } 590 591 602 public static Parameters fromConfiguration( final Configuration configuration ) 603 throws ConfigurationException 604 { 605 return fromConfiguration( configuration, "parameter" ); 606 } 607 608 618 public static Parameters fromConfiguration( final Configuration configuration, 619 final String elementName ) 620 throws ConfigurationException 621 { 622 if( null == configuration ) 623 { 624 throw new ConfigurationException( 625 "You cannot convert to parameters with a null Configuration" ); 626 } 627 628 final Configuration[] parameters = configuration.getChildren( elementName ); 629 final Parameters params = new Parameters(); 630 631 for( int i = 0; i < parameters.length; i++ ) 632 { 633 try 634 { 635 final String name = parameters[ i ].getAttribute( "name" ); 636 final String value = parameters[ i ].getAttribute( "value" ); 637 params.setParameter( name, value ); 638 } 639 catch( final Exception e ) 640 { 641 throw new ConfigurationException( "Cannot process Configurable", e ); 642 } 643 } 644 645 return params; 646 } 647 648 655 public static Parameters fromProperties( final Properties properties ) 656 { 657 final Parameters parameters = new Parameters(); 658 final Enumeration names = properties.propertyNames(); 659 660 while( names.hasMoreElements() ) 661 { 662 final String key = (String )names.nextElement().toString(); 663 final String value = properties.getProperty( key ); 664 parameters.setParameter( key, value ); 665 } 666 667 return parameters; 668 } 669 670 677 public static Properties toProperties( final Parameters params ) 678 { 679 final Properties properties = new Properties (); 680 final String [] names = params.getNames(); 681 682 for( int i = 0; i < names.length; ++i ) 683 { 684 properties.setProperty( names[ i ], params.getParameter( names[ i ], "" ) ); 686 } 687 688 return properties; 689 } 690 } 691 | Popular Tags |