Make MessageBundle.getResourceString public so MessageBundles can be composed instead of requiring inheritance to customize them, and open up the bundle creation process in MessageManager to subclass customization

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5901 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Charlie Groves
2009-08-12 20:04:36 +00:00
parent 8ca961fc9d
commit 505a9c3a53
2 changed files with 32 additions and 12 deletions
@@ -21,13 +21,12 @@
package com.threerings.util; package com.threerings.util;
import java.text.MessageFormat;
import java.util.Collection; import java.util.Collection;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.MissingResourceException; import java.util.MissingResourceException;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.text.MessageFormat;
import com.samskivert.text.MessageUtil; import com.samskivert.text.MessageUtil;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
@@ -236,11 +235,12 @@ public class MessageBundle
/** /**
* Get a String from the resource bundle, or null if there was an error. * Get a String from the resource bundle, or null if there was an error.
*/ */
protected String getResourceString (String key) public String getResourceString (String key)
{ {
return getResourceString(key, true); return getResourceString(key, true);
} }
/** /**
* Get a String from the resource bundle, or null if there was an * Get a String from the resource bundle, or null if there was an
* error. * error.
@@ -249,7 +249,7 @@ public class MessageBundle
* @param reportMissing whether or not the method should log an error * @param reportMissing whether or not the method should log an error
* if the resource didn't exist. * if the resource didn't exist.
*/ */
protected String getResourceString (String key, boolean reportMissing) public String getResourceString (String key, boolean reportMissing)
{ {
try { try {
if (_bundle != null) { if (_bundle != null) {
@@ -140,12 +140,13 @@ public class MessageManager
// if the resource bundle contains a special resource, we'll interpret that as a derivation // if the resource bundle contains a special resource, we'll interpret that as a derivation
// of MessageBundle to instantiate for handling that class // of MessageBundle to instantiate for handling that class
MessageBundle customBundle = null;
if (rbundle != null) { if (rbundle != null) {
String mbclass = null; String mbclass = null;
try { try {
mbclass = rbundle.getString(MBUNDLE_CLASS_KEY).trim(); mbclass = rbundle.getString(MBUNDLE_CLASS_KEY).trim();
if (!StringUtil.isBlank(mbclass)) { if (!StringUtil.isBlank(mbclass)) {
bundle = (MessageBundle)Class.forName(mbclass).newInstance(); customBundle = (MessageBundle)Class.forName(mbclass).newInstance();
} }
} catch (MissingResourceException mre) { } catch (MissingResourceException mre) {
@@ -157,19 +158,38 @@ public class MessageManager
} }
} }
// if there was no custom class, or we failed to instantiate the custom class, use a
// standard message bundle
if (bundle == null) {
bundle = new MessageBundle();
}
// initialize our message bundle, cache it and return it (if we couldn't resolve the // initialize our message bundle, cache it and return it (if we couldn't resolve the
// bundle, the message bundle will cope with its null resource bundle) // bundle, the message bundle will cope with its null resource bundle)
bundle.init(this, path, rbundle, _global); bundle = createBundle(path, rbundle, customBundle);
_cache.put(path, bundle); _cache.put(path, bundle);
return bundle; return bundle;
} }
/**
* Returns the bundle to use for the given path and resource bundle. If customBundle is
* non-null, it's an instance of the bundle class specified by the bundle itself and should be
* used as part of the created bundle.
*/
protected MessageBundle createBundle (String path, ResourceBundle rbundle,
MessageBundle customBundle)
{
// if there was no custom class, or we failed to instantiate the custom class, use a
// standard message bundle
if (customBundle == null) {
customBundle = new MessageBundle();
}
initBundle(customBundle, path, rbundle);
return customBundle;
}
/**
* Initializes the given bundle with this manager and the given path and resource bundle.
*/
protected void initBundle (MessageBundle bundle, String path, ResourceBundle rbundle)
{
bundle.init(this, path, rbundle, _global);
}
/** /**
* Loads a bundle from the given path, or returns null if it can't be found. * Loads a bundle from the given path, or returns null if it can't be found.
*/ */