View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.jdo.impl.enhancer.util;
19  
20  import java.util.ResourceBundle;
21  import java.util.Locale;
22  import java.util.Hashtable;
23  import java.text.MessageFormat;
24  
25  
26  
27  class I18NHelper
28  {
29      private static Hashtable	bundles = new Hashtable();
30      private static Locale 	locale = Locale.getDefault();
31  
32      /***
33       * Constructor
34       */
35      public I18NHelper()
36      {}
37  
38      /***
39       * Load ResourceBundle by bundle name
40       */
41      public static ResourceBundle loadBundle(String bundleName)
42      {
43          ResourceBundle messages = (ResourceBundle)bundles.get(bundleName);
44  
45          if (messages == null) //not found as loaded - add
46          {
47              messages = ResourceBundle.getBundle(bundleName, locale);
48              bundles.put(bundleName, messages);
49          }
50          return messages;
51      }
52  
53    
54      /***
55       * Returns message as String
56       */
57      final public static String getMessage(ResourceBundle messages, String messageKey) 
58      {
59          return messages.getString(messageKey);
60      }
61  
62      /***
63       * Formats message by adding Array of arguments
64       */
65      final public static String getMessage(ResourceBundle messages, String messageKey, Object msgArgs[]) 
66      {
67          for (int i=0; i<msgArgs.length; i++) {
68              if (msgArgs[i] == null) msgArgs[i] = ""; // NOI18N
69          }
70          MessageFormat formatter = new MessageFormat(messages.getString(messageKey));
71          return formatter.format(msgArgs);
72      }
73      /***
74       * Formats message by adding a String argument
75       */
76      final public static String getMessage(ResourceBundle messages, String messageKey, String arg) 
77      {
78          Object []args = {arg};
79          return getMessage(messages, messageKey, args);
80      }
81      /***
82       * Formats message by adding two String arguments
83       */
84      final public static String getMessage(ResourceBundle messages, String messageKey, String arg1,
85                                            String arg2) 
86      {
87          Object []args = {arg1, arg2};
88          return getMessage(messages, messageKey, args);
89      }
90      /***
91       * Formats message by adding three String arguments
92       */
93      final public static String getMessage(ResourceBundle messages, String messageKey, String arg1,
94                                            String arg2, String arg3) 
95      {
96          Object []args = {arg1, arg2, arg3};
97          return getMessage(messages, messageKey, args);
98      }
99      /***
100      *
101      * Formats message by adding an Object as an argument
102      */
103     final public static String getMessage(ResourceBundle messages, String messageKey, Object arg) 
104     {
105         Object []args = {arg};
106         return getMessage(messages, messageKey, args);
107     }
108     /***
109      * Formats message by adding an int as an argument
110      */
111     final public static String getMessage(ResourceBundle messages, String messageKey, int arg) 
112     {
113         Object []args = {new Integer(arg)};
114         return getMessage(messages, messageKey, args);
115     }
116     /***
117      * Formats message by adding a boolean as an argument
118      */
119     final public static String getMessage(ResourceBundle messages, String messageKey, boolean arg) 
120     {
121         Object []args = {String.valueOf(arg)};
122         return getMessage(messages, messageKey, args);
123     }
124 }
125 
126 
127 /***
128  * Basic support for enhancer implementation.
129  */
130 public class Support
131     extends Assertion
132 {
133     //^olsen: hack
134     static public Timer timer = new Timer();
135 
136     /***
137      * I18N message handler
138      */
139     static private ResourceBundle MESSAGES;
140 
141 
142     /***
143      *
144      */
145     static
146     {
147         try
148         {
149             MESSAGES = I18NHelper.loadBundle("org.apache.jdo.impl.enhancer.Bundle");
150         }
151         catch (java.util.MissingResourceException ex)
152         {
153             ex.printStackTrace ();
154         }
155     }
156 
157     /***
158      * Returns the I18N message.
159      */
160     static protected final String getI18N(String key)
161     {
162         return I18NHelper.getMessage(MESSAGES, key);
163     }
164 
165     /***
166      * Returns the I18N message.
167      */
168     static protected final String getI18N(String key,
169                                           String arg)
170     {
171         return I18NHelper.getMessage(MESSAGES, key, arg);
172     }
173 
174     /***
175      * Returns the I18N message.
176      */
177     static protected final String getI18N(String key,
178                                           String arg1,
179                                           String arg2)
180     {
181         return I18NHelper.getMessage(MESSAGES, key, arg1, arg2);
182     }
183 
184     /***
185      * Returns the I18N message.
186      */
187     static protected final String getI18N(String key,
188                                           String arg1,
189                                           String arg2,
190                                           String arg3)
191     {
192         return I18NHelper.getMessage(MESSAGES, key, arg1, arg2, arg3);
193     }
194 
195     /***
196      * Returns the I18N message.
197      */
198     static protected final String getI18N(String key,
199                                           int arg1,
200                                           String arg2)
201     {
202         return I18NHelper.getMessage(MESSAGES, key,
203                                      new Object[]{new Integer(arg1), arg2});
204     }
205 
206     /***
207      * Returns the I18N message.
208      */
209     static protected final String getI18N(String key,
210                                           Object[] args)
211     {
212         return I18NHelper.getMessage(MESSAGES, key, args);
213     }
214 }