corda/classpath/java/util/ResourceBundle.java

112 lines
3.1 KiB
Java
Raw Normal View History

package java.util;
2007-09-12 02:56:02 +00:00
import java.lang.reflect.Method;
import java.io.InputStream;
import java.io.IOException;
public abstract class ResourceBundle {
protected String name;
2007-09-12 02:56:02 +00:00
protected ResourceBundle parent;
private static String replace(char a, char b, String s) {
char[] array = new char[s.length()];
for (int i = 0; i < array.length; ++i) {
char c = s.charAt(i);
array[i] = (c == a ? b : c);
}
return new String(array, 0, array.length, false);
}
private static ResourceBundle findProperties(String name, ClassLoader loader,
ResourceBundle parent)
throws IOException
{
InputStream in = loader.getResourceAsStream
(replace('.', '/', name) + ".properties");
if (in != null) {
try {
ResourceBundle r = new PropertyResourceBundle(in);
r.name = name;
r.parent = parent;
return r;
2007-09-12 02:56:02 +00:00
} finally {
in.close();
}
} else {
return null;
}
}
private static ResourceBundle find(String name, ClassLoader loader,
ResourceBundle parent)
throws Exception
{
try {
Class c = Class.forName(name, true, loader);
if (c.isAssignableFrom(ResourceBundle.class)) {
return (ResourceBundle) c.getConstructor().newInstance();
}
} catch (ClassNotFoundException ok) {
} catch (NoSuchMethodException ok) { }
return findProperties(name, loader, parent);
}
public static ResourceBundle getBundle(String name, Locale locale,
ClassLoader loader)
{
try {
ResourceBundle b = find(name, loader, null);
2007-09-14 03:59:39 +00:00
2007-09-12 02:56:02 +00:00
if (locale.getLanguage() != null) {
name = name + "_" + locale.getLanguage();
2007-09-14 03:59:39 +00:00
ResourceBundle b2 = find(name, loader, b);
if (b2 != null) b = b2;
2007-09-12 02:56:02 +00:00
if (locale.getCountry() != null) {
name = name + "_" + locale.getCountry();
2007-09-14 03:59:39 +00:00
b2 = find(name, loader, b);
if (b2 != null) b = b2;
2007-09-12 02:56:02 +00:00
if (locale.getVariant() != null) {
name = name + "_" + locale.getVariant();
2007-09-14 03:59:39 +00:00
b2 = find(name, loader, b);
if (b2 != null) b = b2;
2007-09-12 02:56:02 +00:00
}
}
}
return b;
} catch (Exception e) {
RuntimeException re = new MissingResourceException(name, name, null);
re.initCause(e);
throw re;
}
}
public static ResourceBundle getBundle(String name, Locale locale) {
return getBundle(name, locale,
Method.getCaller().getDeclaringClass().getClassLoader());
}
public static ResourceBundle getBundle(String name) {
return getBundle(name, Locale.getDefault(),
Method.getCaller().getDeclaringClass().getClassLoader());
}
public Object getObject(String key) {
for (ResourceBundle b = this; b != null; b = b.parent) {
Object value = b.handleGetObject(key);
if (value != null) {
return value;
}
}
throw new MissingResourceException(key, name, key);
2007-09-12 02:56:02 +00:00
}
public String getString(String key) {
return (String) getObject(key);
}
protected abstract Object handleGetObject(String key);
}