2000-03-08 03:55:28 +08:00
|
|
|
/* Copyright (C) 1998, 1999 Free Software Foundation
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
package java.util;
|
|
|
|
|
1999-10-17 11:11:50 +08:00
|
|
|
import java.io.InputStream;
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
/**
|
|
|
|
* @author Anthony Green <green@cygnus.com>
|
|
|
|
* @date November 26, 1998.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3,
|
|
|
|
* and "The Java Language Specification", ISBN 0-201-63451-1. */
|
|
|
|
|
|
|
|
public abstract class ResourceBundle
|
|
|
|
{
|
|
|
|
protected ResourceBundle parent;
|
|
|
|
|
1999-04-30 17:31:00 +08:00
|
|
|
// This is used to cache resource bundles.
|
|
|
|
private static Hashtable resource_cache = new Hashtable ();
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
public ResourceBundle ()
|
|
|
|
{
|
|
|
|
}
|
2000-07-12 11:32:07 +08:00
|
|
|
|
|
|
|
public Locale getLocale()
|
|
|
|
{
|
|
|
|
// FIXME: Stub added for this missing method because it is needed for AWT.
|
|
|
|
return null;
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public final String getString (String key) throws MissingResourceException
|
|
|
|
{
|
|
|
|
return (String) getObject(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public final String[] getStringArray (String key)
|
|
|
|
throws MissingResourceException
|
|
|
|
{
|
|
|
|
return (String[]) getObject(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public final Object getObject(String key) throws MissingResourceException
|
|
|
|
{
|
|
|
|
Object result;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return handleGetObject (key);
|
|
|
|
}
|
|
|
|
catch (MissingResourceException ex)
|
|
|
|
{
|
|
|
|
if (parent != null)
|
|
|
|
return parent.getObject(key);
|
|
|
|
else
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final ResourceBundle getBundle(String baseName)
|
|
|
|
throws MissingResourceException
|
|
|
|
{
|
|
|
|
return getBundle(baseName, Locale.getDefault());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start searching with the name bundleName. Continue searching by
|
|
|
|
// stripping off the '_' delimited tails until the search name is
|
|
|
|
// the same as stopHere.
|
|
|
|
private static final ResourceBundle trySomeGetBundle (String bundleName,
|
|
|
|
String stopHere)
|
|
|
|
{
|
|
|
|
Class rbc;
|
1999-04-30 17:31:00 +08:00
|
|
|
ResourceBundle needs_parent = null, r, result = null;
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
rbc = Class.forName(bundleName);
|
1999-04-30 17:31:00 +08:00
|
|
|
r = null;
|
1999-04-07 22:42:40 +08:00
|
|
|
try
|
|
|
|
{
|
1999-04-30 17:31:00 +08:00
|
|
|
r = (ResourceBundle) rbc.newInstance();
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
catch (IllegalAccessException ex)
|
|
|
|
{
|
|
|
|
// Fall through
|
|
|
|
}
|
|
|
|
catch (InstantiationException ex)
|
|
|
|
{
|
|
|
|
// Fall through
|
|
|
|
}
|
1999-04-30 17:31:00 +08:00
|
|
|
if (r != null)
|
|
|
|
{
|
|
|
|
if (result == null)
|
|
|
|
result = r;
|
|
|
|
if (needs_parent != null)
|
|
|
|
{
|
|
|
|
// We've been through the loop one or more times
|
|
|
|
// already. Set the parent and keep going.
|
|
|
|
needs_parent.setParent(r);
|
|
|
|
}
|
|
|
|
needs_parent = r;
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
catch (ClassNotFoundException ex)
|
|
|
|
{
|
1999-04-30 17:31:00 +08:00
|
|
|
// Fall through.
|
|
|
|
}
|
|
|
|
|
1999-10-17 11:11:50 +08:00
|
|
|
// Look for a properties file.
|
|
|
|
{
|
2000-07-12 11:32:07 +08:00
|
|
|
String prop_name = (bundleName.replace('.', '/') + ".properties");
|
|
|
|
System.out.println ("trying '" + prop_name + "' for '" + bundleName);
|
|
|
|
InputStream i = ClassLoader.getSystemResourceAsStream (prop_name);
|
|
|
|
/*
|
1999-10-17 11:11:50 +08:00
|
|
|
InputStream i =
|
|
|
|
ClassLoader.getSystemResourceAsStream (bundleName.replace ('.', '/')
|
|
|
|
+ ".properties");
|
2000-07-12 11:32:07 +08:00
|
|
|
*/
|
1999-10-17 11:11:50 +08:00
|
|
|
if (i != null)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return new PropertyResourceBundle (i);
|
|
|
|
} catch (java.io.IOException e) {
|
|
|
|
// The docs don't appear to define what happens in
|
|
|
|
// this case, but it seems like continuing the
|
|
|
|
// search is a reasonable thing to do.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-04-30 17:31:00 +08:00
|
|
|
if (bundleName.equals(stopHere))
|
|
|
|
return result;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int last = bundleName.lastIndexOf('_');
|
1999-04-07 22:42:40 +08:00
|
|
|
|
1999-04-30 17:31:00 +08:00
|
|
|
// No more underscores?
|
|
|
|
if (last == -1)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
// Loop around, testing this new shorter name.
|
|
|
|
bundleName = bundleName.substring(0, last);
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-04-30 17:31:00 +08:00
|
|
|
|
|
|
|
// Search for bundles, but stop at baseName_language (if required).
|
|
|
|
// This is synchronized so that the cache works correctly.
|
|
|
|
private static final synchronized ResourceBundle
|
|
|
|
partialGetBundle (String baseName, Locale locale, boolean langStop)
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
|
|
|
ResourceBundle rb;
|
|
|
|
|
1999-05-10 16:58:54 +08:00
|
|
|
// Explicitly invoke locale.toString() to force a
|
|
|
|
// NullPointerException when required.
|
|
|
|
String bundleName = baseName + "_" + locale.toString();
|
1999-04-30 17:31:00 +08:00
|
|
|
|
|
|
|
// Check the cache.
|
|
|
|
Object obj = resource_cache.get(bundleName);
|
|
|
|
if (obj != null)
|
|
|
|
return (ResourceBundle) obj;
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
String stopHere = (baseName
|
1999-04-30 17:31:00 +08:00
|
|
|
+ (langStop ? ("_" + locale.getLanguage()) : ""));
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
rb = trySomeGetBundle(bundleName, stopHere);
|
1999-04-30 17:31:00 +08:00
|
|
|
if (rb != null)
|
|
|
|
resource_cache.put(bundleName, rb);
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
return rb;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final ResourceBundle getBundle (String baseName,
|
|
|
|
Locale locale)
|
|
|
|
throws MissingResourceException
|
|
|
|
{
|
|
|
|
ResourceBundle rb;
|
|
|
|
Class rbc;
|
|
|
|
|
1999-05-24 23:00:02 +08:00
|
|
|
if (baseName == null)
|
|
|
|
throw new NullPointerException ();
|
|
|
|
|
1999-04-30 17:31:00 +08:00
|
|
|
rb = partialGetBundle(baseName, locale, false);
|
1999-04-07 22:42:40 +08:00
|
|
|
if (rb != null)
|
|
|
|
return rb;
|
|
|
|
|
1999-04-30 17:31:00 +08:00
|
|
|
// Finally, try the default locale.
|
1999-04-07 22:42:40 +08:00
|
|
|
if (! locale.equals(Locale.getDefault()))
|
|
|
|
{
|
1999-04-30 17:31:00 +08:00
|
|
|
rb = partialGetBundle(baseName, Locale.getDefault(), true);
|
1999-04-07 22:42:40 +08:00
|
|
|
if (rb != null)
|
|
|
|
return rb;
|
|
|
|
}
|
1999-05-24 23:00:02 +08:00
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
throw new MissingResourceException("can't load bundle",
|
|
|
|
baseName,
|
|
|
|
"bundle");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void setParent(ResourceBundle parent)
|
|
|
|
{
|
|
|
|
this.parent = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract Object handleGetObject(String key)
|
|
|
|
throws MissingResourceException;
|
|
|
|
|
|
|
|
public abstract Enumeration getKeys();
|
|
|
|
}
|