Commit 8cf35364 by Tom Tromey Committed by Tom Tromey

re PR libgcj/6576 (java.util.ResourceBundle.getResource ignores locale)

	Fix for PR libgcj/6576:
	* java/util/ResourceBundle.java (tryBundle): Cache `null' if we
	didn't find a given bundle.
	(getBundle): Don't require base bundle.
	(setParent): Removed old comment.
	(tryLocalBundle): Try components even if preceding components were
	empty.

From-SVN: r57442
parent 97d48e5a
2002-09-22 Tom Tromey <tromey@redhat.com>
Fix for PR libgcj/6576:
* java/util/ResourceBundle.java (tryBundle): Cache `null' if we
didn't find a given bundle.
(getBundle): Don't require base bundle.
(setParent): Removed old comment.
(tryLocalBundle): Try components even if preceding components were
empty.
2002-09-22 Kaveh R. Ghazi <ghazi@caip.rutgers.edu> 2002-09-22 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
* Makefile.am (all-multi): Fix multilib parallel build. * Makefile.am (all-multi): Fix multilib parallel build.
......
...@@ -65,7 +65,7 @@ baseName_<i>def. language</i>_<i>def. country</i> ...@@ -65,7 +65,7 @@ baseName_<i>def. language</i>_<i>def. country</i>
baseName_<i>def. language</i> baseName_<i>def. language</i>
baseName</pre> baseName</pre>
* *
* <p>A bundle is backed up by less specific bundles (omiting variant, country * <p>A bundle is backed up by less specific bundles (omitting variant, country
* or language). But it is not backed up by the default language locale. * or language). But it is not backed up by the default language locale.
* *
* <p>If you provide a bundle for a given locale, say * <p>If you provide a bundle for a given locale, say
...@@ -239,7 +239,6 @@ public abstract class ResourceBundle ...@@ -239,7 +239,6 @@ public abstract class ResourceBundle
*/ */
protected void setParent(ResourceBundle parent) protected void setParent(ResourceBundle parent)
{ {
// Shall we ignore the old parent?
this.parent = parent; this.parent = parent;
} }
...@@ -362,42 +361,59 @@ public abstract class ResourceBundle ...@@ -362,42 +361,59 @@ public abstract class ResourceBundle
cache = new HashMap(); cache = new HashMap();
resourceBundleCache.put(classLoader, cache); resourceBundleCache.put(classLoader, cache);
} }
else else if (cache.containsKey(name))
{ {
Reference ref = (Reference) cache.get(name); Reference ref = (Reference) cache.get(name);
if (ref != null) ResourceBundle result = null;
// If REF is null, that means that we added a `null' value to
// the hash map. That means we failed to find the bundle
// previously, and we cached that fact. The JDK does this, so
// it must be ok.
if (ref == null)
throw new MissingResourceException("Bundle " + baseName
+ " not found",
baseName, "");
else
{ {
ResourceBundle rb = (ResourceBundle) ref.get(); ResourceBundle rb = (ResourceBundle) ref.get();
if (rb != null) if (rb != null)
// rb should already have the right parent, except if {
// RB should already have the right parent, except if
// something very strange happened. // something very strange happened.
return rb; return rb;
} }
// If RB is null, then we previously found it but it was
// collected. So we try again.
}
} }
// It is ok if this returns null. We aren't required to have the
// base bundle.
ResourceBundle baseBundle = tryBundle(baseName, emptyLocale, ResourceBundle baseBundle = tryBundle(baseName, emptyLocale,
classLoader, null, cache); classLoader, null, cache);
if (baseBundle == null)
// JDK says, that if one provides a bundle base_en_UK, one
// must also provide the bundles base_en and base.
// This implies that if there is no bundle for base, there
// is no bundle at all.
throw new MissingResourceException("Bundle " + baseName + " not found",
baseName, "");
// Now use the default locale. // Now use our locale, followed by the default locale. We only
// need to try the default locale if our locale is different, and
// if our locale failed to yield a result other than the base
// bundle.
ResourceBundle bundle = tryLocalBundle(baseName, locale, ResourceBundle bundle = tryLocalBundle(baseName, locale,
classLoader, baseBundle, cache); classLoader, baseBundle, cache);
if (bundle == baseBundle && !locale.equals(Locale.getDefault())) if (bundle == baseBundle && !locale.equals(Locale.getDefault()))
{
bundle = tryLocalBundle(baseName, Locale.getDefault(), bundle = tryLocalBundle(baseName, Locale.getDefault(),
classLoader, baseBundle, cache); classLoader, baseBundle, cache);
// We need to record that the argument locale maps to the
// Check whether baseName_locale has been loaded; if not, map the // bundle we just found. If we didn't find a bundle, record
// "baseName" bundle to "baseName_locale" to avoid retrying to load // that instead.
// baseName_locale. if (bundle == null)
Reference ref = (Reference) cache.get(name); cache.put(name, null);
if (ref == null) else
cache.put(name, new SoftReference(bundle)); cache.put(name, new SoftReference(bundle));
}
if (bundle == null)
throw new MissingResourceException("Bundle " + baseName + " not found",
baseName, "");
return bundle; return bundle;
} }
...@@ -440,16 +456,29 @@ public abstract class ResourceBundle ...@@ -440,16 +456,29 @@ public abstract class ResourceBundle
HashMap cache) HashMap cache)
{ {
// First look into the cache. // First look into the cache.
// XXX We should remove cleared references from the cache. if (cache.containsKey(localizedName))
{
Reference ref = (Reference) cache.get(localizedName); Reference ref = (Reference) cache.get(localizedName);
if (ref != null) ResourceBundle result = null;
// If REF is null, that means that we added a `null' value to
// the hash map. That means we failed to find the bundle
// previously, and we cached that fact. The JDK does this, so
// it must be ok.
if (ref == null)
return null;
else
{ {
ResourceBundle rb = (ResourceBundle) ref.get(); ResourceBundle rb = (ResourceBundle) ref.get();
if (rb != null) if (rb != null)
// rb should already have the right parent, except if {
// RB should already have the right parent, except if
// something very strange happened. // something very strange happened.
return rb; return rb;
} }
// If RB is null, then we previously found it but it was
// collected. So we try again.
}
}
// foundBundle holds exact matches for the localizedName resource // foundBundle holds exact matches for the localizedName resource
// bundle, which may later be cached. // bundle, which may later be cached.
...@@ -470,6 +499,7 @@ public abstract class ResourceBundle ...@@ -470,6 +499,7 @@ public abstract class ResourceBundle
// ignore them all // ignore them all
} }
if (foundBundle == null) if (foundBundle == null)
{
try try
{ {
InputStream is; InputStream is;
...@@ -489,11 +519,18 @@ public abstract class ResourceBundle ...@@ -489,11 +519,18 @@ public abstract class ResourceBundle
catch (IOException ex) catch (IOException ex)
{ {
} }
}
if (foundBundle != null) // Put the result into the hash table. If we didn't find anything
// here, we record our parent bundle. If we record `null' that means
// nothing, not even the base, was found.
if (foundBundle == null)
foundBundle = bundle;
if (foundBundle == null)
cache.put(localizedName, null);
else
cache.put(localizedName, new SoftReference(foundBundle)); cache.put(localizedName, new SoftReference(foundBundle));
return foundBundle;
return foundBundle != null ? foundBundle : bundle;
} }
/** /**
...@@ -501,7 +538,7 @@ public abstract class ResourceBundle ...@@ -501,7 +538,7 @@ public abstract class ResourceBundle
* locales with the same language. * locales with the same language.
* *
* @param name the name * @param name the name
* @param locale the locale, that must be used exactly * @param locale the locale
* @param classloader the classloader * @param classloader the classloader
* @param bundle the backup (parent) bundle * @param bundle the backup (parent) bundle
* @return the resource bundle if it was loaded, otherwise the backup * @return the resource bundle if it was loaded, otherwise the backup
...@@ -513,33 +550,38 @@ public abstract class ResourceBundle ...@@ -513,33 +550,38 @@ public abstract class ResourceBundle
HashMap cache) HashMap cache)
{ {
final String language = locale.getLanguage(); final String language = locale.getLanguage();
final String country = locale.getCountry();
final String variant = locale.getVariant();
StringBuffer sb = new StringBuffer(60); StringBuffer sb = new StringBuffer(60);
sb.append(baseName);
sb.append('_');
if (language.length() > 0) if (language.length() > 0)
{ {
final String country = locale.getCountry(); sb.append(language);
sb.append(baseName).append('_').append(language); bundle = tryBundle(sb.toString(), new Locale(language),
String name = sb.toString(); classloader, bundle, cache);
}
// If LANGUAGE was empty, we still need to try the other
// components, and the `_' is required.
sb.append('_');
if (country.length() != 0) if (country.length() > 0)
{ {
bundle = tryBundle(name, new Locale(language), sb.append(country);
bundle = tryBundle(sb.toString(), new Locale(language, country),
classloader, bundle, cache); classloader, bundle, cache);
sb.append('_').append(country); }
name = sb.toString(); sb.append('_');
final String variant = locale.getVariant();
if (variant.length() != 0) if (variant.length() > 0)
{ {
bundle = tryBundle(name, new Locale(language, country), sb.append(variant);
bundle = tryBundle(sb.toString(), locale,
classloader, bundle, cache); classloader, bundle, cache);
sb.append('_').append(variant);
name = sb.toString();
}
}
bundle = tryBundle(name, locale, classloader, bundle, cache);
} }
return bundle; return bundle;
} }
} // class ResourceBundle }
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