View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software 
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License.
16   */
17  
18  
19  package org.apache.jdo.impl.enhancer.util;
20  
21  import java.util.ResourceBundle;
22  import java.util.Locale;
23  import java.util.Hashtable;
24  import java.text.MessageFormat;
25  
26  
27  
28  class I18NHelper
29  {
30      private static Hashtable	bundles = new Hashtable();
31      private static Locale 	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 loadBundle(String bundleName)
43      {
44          ResourceBundle messages = (ResourceBundle)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 getMessage(ResourceBundle messages, String messageKey) 
59      {
60          return messages.getString(messageKey);
61      }
62  
63      /***
64       * Formats message by adding Array of arguments
65       */
66      final public static String getMessage(ResourceBundle messages, String messageKey, Object msgArgs[]) 
67      {
68          for (int i=0; i<msgArgs.length; i++) {
69              if (msgArgs[i] == null) msgArgs[i] = ""; // NOI18N
70          }
71          MessageFormat formatter = new MessageFormat(messages.getString(messageKey));
72          return formatter.format(msgArgs);
73      }
74      /***
75       * Formats message by adding a String argument
76       */
77      final public static String getMessage(ResourceBundle messages, String messageKey, String arg) 
78      {
79          Object []args = {arg};
80          return getMessage(messages, messageKey, args);
81      }
82      /***
83       * Formats message by adding two String arguments
84       */
85      final public static String getMessage(ResourceBundle messages, String messageKey, String arg1,
86                                            String arg2) 
87      {
88          Object []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 getMessage(ResourceBundle messages, String messageKey, String arg1,
95                                            String arg2, String arg3) 
96      {
97          Object []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 getMessage(ResourceBundle messages, String messageKey, Object arg) 
105     {
106         Object []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 getMessage(ResourceBundle messages, String messageKey, int arg) 
113     {
114         Object []args = {new Integer(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 getMessage(ResourceBundle messages, String messageKey, boolean arg) 
121     {
122         Object []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 MESSAGES;
141 
142 
143     /***
144      *
145      */
146     static
147     {
148         try
149         {
150             MESSAGES = I18NHelper.loadBundle("org.apache.jdo.impl.enhancer.Bundle");
151         }
152         catch (java.util.MissingResourceException ex)
153         {
154             ex.printStackTrace ();
155         }
156     }
157 
158     /***
159      * Returns the I18N message.
160      */
161     static protected final String getI18N(String key)
162     {
163         return I18NHelper.getMessage(MESSAGES, key);
164     }
165 
166     /***
167      * Returns the I18N message.
168      */
169     static protected final String getI18N(String key,
170                                           String arg)
171     {
172         return I18NHelper.getMessage(MESSAGES, key, arg);
173     }
174 
175     /***
176      * Returns the I18N message.
177      */
178     static protected final String getI18N(String key,
179                                           String arg1,
180                                           String arg2)
181     {
182         return I18NHelper.getMessage(MESSAGES, key, arg1, arg2);
183     }
184 
185     /***
186      * Returns the I18N message.
187      */
188     static protected final String getI18N(String key,
189                                           String arg1,
190                                           String arg2,
191                                           String 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 getI18N(String key,
200                                           int arg1,
201                                           String arg2)
202     {
203         return I18NHelper.getMessage(MESSAGES, key,
204                                      new Object[]{new Integer(arg1), arg2});
205     }
206 
207     /***
208      * Returns the I18N message.
209      */
210     static protected final String getI18N(String key,
211                                           Object[] args)
212     {
213         return I18NHelper.getMessage(MESSAGES, key, args);
214     }
215 }