RBpublic class RB extends Object CURRENTLY NOT USED
KEEPING FOR REFERENCE 9/19/2002
Wrapper class for resource bundles. Property files are used to store
resource strings, which are the only types of resources available.
Property files can inherit properties from other files so that
a base property file can be used and a small number of properties
can be over-ridden by another property file. For example you may
create an english version of a resource file named "resource.properties".
You then decide that the British English version of all of the properties
except one are the same, so there is no need to redefine all of the
properties in "resource_en_GB", just the one that is different.
The property file lookup searches for classes with various suffixes
on the basis if the desired local and the current default local
(as returned by Local.getDefault()). As property files are found the
property values are merged so that inheritance is preserved.
The order of searching is:
basename + "_" + langage + "_" + country + "_" + variant
basename + "_" + langage + "_" + country
basename + "_" + langage
basename + "_" + defaultLanguage + "_" + defaultCountry + "_" + defaultVariant
basename + "_" + defaultLanguage + "_" + defaultCountry
basename + "_" + defaultLanguage
basename
The basename is the name of the property file without the ".properties"
extension.
Properties will be cached for performance.
Property values stored in the property files can also contain dynamic
variables. Any dynamic variable defined in PropertiesUtil.getVariableValue()
can be used (such as {date}), as well as arguments in the form {0}, {1}, etc.
Argument values are specified in the various overloaded getString() methods. |
Fields Summary |
---|
static Hashtable | propertyCache | public static final String | BASE_NAME | public static final String | PROPERTY_EXT | protected String | basePropertyFileName | protected Properties | resourceProperties |
Constructors Summary |
---|
public RB(String name)Construct a new RB
this(null, name, null);
| public RB(Object caller, String name)Construct a new RB
this(caller, name, null);
| public RB(Object caller, String name, Locale locale)Construct a new RB
ClassLoader cl = null;
if (caller != null) {
Class c;
if (caller instanceof Class) {
c = (Class) caller;
}
else {
c = caller.getClass();
}
// Get the appropriate class loader
cl = c.getClassLoader();
if (name.indexOf("/") == -1) {
// Create the full basename only if not given
String fullName = c.getName();
int pos = fullName.lastIndexOf(".");
if (pos > 0) {
name = fullName.substring(0, pos + 1).replace('.", '/") + name;
}
}
} else {
// Try the shared default properties file...
if (name.indexOf("/") == -1) {
name = "org/apache/axis/default-resource";
}
}
Locale defaultLocale = Locale.getDefault();
// If the locale given is the same as the default locale, ignore it
if (locale != null) {
if (locale.equals(defaultLocale)) {
locale = null;
}
}
// Load the properties. If no property files exist then a
// MissingResourceException will be thrown
loadProperties(name, cl, locale, defaultLocale);
|
Methods Summary |
---|
public static void | clearCache()Clears the internal cache
propertyCache.clear();
| public static java.lang.String | getMessage(java.lang.Object caller, java.lang.String basename, java.util.Locale locale, java.lang.String key, java.lang.Object[] args)
String msg = null;
MissingResourceException firstEx = null;
String fullName = null;
Class curClass = null;
boolean didNull = false;
if (caller != null) {
if(caller instanceof Class)
curClass = (Class) caller;
else
curClass = caller.getClass();
}
while (msg == null) {
// Get the full name of the resource
if (curClass != null) {
// Create the full basename
String pkgName = curClass.getName();
int pos = pkgName.lastIndexOf(".");
if (pos > 0) {
fullName = pkgName.substring(0, pos + 1).replace('.", '/") + basename;
}
else {
fullName = basename;
}
}
else {
fullName = basename;
}
try {
RB rb = new RB(caller, fullName, locale);
msg = rb.getString(key, args);
}
catch (MissingResourceException ex) {
if (curClass == null) {
throw ex;
}
// Save the first exception
if (firstEx == null) {
firstEx = ex;
}
// Get the superclass
curClass = curClass.getSuperclass();
if (curClass == null) {
if (didNull)
throw firstEx;
didNull = true;
caller = null;
} else {
String cname = curClass.getName();
if (cname.startsWith("java.") ||
cname.startsWith("javax.")) {
if (didNull)
throw firstEx;
didNull = true;
caller = null;
curClass = null;
}
}
}
}
return msg;
| public java.util.Properties | getProperties()Get the underlying properties
return resourceProperties;
| public static java.lang.String | getString(java.lang.Object caller, java.lang.String key)Get a message from resource.properties from the package of the given object.
return getMessage(caller, BASE_NAME, null, key, null);
| public static java.lang.String | getString(java.lang.Object caller, java.lang.String key, java.lang.Object arg0)Get a message from resource.properties from the package of the given object.
Object[] o = new Object[1];
o[0] = arg0;
return getMessage(caller, BASE_NAME, null, key, o);
| public static java.lang.String | getString(java.lang.Object caller, java.lang.String key, java.lang.Object arg0, java.lang.Object arg1)Get a message from resource.properties from the package of the given object.
Object[] o = new Object[2];
o[0] = arg0;
o[1] = arg1;
return getMessage(caller, BASE_NAME, null, key, o);
| public static java.lang.String | getString(java.lang.Object caller, java.lang.String key, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2)Get a message from resource.properties from the package of the given object.
Object[] o = new Object[3];
o[0] = arg0;
o[1] = arg1;
o[2] = arg2;
return getMessage(caller, BASE_NAME, null, key, o);
| public static java.lang.String | getString(java.lang.Object caller, java.lang.String key, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)Get a message from resource.properties from the package of the given object.
Object[] o = new Object[4];
o[0] = arg0;
o[1] = arg1;
o[2] = arg2;
o[3] = arg3;
return getMessage(caller, BASE_NAME, null, key, o);
| public static java.lang.String | getString(java.lang.Object caller, java.lang.String key, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4)Get a message from resource.properties from the package of the given object.
Object[] o = new Object[5];
o[0] = arg0;
o[1] = arg1;
o[2] = arg2;
o[3] = arg3;
o[4] = arg4;
return getMessage(caller, BASE_NAME, null, key, o);
| public static java.lang.String | getString(java.lang.Object caller, java.lang.String key, java.lang.Object[] args)Get a message from resource.properties from the package of the given object.
return getMessage(caller, BASE_NAME, null, key, args);
| public static java.lang.String | getString(java.lang.Object caller, java.util.Locale locale, java.lang.String key)Get a message from resource.properties from the package of the given object.
return getMessage(caller, BASE_NAME, locale, key, null);
| public static java.lang.String | getString(java.lang.Object caller, java.util.Locale locale, java.lang.String key, java.lang.Object arg0)Get a message from resource.properties from the package of the given object.
Object[] o = new Object[1];
o[0] = arg0;
return getMessage(caller, BASE_NAME, locale, key, o);
| public static java.lang.String | getString(java.lang.Object caller, java.util.Locale locale, java.lang.String key, java.lang.Object arg0, java.lang.Object arg1)Get a message from resource.properties from the package of the given object.
Object[] o = new Object[2];
o[0] = arg0;
o[1] = arg1;
return getMessage(caller, BASE_NAME, locale, key, o);
| public static java.lang.String | getString(java.lang.Object caller, java.util.Locale locale, java.lang.String key, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2)Get a message from resource.properties from the package of the given object.
Object[] o = new Object[3];
o[0] = arg0;
o[1] = arg1;
o[2] = arg2;
return getMessage(caller, BASE_NAME, locale, key, o);
| public static java.lang.String | getString(java.lang.Object caller, java.util.Locale locale, java.lang.String key, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)Get a message from resource.properties from the package of the given object.
Object[] o = new Object[4];
o[0] = arg0;
o[1] = arg1;
o[2] = arg2;
o[3] = arg3;
return getMessage(caller, BASE_NAME, locale, key, o);
| public static java.lang.String | getString(java.lang.Object caller, java.util.Locale locale, java.lang.String key, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4)Get a message from resource.properties from the package of the given object.
Object[] o = new Object[5];
o[0] = arg0;
o[1] = arg1;
o[2] = arg2;
o[3] = arg3;
o[4] = arg4;
return getMessage(caller, BASE_NAME, locale, key, o);
| public static java.lang.String | getString(java.lang.Object caller, java.util.Locale locale, java.lang.String key, java.lang.Object[] args)Get a message from resource.properties from the package of the given object.
return getMessage(caller, BASE_NAME, locale, key, args);
| public java.lang.String | getString(java.lang.String key)Gets a string message from the resource bundle for the given key
return getString(key, (Object[]) null);
| public java.lang.String | getString(java.lang.String key, java.lang.Object arg0)Gets a string message from the resource bundle for the given key. The
message may contain variables that will be substituted with the given
arguments. Variables have the format:
This message has two variables: {0} and {1}
Object[] o = new Object[1];
o[0] = arg0;
return getString(key, o);
| public java.lang.String | getString(java.lang.String key, java.lang.Object arg0, java.lang.Object arg1)Gets a string message from the resource bundle for the given key. The
message may contain variables that will be substituted with the given
arguments. Variables have the format:
This message has two variables: {0} and {1}
Object[] o = new Object[2];
o[0] = arg0;
o[1] = arg1;
return getString(key, o);
| public java.lang.String | getString(java.lang.String key, java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2)Gets a string message from the resource bundle for the given key. The
message may contain variables that will be substituted with the given
arguments. Variables have the format:
This message has two variables: {0} and {1}
Object[] o = new Object[3];
o[0] = arg0;
o[1] = arg1;
o[2] = arg2;
return getString(key, o);
| public java.lang.String | getString(java.lang.String key, java.lang.Object[] array)Gets a string message from the resource bundle for the given key. The
message may contain variables that will be substituted with the given
arguments. Variables have the format:
This message has two variables: {0} and {1}
String msg = null;
if (resourceProperties != null) {
msg = resourceProperties.getProperty(key);
}
if (msg == null) {
throw new MissingResourceException("Cannot find resource key \"" + key +
"\" in base name " + basePropertyFileName,
basePropertyFileName, key);
}
msg = MessageFormat.format(msg, array);
return msg;
| protected java.util.Properties | loadProperties(java.lang.String basename, java.lang.ClassLoader loader, java.util.Locale locale, java.util.Properties props)
String language = locale.getLanguage();
String country = locale.getCountry();
String variant = locale.getVariant();
if (variant != null) {
if (variant.trim().length() == 0) {
variant = null;
}
}
if (language != null) {
if (country != null) {
if (variant != null) {
props = merge(props, loadProperties(basename + "_" + language +"_" + country + "_" + variant +
PROPERTY_EXT, loader));
}
props = merge(props, loadProperties(basename + "_" + language +"_" + country +
PROPERTY_EXT, loader));
}
props = merge(props, loadProperties(basename + "_" + language + PROPERTY_EXT, loader));
}
return props;
| protected java.util.Properties | loadProperties(java.lang.String resname, java.lang.ClassLoader loader)
Properties props = null;
// Attempt to open and load the properties
InputStream in = null;
try {
if (loader != null) {
in = loader.getResourceAsStream(resname);
}
// Either we're using the system class loader or we didn't find the
// resource using the given class loader
if (in == null) {
in = ClassLoader.getSystemResourceAsStream(resname);
}
if (in != null) {
props = new Properties();
try {
props.load(in);
}
catch (IOException ex) {
// On error, clear the props
props = null;
}
}
}
finally {
if (in != null) {
try {
in.close();
}
catch (Exception ex) {
// Ignore error on close
}
}
}
return props;
| protected void | loadProperties(java.lang.String basename, java.lang.ClassLoader loader, java.util.Locale locale, java.util.Locale defaultLocale)
// Check the cache first
String loaderName = "";
if (loader != null) {
loaderName = ":" + loader.hashCode();
}
String cacheKey = basename + ":" + locale + ":" + defaultLocale + loaderName;
Properties p = (Properties) propertyCache.get(cacheKey);
basePropertyFileName = basename + PROPERTY_EXT;
if (p == null) {
// The properties were not found in the cache. Search the given locale
// first
if (locale != null) {
p = loadProperties(basename, loader, locale, p);
}
// Search the default locale
if (defaultLocale != null) {
p = loadProperties(basename, loader, defaultLocale, p);
}
// Search for the basename
p = merge(p, loadProperties(basePropertyFileName, loader));
if (p == null) {
throw new MissingResourceException("Cannot find resource for base name " +
basePropertyFileName, basePropertyFileName, "");
}
// Cache the properties
propertyCache.put(cacheKey, p);
}
resourceProperties = p;
| protected java.util.Properties | merge(java.util.Properties p1, java.util.Properties p2)Merge two Properties objects
if ((p1 == null) &&
(p2 == null)) {
return null;
}
else if (p1 == null) {
return p2;
}
else if (p2 == null) {
return p1;
}
// Now merge. p1 takes precedence
Enumeration enumeration = p2.keys();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
if (p1.getProperty(key) == null) {
p1.put(key, p2.getProperty(key));
}
}
return p1;
|
|