1 17 package org.apache.avalon.util.i18n; 18 19 import java.text.DateFormat ; 20 import java.text.MessageFormat ; 21 import java.text.ParseException ; 22 import java.util.Date ; 23 import java.util.Locale ; 24 import java.util.MissingResourceException ; 25 import java.util.Random ; 26 import java.util.ResourceBundle ; 27 28 36 public class Resources 37 { 38 private static final Random RANDOM = new Random (); 39 40 private final Locale m_locale; 42 43 private ResourceBundle m_bundle; 45 46 private String m_baseName; 48 49 private ClassLoader m_classLoader; 51 52 57 public Resources( final String baseName ) 58 { 59 this( baseName, Locale.getDefault(), null ); 60 } 61 62 69 public Resources( final String baseName, final ClassLoader classLoader ) 70 { 71 this( baseName, Locale.getDefault(), classLoader ); 72 } 73 74 80 public Resources( final String baseName, final Locale locale ) 81 { 82 this( baseName, locale, null ); 83 } 84 85 92 public Resources( final String baseName, 93 final Locale locale, 94 final ClassLoader classLoader ) 95 { 96 if( null == baseName ) 97 { 98 throw new NullPointerException ( "baseName property is null" ); 99 } 100 if( null == locale ) 101 { 102 throw new NullPointerException ( "locale property is null" ); 103 } 104 m_baseName = baseName; 105 m_locale = locale; 106 m_classLoader = classLoader; 107 } 108 109 116 public boolean getBoolean( final String key, final boolean defaultValue ) 117 throws MissingResourceException 118 { 119 try 120 { 121 return getBoolean( key ); 122 } 123 catch( final MissingResourceException mre ) 124 { 125 return defaultValue; 126 } 127 } 128 129 135 public boolean getBoolean( final String key ) 136 throws MissingResourceException 137 { 138 final ResourceBundle bundle = getBundle(); 139 final String value = bundle.getString( key ); 140 return value.equalsIgnoreCase( "true" ); 141 } 142 143 150 public byte getByte( final String key, final byte defaultValue ) 151 throws MissingResourceException 152 { 153 try 154 { 155 return getByte( key ); 156 } 157 catch( final MissingResourceException mre ) 158 { 159 return defaultValue; 160 } 161 } 162 163 169 public byte getByte( final String key ) 170 throws MissingResourceException 171 { 172 final ResourceBundle bundle = getBundle(); 173 final String value = bundle.getString( key ); 174 try 175 { 176 return Byte.parseByte( value ); 177 } 178 catch( final NumberFormatException nfe ) 179 { 180 throw new MissingResourceException ( "Expecting a byte value but got " + value, 181 "java.lang.String", 182 key ); 183 } 184 } 185 186 193 public char getChar( final String key, final char defaultValue ) 194 throws MissingResourceException 195 { 196 try 197 { 198 return getChar( key ); 199 } 200 catch( final MissingResourceException mre ) 201 { 202 return defaultValue; 203 } 204 } 205 206 212 public char getChar( final String key ) 213 throws MissingResourceException 214 { 215 final ResourceBundle bundle = getBundle(); 216 final String value = bundle.getString( key ); 217 218 if( 1 == value.length() ) 219 { 220 return value.charAt( 0 ); 221 } 222 else 223 { 224 throw new MissingResourceException ( "Expecting a char value but got " + value, 225 "java.lang.String", 226 key ); 227 } 228 } 229 230 237 public short getShort( final String key, final short defaultValue ) 238 throws MissingResourceException 239 { 240 try 241 { 242 return getShort( key ); 243 } 244 catch( final MissingResourceException mre ) 245 { 246 return defaultValue; 247 } 248 } 249 250 256 public short getShort( final String key ) 257 throws MissingResourceException 258 { 259 final ResourceBundle bundle = getBundle(); 260 final String value = bundle.getString( key ); 261 try 262 { 263 return Short.parseShort( value ); 264 } 265 catch( final NumberFormatException nfe ) 266 { 267 throw new MissingResourceException ( "Expecting a short value but got " + value, 268 "java.lang.String", 269 key ); 270 } 271 } 272 273 280 public int getInteger( final String key, final int defaultValue ) 281 throws MissingResourceException 282 { 283 try 284 { 285 return getInteger( key ); 286 } 287 catch( final MissingResourceException mre ) 288 { 289 return defaultValue; 290 } 291 } 292 293 299 public int getInteger( final String key ) 300 throws MissingResourceException 301 { 302 final ResourceBundle bundle = getBundle(); 303 final String value = bundle.getString( key ); 304 try 305 { 306 return Integer.parseInt( value ); 307 } 308 catch( final NumberFormatException nfe ) 309 { 310 throw new MissingResourceException ( "Expecting a integer value but got " + value, 311 "java.lang.String", 312 key ); 313 } 314 } 315 316 323 public long getLong( final String key, final long defaultValue ) 324 throws MissingResourceException 325 { 326 try 327 { 328 return getLong( key ); 329 } 330 catch( final MissingResourceException mre ) 331 { 332 return defaultValue; 333 } 334 } 335 336 342 public long getLong( final String key ) 343 throws MissingResourceException 344 { 345 final ResourceBundle bundle = getBundle(); 346 final String value = bundle.getString( key ); 347 try 348 { 349 return Long.parseLong( value ); 350 } 351 catch( final NumberFormatException nfe ) 352 { 353 throw new MissingResourceException ( "Expecting a long value but got " + value, 354 "java.lang.String", 355 key ); 356 } 357 } 358 359 366 public float getFloat( final String key, final float defaultValue ) 367 throws MissingResourceException 368 { 369 try 370 { 371 return getFloat( key ); 372 } 373 catch( final MissingResourceException mre ) 374 { 375 return defaultValue; 376 } 377 } 378 379 385 public float getFloat( final String key ) 386 throws MissingResourceException 387 { 388 final ResourceBundle bundle = getBundle(); 389 final String value = bundle.getString( key ); 390 try 391 { 392 return Float.parseFloat( value ); 393 } 394 catch( final NumberFormatException nfe ) 395 { 396 throw new MissingResourceException ( "Expecting a float value but got " + value, 397 "java.lang.String", 398 key ); 399 } 400 } 401 402 409 public double getDouble( final String key, final double defaultValue ) 410 throws MissingResourceException 411 { 412 try 413 { 414 return getDouble( key ); 415 } 416 catch( final MissingResourceException mre ) 417 { 418 return defaultValue; 419 } 420 } 421 422 428 public double getDouble( final String key ) 429 throws MissingResourceException 430 { 431 final ResourceBundle bundle = getBundle(); 432 final String value = bundle.getString( key ); 433 try 434 { 435 return Double.parseDouble( value ); 436 } 437 catch( final NumberFormatException nfe ) 438 { 439 throw new MissingResourceException ( "Expecting a double value but got " + value, 440 "java.lang.String", 441 key ); 442 } 443 } 444 445 452 public Date getDate( final String key, final Date defaultValue ) 453 throws MissingResourceException 454 { 455 try 456 { 457 return getDate( key ); 458 } 459 catch( final MissingResourceException mre ) 460 { 461 return defaultValue; 462 } 463 } 464 465 471 public Date getDate( final String key ) 472 throws MissingResourceException 473 { 474 final ResourceBundle bundle = getBundle(); 475 final String value = bundle.getString( key ); 476 try 477 { 478 final DateFormat format = 479 DateFormat.getDateInstance( DateFormat.DEFAULT, m_locale ); 480 return format.parse( value ); 481 } 482 catch( final ParseException pe ) 483 { 484 throw new MissingResourceException ( "Expecting a date value but got " + value, 485 "java.lang.String", 486 key ); 487 } 488 } 489 490 497 public Date getTime( final String key, final Date defaultValue ) 498 throws MissingResourceException 499 { 500 try 501 { 502 return getTime( key ); 503 } 504 catch( final MissingResourceException mre ) 505 { 506 return defaultValue; 507 } 508 } 509 510 516 public Date getTime( final String key ) 517 throws MissingResourceException 518 { 519 final ResourceBundle bundle = getBundle(); 520 final String value = bundle.getString( key ); 521 try 522 { 523 final DateFormat format = 524 DateFormat.getTimeInstance( DateFormat.DEFAULT, m_locale ); 525 return format.parse( value ); 526 } 527 catch( final ParseException pe ) 528 { 529 throw new MissingResourceException ( "Expecting a time value but got " + value, 530 "java.lang.String", 531 key ); 532 } 533 } 534 535 542 public Date getDateTime( final String key, final Date defaultValue ) 543 throws MissingResourceException 544 { 545 try 546 { 547 return getDateTime( key ); 548 } 549 catch( final MissingResourceException mre ) 550 { 551 return defaultValue; 552 } 553 } 554 555 561 public Date getDateTime( final String key ) 562 throws MissingResourceException 563 { 564 final ResourceBundle bundle = getBundle(); 565 final String value = bundle.getString( key ); 566 try 567 { 568 final DateFormat format = 569 DateFormat.getDateTimeInstance( DateFormat.DEFAULT, DateFormat.DEFAULT, m_locale ); 570 return format.parse( value ); 571 } 572 catch( final ParseException pe ) 573 { 574 throw new MissingResourceException ( "Expecting a time value but got " + value, 575 "java.lang.String", 576 key ); 577 } 578 } 579 580 586 public String getString( final String key ) 587 throws MissingResourceException 588 { 589 final ResourceBundle bundle = getBundle(); 590 return bundle.getString( key ); 591 } 592 593 600 public String getString( final String key, final Object arg1 ) 601 { 602 final Object [] args = new Object []{arg1}; 603 return format( key, args ); 604 } 605 606 614 public String getString( final String key, final Object arg1, final Object arg2 ) 615 { 616 final Object [] args = new Object []{arg1, arg2}; 617 return format( key, args ); 618 } 619 620 629 public String getString( final String key, 630 final Object arg1, 631 final Object arg2, 632 final Object arg3 ) 633 { 634 final Object [] args = new Object []{arg1, arg2, arg3}; 635 return format( key, args ); 636 } 637 638 648 public String getString( final String key, 649 final Object arg1, 650 final Object arg2, 651 final Object arg3, 652 final Object arg4 ) 653 { 654 final Object [] args = new Object []{arg1, arg2, arg3, arg4}; 655 return format( key, args ); 656 } 657 658 669 public String getString( final String key, 670 final Object arg1, 671 final Object arg2, 672 final Object arg3, 673 final Object arg4, 674 final Object arg5 ) 675 { 676 final Object [] args = new Object []{arg1, arg2, arg3, arg4, arg5}; 677 return format( key, args ); 678 } 679 680 692 public String getString( final String key, 693 final Object arg1, 694 final Object arg2, 695 final Object arg3, 696 final Object arg4, 697 final Object arg5, 698 final Object arg6 ) 699 { 700 final Object [] args = new Object []{arg1, arg2, arg3, arg4, arg5, arg6}; 701 return format( key, args ); 702 } 703 704 717 public String getString( final String key, 718 final Object arg1, 719 final Object arg2, 720 final Object arg3, 721 final Object arg4, 722 final Object arg5, 723 final Object arg6, 724 final Object arg7 ) 725 { 726 final Object [] args = new Object []{arg1, arg2, arg3, arg4, arg5, arg6, arg7}; 727 return format( key, args ); 728 } 729 730 737 public String format( final String key, final Object [] args ) 738 { 739 try 740 { 741 final String pattern = getPatternString( key ); 742 return MessageFormat.format( pattern, args ); 743 } 744 catch( final MissingResourceException mre ) 745 { 746 final StringBuffer sb = new StringBuffer (); 747 sb.append( "Unknown resource. Bundle: '" ); 748 sb.append( m_baseName ); 749 sb.append( "' Key: '" ); 750 sb.append( key ); 751 sb.append( "' Args: '" ); 752 753 for( int i = 0; i < args.length; i++ ) 754 { 755 if( 0 != i ) sb.append( "', '" ); 756 sb.append( args[ i ] ); 757 } 758 759 sb.append( "' Reason: " ); 760 sb.append( mre ); 761 762 return sb.toString(); 763 } 764 } 765 766 775 public final ResourceBundle getBundle() 776 throws MissingResourceException 777 { 778 if( null == m_bundle ) 779 { 780 ClassLoader classLoader = m_classLoader; 782 if( null == classLoader ) 783 { 784 classLoader = Thread.currentThread().getContextClassLoader(); 785 } 786 if( null != classLoader ) 787 { 788 m_bundle = ResourceBundle.getBundle( m_baseName, m_locale, classLoader ); 789 } 790 else 791 { 792 m_bundle = ResourceBundle.getBundle( m_baseName, m_locale ); 793 } 794 } 795 return m_bundle; 796 } 797 798 808 private String getPatternString( final String key ) 809 throws MissingResourceException 810 { 811 final ResourceBundle bundle = getBundle(); 812 final Object object = bundle.getObject( key ); 813 814 if( object instanceof String ) 816 { 817 return (String )object; 818 } 819 else if( object instanceof String [] ) 820 { 821 final String [] strings = (String [])object; 823 return strings[ RANDOM.nextInt( strings.length ) ]; 824 } 825 else 826 { 827 throw new MissingResourceException ( "Unable to find resource of appropriate type.", 828 "java.lang.String", 829 key ); 830 } 831 } 832 } 833 | Popular Tags |