1 55 56 package org.jboss.axis.i18n; 57 58 import java.io.IOException ; 59 import java.io.InputStream ; 60 import java.text.MessageFormat ; 61 import java.util.Enumeration ; 62 import java.util.Hashtable ; 63 import java.util.Locale ; 64 import java.util.MissingResourceException ; 65 import java.util.Properties ; 66 67 105 public class RB 106 { 107 static Hashtable propertyCache = new Hashtable (); 111 112 public static final String BASE_NAME = "resource"; 114 115 public static final String PROPERTY_EXT = ".properties"; 117 118 protected String basePropertyFileName; 120 121 protected Properties resourceProperties; 123 124 129 public RB(String name) throws MissingResourceException 130 { 131 this(null, name, null); 132 } 133 134 141 public RB(Object caller, String name) throws MissingResourceException 142 { 143 this(caller, name, null); 144 } 145 146 154 public RB(Object caller, String name, Locale locale) throws MissingResourceException 155 { 156 ClassLoader cl = null; 157 158 if (caller != null) 159 { 160 161 Class c; 162 if (caller instanceof Class ) 163 { 164 c = (Class )caller; 165 } 166 else 167 { 168 c = caller.getClass(); 169 } 170 171 cl = c.getClassLoader(); 173 174 if (name.indexOf("/") == -1) 175 { 176 177 String fullName = c.getName(); 179 180 int pos = fullName.lastIndexOf("."); 181 if (pos > 0) 182 { 183 name = fullName.substring(0, pos + 1).replace('.', '/') + name; 184 } 185 } 186 } 187 else 188 { 189 if (name.indexOf("/") == -1) 191 { 192 name = "org/jboss/axis/default-resource"; 193 } 194 } 195 196 Locale defaultLocale = Locale.getDefault(); 197 198 if (locale != null) 200 { 201 if (locale.equals(defaultLocale)) 202 { 203 locale = null; 204 } 205 } 206 207 loadProperties(name, cl, locale, defaultLocale); 210 } 211 212 218 public String getString(String key) throws MissingResourceException 219 { 220 return getString(key, (Object [])null); 221 } 222 223 235 public String getString(String key, Object arg0) throws MissingResourceException 236 { 237 Object [] o = new Object [1]; 238 o[0] = arg0; 239 return getString(key, o); 240 } 241 242 255 public String getString(String key, Object arg0, Object arg1) throws MissingResourceException 256 { 257 Object [] o = new Object [2]; 258 o[0] = arg0; 259 o[1] = arg1; 260 return getString(key, o); 261 } 262 263 277 public String getString(String key, Object arg0, Object arg1, Object arg2) throws MissingResourceException 278 { 279 Object [] o = new Object [3]; 280 o[0] = arg0; 281 o[1] = arg1; 282 o[2] = arg2; 283 return getString(key, o); 284 } 285 286 298 public String getString(String key, Object [] array) throws MissingResourceException 299 { 300 String msg = null; 301 if (resourceProperties != null) 302 { 303 msg = resourceProperties.getProperty(key); 304 } 305 306 if (msg == null) 307 { 308 throw new MissingResourceException ("Cannot find resource key \"" + key + 309 "\" in base name " + basePropertyFileName, 310 basePropertyFileName, key); 311 } 312 313 msg = MessageFormat.format(msg, array); 314 return msg; 315 } 316 317 protected void loadProperties(String basename, ClassLoader loader, Locale locale, 318 Locale defaultLocale) 319 throws MissingResourceException 320 { 321 String loaderName = ""; 323 if (loader != null) 324 { 325 loaderName = ":" + loader.hashCode(); 326 } 327 String cacheKey = basename + ":" + locale + ":" + defaultLocale + loaderName; 328 Properties p = (Properties )propertyCache.get(cacheKey); 329 basePropertyFileName = basename + PROPERTY_EXT; 330 331 if (p == null) 332 { 333 if (locale != null) 336 { 337 p = loadProperties(basename, loader, locale, p); 338 } 339 340 if (defaultLocale != null) 342 { 343 p = loadProperties(basename, loader, defaultLocale, p); 344 } 345 346 p = merge(p, loadProperties(basePropertyFileName, loader)); 348 349 if (p == null) 350 { 351 throw new MissingResourceException ("Cannot find resource for base name " + 352 basePropertyFileName, basePropertyFileName, ""); 353 } 354 355 propertyCache.put(cacheKey, p); 357 358 } 359 360 resourceProperties = p; 361 } 362 363 protected Properties loadProperties(String basename, ClassLoader loader, Locale locale, 364 Properties props) 365 { 366 367 String language = locale.getLanguage(); 368 String country = locale.getCountry(); 369 String variant = locale.getVariant(); 370 if (variant != null) 371 { 372 if (variant.trim().length() == 0) 373 { 374 variant = null; 375 } 376 } 377 378 if (language != null) 379 { 380 381 if (country != null) 382 { 383 384 if (variant != null) 385 { 386 props = merge(props, loadProperties(basename + "_" + language + "_" + country + "_" + variant + 387 PROPERTY_EXT, loader)); 388 } 389 props = merge(props, loadProperties(basename + "_" + language + "_" + country + 390 PROPERTY_EXT, loader)); 391 } 392 props = merge(props, loadProperties(basename + "_" + language + PROPERTY_EXT, loader)); 393 } 394 return props; 395 } 396 397 protected Properties loadProperties(String resname, ClassLoader loader) 398 { 399 Properties props = null; 400 401 InputStream in = null; 403 try 404 { 405 if (loader != null) 406 { 407 in = loader.getResourceAsStream(resname); 408 } 409 410 if (in == null) 413 { 414 in = ClassLoader.getSystemResourceAsStream(resname); 415 } 416 if (in != null) 417 { 418 props = new Properties (); 419 try 420 { 421 props.load(in); 422 } 423 catch (IOException ex) 424 { 425 props = null; 427 } 428 } 429 } 430 finally 431 { 432 if (in != null) 433 { 434 try 435 { 436 in.close(); 437 } 438 catch (Exception ex) 439 { 440 } 442 } 443 } 444 return props; 445 } 446 447 450 protected Properties merge(Properties p1, Properties p2) 451 { 452 if ((p1 == null) && 453 (p2 == null)) 454 { 455 return null; 456 } 457 else if (p1 == null) 458 { 459 return p2; 460 } 461 else if (p2 == null) 462 { 463 return p1; 464 } 465 466 Enumeration en = p2.keys(); 468 while (en.hasMoreElements()) 469 { 470 String key = (String )en.nextElement(); 471 if (p1.getProperty(key) == null) 472 { 473 p1.put(key, p2.getProperty(key)); 474 } 475 } 476 477 return p1; 478 } 479 480 483 public Properties getProperties() 484 { 485 return resourceProperties; 486 } 487 488 490 497 public static String getString(Object caller, String key) 498 throws MissingResourceException 499 { 500 return getMessage(caller, BASE_NAME, null, key, null); 501 } 502 503 511 public static String getString(Object caller, String key, Object arg0) 512 throws MissingResourceException 513 { 514 Object [] o = new Object [1]; 515 o[0] = arg0; 516 return getMessage(caller, BASE_NAME, null, key, o); 517 } 518 519 528 public static String getString(Object caller, String key, Object arg0, Object arg1) 529 throws MissingResourceException 530 { 531 Object [] o = new Object [2]; 532 o[0] = arg0; 533 o[1] = arg1; 534 return getMessage(caller, BASE_NAME, null, key, o); 535 } 536 537 547 public static String getString(Object caller, String key, Object arg0, Object arg1, Object arg2) 548 throws MissingResourceException 549 { 550 Object [] o = new Object [3]; 551 o[0] = arg0; 552 o[1] = arg1; 553 o[2] = arg2; 554 return getMessage(caller, BASE_NAME, null, key, o); 555 } 556 557 568 public static String getString(Object caller, String key, Object arg0, Object arg1, Object arg2, Object arg3) 569 throws MissingResourceException 570 { 571 Object [] o = new Object [4]; 572 o[0] = arg0; 573 o[1] = arg1; 574 o[2] = arg2; 575 o[3] = arg3; 576 return getMessage(caller, BASE_NAME, null, key, o); 577 } 578 579 580 592 public static String getString(Object caller, String key, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) 593 throws MissingResourceException 594 { 595 Object [] o = new Object [5]; 596 o[0] = arg0; 597 o[1] = arg1; 598 o[2] = arg2; 599 o[3] = arg3; 600 o[4] = arg4; 601 return getMessage(caller, BASE_NAME, null, key, o); 602 } 603 604 605 613 public static String getString(Object caller, String key, Object [] args) 614 throws MissingResourceException 615 { 616 return getMessage(caller, BASE_NAME, null, key, args); 617 } 618 619 620 628 public static String getString(Object caller, Locale locale, String key) 629 throws MissingResourceException 630 { 631 return getMessage(caller, BASE_NAME, locale, key, null); 632 } 633 634 643 public static String getString(Object caller, Locale locale, String key, Object arg0) 644 throws MissingResourceException 645 { 646 Object [] o = new Object [1]; 647 o[0] = arg0; 648 return getMessage(caller, BASE_NAME, locale, key, o); 649 } 650 651 661 public static String getString(Object caller, Locale locale, String key, Object arg0, Object arg1) 662 throws MissingResourceException 663 { 664 Object [] o = new Object [2]; 665 o[0] = arg0; 666 o[1] = arg1; 667 return getMessage(caller, BASE_NAME, locale, key, o); 668 } 669 670 681 public static String getString(Object caller, Locale locale, String key, Object arg0, Object arg1, Object arg2) 682 throws MissingResourceException 683 { 684 Object [] o = new Object [3]; 685 o[0] = arg0; 686 o[1] = arg1; 687 o[2] = arg2; 688 return getMessage(caller, BASE_NAME, locale, key, o); 689 } 690 691 703 public static String getString(Object caller, Locale locale, String key, Object arg0, Object arg1, Object arg2, Object arg3) 704 throws MissingResourceException 705 { 706 Object [] o = new Object [4]; 707 o[0] = arg0; 708 o[1] = arg1; 709 o[2] = arg2; 710 o[3] = arg3; 711 return getMessage(caller, BASE_NAME, locale, key, o); 712 } 713 714 726 public static String getString(Object caller, Locale locale, String key, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) 727 throws MissingResourceException 728 { 729 Object [] o = new Object [5]; 730 o[0] = arg0; 731 o[1] = arg1; 732 o[2] = arg2; 733 o[3] = arg3; 734 o[4] = arg4; 735 return getMessage(caller, BASE_NAME, locale, key, o); 736 } 737 738 747 public static String getString(Object caller, Locale locale, String key, Object [] args) 748 throws MissingResourceException 749 { 750 return getMessage(caller, BASE_NAME, locale, key, args); 751 } 752 753 public static String getMessage(Object caller, String basename, Locale locale, String key, 755 Object [] args) 756 throws MissingResourceException 757 { 758 String msg = null; 759 MissingResourceException firstEx = null; 760 String fullName = null; 761 Class curClass = null; 762 boolean didNull = false; 763 764 if (caller != null) 765 { 766 if (caller instanceof Class ) 767 curClass = (Class )caller; 768 else 769 curClass = caller.getClass(); 770 } 771 772 while (msg == null) 773 { 774 775 if (curClass != null) 777 { 778 779 String pkgName = curClass.getName(); 781 782 int pos = pkgName.lastIndexOf("."); 783 if (pos > 0) 784 { 785 fullName = pkgName.substring(0, pos + 1).replace('.', '/') + basename; 786 } 787 else 788 { 789 fullName = basename; 790 } 791 } 792 else 793 { 794 fullName = basename; 795 } 796 797 try 798 { 799 RB rb = new RB(caller, fullName, locale); 800 msg = rb.getString(key, args); 801 } 802 catch (MissingResourceException ex) 803 { 804 if (curClass == null) 805 { 806 throw ex; 807 } 808 809 if (firstEx == null) 811 { 812 firstEx = ex; 813 } 814 815 curClass = curClass.getSuperclass(); 817 if (curClass == null) 818 { 819 if (didNull) 820 throw firstEx; 821 didNull = true; 822 caller = null; 823 } 824 else 825 { 826 String cname = curClass.getName(); 827 if (cname.startsWith("java.") || 828 cname.startsWith("javax.")) 829 { 830 if (didNull) 831 throw firstEx; 832 didNull = true; 833 caller = null; 834 curClass = null; 835 } 836 } 837 } 838 839 } 840 return msg; 841 } 842 843 846 public static void clearCache() 847 { 848 propertyCache.clear(); 849 } 850 } 851 | Popular Tags |