aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/util/ResourceBundle.java
blob: ba1d3f9fdcbf70b5fa4598bc3247527d6aa0d9a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Copyright (C) 1998, 1999  Cygnus Solutions

   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;

/**
 * @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;

  public ResourceBundle ()
    {
    }

  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;

      while (true)
	{
	  try 
	    {
	      rbc = Class.forName(bundleName);
	      try 
		{
		  return (ResourceBundle) rbc.newInstance();
		}
	      catch (IllegalAccessException ex)
		{
		  // Fall through
		}
	      catch (InstantiationException ex)
		{
		  // Fall through
		}
	      return null;
	    }
	  catch (ClassNotFoundException ex)
	    {
	      if (bundleName.compareTo(stopHere) == 0)
		return null;
	      else
		{
		  int last = bundleName.lastIndexOf('_');
		  
		  // No more underscores?
		  if (last == -1)
		    return null;
		  
		  // Loop around, testing this new shorter name.
		  bundleName = bundleName.substring(0, last);
		}
	    }
	}
    }
  
  // Search for bundles, but stop at baseName_language.
  private static final ResourceBundle partialGetBundle (String baseName,
							Locale locale)
    {
      ResourceBundle rb;

      String bundleName = (baseName 
			   + "_" 
			   + locale.getLanguage() + "_"
			   + locale.getCountry() + "_"
			   + locale.getVariant());

      String stopHere = (baseName 
			 + "_" 
			 + locale.getLanguage());


      rb = trySomeGetBundle(bundleName, stopHere);

      return rb;
    }

  public static final ResourceBundle getBundle (String baseName, 
						Locale locale)
    throws MissingResourceException
    {
      ResourceBundle rb;
      Class rbc;

      // FIXME: We can't currently rely on NullPointerException being
      // thrown when we invoke a method on a null object.
      if (locale == null)
	throw new NullPointerException ();

      rb = partialGetBundle(baseName, locale);
      if (rb != null)
	return rb;

      if (! locale.equals(Locale.getDefault()))
	{
	  rb = partialGetBundle(baseName, Locale.getDefault());
	  if (rb != null)
	    return rb;
	}
			   
      // Try just the baseName.
      try
	{
	  rbc = Class.forName (baseName);
	  try 
	    {
	      return (ResourceBundle) rbc.newInstance();
	    }
	  catch (IllegalAccessException ex)
	    {
	      // Fall through.
	    }
	  catch (InstantiationException ex)
	    {
	      // Fall through.
	    }
	}
      catch (ClassNotFoundException ex)
	{
	  // Fall through.
	}

      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();
}