001: /*
002: * Copyright 2006 Assaf Arkin, Ralf Joachim
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: * $Id: Messages.java 6907 2007-03-28 21:24:52Z rjoachim $
017: */
018: package org.castor.util;
019:
020: import java.text.MessageFormat;
021: import java.util.Enumeration;
022: import java.util.Hashtable;
023: import java.util.Locale;
024: import java.util.MissingResourceException;
025: import java.util.ResourceBundle;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: /**
031: * I18N message formatting class. A static factory for obtaining
032: * messages and formatting messages with arguments.
033: * <p>
034: * The resource file <tt>org.exolab.castor.util.resources.messages</tt>
035: * contains a list of all the messages in English. Additional resource
036: * files can be added for other languages and locales by placing them
037: * in the same package with a language/locale prefix. See the I18N
038: * documentation and use of resource bundles in the JDK docs.
039: *
040: * @author <a href="mailto:arkin AT intalio DOT com">Assaf Arkin</a>
041: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
042: * @version $Revision: 6907 $ $Date: 2006-04-10 16:39:24 -0600 (Mon, 10 Apr 2006) $
043: * @since 1.0.1
044: */
045: public final class Messages {
046: //--------------------------------------------------------------------------
047:
048: /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons
049: * Logging </a> instance used for all logging. */
050: private static final Log LOG = LogFactory.getLog(Messages.class);
051:
052: /** The name of the resource holding all the messages in the English
053: * language. Resources for other languages and locales use the same
054: * name with a language/locale prefix. */
055: public static final String RESOURCE_NAME = "org.castor.messages";
056:
057: /** The resource bundle holds all the messages. */
058: private static ResourceBundle _messages;
059:
060: /** Once a format has been created once, it is cached here. */
061: private static Hashtable _formats;
062: //--------------------------------------------------------------------------
063:
064: static {
065: setDefaultLocale();
066: }
067:
068: //--------------------------------------------------------------------------
069:
070: /**
071: * Set the default locale to use for loading messages. Calling this method
072: * will reload all the messages based on the new locale name.
073: */
074: public static void setDefaultLocale() {
075: setLocale(Locale.getDefault());
076: }
077:
078: /**
079: * Set the locale to use for loading messages. Calling this method
080: * will reload all the messages based on the new locale name.
081: *
082: * @param locale the locale for which a resource bundle is desired.
083: */
084: public static void setLocale(final Locale locale) {
085: try {
086: _messages = ResourceBundle.getBundle(RESOURCE_NAME, locale);
087: } catch (Exception except) {
088: _messages = new EmptyResourceBundle();
089: LOG.error("Failed to locate messages resource "
090: + RESOURCE_NAME);
091: }
092: _formats = new Hashtable();
093: }
094:
095: /**
096: * Format the named message using a single argument and return the
097: * full message text.
098: *
099: * @param message The message name
100: * @param arg1 The first argument
101: * @return The full message text
102: */
103: public static String format(final String message, final Object arg1) {
104: return format(message, new Object[] { arg1 });
105: }
106:
107: /**
108: * Format the named message using two argument and return the
109: * full message text.
110: *
111: * @param message The message name
112: * @param arg1 The first argument
113: * @param arg2 The second argument
114: * @return The full message text
115: */
116: public static String format(final String message,
117: final Object arg1, final Object arg2) {
118: return format(message, new Object[] { arg1, arg2 });
119: }
120:
121: /**
122: * Format the named message using three argument and return the
123: * full message text.
124: *
125: * @param message The message name
126: * @param arg1 The first argument
127: * @param arg2 The second argument
128: * @param arg3 The third argument
129: * @return The full message text
130: */
131: public static String format(final String message,
132: final Object arg1, final Object arg2, final Object arg3) {
133: return format(message, new Object[] { arg1, arg2, arg3 });
134: }
135:
136: /**
137: * Format the named message using any number of arguments and return the
138: * full message text.
139: *
140: * @param message The message name
141: * @param args Argument list
142: * @return The full message text
143: */
144: public static String format(final String message,
145: final Object[] args) {
146:
147: try {
148: MessageFormat mf = (MessageFormat) _formats.get(message);
149: if (mf == null) {
150: String msg;
151: try {
152: msg = _messages.getString(message);
153: } catch (MissingResourceException except) {
154: return message;
155: }
156: mf = new MessageFormat(msg);
157: _formats.put(message, mf);
158: }
159: return mf.format(args);
160: } catch (Exception except) {
161: return "An internal error occured while processing message "
162: + message;
163: }
164: }
165:
166: /**
167: * Return the text of the named message without formatting.
168: *
169: * @param message The message name
170: * @return The full message text
171: */
172: public static String message(final String message) {
173: try {
174: return _messages.getString(message);
175: } catch (MissingResourceException except) {
176: return message;
177: }
178: }
179:
180: //--------------------------------------------------------------------------
181:
182: /**
183: * Hide default constructor of utility class.
184: */
185: private Messages() {
186: }
187:
188: //--------------------------------------------------------------------------
189:
190: /**
191: * A empty resource bundle.
192: */
193: private static class EmptyResourceBundle extends ResourceBundle
194: implements Enumeration {
195: /**
196: * {@inheritDoc}
197: * @see java.util.ResourceBundle#getKeys()
198: */
199: public Enumeration getKeys() {
200: return this ;
201: }
202:
203: /**
204: * {@inheritDoc}
205: * @see java.util.ResourceBundle#handleGetObject(java.lang.String)
206: */
207: protected Object handleGetObject(final String name) {
208: return "[Missing message " + name + "]";
209: }
210:
211: /**
212: * {@inheritDoc}
213: * @see java.util.Enumeration#hasMoreElements()
214: */
215: public boolean hasMoreElements() {
216: return false;
217: }
218:
219: /**
220: * {@inheritDoc}
221: * @see java.util.Enumeration#nextElement()
222: */
223: public Object nextElement() {
224: return null;
225: }
226: }
227:
228: //--------------------------------------------------------------------------
229: }
|