1 /*
2  * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3  * Copyright (C) 2009  Mickael Guessant
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 package davmail;
20 
21 import davmail.exception.DavMailException;
22 
23 import java.io.Serializable;
24 import java.text.MessageFormat;
25 import java.util.ArrayList;
26 import java.util.Locale;
27 import java.util.ResourceBundle;
28 
29 /**
30  * Internationalization message.
31  */
32 public class BundleMessage implements Serializable {
33     /**
34      * Root locale to get english messages for logging.
35      */
36     public static final Locale ROOT_LOCALE = new Locale("", "");
37     protected static final String MESSAGE_BUNDLE_NAME = "davmailmessages";
38     protected final String key;
39     private final Object[] arguments;
40 
41     /**
42      * Internationalization message.
43      *
44      * @param key       message key in resource bundle
45      * @param arguments message values
46      */
BundleMessage(String key, Object... arguments)47     public BundleMessage(String key, Object... arguments) {
48         this.key = key;
49         this.arguments = arguments;
50     }
51 
52 
53     /**
54      * Format message with the default locale.
55      *
56      * @return formatted message
57      */
format()58     public String format() {
59         return format(null);
60     }
61 
62     /**
63      * Format message with the given locale.
64      *
65      * @param locale resource bundle locale
66      * @return formatted message
67      */
format(Locale locale)68     public String format(Locale locale) {
69         return BundleMessage.format(locale, key, arguments);
70     }
71 
72     /**
73      * Format message for logging (with the root locale).
74      * Log file should remain in english
75      *
76      * @return log formatted message
77      */
formatLog()78     public String formatLog() {
79         return format(ROOT_LOCALE);
80     }
81 
82     /**
83      * Format message for logging (with the root locale).
84      * Log file should remain in english
85      *
86      * @return log formatted message
87      */
88     @Override
toString()89     public String toString() {
90         return formatLog();
91     }
92 
93     /**
94      * Get bundle for the given locale.
95      * Load the properties file for the given locale in a resource bundle
96      *
97      * @param locale resource bundle locale
98      * @return resource bundle
99      */
getBundle(Locale locale)100     protected static ResourceBundle getBundle(Locale locale) {
101         if (locale == null) {
102             return ResourceBundle.getBundle(MESSAGE_BUNDLE_NAME);
103         } else {
104             return ResourceBundle.getBundle(MESSAGE_BUNDLE_NAME, locale);
105         }
106     }
107 
108     /**
109      * Get formatted message for message key and values with the default locale.
110      *
111      * @param key       message key in resource bundle
112      * @param arguments message values
113      * @return formatted message
114      */
format(String key, Object... arguments)115     public static String format(String key, Object... arguments) {
116         return format(null, key, arguments);
117     }
118 
119     /**
120      * Get formatted message for message key and values with the given locale.
121      *
122      * @param locale    resource bundle locale
123      * @param key       message key in resource bundle
124      * @param arguments message values
125      * @return formatted message
126      */
format(Locale locale, String key, Object... arguments)127     public static String format(Locale locale, String key, Object... arguments) {
128         Object[] formattedArguments = null;
129         if (arguments != null) {
130             formattedArguments = new Object[arguments.length];
131             for (int i = 0; i < arguments.length; i++) {
132                 if (arguments[i] instanceof BundleMessage) {
133                     formattedArguments[i] = ((BundleMessage) arguments[i]).format(locale);
134                 } else if (arguments[i] instanceof BundleMessageList) {
135                     StringBuilder buffer = new StringBuilder();
136                     for (BundleMessage bundleMessage : (BundleMessageList) arguments[i]) {
137                         buffer.append(bundleMessage.format(locale));
138                     }
139                     formattedArguments[i] = buffer.toString();
140                 } else if (arguments[i] instanceof DavMailException) {
141                     formattedArguments[i] = ((DavMailException) arguments[i]).getMessage(locale);
142                 } else if (arguments[i] instanceof Throwable) {
143                     formattedArguments[i] = ((Throwable) arguments[i]).getMessage();
144                     if (formattedArguments[i] == null) {
145                         formattedArguments[i] = arguments[i].toString();
146                     }
147                 } else {
148                     formattedArguments[i] = arguments[i];
149                 }
150             }
151         }
152         return MessageFormat.format(getBundle(locale).getString(key), formattedArguments);
153     }
154 
155     /**
156      * Get formatted log message for message key and values.
157      * Use the root locale
158      *
159      * @param key       message key in resource bundle
160      * @param arguments message values
161      * @return formatted message
162      */
formatLog(String key, Object... arguments)163     public static String formatLog(String key, Object... arguments) {
164         return format(ROOT_LOCALE, key, arguments);
165     }
166 
167     /**
168      * Get formatted error message for bundle message and exception for logging.
169      * Use the root locale
170      *
171      * @param message bundle message
172      * @param e       exception
173      * @return formatted message
174      */
getExceptionLogMessage(BundleMessage message, Exception e)175     public static String getExceptionLogMessage(BundleMessage message, Exception e) {
176         return getExceptionMessage(message, e, ROOT_LOCALE);
177     }
178 
179     /**
180      * Get formatted error message for bundle message and exception with default locale.
181      *
182      * @param message bundle message
183      * @param e       exception
184      * @return formatted message
185      */
getExceptionMessage(BundleMessage message, Exception e)186     public static String getExceptionMessage(BundleMessage message, Exception e) {
187         return getExceptionMessage(message, e, null);
188     }
189 
190     /**
191      * Get formatted error message for bundle message and exception with given locale.
192      *
193      * @param message bundle message
194      * @param e       exception
195      * @param locale  bundle locale
196      * @return formatted message
197      */
getExceptionMessage(BundleMessage message, Exception e, Locale locale)198     public static String getExceptionMessage(BundleMessage message, Exception e, Locale locale) {
199         StringBuilder buffer = new StringBuilder();
200         if (message != null) {
201             buffer.append(message.format(locale)).append(' ');
202         }
203         if (e instanceof DavMailException) {
204             buffer.append(((DavMailException) e).getMessage(locale));
205         } else if (e.getMessage() != null) {
206             buffer.append(e.getMessage());
207         } else {
208             buffer.append(e.toString());
209         }
210         return buffer.toString();
211     }
212 
213     /**
214      * Typed bundle message collection
215      */
216     public static class BundleMessageList extends ArrayList<BundleMessage> {
217     }
218 }
219