001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * Messages.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.util.i18n;
030:
031: import java.util.Locale;
032: import java.util.MissingResourceException;
033: import java.util.ResourceBundle;
034:
035: import org.jfree.util.ResourceBundleSupport;
036:
037: /**
038: * A helper class for a simplified resource-bundle access. This class simply ignores all resource-bundle related errors
039: * and prints place-holder strings if a localization key cannot be found.
040: *
041: * @author David Kincade
042: */
043: public class Messages extends ResourceBundleSupport {
044: /**
045: * Creates a new Messages-collection. The locale and baseName will be used to create the resource-bundle that backs up
046: * this implementation.
047: *
048: * @param locale the locale.
049: * @param baseName the baseName of the resource-bundle.
050: * @see java.util.ResourceBundle#getBundle(String,java.util.Locale)
051: */
052: public Messages(final Locale locale, final String baseName) {
053: super (locale, baseName);
054: }
055:
056: /**
057: * Creates a new Messages-collection. The locale and baseName will be used to create the resource-bundle that backs up
058: * this implementation.
059: *
060: * @param locale the locale.
061: * @param baseName the baseName of the resource-bundle.
062: * @param resourceBundle a predefined resource-bundle.
063: */
064: public Messages(final Locale locale,
065: final ResourceBundle resourceBundle, final String baseName) {
066: super (locale, resourceBundle, baseName);
067: }
068:
069: /**
070: * Creates a new Messages-collection. The locale and baseName will be used to create the resource-bundle that backs up
071: * this implementation.
072: *
073: * @param locale the locale.
074: * @param resourceBundle a predefined resource-bundle.
075: */
076: public Messages(final Locale locale,
077: final ResourceBundle resourceBundle) {
078: super (locale, resourceBundle);
079: }
080:
081: /**
082: * Creates a new Messages-collection. The default locale and baseName will be used to create the resource-bundle that
083: * backs up this implementation.
084: *
085: * @param baseName the baseName of the resource-bundle.
086: */
087: public Messages(final String baseName) {
088: super (baseName);
089: }
090:
091: /**
092: * Creates a new Messages-collection. The default locale and baseName will be used to create the resource-bundle that
093: * backs up this implementation.
094: *
095: * @param baseName the baseName of the resource-bundle.
096: * @param resourceBundle a predefined resource-bundle.
097: */
098: public Messages(final ResourceBundle resourceBundle,
099: final String baseName) {
100: super (resourceBundle, baseName);
101: }
102:
103: /**
104: * Creates a new Messages-collection. The default locale and baseName will be used to create the resource-bundle that
105: * backs up this implementation.
106: *
107: * @param resourceBundle a predefined resource-bundle.
108: */
109: public Messages(final ResourceBundle resourceBundle) {
110: super (resourceBundle);
111: }
112:
113: /**
114: * Gets a string for the given key from this resource bundle or one of its parents. If the key is a link, the link is
115: * resolved and the referenced string is returned instead. If the given key cannot be resolved, no exception will be
116: * thrown and a generic placeholder is used instead.
117: *
118: * @param key the key for the desired string
119: * @return the string for the given key
120: * @throws NullPointerException if <code>key</code> is <code>null</code>
121: * @throws MissingResourceException if no object for the given key can be found
122: */
123: public synchronized String getString(final String key) {
124: try {
125: return super .getString(key);
126: } catch (MissingResourceException e) {
127: return '!' + key + '!';
128: }
129: }
130:
131: /**
132: * Formats the message stored in the resource bundle (using a MessageFormat).
133: *
134: * @param key the resourcebundle key
135: * @param param1 the parameter for the message
136: * @return the formated string
137: */
138: public String getString(final String key, final String param1) {
139: try {
140: return formatMessage(key, param1);
141: } catch (MissingResourceException e) {
142: return '!' + key + '!';
143: }
144: }
145:
146: /**
147: * Formats the message stored in the resource bundle (using a MessageFormat).
148: *
149: * @param key the resourcebundle key
150: * @param param1 the parameter for the message
151: * @param param2 the parameter for the message
152: * @return the formated string
153: */
154: public String getString(final String key, final String param1,
155: final String param2) {
156: try {
157: return formatMessage(key, param1, param2);
158: } catch (MissingResourceException e) {
159: return '!' + key + '!';
160: }
161: }
162:
163: /**
164: * Formats the message stored in the resource bundle (using a MessageFormat).
165: *
166: * @param key the resourcebundle key
167: * @param param1 the parameter for the message
168: * @param param2 the parameter for the message
169: * @param param3 the parameter for the message
170: * @return the formated string
171: */
172: public String getString(final String key, final String param1,
173: final String param2, final String param3) {
174: try {
175: return formatMessage(key, new Object[] { param1, param2,
176: param3 });
177: } catch (MissingResourceException e) {
178: return '!' + key + '!';
179: }
180: }
181:
182: /**
183: * Formats the message stored in the resource bundle (using a MessageFormat).
184: *
185: * @param key the resourcebundle key
186: * @param param1 the parameter for the message
187: * @param param2 the parameter for the message
188: * @param param3 the parameter for the message
189: * @param param4 the parameter for the message
190: * @return the formated string
191: */
192: public String getString(final String key, final String param1,
193: final String param2, final String param3,
194: final String param4) {
195: try {
196: return formatMessage(key, new Object[] { param1, param2,
197: param3, param4 });
198: } catch (MissingResourceException e) {
199: return '!' + key + '!';
200: }
201: }
202:
203: /**
204: * Get a formatted error message. The message consists of two parts. The first part is the error numeric Id associated
205: * with the key used to identify the message in the resource file. For instance, suppose the error key is
206: * MyClass.ERROR_0069_DONKEY_PUNCH. The first part of the error msg would be "0069". The second part of the returned
207: * string is simply the <code>msg</code> parameter.
208: * <p/>
209: * Currently the format is: error key - error msg For instance: "0069 - You were punched by the donkey."
210: *
211: * @param key String containing the key that was used to obtain the <code>msg</code> parameter from the resource
212: * file.
213: * @param msg String containing the message that was obtained from the resource file using the <code>key</code>
214: * parameter.
215: * @return String containing the formatted error message.
216: */
217: public String formatErrorMessage(final String key, final String msg) {
218: try {
219: final int end;
220: final int errorMarker = key.indexOf(".ERROR_");
221: if (errorMarker < 0) {
222: end = key.length();
223: } else {
224: end = Math
225: .min(
226: errorMarker + ".ERROR_0000".length(), key.length()); //$NON-NLS-1$
227: }
228: return getString(
229: "MESSUTIL.ERROR_FORMAT_MASK", key.substring(0, end), msg); //$NON-NLS-1$
230: } catch (Exception e) {
231: return "!MESSUTIL.ERROR_FORMAT_MASK:" + key + '!';
232: }
233: }
234:
235: /**
236: * Get a formatted error message from the resource-bundle. The message consists of two parts. The first part is the
237: * error numeric Id associated with the key used to identify the message in the resource file. For instance, suppose
238: * the error key is MyClass.ERROR_0069_DONKEY_PUNCH. The first part of the error msg would be "0069". The second part
239: * of the returned string is simply the <code>msg</code> parameter.
240: * <p/>
241: * Currently the format is: error key - error msg For instance: "0069 - You were punched by the donkey."
242: *
243: * @param key String containing the key that was used to obtain the <code>msg</code> parameter from the resource
244: * file.
245: * @return String containing the formatted error message.
246: */
247: public String getErrorString(final String key) {
248: return formatErrorMessage(key, getString(key));
249: }
250:
251: /**
252: * Get a parametrized formatted error message from the resource-bundle. The message consists of two parts. The first
253: * part is the error numeric Id associated with the key used to identify the message in the resource file. For
254: * instance, suppose the error key is MyClass.ERROR_0069_DONKEY_PUNCH. The first part of the error msg would be
255: * "0069". The second part of the returned string is simply the <code>msg</code> parameter.
256: * <p/>
257: * Currently the format is: error key - error msg For instance: "0069 - You were punched by the donkey."
258: *
259: * @param key String containing the key that was used to obtain the <code>msg</code> parameter from the resource
260: * file.
261: * @param param1 the parameter for the message
262: * @return String containing the formatted error message.
263: */
264: public String getErrorString(final String key, final String param1) {
265: return formatErrorMessage(key, getString(key, param1));
266: }
267:
268: /**
269: * Get a parametrized formatted error message from the resource-bundle. The message consists of two parts. The first
270: * part is the error numeric Id associated with the key used to identify the message in the resource file. For
271: * instance, suppose the error key is MyClass.ERROR_0069_DONKEY_PUNCH. The first part of the error msg would be
272: * "0069". The second part of the returned string is simply the <code>msg</code> parameter.
273: * <p/>
274: * Currently the format is: error key - error msg For instance: "0069 - You were punched by the donkey."
275: *
276: * @param key String containing the key that was used to obtain the <code>msg</code> parameter from the resource
277: * file.
278: * @param param1 the parameter for the message
279: * @param param2 the parameter for the message
280: * @return String containing the formatted error message.
281: */
282: public String getErrorString(final String key, final String param1,
283: final String param2) {
284: return formatErrorMessage(key, getString(key, param1, param2));
285: }
286:
287: /**
288: * Get a parametrized formatted error message from the resource-bundle. The message consists of two parts. The first
289: * part is the error numeric Id associated with the key used to identify the message in the resource file. For
290: * instance, suppose the error key is MyClass.ERROR_0069_DONKEY_PUNCH. The first part of the error msg would be
291: * "0069". The second part of the returned string is simply the <code>msg</code> parameter.
292: * <p/>
293: * Currently the format is: error key - error msg For instance: "0069 - You were punched by the donkey."
294: *
295: * @param key String containing the key that was used to obtain the <code>msg</code> parameter from the resource
296: * file.
297: * @param param1 the parameter for the message
298: * @param param2 the parameter for the message
299: * @param param3 the parameter for the message
300: * @return String containing the formatted error message.
301: */
302: public String getErrorString(final String key, final String param1,
303: final String param2, final String param3) {
304: return formatErrorMessage(key, getString(key, param1, param2,
305: param3));
306: }
307:
308: }
|