1 8 package org.apache.avalon.excalibur.i18n; 9 10 import java.text.MessageFormat ; 11 import java.util.Locale ; 12 import java.util.MissingResourceException ; 13 import java.util.Random ; 14 import java.util.ResourceBundle ; 15 import java.text.DateFormat ; 16 import java.text.ParseException ; 17 import java.util.Date ; 18 19 27 public class Resources 28 { 29 private final static Random RANDOM = new Random (); 30 31 private final Locale m_locale; 33 34 private ResourceBundle m_bundle; 36 37 private String m_baseName; 39 40 private ClassLoader m_classLoader; 42 43 48 public Resources( final String baseName ) 49 { 50 this( baseName, Locale.getDefault(), null ); 51 } 52 53 60 public Resources( final String baseName, final ClassLoader classLoader ) 61 { 62 this( baseName, Locale.getDefault(), classLoader ); 63 } 64 65 71 public Resources( final String baseName, final Locale locale ) 72 { 73 this( baseName, locale, null ); 74 } 75 76 83 public Resources( final String baseName, 84 final Locale locale, 85 final ClassLoader classLoader ) 86 { 87 m_baseName = baseName; 88 m_locale = locale; 89 m_classLoader = classLoader; 90 91 if( null == baseName ) 92 { 93 throw new NullPointerException ( "baseName property is null" ); 94 } 95 96 if( null == locale ) 97 { 98 throw new NullPointerException ( "locale property is null" ); 99 } 100 } 101 102 109 public boolean getBoolean( final String key, final boolean defaultValue ) 110 throws MissingResourceException 111 { 112 try 113 { 114 return getBoolean( key ); 115 } 116 catch( final MissingResourceException mre ) 117 { 118 return defaultValue; 119 } 120 } 121 122 128 public boolean getBoolean( final String key ) 129 throws MissingResourceException 130 { 131 final ResourceBundle bundle = getBundle(); 132 final String value = bundle.getString( key ); 133 return value.equalsIgnoreCase( "true" ); 134 } 135 136 143 public byte getByte( final String key, final byte defaultValue ) 144 throws MissingResourceException 145 { 146 try 147 { 148 return getByte( key ); 149 } 150 catch( final MissingResourceException mre ) 151 { 152 return defaultValue; 153 } 154 } 155 156 162 public byte getByte( final String key ) 163 throws MissingResourceException 164 { 165 final ResourceBundle bundle = getBundle(); 166 final String value = bundle.getString( key ); 167 try 168 { 169 return Byte.parseByte( value ); 170 } 171 catch( final NumberFormatException nfe ) 172 { 173 throw new MissingResourceException ( "Expecting a byte value but got " + value, 174 "java.lang.String", 175 key ); 176 } 177 } 178 179 186 public char getChar( final String key, final char defaultValue ) 187 throws MissingResourceException 188 { 189 try 190 { 191 return getChar( key ); 192 } 193 catch( final MissingResourceException mre ) 194 { 195 return defaultValue; 196 } 197 } 198 199 205 public char getChar( final String key ) 206 throws MissingResourceException 207 { 208 final ResourceBundle bundle = getBundle(); 209 final String value = bundle.getString( key ); 210 211 if( 1 == value.length() ) 212 { 213 return value.charAt( 0 ); 214 } 215 else 216 { 217 throw new MissingResourceException ( "Expecting a char value but got " + value, 218 "java.lang.String", 219 key ); 220 } 221 } 222 223 230 public short getShort( final String key, final short defaultValue ) 231 throws MissingResourceException 232 { 233 try 234 { 235 return getShort( key ); 236 } 237 catch( final MissingResourceException mre ) 238 { 239 return defaultValue; 240 } 241 } 242 243 249 public short getShort( final String key ) 250 throws MissingResourceException 251 { 252 final ResourceBundle bundle = getBundle(); 253 final String value = bundle.getString( key ); 254 try 255 { 256 return Short.parseShort( value ); 257 } 258 catch( final NumberFormatException nfe ) 259 { 260 throw new MissingResourceException ( "Expecting a short value but got " + value, 261 "java.lang.String", 262 key ); 263 } 264 } 265 266 273 public int getInteger( final String key, final int defaultValue ) 274 throws MissingResourceException 275 { 276 try 277 { 278 return getInteger( key ); 279 } 280 catch( final MissingResourceException mre ) 281 { 282 return defaultValue; 283 } 284 } 285 286 292 public int getInteger( final String key ) 293 throws MissingResourceException 294 { 295 final ResourceBundle bundle = getBundle(); 296 final String value = bundle.getString( key ); 297 try 298 { 299 return Integer.parseInt( value ); 300 } 301 catch( final NumberFormatException nfe ) 302 { 303 throw new MissingResourceException ( "Expecting a integer value but got " + value, 304 "java.lang.String", 305 key ); 306 } 307 } 308 309 316 public long getLong( final String key, final long defaultValue ) 317 throws MissingResourceException 318 { 319 try 320 { 321 return getLong( key ); 322 } 323 catch( final MissingResourceException mre ) 324 { 325 return defaultValue; 326 } 327 } 328 329 335 public long getLong( final String key ) 336 throws MissingResourceException 337 { 338 final ResourceBundle bundle = getBundle(); 339 final String value = bundle.getString( key ); 340 try 341 { 342 return Long.parseLong( value ); 343 } 344 catch( final NumberFormatException nfe ) 345 { 346 throw new MissingResourceException ( "Expecting a long value but got " + value, 347 "java.lang.String", 348 key ); 349 } 350 } 351 352 359 public float getFloat( final String key, final float defaultValue ) 360 throws MissingResourceException 361 { 362 try 363 { 364 return getFloat( key ); 365 } 366 catch( final MissingResourceException mre ) 367 { 368 return defaultValue; 369 } 370 } 371 372 378 public float getFloat( final String key ) 379 throws MissingResourceException 380 { 381 final ResourceBundle bundle = getBundle(); 382 final String value = bundle.getString( key ); 383 try 384 { 385 return Float.parseFloat( value ); 386 } 387 catch( final NumberFormatException nfe ) 388 { 389 throw new MissingResourceException ( "Expecting a float value but got " + value, 390 "java.lang.String", 391 key ); 392 } 393 } 394 395 402 public double getDouble( final String key, final double defaultValue ) 403 throws MissingResourceException 404 { 405 try 406 { 407 return getDouble( key ); 408 } 409 catch( final MissingResourceException mre ) 410 { 411 return defaultValue; 412 } 413 } 414 415 421 public double getDouble( final String key ) 422 throws MissingResourceException 423 { 424 final ResourceBundle bundle = getBundle(); 425 final String value = bundle.getString( key ); 426 try 427 { 428 return Double.parseDouble( value ); 429 } 430 catch( final NumberFormatException nfe ) 431 { 432 throw new MissingResourceException ( "Expecting a double value but got " + value, 433 "java.lang.String", 434 key ); 435 } 436 } 437 438 445 public Date getDate( final String key, final Date defaultValue ) 446 throws MissingResourceException 447 { 448 try 449 { 450 return getDate( key ); 451 } 452 catch( final MissingResourceException mre ) 453 { 454 return defaultValue; 455 } 456 } 457 458 464 public Date getDate( final String key ) 465 throws MissingResourceException 466 { 467 final ResourceBundle bundle = getBundle(); 468 final String value = bundle.getString( key ); 469 try 470 { 471 final DateFormat format = 472 DateFormat.getDateInstance( DateFormat.DEFAULT, m_locale ); 473 return format.parse( value ); 474 } 475 catch( final ParseException pe ) 476 { 477 throw new MissingResourceException ( "Expecting a date value but got " + value, 478 "java.lang.String", 479 key ); 480 } 481 } 482 483 490 public Date getTime( final String key, final Date defaultValue ) 491 throws MissingResourceException 492 { 493 try 494 { 495 return getTime( key ); 496 } 497 catch( final MissingResourceException mre ) 498 { 499 return defaultValue; 500 } 501 } 502 503 509 public Date getTime( final String key ) 510 throws MissingResourceException 511 { 512 final ResourceBundle bundle = getBundle(); 513 final String value = bundle.getString( key ); 514 try 515 { 516 final DateFormat format = 517 DateFormat.getTimeInstance( DateFormat.DEFAULT, m_locale ); 518 return format.parse( value ); 519 } 520 catch( final ParseException pe ) 521 { 522 throw new MissingResourceException ( "Expecting a time value but got " + value, 523 "java.lang.String", 524 key ); 525 } 526 } 527 528 535 public Date getDateTime( final String key, final Date defaultValue ) 536 throws MissingResourceException 537 { 538 try 539 { 540 return getDateTime( key ); 541 } 542 catch( final MissingResourceException mre ) 543 { 544 return defaultValue; 545 } 546 } 547 548 554 public Date getDateTime( final String key ) 555 throws MissingResourceException 556 { 557 final ResourceBundle bundle = getBundle(); 558 final String value = bundle.getString( key ); 559 try 560 { 561 final DateFormat format = 562 DateFormat.getDateTimeInstance( DateFormat.DEFAULT, DateFormat.DEFAULT, m_locale ); 563 return format.parse( value ); 564 } 565 catch( final ParseException pe ) 566 { 567 throw new MissingResourceException ( "Expecting a time value but got " + value, 568 "java.lang.String", 569 key ); 570 } 571 } 572 573 579 public String getString( final String key ) 580 throws MissingResourceException 581 { 582 final ResourceBundle bundle = getBundle(); 583 return bundle.getString( key ); 584 } 585 586 593 public String getString( final String key, final Object arg1 ) 594 { 595 final Object [] args = new Object [] { arg1 }; 596 return format( key, args ); 597 } 598 599 607 public String getString( final String key, final Object arg1, final Object arg2 ) 608 { 609 final Object [] args = new Object [] { arg1, arg2 }; 610 return format( key, args ); 611 } 612 613 622 public String getString( final String key, 623 final Object arg1, 624 final Object arg2, 625 final Object arg3 ) 626 { 627 final Object [] args = new Object [] { arg1, arg2, arg3 }; 628 return format( key, args ); 629 } 630 631 641 public String getString( final String key, 642 final Object arg1, 643 final Object arg2, 644 final Object arg3, 645 final Object arg4 ) 646 { 647 final Object [] args = new Object [] { arg1, arg2, arg3, arg4 }; 648 return format( key, args ); 649 } 650 651 662 public String getString( final String key, 663 final Object arg1, 664 final Object arg2, 665 final Object arg3, 666 final Object arg4, 667 final Object arg5 ) 668 { 669 final Object [] args = new Object [] { arg1, arg2, arg3, arg4, arg5 }; 670 return format( key, args ); 671 } 672 673 680 public String format( final String key, final Object [] args ) 681 { 682 try 683 { 684 final String pattern = getPatternString( key ); 685 return MessageFormat.format( pattern, args ); 686 } 687 catch( final MissingResourceException mre ) 688 { 689 final StringBuffer sb = new StringBuffer (); 690 sb.append( "Unknown resource. Bundle: '" ); 691 sb.append( m_baseName ); 692 sb.append( "' Key: '" ); 693 sb.append( key ); 694 sb.append( "' Args: '" ); 695 696 for( int i = 0; i < args.length; i++ ) 697 { 698 if( 0 != i ) sb.append( "', '" ); 699 sb.append( args[ i ] ); 700 } 701 702 sb.append( "' Reason: " ); 703 sb.append( mre ); 704 705 return sb.toString(); 706 } 707 } 708 709 718 public final ResourceBundle getBundle() 719 throws MissingResourceException 720 { 721 if( null == m_bundle ) 722 { 723 ClassLoader classLoader = m_classLoader; 725 if( null == classLoader ) 726 { 727 classLoader = Thread.currentThread().getContextClassLoader(); 728 } 729 if( null != classLoader ) 730 { 731 m_bundle = ResourceBundle.getBundle( m_baseName, m_locale, classLoader ); 732 } 733 else 734 { 735 m_bundle = ResourceBundle.getBundle( m_baseName, m_locale ); 736 } 737 } 738 return m_bundle; 739 } 740 741 751 private String getPatternString( final String key ) 752 throws MissingResourceException 753 { 754 final ResourceBundle bundle = getBundle(); 755 final Object object = bundle.getObject( key ); 756 757 if( object instanceof String ) 759 { 760 return (String )object; 761 } 762 else if( object instanceof String [] ) 763 { 764 final String [] strings = (String [])object; 766 return strings[ RANDOM.nextInt( strings.length ) ]; 767 } 768 else 769 { 770 throw new MissingResourceException ( "Unable to find resource of appropriate type.", 771 "java.lang.String", 772 key ); 773 } 774 } 775 } 776 | Popular Tags |