001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.i18n;
021:
022: import java.util.HashMap;
023: import java.util.Locale;
024: import java.util.MissingResourceException;
025: import java.util.ResourceBundle;
026:
027: public class Messages {
028: private static final Class this Class = Messages.class;
029:
030: private static final String projectName = MessagesConstants.projectName;
031:
032: private static final String resourceName = MessagesConstants.resourceName;
033: private static final Locale locale = MessagesConstants.locale;
034:
035: public static final String DEFAULT_MESSAGE_BUNDLE_KEY = "default";
036: private static final String NO_MESSAGE_BUNDLE = "Message Bundle is not available";
037:
038: private static final String packageName = getPackage(this Class
039: .getName());
040: private static final ClassLoader classLoader = this Class
041: .getClassLoader();
042:
043: private static final ResourceBundle parent = (MessagesConstants.rootPackageName
044: .equals(packageName)) ? null : MessagesConstants.rootBundle;
045:
046: private static HashMap messageBundleMap = new HashMap();
047:
048: static {
049: MessageBundle defaultMessageBundle = new MessageBundle(
050: projectName, packageName, resourceName, locale,
051: classLoader, parent);
052: addMessageBundle(DEFAULT_MESSAGE_BUNDLE_KEY,
053: defaultMessageBundle);
054: }
055:
056: /**
057: * To add a new Message Bundle to the MessageBundle list.
058: *
059: * @param messageBundleKey The key which will be used to refer to this message bundle later.
060: * @param messageBundle The message bundle.
061: */
062: public static void addMessageBundle(String messageBundleKey,
063: MessageBundle messageBundle) {
064: messageBundleMap.put(messageBundleKey, messageBundle);
065: }
066:
067: /**
068: * Get a message from resource.properties from the package of the given object.
069: *
070: * @param key The resource key
071: * @return The formatted message
072: */
073: public static String getMessage(String key)
074: throws MissingResourceException {
075: MessageBundle messageBundle = getMessageBundle(DEFAULT_MESSAGE_BUNDLE_KEY);
076: return messageBundle.getMessage(key);
077: }
078:
079: /**
080: * Get a message from resource.properties from the package of the given object.
081: *
082: * @param key The resource key
083: * @param arg0 The argument to place in variable {0}
084: * @return The formatted message
085: */
086: public static String getMessage(String key, String arg0)
087: throws MissingResourceException {
088: MessageBundle messageBundle = getMessageBundle(DEFAULT_MESSAGE_BUNDLE_KEY);
089: return messageBundle.getMessage(key, arg0);
090: }
091:
092: /**
093: * Get a message from resource.properties from the package of the given object.
094: *
095: * @param key The resource key
096: * @param arg0 The argument to place in variable {0}
097: * @param arg1 The argument to place in variable {1}
098: * @return The formatted message
099: */
100: public static String getMessage(String key, String arg0, String arg1)
101: throws MissingResourceException {
102: MessageBundle messageBundle = getMessageBundle(DEFAULT_MESSAGE_BUNDLE_KEY);
103: return messageBundle.getMessage(key, arg0, arg1);
104: }
105:
106: /**
107: * Get a message from resource.properties from the package of the given object.
108: *
109: * @param key The resource key
110: * @param arg0 The argument to place in variable {0}
111: * @param arg1 The argument to place in variable {1}
112: * @param arg2 The argument to place in variable {2}
113: * @return The formatted message
114: */
115: public static String getMessage(String key, String arg0,
116: String arg1, String arg2) throws MissingResourceException {
117: MessageBundle messageBundle = getMessageBundle(DEFAULT_MESSAGE_BUNDLE_KEY);
118: return messageBundle.getMessage(key, arg0, arg1, arg2);
119: }
120:
121: /**
122: * Get a message from resource.properties from the package of the given object.
123: *
124: * @param key The resource key
125: * @param arg0 The argument to place in variable {0}
126: * @param arg1 The argument to place in variable {1}
127: * @param arg2 The argument to place in variable {2}
128: * @param arg3 The argument to place in variable {3}
129: * @return The formatted message
130: */
131: public static String getMessage(String key, String arg0,
132: String arg1, String arg2, String arg3)
133: throws MissingResourceException {
134: MessageBundle messageBundle = getMessageBundle(DEFAULT_MESSAGE_BUNDLE_KEY);
135: return messageBundle.getMessage(key, arg0, arg1, arg2, arg3);
136: }
137:
138: /**
139: * Get a message from resource.properties from the package of the given object.
140: *
141: * @param key The resource key
142: * @param arg0 The argument to place in variable {0}
143: * @param arg1 The argument to place in variable {1}
144: * @param arg2 The argument to place in variable {2}
145: * @param arg3 The argument to place in variable {3}
146: * @param arg4 The argument to place in variable {4}
147: * @return The formatted message
148: */
149: public static String getMessage(String key, String arg0,
150: String arg1, String arg2, String arg3, String arg4)
151: throws MissingResourceException {
152: MessageBundle messageBundle = getMessageBundle(DEFAULT_MESSAGE_BUNDLE_KEY);
153: return messageBundle.getMessage(key, arg0, arg1, arg2, arg3,
154: arg4);
155: }
156:
157: /**
158: * Get a message from resource.properties from the package of the given object.
159: *
160: * @param key The resource key
161: * @param args An array of objects to place in corresponding variables
162: * @return The formatted message
163: */
164: public static String getMessage(String key, String[] args)
165: throws MissingResourceException {
166: MessageBundle messageBundle = getMessageBundle(DEFAULT_MESSAGE_BUNDLE_KEY);
167: return messageBundle.getMessage(key, args);
168: }
169:
170: public static ResourceBundle getResourceBundle() {
171: MessageBundle messageBundle = getMessageBundle(DEFAULT_MESSAGE_BUNDLE_KEY);
172: return messageBundle.getResourceBundle();
173: }
174:
175: public static MessageBundle getMessageBundle() {
176: return getMessageBundle(DEFAULT_MESSAGE_BUNDLE_KEY);
177: }
178:
179: public static MessageBundle getMessageBundle(String messageBundleKey) {
180: return (MessageBundle) messageBundleMap.get(messageBundleKey);
181: }
182:
183: /**
184: * Get a message from resource.properties from the package of the given object.
185: *
186: * @param messageBundleKey The key for getting the correct message bundle.
187: * @param key The resource key
188: * @return The formatted message
189: */
190: public static String getMessageFromBundle(String messageBundleKey,
191: String key) throws MissingResourceException, Exception {
192: MessageBundle messageBundle = getMessageBundle(messageBundleKey);
193: if (messageBundle == null) {
194: throw new Exception(NO_MESSAGE_BUNDLE);
195: }
196:
197: return messageBundle.getMessage(key);
198: }
199:
200: /**
201: * Get a message from resource.properties from the package of the given object.
202: *
203: * @param messageBundleKey The key for getting the correct message bundle.
204: * @param key The resource key
205: * @param arg0 The argument to place in variable {0}
206: * @return The formatted message
207: */
208: public static String getMessageFromBundle(String messageBundleKey,
209: String key, String arg0) throws MissingResourceException,
210: Exception {
211: MessageBundle messageBundle = getMessageBundle(messageBundleKey);
212: if (messageBundle == null) {
213: throw new Exception(NO_MESSAGE_BUNDLE);
214: }
215:
216: return messageBundle.getMessage(key, arg0);
217: }
218:
219: /**
220: * Get a message from resource.properties from the package of the given object.
221: *
222: * @param messageBundleKey The key for getting the correct message bundle.
223: * @param key The resource key
224: * @param arg0 The argument to place in variable {0}
225: * @param arg1 The argument to place in variable {1}
226: * @return The formatted message
227: */
228: public static String getMessageFromBundle(String messageBundleKey,
229: String key, String arg0, String arg1)
230: throws MissingResourceException, Exception {
231: MessageBundle messageBundle = getMessageBundle(messageBundleKey);
232: if (messageBundle == null) {
233: throw new Exception(NO_MESSAGE_BUNDLE);
234: }
235:
236: return messageBundle.getMessage(key, arg0, arg1);
237: }
238:
239: /**
240: * Get a message from resource.properties from the package of the given object.
241: *
242: * @param messageBundleKey The key for getting the correct message bundle.
243: * @param key The resource key
244: * @param arg0 The argument to place in variable {0}
245: * @param arg1 The argument to place in variable {1}
246: * @param arg2 The argument to place in variable {2}
247: * @return The formatted message
248: */
249: public static String getMessageFromBundle(String messageBundleKey,
250: String key, String arg0, String arg1, String arg2)
251: throws MissingResourceException, Exception {
252: MessageBundle messageBundle = getMessageBundle(messageBundleKey);
253: if (messageBundle == null) {
254: throw new Exception(NO_MESSAGE_BUNDLE);
255: }
256:
257: return messageBundle.getMessage(key, arg0, arg1, arg2);
258: }
259:
260: /**
261: * Get a message from resource.properties from the package of the given object.
262: *
263: * @param messageBundleKey The key for getting the correct message bundle.
264: * @param key The resource key
265: * @param arg0 The argument to place in variable {0}
266: * @param arg1 The argument to place in variable {1}
267: * @param arg2 The argument to place in variable {2}
268: * @param arg3 The argument to place in variable {3}
269: * @return The formatted message
270: */
271: public static String getMessageFromBundle(String messageBundleKey,
272: String key, String arg0, String arg1, String arg2,
273: String arg3) throws MissingResourceException, Exception {
274: MessageBundle messageBundle = getMessageBundle(messageBundleKey);
275: if (messageBundle == null) {
276: throw new Exception(NO_MESSAGE_BUNDLE);
277: }
278:
279: return messageBundle.getMessage(key, arg0, arg1, arg2, arg3);
280: }
281:
282: /**
283: * Get a message from resource.properties from the package of the given object.
284: *
285: * @param messageBundleKey The key for getting the correct message bundle.
286: * @param key The resource key
287: * @param arg0 The argument to place in variable {0}
288: * @param arg1 The argument to place in variable {1}
289: * @param arg2 The argument to place in variable {2}
290: * @param arg3 The argument to place in variable {3}
291: * @param arg4 The argument to place in variable {4}
292: * @return The formatted message
293: */
294: public static String getMessageFromBundle(String messageBundleKey,
295: String key, String arg0, String arg1, String arg2,
296: String arg3, String arg4) throws MissingResourceException,
297: Exception {
298: MessageBundle messageBundle = getMessageBundle(messageBundleKey);
299: if (messageBundle == null) {
300: throw new Exception(NO_MESSAGE_BUNDLE);
301: }
302:
303: return messageBundle.getMessage(key, arg0, arg1, arg2, arg3,
304: arg4);
305: }
306:
307: private static String getPackage(String name) {
308: return name.substring(0, name.lastIndexOf('.')).intern();
309: }
310: }
|