001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.generation.start;
018:
019: import java.util.ResourceBundle;
020: import java.util.Locale;
021: import java.util.Hashtable;
022: import java.text.MessageFormat;
023:
024: class I18NHelper {
025: private static Hashtable bundles = new Hashtable();
026: private static Locale locale = Locale.getDefault();
027:
028: /**
029: * Constructor
030: */
031: public I18NHelper() {
032: }
033:
034: /**
035: * Load ResourceBundle by bundle name
036: */
037: public static ResourceBundle loadBundle(String bundleName) {
038: ResourceBundle messages = (ResourceBundle) bundles
039: .get(bundleName);
040:
041: if (messages == null) //not found as loaded - add
042: {
043: messages = ResourceBundle.getBundle(bundleName, locale);
044: bundles.put(bundleName, messages);
045: }
046: return messages;
047: }
048:
049: /**
050: * Returns message as String
051: */
052: final public static String getMessage(ResourceBundle messages,
053: String messageKey) {
054: return messages.getString(messageKey);
055: }
056:
057: /**
058: * Formats message by adding Array of arguments
059: */
060: final public static String getMessage(ResourceBundle messages,
061: String messageKey, Object msgArgs[]) {
062: for (int i = 0; i < msgArgs.length; i++) {
063: if (msgArgs[i] == null)
064: msgArgs[i] = ""; // NOI18N
065: }
066: MessageFormat formatter = new MessageFormat(messages
067: .getString(messageKey));
068: return formatter.format(msgArgs);
069: }
070:
071: /**
072: * Formats message by adding a String argument
073: */
074: final public static String getMessage(ResourceBundle messages,
075: String messageKey, String arg) {
076: Object[] args = { arg };
077: return getMessage(messages, messageKey, args);
078: }
079:
080: /**
081: * Formats message by adding two String arguments
082: */
083: final public static String getMessage(ResourceBundle messages,
084: String messageKey, String arg1, String arg2) {
085: Object[] args = { arg1, arg2 };
086: return getMessage(messages, messageKey, args);
087: }
088:
089: /**
090: * Formats message by adding three String arguments
091: */
092: final public static String getMessage(ResourceBundle messages,
093: String messageKey, String arg1, String arg2, String arg3) {
094: Object[] args = { arg1, arg2, arg3 };
095: return getMessage(messages, messageKey, args);
096: }
097:
098: /**
099: *
100: * Formats message by adding an Object as an argument
101: */
102: final public static String getMessage(ResourceBundle messages,
103: String messageKey, Object arg) {
104: Object[] args = { arg };
105: return getMessage(messages, messageKey, args);
106: }
107:
108: /**
109: * Formats message by adding an int as an argument
110: */
111: final public static String getMessage(ResourceBundle messages,
112: String messageKey, int arg) {
113: Object[] args = { new Integer(arg) };
114: return getMessage(messages, messageKey, args);
115: }
116:
117: /**
118: * Formats message by adding a boolean as an argument
119: */
120: final public static String getMessage(ResourceBundle messages,
121: String messageKey, boolean arg) {
122: Object[] args = { String.valueOf(arg) };
123: return getMessage(messages, messageKey, args);
124: }
125: }
126:
127: /**
128: * Basic support for enhancer implementation.
129: */
130: public class Support extends Assertion {
131: //^olsen: hack
132: static public Timer timer = new Timer();
133:
134: /**
135: * I18N message handler
136: */
137: static private ResourceBundle MESSAGES;
138:
139: /**
140: *
141: */
142: static {
143: try {
144: MESSAGES = I18NHelper
145: .loadBundle("org.objectweb.speedo.generation.start.Bundle");
146: } catch (java.util.MissingResourceException ex) {
147: ex.printStackTrace();
148: }
149: }
150:
151: /**
152: * Returns the I18N message.
153: */
154: static protected final String getI18N(String key) {
155: return I18NHelper.getMessage(MESSAGES, key);
156: }
157:
158: /**
159: * Returns the I18N message.
160: */
161: static protected final String getI18N(String key, String arg) {
162: return I18NHelper.getMessage(MESSAGES, key, arg);
163: }
164:
165: /**
166: * Returns the I18N message.
167: */
168: static protected final String getI18N(String key, String arg1,
169: String arg2) {
170: return I18NHelper.getMessage(MESSAGES, key, arg1, arg2);
171: }
172:
173: /**
174: * Returns the I18N message.
175: */
176: static protected final String getI18N(String key, String arg1,
177: String arg2, String arg3) {
178: return I18NHelper.getMessage(MESSAGES, key, arg1, arg2, arg3);
179: }
180:
181: /**
182: * Returns the I18N message.
183: */
184: static protected final String getI18N(String key, int arg1,
185: String arg2) {
186: return I18NHelper.getMessage(MESSAGES, key, new Object[] {
187: new Integer(arg1), arg2 });
188: }
189:
190: /**
191: * Returns the I18N message.
192: */
193: static protected final String getI18N(String key, Object[] args) {
194: return I18NHelper.getMessage(MESSAGES, key, args);
195: }
196: }
|