1 /* Copyright (C) 1998, 1999 Cygnus Solutions
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 * @author Anthony Green <green@cygnus.com>
13 * @date November 26, 1998.
16 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3,
17 * and "The Java Language Specification", ISBN 0-201-63451-1. */
19 public abstract class ResourceBundle
21 protected ResourceBundle parent;
23 // This is used to cache resource bundles.
24 private static Hashtable resource_cache = new Hashtable ();
26 public ResourceBundle ()
30 public final String getString (String key) throws MissingResourceException
32 return (String) getObject(key);
35 public final String[] getStringArray (String key)
36 throws MissingResourceException
38 return (String[]) getObject(key);
41 public final Object getObject(String key) throws MissingResourceException
47 return handleGetObject (key);
49 catch (MissingResourceException ex)
52 return parent.getObject(key);
58 public static final ResourceBundle getBundle(String baseName)
59 throws MissingResourceException
61 return getBundle(baseName, Locale.getDefault());
64 // Start searching with the name bundleName. Continue searching by
65 // stripping off the '_' delimited tails until the search name is
66 // the same as stopHere.
67 private static final ResourceBundle trySomeGetBundle (String bundleName,
71 ResourceBundle needs_parent = null, r, result = null;
77 rbc = Class.forName(bundleName);
81 r = (ResourceBundle) rbc.newInstance();
83 catch (IllegalAccessException ex)
87 catch (InstantiationException ex)
95 if (needs_parent != null)
97 // We've been through the loop one or more times
98 // already. Set the parent and keep going.
99 needs_parent.setParent(r);
104 catch (ClassNotFoundException ex)
109 if (bundleName.equals(stopHere))
113 int last = bundleName.lastIndexOf('_');
115 // No more underscores?
119 // Loop around, testing this new shorter name.
120 bundleName = bundleName.substring(0, last);
125 // Search for bundles, but stop at baseName_language (if required).
126 // This is synchronized so that the cache works correctly.
127 private static final synchronized ResourceBundle
128 partialGetBundle (String baseName, Locale locale, boolean langStop)
132 String bundleName = baseName + "_" + locale;
135 Object obj = resource_cache.get(bundleName);
137 return (ResourceBundle) obj;
139 String stopHere = (baseName
140 + (langStop ? ("_" + locale.getLanguage()) : ""));
143 rb = trySomeGetBundle(bundleName, stopHere);
145 resource_cache.put(bundleName, rb);
150 public static final ResourceBundle getBundle (String baseName,
152 throws MissingResourceException
157 // FIXME: We can't currently rely on NullPointerException being
158 // thrown when we invoke a method on a null object.
160 throw new NullPointerException ();
162 rb = partialGetBundle(baseName, locale, false);
166 // Finally, try the default locale.
167 if (! locale.equals(Locale.getDefault()))
169 rb = partialGetBundle(baseName, Locale.getDefault(), true);
174 throw new MissingResourceException("can't load bundle",
179 protected void setParent(ResourceBundle parent)
181 this.parent = parent;
184 protected abstract Object handleGetObject(String key)
185 throws MissingResourceException;
187 public abstract Enumeration getKeys();