OSDN Git Service

* java/lang/StringBuffer.java (ensureCapacity): Don't resize
[pf3gnuchains/gcc-fork.git] / libjava / java / util / ResourceBundle.java
1 /* Copyright (C) 1998, 1999  Cygnus Solutions
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package java.util;
10
11 /**
12  * @author Anthony Green <green@cygnus.com>
13  * @date November 26, 1998.
14  */
15
16 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3,
17  * and "The Java Language Specification", ISBN 0-201-63451-1.  */
18
19 public abstract class ResourceBundle
20 {
21   protected ResourceBundle parent;
22
23   // This is used to cache resource bundles.
24   private static Hashtable resource_cache = new Hashtable ();
25
26   public ResourceBundle ()
27     {
28     }
29
30   public final String getString (String key) throws MissingResourceException
31     {
32       return (String) getObject(key);
33     }
34
35   public final String[] getStringArray (String key) 
36     throws MissingResourceException
37     {
38       return (String[]) getObject(key);
39     }
40
41   public final Object getObject(String key) throws MissingResourceException
42     {
43       Object result;
44
45       try 
46         {
47           return handleGetObject (key);
48         }
49       catch (MissingResourceException ex)
50         {
51           if (parent != null)
52             return parent.getObject(key);
53           else 
54             throw ex;
55         }
56     }
57
58   public static final ResourceBundle getBundle(String baseName) 
59     throws MissingResourceException
60     {
61       return getBundle(baseName, Locale.getDefault());
62     }
63
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,
68                                                         String stopHere)
69     {
70       Class rbc;
71       ResourceBundle needs_parent = null, r, result = null;
72
73       while (true)
74         {
75           try 
76             {
77               rbc = Class.forName(bundleName);
78               r = null;
79               try 
80                 {
81                   r = (ResourceBundle) rbc.newInstance();
82                 }
83               catch (IllegalAccessException ex)
84                 {
85                   // Fall through
86                 }
87               catch (InstantiationException ex)
88                 {
89                   // Fall through
90                 }
91               if (r != null)
92                 {
93                   if (result == null)
94                     result = r;
95                   if (needs_parent != null)
96                     {
97                       // We've been through the loop one or more times
98                       // already.  Set the parent and keep going.
99                       needs_parent.setParent(r);
100                     }
101                   needs_parent = r;
102                 }
103             }
104           catch (ClassNotFoundException ex)
105             {
106               // Fall through.
107             }
108
109           if (bundleName.equals(stopHere))
110             return result;
111           else
112             {
113               int last = bundleName.lastIndexOf('_');
114                   
115               // No more underscores?
116               if (last == -1)
117                 return result;
118
119               // Loop around, testing this new shorter name.
120               bundleName = bundleName.substring(0, last);
121             }
122         }
123     }
124
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)
129     {
130       ResourceBundle rb;
131
132       String bundleName = baseName + "_" + locale;
133
134       // Check the cache.
135       Object obj = resource_cache.get(bundleName);
136       if (obj != null)
137         return (ResourceBundle) obj;
138
139       String stopHere = (baseName 
140                          + (langStop ? ("_" + locale.getLanguage()) : ""));
141
142
143       rb = trySomeGetBundle(bundleName, stopHere);
144       if (rb != null)
145         resource_cache.put(bundleName, rb);
146
147       return rb;
148     }
149
150   public static final ResourceBundle getBundle (String baseName, 
151                                                 Locale locale)
152     throws MissingResourceException
153     {
154       ResourceBundle rb;
155       Class rbc;
156
157       // FIXME: We can't currently rely on NullPointerException being
158       // thrown when we invoke a method on a null object.
159       if (locale == null)
160         throw new NullPointerException ();
161
162       rb = partialGetBundle(baseName, locale, false);
163       if (rb != null)
164         return rb;
165
166       // Finally, try the default locale.
167       if (! locale.equals(Locale.getDefault()))
168         {
169           rb = partialGetBundle(baseName, Locale.getDefault(), true);
170           if (rb != null)
171             return rb;
172         }
173                            
174       throw new MissingResourceException("can't load bundle", 
175                                          baseName,
176                                          "bundle");
177     }
178
179   protected void setParent(ResourceBundle parent)
180     {
181       this.parent = parent;
182     }
183
184   protected abstract Object handleGetObject(String key) 
185     throws MissingResourceException;
186
187   public abstract Enumeration getKeys();
188 }