Commit 99780529 by Anthony Green Committed by Anthony Green

ResourceBundle fixes

From-SVN: r45472
parent 5660465a
2001-09-06 Anthony Green <green@redhat.com>
* java/util/ResourceBundle.java (tryLocalBundle): Eliminate
redundant method calls.
(emptyLocale): New private member.
(tryBundle): Use emptyLocale. Remove duplicate code. Only cache
exact matches.
2001-09-06 Tom Tromey <tromey@redhat.com> 2001-09-06 Tom Tromey <tromey@redhat.com>
* java/text/RuleBasedCollator.java (clone): Rewrote. * java/text/RuleBasedCollator.java (clone): Rewrote.
......
...@@ -201,8 +201,14 @@ public abstract class ResourceBundle ...@@ -201,8 +201,14 @@ public abstract class ResourceBundle
private static Map resourceBundleCache = new HashMap(); private static Map resourceBundleCache = new HashMap();
/** /**
* The `empty' locale is created once in order to optimize
* tryBundle().
*/
private static final Locale emptyLocale = new Locale ("", "");
/**
* Tries to load a class or a property file with the specified name. * Tries to load a class or a property file with the specified name.
* @param name the name. * @param localizedName the name.
* @param locale the locale, that must be used exactly. * @param locale the locale, that must be used exactly.
* @param classloader the classloader. * @param classloader the classloader.
* @param bundle the back up (parent) bundle * @param bundle the back up (parent) bundle
...@@ -229,21 +235,24 @@ public abstract class ResourceBundle ...@@ -229,21 +235,24 @@ public abstract class ResourceBundle
} }
} }
// foundBundle holds exact matches for the localizedName resource
// bundle, which may later be cached.
ResourceBundle foundBundle = null;
try try
{ {
java.io.InputStream is; java.io.InputStream is;
final String resourceName =
localizedName.replace('.', '/') + ".properties";
if (classloader == null) if (classloader == null)
is = ClassLoader.getSystemResourceAsStream is = ClassLoader.getSystemResourceAsStream (resourceName);
(localizedName.replace('.', '/') + ".properties");
else else
is = classloader.getResourceAsStream is = classloader.getResourceAsStream (resourceName);
(localizedName.replace('.', '/') + ".properties");
if (is != null) if (is != null)
{ {
ResourceBundle rb = new PropertyResourceBundle(is); foundBundle = new PropertyResourceBundle(is);
rb.parent = bundle; foundBundle.parent = bundle;
rb.locale = locale; foundBundle.locale = locale;
bundle = rb;
} }
} }
catch (java.io.IOException ex) catch (java.io.IOException ex)
...@@ -257,10 +266,9 @@ public abstract class ResourceBundle ...@@ -257,10 +266,9 @@ public abstract class ResourceBundle
rbClass = Class.forName(localizedName); rbClass = Class.forName(localizedName);
else else
rbClass = classloader.loadClass(localizedName); rbClass = classloader.loadClass(localizedName);
ResourceBundle rb = (ResourceBundle) rbClass.newInstance(); foundBundle = (ResourceBundle) rbClass.newInstance();
rb.parent = bundle; foundBundle.parent = bundle;
rb.locale = locale; foundBundle.locale = locale;
bundle = rb;
} }
catch (ClassNotFoundException ex) catch (ClassNotFoundException ex)
{ {
...@@ -274,11 +282,10 @@ public abstract class ResourceBundle ...@@ -274,11 +282,10 @@ public abstract class ResourceBundle
// XXX should we also ignore ClassCastException? // XXX should we also ignore ClassCastException?
} }
// Put the bundle in the cache if (foundBundle != null)
if (bundle != null) cache.put(localizedName, new SoftReference(foundBundle));
cache.put(localizedName, new SoftReference(bundle));
return bundle; return foundBundle != null ? foundBundle : bundle;
} }
/** /**
...@@ -298,26 +305,31 @@ public abstract class ResourceBundle ...@@ -298,26 +305,31 @@ public abstract class ResourceBundle
ResourceBundle bundle, ResourceBundle bundle,
HashMap cache) HashMap cache)
{ {
if (locale.getLanguage().length() > 0) final String language = locale.getLanguage();
if (language.length() > 0)
{ {
String name = baseName + "_" + locale.getLanguage(); final String country = locale.getCountry();
String name = baseName + "_" + language;
if (locale.getCountry().length() != 0) if (country.length() != 0)
{ {
bundle = tryBundle(name, bundle = tryBundle(name,
new Locale(locale.getLanguage(), ""), new Locale(language, ""),
classloader, bundle, cache); classloader, bundle, cache);
name += "_" + locale.getCountry(); name += "_" + country;
final String variant = locale.getVariant();
if (locale.getVariant().length() != 0) if (variant.length() != 0)
{ {
bundle = tryBundle(name, bundle = tryBundle(name,
new Locale(locale.getLanguage(), new Locale(language,
locale.getCountry()), country),
classloader, bundle, cache); classloader, bundle, cache);
name += "_" + locale.getVariant(); name += "_" + variant;
} }
} }
bundle = tryBundle(name, locale, classloader, bundle, cache); bundle = tryBundle(name, locale, classloader, bundle, cache);
...@@ -367,14 +379,14 @@ public abstract class ResourceBundle ...@@ -367,14 +379,14 @@ public abstract class ResourceBundle
} }
} }
ResourceBundle baseBundle = tryBundle(baseName, new Locale("", ""), ResourceBundle baseBundle = tryBundle(baseName, emptyLocale,
classLoader, null, cache); classLoader, null, cache);
if (baseBundle == null) if (baseBundle == null)
// JDK says, that if one provides a bundle base_en_UK, one // JDK says, that if one provides a bundle base_en_UK, one
// must also provide the bundles base_en and base. // must also provide the bundles base_en and base.
// This implies that if there is no bundle for base, there // This implies that if there is no bundle for base, there
// is no bundle at all. // is no bundle at all.
throw new MissingResourceException("Bundle not found", baseName, ""); throw new MissingResourceException("Bundle " + baseName + " not found", baseName, "");
// Now use the default locale. // Now use the default locale.
ResourceBundle bundle = tryLocalBundle(baseName, locale, ResourceBundle bundle = tryLocalBundle(baseName, locale,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment