KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > start > Support


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19
20 package org.objectweb.speedo.generation.start;
21
22 import java.util.ResourceBundle JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26
27
28 class I18NHelper
29 {
30     private static Hashtable JavaDoc bundles = new Hashtable JavaDoc();
31     private static Locale JavaDoc locale = Locale.getDefault();
32
33     /**
34      * Constructor
35      */

36     public I18NHelper()
37     {}
38
39     /**
40      * Load ResourceBundle by bundle name
41      */

42     public static ResourceBundle JavaDoc loadBundle(String JavaDoc bundleName)
43     {
44         ResourceBundle JavaDoc messages = (ResourceBundle JavaDoc)bundles.get(bundleName);
45
46         if (messages == null) //not found as loaded - add
47
{
48             messages = ResourceBundle.getBundle(bundleName, locale);
49             bundles.put(bundleName, messages);
50         }
51         return messages;
52     }
53
54   
55     /**
56      * Returns message as String
57      */

58     final public static String JavaDoc getMessage(ResourceBundle JavaDoc messages, String JavaDoc messageKey)
59     {
60         return messages.getString(messageKey);
61     }
62
63     /**
64      * Formats message by adding Array of arguments
65      */

66     final public static String JavaDoc getMessage(ResourceBundle JavaDoc messages, String JavaDoc messageKey, Object JavaDoc msgArgs[])
67     {
68         for (int i=0; i<msgArgs.length; i++) {
69             if (msgArgs[i] == null) msgArgs[i] = ""; // NOI18N
70
}
71         MessageFormat JavaDoc formatter = new MessageFormat JavaDoc(messages.getString(messageKey));
72         return formatter.format(msgArgs);
73     }
74     /**
75      * Formats message by adding a String argument
76      */

77     final public static String JavaDoc getMessage(ResourceBundle JavaDoc messages, String JavaDoc messageKey, String JavaDoc arg)
78     {
79         Object JavaDoc []args = {arg};
80         return getMessage(messages, messageKey, args);
81     }
82     /**
83      * Formats message by adding two String arguments
84      */

85     final public static String JavaDoc getMessage(ResourceBundle JavaDoc messages, String JavaDoc messageKey, String JavaDoc arg1,
86                                           String JavaDoc arg2)
87     {
88         Object JavaDoc []args = {arg1, arg2};
89         return getMessage(messages, messageKey, args);
90     }
91     /**
92      * Formats message by adding three String arguments
93      */

94     final public static String JavaDoc getMessage(ResourceBundle JavaDoc messages, String JavaDoc messageKey, String JavaDoc arg1,
95                                           String JavaDoc arg2, String JavaDoc arg3)
96     {
97         Object JavaDoc []args = {arg1, arg2, arg3};
98         return getMessage(messages, messageKey, args);
99     }
100     /**
101      *
102      * Formats message by adding an Object as an argument
103      */

104     final public static String JavaDoc getMessage(ResourceBundle JavaDoc messages, String JavaDoc messageKey, Object JavaDoc arg)
105     {
106         Object JavaDoc []args = {arg};
107         return getMessage(messages, messageKey, args);
108     }
109     /**
110      * Formats message by adding an int as an argument
111      */

112     final public static String JavaDoc getMessage(ResourceBundle JavaDoc messages, String JavaDoc messageKey, int arg)
113     {
114         Object JavaDoc []args = {new Integer JavaDoc(arg)};
115         return getMessage(messages, messageKey, args);
116     }
117     /**
118      * Formats message by adding a boolean as an argument
119      */

120     final public static String JavaDoc getMessage(ResourceBundle JavaDoc messages, String JavaDoc messageKey, boolean arg)
121     {
122         Object JavaDoc []args = {String.valueOf(arg)};
123         return getMessage(messages, messageKey, args);
124     }
125 }
126
127
128 /**
129  * Basic support for enhancer implementation.
130  */

131 public class Support
132     extends Assertion
133 {
134     //^olsen: hack
135
static public Timer timer = new Timer();
136
137     /**
138      * I18N message handler
139      */

140     static private ResourceBundle JavaDoc MESSAGES;
141
142
143     /**
144      *
145      */

146     static
147     {
148         try
149         {
150             MESSAGES = I18NHelper.loadBundle("org.objectweb.speedo.generation.start.Bundle");
151         }
152         catch (java.util.MissingResourceException JavaDoc ex)
153         {
154             ex.printStackTrace ();
155         }
156     }
157
158     /**
159      * Returns the I18N message.
160      */

161     static protected final String JavaDoc getI18N(String JavaDoc key)
162     {
163         return I18NHelper.getMessage(MESSAGES, key);
164     }
165
166     /**
167      * Returns the I18N message.
168      */

169     static protected final String JavaDoc getI18N(String JavaDoc key,
170                                           String JavaDoc arg)
171     {
172         return I18NHelper.getMessage(MESSAGES, key, arg);
173     }
174
175     /**
176      * Returns the I18N message.
177      */

178     static protected final String JavaDoc getI18N(String JavaDoc key,
179                                           String JavaDoc arg1,
180                                           String JavaDoc arg2)
181     {
182         return I18NHelper.getMessage(MESSAGES, key, arg1, arg2);
183     }
184
185     /**
186      * Returns the I18N message.
187      */

188     static protected final String JavaDoc getI18N(String JavaDoc key,
189                                           String JavaDoc arg1,
190                                           String JavaDoc arg2,
191                                           String JavaDoc arg3)
192     {
193         return I18NHelper.getMessage(MESSAGES, key, arg1, arg2, arg3);
194     }
195
196     /**
197      * Returns the I18N message.
198      */

199     static protected final String JavaDoc getI18N(String JavaDoc key,
200                                           int arg1,
201                                           String JavaDoc arg2)
202     {
203         return I18NHelper.getMessage(MESSAGES, key,
204                                      new Object JavaDoc[]{new Integer JavaDoc(arg1), arg2});
205     }
206
207     /**
208      * Returns the I18N message.
209      */

210     static protected final String JavaDoc getI18N(String JavaDoc key,
211                                           Object JavaDoc[] args)
212     {
213         return I18NHelper.getMessage(MESSAGES, key, args);
214     }
215 }
216
Popular Tags