001: /*
002: ** ResourceBundle Manager Utility class.
003: ** Authored in 1999 by Timothy Gerard Endres.
004: **
005: ** This Java Class source has been placed in the public domain.
006: **
007: */
008:
009: package com.ice.util;
010:
011: import java.text.MessageFormat;
012: import java.util.Hashtable;
013: import java.util.ResourceBundle;
014: import java.util.MissingResourceException;
015:
016: /**
017: * This class attempts to manage a set of ResourceBundles. For example,
018: * an application may have one set of resources for the GUI elements in
019: * the application, and another set of resources for context help, and
020: * yet another set for builtin scripts.
021: *
022: * Each ResourceManager instance is identified by an id. Class methods
023: * provided to allow quick access to any ResourceManager by id.
024: *
025: * ResourceManager, aside from manager multiple instances by id, also
026: * add an additional method beyond that provided by ResourceBundle -
027: * getFormat(). This method is like getString() in that is returns a
028: * String resource, however, getFormat() formats the resource using
029: * arguments passed to the method. This makes it easy to use resources
030: * for both constant strings and formatted strings.
031: *
032: * @version $Revision: 1.1 $
033: * @author Timothy Gerard Endres,
034: * <a href="mailto:time@ice.com">time@ice.com</a>.
035: */
036:
037: public class ResourceManager {
038: static public final String RCS_ID = "$Id: ResourceManager.java,v 1.1 2000/01/28 02:24:45 time Exp $";
039: static public final String RCS_REV = "$Revision: 1.1 $";
040:
041: /**
042: * The table of all ResourceManagers keyed by 'id'.
043: */
044: private static Hashtable bundles;
045:
046: /**
047: * Set to true to get processing debugging on stderr.
048: */
049: private boolean debug;
050:
051: /**
052: * The resource bundle's name. Used for easy identification.
053: */
054: private String name;
055:
056: /**
057: * The resource bundle.
058: */
059: private ResourceBundle rsrc;
060:
061: /**
062: * Initializes the class by instantiating the bundles Hashtable.
063: * Your application must call this class method only once, and
064: * before calling any other methods in this class.
065: */
066:
067: public static void initialize() {
068: ResourceManager.bundles = new Hashtable();
069: }
070:
071: /**
072: * Load a PropertyResourceBundle using the name, and add it to the
073: * bundles Hashtable keyed by id.
074: *
075: * @param id The id of the ResourceManager. This is used as the
076: * key into the bundles table.
077: * @param name The name of the ResourceManager. This is used to
078: * load the resource bundle.
079: */
080: public static void load(String id, String name) {
081: try {
082: ResourceBundle rsrc = ResourceBundle.getBundle(name);
083: ResourceManager rMgr = new ResourceManager(name, rsrc);
084: ResourceManager.bundles.put(id, rMgr);
085: } catch (MissingResourceException ex) {
086: ex.printStackTrace();
087: }
088: }
089:
090: /**
091: * Get a ResourceManager keyed by id.
092: *
093: * @param id The id of the ResourceManager to be returned.
094: * @return The ResourceManager identied by id.
095: */
096: public static ResourceManager get(String id) {
097: return (ResourceManager) ResourceManager.bundles.get(id);
098: }
099:
100: /**
101: * Put a ResourceManager into the bundles Hashtable, keyed by id.
102: *
103: * @param id The id used to identify this ResourceManager.
104: * @param rMgr The resource manager to be managed.
105: * @return The previous ResourceManager identied by id, or null.
106: */
107: public static ResourceManager put(String id, ResourceManager rMgr) {
108: return (ResourceManager) ResourceManager.bundles.put(id, rMgr);
109: }
110:
111: /**
112: * This constructor is not used.
113: */
114: private ResourceManager() {
115: }
116:
117: /**
118: * Construct a ResourceManager with the given name and ResourceBundle.
119: *
120: * @param name The (display) name of this resource bundle.
121: * @param rsrc The resource bundle to be managed.
122: */
123: public ResourceManager(String name, ResourceBundle rsrc)
124: throws MissingResourceException {
125: this .name = name;
126: this .rsrc = rsrc;
127: }
128:
129: /**
130: * Set the debugging flag for this ResourceManager. If debugging is
131: * set to true, debugging will be printed to System.err.
132: *
133: * @param debug The new debugging setting.
134: */
135: public void setDebug(boolean debug) {
136: this .debug = debug;
137: }
138:
139: /**
140: * Get a string resource from the ResourceBundle that this
141: * ResourceManager is managing.
142: *
143: * @param key The key of the resource to retrieve.
144: * @return The resource string.
145: */
146: public String getString(String key) {
147: return this .rsrc.getString(key);
148: }
149:
150: /**
151: * Format a string resource from the ResourceBundle that this
152: * ResourceManager is managing. The key is used to retrieve a
153: * resource that is the format, which is then formatted using
154: * the provided arguments.
155: *
156: * @param key The key of the resource that is the message format.
157: * @param args The arguments to be used to format the message.
158: * @return The formatted resource message.
159: */
160:
161: public String getFormat(String key, Object[] args) {
162: return MessageFormat.format(this.rsrc.getString(key), args);
163: }
164:
165: }
|