001: package net.sourceforge.squirrel_sql.fw.util;
002:
003: /*
004: * Copyright (C) 2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.text.MessageFormat;
022: import java.util.Locale;
023: import java.util.MissingResourceException;
024: import java.util.ResourceBundle;
025: import java.net.URLClassLoader;
026: import java.net.URL;
027:
028: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
029: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
030:
031: /**
032: * This class defines i18nized strings. These strings are stored in a file
033: * with a base name I18NStrings.properties in each package directory.
034: *
035: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
036: */
037: public class StringManager {
038: /** Logger for this class. */
039: private static ILogger s_log = LoggerController
040: .createLogger(StringManager.class);
041:
042: /** Contains the localised strings. */
043: private ResourceBundle _rsrcBundle;
044: private String _bundleBaseName;
045: private URL[] _bundleLoaderUrLs = new URL[0];
046:
047: /**
048: * Ctor specifying the package name. Attempt to load a resource bundle
049: * from the package directory.
050: *
051: * @param packageName Name of package
052: * @param classLoader Class loader to use
053: */
054: StringManager(String packageName, ClassLoader loader) {
055: super ();
056: _bundleBaseName = packageName + ".I18NStrings";
057: _rsrcBundle = ResourceBundle.getBundle(_bundleBaseName, Locale
058: .getDefault(), loader);
059:
060: if (loader instanceof URLClassLoader) {
061: _bundleLoaderUrLs = ((URLClassLoader) loader).getURLs();
062: }
063:
064: }
065:
066: /**
067: * Retrieve the localized string for the passed key. If it isn't found
068: * an error message is returned instead.
069: *
070: * @param key Key to retrieve string for.
071: *
072: * @return Localized string or error message.
073: *
074: * @throws IllegalArgumentException
075: * Thrown if <TT>null</TT> <TT>key</TT> passed.
076: */
077: public String getString(String key) {
078: if (key == null) {
079: throw new IllegalArgumentException("key == null");
080: }
081:
082: try {
083: return _rsrcBundle.getString(key);
084: } catch (MissingResourceException ex) {
085: StringBuffer sb = new StringBuffer();
086: sb.append("No resource string found for key '" + key
087: + "' in bundle " + _bundleBaseName + "\n\n");
088:
089: if (0 < _bundleLoaderUrLs.length) {
090: sb
091: .append("The following classpath entries are available to the bundle loader:\n");
092: for (int i = 0; i < _bundleLoaderUrLs.length; i++) {
093: sb.append(_bundleLoaderUrLs[i]).append("\n");
094: }
095: }
096: s_log.error(sb.toString());
097: return "No resource found for key " + key;
098: }
099: }
100:
101: /**
102: * Retrieve the localized string for the passed key and format it with the
103: * passed arguments.
104: *
105: * @param key Key to retrieve string for.
106: * @param args Any string arguments that should be used as values to
107: * parameters found in the localized string.
108: *
109: * @return Localized string or error message.
110: *
111: * @throws IllegalArgumentException
112: * Thrown if <TT>null</TT> <TT>key</TT> passed.
113: */
114: public String getString(String key, String[] args) {
115: return getString(key, (Object[]) args);
116: }
117:
118: /**
119: * Retrieve the localized string for the passed key and format it with the
120: * passed arguments.
121: *
122: * @param key Key to retrieve string for.
123: * @param args Any string arguments that should be used as values to
124: * parameters found in the localized string.
125: *
126: * @return Localized string or error message.
127: *
128: * @throws IllegalArgumentException
129: * Thrown if <TT>null</TT> <TT>key</TT> passed.
130: */
131: public String getString(String key, Object... args) {
132: if (key == null) {
133: throw new IllegalArgumentException("key == null");
134: }
135:
136: if (args == null) {
137: args = new Object[0];
138: }
139:
140: final String str = getString(key);
141: try {
142: return MessageFormat.format(str, args);
143: } catch (IllegalArgumentException ex) {
144: String msg = "Error formatting i18 string. Key is '" + key
145: + "'";
146: s_log.error(msg, ex);
147: return msg + ": " + ex.toString();
148: }
149: }
150: }
|