1 16 17 package org.apache.axis.i18n; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.text.MessageFormat ; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 import java.util.Locale ; 25 import java.util.MissingResourceException ; 26 import java.util.Properties ; 27 28 66 public class RB { 67 static Hashtable propertyCache = new Hashtable (); 71 72 public static final String BASE_NAME = "resource"; 74 75 public static final String PROPERTY_EXT = ".properties"; 77 78 protected String basePropertyFileName; 80 81 protected Properties resourceProperties; 83 84 88 public RB(String name) throws MissingResourceException 89 { 90 this(null, name, null); 91 } 92 93 99 public RB(Object caller, String name) throws MissingResourceException 100 { 101 this(caller, name, null); 102 } 103 104 111 public RB(Object caller, String name, Locale locale) throws MissingResourceException 112 { 113 ClassLoader cl = null; 114 115 if (caller != null) { 116 117 Class c; 118 if (caller instanceof Class ) { 119 c = (Class ) caller; 120 } 121 else { 122 c = caller.getClass(); 123 } 124 125 cl = c.getClassLoader(); 127 128 if (name.indexOf("/") == -1) { 129 130 String fullName = c.getName(); 132 133 int pos = fullName.lastIndexOf("."); 134 if (pos > 0) { 135 name = fullName.substring(0, pos + 1).replace('.', '/') + name; 136 } 137 } 138 } else { 139 if (name.indexOf("/") == -1) { 141 name = "org/apache/axis/default-resource"; 142 } 143 } 144 145 Locale defaultLocale = Locale.getDefault(); 146 147 if (locale != null) { 149 if (locale.equals(defaultLocale)) { 150 locale = null; 151 } 152 } 153 154 loadProperties(name, cl, locale, defaultLocale); 157 } 158 159 164 public String getString(String key) throws MissingResourceException 165 { 166 return getString(key, (Object []) null); 167 } 168 169 180 public String getString(String key, Object arg0) throws MissingResourceException 181 { 182 Object [] o = new Object [1]; 183 o[0] = arg0; 184 return getString(key, o); 185 } 186 187 199 public String getString(String key, Object arg0, Object arg1) throws MissingResourceException 200 { 201 Object [] o = new Object [2]; 202 o[0] = arg0; 203 o[1] = arg1; 204 return getString(key, o); 205 } 206 207 220 public String getString(String key, Object arg0, Object arg1, Object arg2) throws MissingResourceException 221 { 222 Object [] o = new Object [3]; 223 o[0] = arg0; 224 o[1] = arg1; 225 o[2] = arg2; 226 return getString(key, o); 227 } 228 229 240 public String getString(String key, Object [] array) throws MissingResourceException 241 { 242 String msg = null; 243 if (resourceProperties != null) { 244 msg = resourceProperties.getProperty(key); 245 } 246 247 if (msg == null) { 248 throw new MissingResourceException ("Cannot find resource key \"" + key + 249 "\" in base name " + basePropertyFileName, 250 basePropertyFileName, key); 251 } 252 253 msg = MessageFormat.format(msg, array); 254 return msg; 255 } 256 257 protected void loadProperties(String basename, ClassLoader loader, Locale locale, 258 Locale defaultLocale) 259 throws MissingResourceException 260 { 261 String loaderName = ""; 263 if (loader != null) { 264 loaderName = ":" + loader.hashCode(); 265 } 266 String cacheKey = basename + ":" + locale + ":" + defaultLocale + loaderName; 267 Properties p = (Properties ) propertyCache.get(cacheKey); 268 basePropertyFileName = basename + PROPERTY_EXT; 269 270 if (p == null) { 271 if (locale != null) { 274 p = loadProperties(basename, loader, locale, p); 275 } 276 277 if (defaultLocale != null) { 279 p = loadProperties(basename, loader, defaultLocale, p); 280 } 281 282 p = merge(p, loadProperties(basePropertyFileName, loader)); 284 285 if (p == null) { 286 throw new MissingResourceException ("Cannot find resource for base name " + 287 basePropertyFileName, basePropertyFileName, ""); 288 } 289 290 propertyCache.put(cacheKey, p); 292 293 } 294 295 resourceProperties = p; 296 } 297 298 protected Properties loadProperties(String basename, ClassLoader loader, Locale locale, 299 Properties props) 300 { 301 302 String language = locale.getLanguage(); 303 String country = locale.getCountry(); 304 String variant = locale.getVariant(); 305 if (variant != null) { 306 if (variant.trim().length() == 0) { 307 variant = null; 308 } 309 } 310 311 if (language != null) { 312 313 if (country != null) { 314 315 if (variant != null) { 316 props = merge(props, loadProperties(basename + "_" + language +"_" + country + "_" + variant + 317 PROPERTY_EXT, loader)); 318 } 319 props = merge(props, loadProperties(basename + "_" + language +"_" + country + 320 PROPERTY_EXT, loader)); 321 } 322 props = merge(props, loadProperties(basename + "_" + language + PROPERTY_EXT, loader)); 323 } 324 return props; 325 } 326 327 protected Properties loadProperties(String resname, ClassLoader loader) 328 { 329 Properties props = null; 330 331 InputStream in = null; 333 try { 334 if (loader != null) { 335 in = loader.getResourceAsStream(resname); 336 } 337 338 if (in == null) { 341 in = ClassLoader.getSystemResourceAsStream(resname); 342 } 343 if (in != null) { 344 props = new Properties (); 345 try { 346 props.load(in); 347 } 348 catch (IOException ex) { 349 props = null; 351 } 352 } 353 } 354 finally { 355 if (in != null) { 356 try { 357 in.close(); 358 } 359 catch (Exception ex) { 360 } 362 } 363 } 364 return props; 365 } 366 367 370 protected Properties merge(Properties p1, Properties p2) 371 { 372 if ((p1 == null) && 373 (p2 == null)) { 374 return null; 375 } 376 else if (p1 == null) { 377 return p2; 378 } 379 else if (p2 == null) { 380 return p1; 381 } 382 383 Enumeration enumeration = p2.keys(); 385 while (enumeration.hasMoreElements()) { 386 String key = (String ) enumeration.nextElement(); 387 if (p1.getProperty(key) == null) { 388 p1.put(key, p2.getProperty(key)); 389 } 390 } 391 392 return p1; 393 } 394 395 398 public Properties getProperties() 399 { 400 return resourceProperties; 401 } 402 403 405 411 public static String getString(Object caller, String key) 412 throws MissingResourceException 413 { 414 return getMessage(caller, BASE_NAME, null, key, null); 415 } 416 417 424 public static String getString(Object caller, String key, Object arg0) 425 throws MissingResourceException 426 { 427 Object [] o = new Object [1]; 428 o[0] = arg0; 429 return getMessage(caller, BASE_NAME, null, key, o); 430 } 431 432 440 public static String getString(Object caller, String key, Object arg0, Object arg1) 441 throws MissingResourceException 442 { 443 Object [] o = new Object [2]; 444 o[0] = arg0; 445 o[1] = arg1; 446 return getMessage(caller, BASE_NAME, null, key, o); 447 } 448 449 458 public static String getString(Object caller, String key, Object arg0, Object arg1, Object arg2) 459 throws MissingResourceException 460 { 461 Object [] o = new Object [3]; 462 o[0] = arg0; 463 o[1] = arg1; 464 o[2] = arg2; 465 return getMessage(caller, BASE_NAME, null, key, o); 466 } 467 468 478 public static String getString(Object caller, String key, Object arg0, Object arg1, Object arg2, Object arg3) 479 throws MissingResourceException 480 { 481 Object [] o = new Object [4]; 482 o[0] = arg0; 483 o[1] = arg1; 484 o[2] = arg2; 485 o[3] = arg3; 486 return getMessage(caller, BASE_NAME, null, key, o); 487 } 488 489 490 501 public static String getString(Object caller, String key, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) 502 throws MissingResourceException 503 { 504 Object [] o = new Object [5]; 505 o[0] = arg0; 506 o[1] = arg1; 507 o[2] = arg2; 508 o[3] = arg3; 509 o[4] = arg4; 510 return getMessage(caller, BASE_NAME, null, key, o); 511 } 512 513 514 521 public static String getString(Object caller, String key, Object [] args) 522 throws MissingResourceException 523 { 524 return getMessage(caller, BASE_NAME, null, key, args); 525 } 526 527 528 535 public static String getString(Object caller, Locale locale, String key) 536 throws MissingResourceException 537 { 538 return getMessage(caller, BASE_NAME, locale, key, null); 539 } 540 541 549 public static String getString(Object caller, Locale locale, String key, Object arg0) 550 throws MissingResourceException 551 { 552 Object [] o = new Object [1]; 553 o[0] = arg0; 554 return getMessage(caller, BASE_NAME, locale, key, o); 555 } 556 557 566 public static String getString(Object caller, Locale locale, String key, Object arg0, Object arg1) 567 throws MissingResourceException 568 { 569 Object [] o = new Object [2]; 570 o[0] = arg0; 571 o[1] = arg1; 572 return getMessage(caller, BASE_NAME, locale, key, o); 573 } 574 575 585 public static String getString(Object caller, Locale locale, String key, Object arg0, Object arg1, Object arg2) 586 throws MissingResourceException 587 { 588 Object [] o = new Object [3]; 589 o[0] = arg0; 590 o[1] = arg1; 591 o[2] = arg2; 592 return getMessage(caller, BASE_NAME, locale, key, o); 593 } 594 595 606 public static String getString(Object caller, Locale locale, String key, Object arg0, Object arg1, Object arg2, Object arg3) 607 throws MissingResourceException 608 { 609 Object [] o = new Object [4]; 610 o[0] = arg0; 611 o[1] = arg1; 612 o[2] = arg2; 613 o[3] = arg3; 614 return getMessage(caller, BASE_NAME, locale, key, o); 615 } 616 617 628 public static String getString(Object caller, Locale locale, String key, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) 629 throws MissingResourceException 630 { 631 Object [] o = new Object [5]; 632 o[0] = arg0; 633 o[1] = arg1; 634 o[2] = arg2; 635 o[3] = arg3; 636 o[4] = arg4; 637 return getMessage(caller, BASE_NAME, locale, key, o); 638 } 639 640 648 public static String getString(Object caller, Locale locale, String key, Object [] args) 649 throws MissingResourceException 650 { 651 return getMessage(caller, BASE_NAME, locale, key, args); 652 } 653 654 public static String getMessage(Object caller, String basename, Locale locale, String key, 656 Object [] args) 657 throws MissingResourceException 658 { 659 String msg = null; 660 MissingResourceException firstEx = null; 661 String fullName = null; 662 Class curClass = null; 663 boolean didNull = false; 664 665 if (caller != null) { 666 if(caller instanceof Class ) 667 curClass = (Class ) caller; 668 else 669 curClass = caller.getClass(); 670 } 671 672 while (msg == null) { 673 674 if (curClass != null) { 676 677 String pkgName = curClass.getName(); 679 680 int pos = pkgName.lastIndexOf("."); 681 if (pos > 0) { 682 fullName = pkgName.substring(0, pos + 1).replace('.', '/') + basename; 683 } 684 else { 685 fullName = basename; 686 } 687 } 688 else { 689 fullName = basename; 690 } 691 692 try { 693 RB rb = new RB(caller, fullName, locale); 694 msg = rb.getString(key, args); 695 } 696 catch (MissingResourceException ex) { 697 if (curClass == null) { 698 throw ex; 699 } 700 701 if (firstEx == null) { 703 firstEx = ex; 704 } 705 706 curClass = curClass.getSuperclass(); 708 if (curClass == null) { 709 if (didNull) 710 throw firstEx; 711 didNull = true; 712 caller = null; 713 } else { 714 String cname = curClass.getName(); 715 if (cname.startsWith("java.") || 716 cname.startsWith("javax.")) { 717 if (didNull) 718 throw firstEx; 719 didNull = true; 720 caller = null; 721 curClass = null; 722 } 723 } 724 } 725 726 } 727 return msg; 728 } 729 730 733 public static void clearCache() 734 { 735 propertyCache.clear(); 736 } 737 } 738 | Popular Tags |