001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Jul 13, 2005
014: * @author Marc Batchelor
015: *
016: */
017:
018: package org.pentaho.messages;
019:
020: import java.text.MessageFormat;
021: import java.util.ResourceBundle;
022:
023: public class MessageUtil {
024:
025: /**
026: * Get a formatted error message. The message consists of two parts. The first part is the
027: * error numeric Id associated with the key used to identify the message in the resource file.
028: * For instance, suppose the error key is MyClass.ERROR_0068_TEST_ERROR. The first
029: * part of the error msg would be "0068". The second part of the returned string
030: * is simply the <code>msg</code> parameter.
031: *
032: * Currently the format is:
033: * error key - error msg
034: * For instance:
035: * "0068 - A test error message."
036: *
037: * @param key String containing the key that was used to obtain the <code>msg</code> parameter
038: * from the resource file.
039: * @param msg String containing the message that was obtained from the resource file using
040: * the <code>key</code> parameter.
041: * @return String containing the formatted error message.
042: */
043: public static String formatErrorMessage(String key, String msg) {
044: int end = key.indexOf(".ERROR_"); //$NON-NLS-1$
045: end = (end < 0) ? key.length() : Math.min(end
046: + ".ERROR_0000".length(), key.length()); //$NON-NLS-1$
047: return Messages
048: .getString(
049: "MESSUTIL.ERROR_FORMAT_MASK", key.substring(0, end), msg); //$NON-NLS-1$
050: }
051:
052: /**
053: * Get the message from the specified resource bundle using the specified key.
054: *
055: * @param bundle ResourceBundle containing the desired String
056: * @param key String containing the key to locate the desired String in the ResourceBundle.
057: * @return String containing the message from the specified resource bundle accessed
058: * using the specified key
059: */
060: public static String getString(ResourceBundle bundle, String key) {
061: try {
062: return bundle.getString(key);
063: } catch (Exception e) {
064: return '!' + key + '!';
065: }
066: }
067:
068: /**
069: * Get a message from the specified resource bundle using the specified key,
070: * and format it. see <code>formatErrorMessage</code> for details on how the
071: * message is formatted.
072: *
073: * @param bundle ResourceBundle containing the desired String
074: * @param key String containing the key to locate the desired String in the ResourceBundle.
075: * @return String containing the formatted message.
076: */
077: public static String getErrorString(ResourceBundle bundle,
078: String key) {
079: return formatErrorMessage(key, getString(bundle, key));
080: }
081:
082: public static String getString(ResourceBundle bundle, String key,
083: String param1) {
084: try {
085: Object[] args = { param1 };
086: return MessageFormat.format(bundle.getString(key), args);
087: } catch (Exception e) {
088: return '!' + key + '!';
089: }
090: }
091:
092: public static String getErrorString(ResourceBundle bundle,
093: String key, String param1) {
094: return formatErrorMessage(key, getString(bundle, key, param1));
095: }
096:
097: public static String getString(ResourceBundle bundle, String key,
098: String param1, String param2) {
099: try {
100: Object[] args = { param1, param2 };
101: return MessageFormat.format(bundle.getString(key), args);
102: } catch (Exception e) {
103: return '!' + key + '!';
104: }
105: }
106:
107: public static String getErrorString(ResourceBundle bundle,
108: String key, String param1, String param2) {
109: return formatErrorMessage(key, getString(bundle, key, param1,
110: param2));
111: }
112:
113: public static String getString(ResourceBundle bundle, String key,
114: String param1, String param2, String param3) {
115: try {
116: Object[] args = { param1, param2, param3 };
117: return MessageFormat.format(bundle.getString(key), args);
118: } catch (Exception e) {
119: return '!' + key + '!';
120: }
121: }
122:
123: public static String getErrorString(ResourceBundle bundle,
124: String key, String param1, String param2, String param3) {
125: return formatErrorMessage(key, getString(bundle, key, param1,
126: param2, param3));
127: }
128:
129: public static String getString(ResourceBundle bundle, String key,
130: String param1, String param2, String param3, String param4) {
131: try {
132: Object[] args = { param1, param2, param3, param4 };
133: return MessageFormat.format(bundle.getString(key), args);
134: } catch (Exception e) {
135: return '!' + key + '!';
136: }
137: }
138:
139: public static String getErrorString(ResourceBundle bundle,
140: String key, String param1, String param2, String param3,
141: String param4) {
142: return formatErrorMessage(key, getString(bundle, key, param1,
143: param2, param3, param4));
144: }
145:
146: public static String formatMessage(String pattern, String param1) {
147: try {
148: Object[] args = { param1 };
149: return MessageFormat.format(pattern, args);
150: } catch (Exception e) {
151: return '!' + pattern + '!';
152: }
153: }
154:
155: public static String formatMessage(String pattern, String param1,
156: String param2) {
157: try {
158: Object[] args = { param1, param2 };
159: return MessageFormat.format(pattern, args);
160: } catch (Exception e) {
161: return '!' + pattern + '!';
162: }
163:
164: }
165:
166: public static String formatMessage(String pattern, String param1,
167: String param2, String param3) {
168: try {
169: Object[] args = { param1, param2, param3 };
170: return MessageFormat.format(pattern, args);
171: } catch (Exception e) {
172: return '!' + pattern + '!';
173: }
174: }
175:
176: public static String formatMessage(String pattern, String param1,
177: String param2, String param3, String param4) {
178: try {
179: Object[] args = { param1, param2, param3, param4 };
180: return MessageFormat.format(pattern, args);
181: } catch (Exception e) {
182: return '!' + pattern + '!';
183: }
184: }
185: }
|