2009-04-17 05:20:11 -04:00
|
|
|
package davmail;
|
|
|
|
|
2009-04-27 19:03:58 -04:00
|
|
|
import davmail.exception.DavMailException;
|
|
|
|
|
2009-04-17 05:20:11 -04:00
|
|
|
import java.text.MessageFormat;
|
2009-04-25 09:13:48 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Locale;
|
2009-04-17 05:20:11 -04:00
|
|
|
import java.util.ResourceBundle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internationalization message.
|
|
|
|
*/
|
|
|
|
public class BundleMessage {
|
2009-04-29 17:02:06 -04:00
|
|
|
public static final Locale ROOT_LOCALE = new Locale("", "");
|
2009-04-25 09:13:48 -04:00
|
|
|
protected static final String MESSAGE_BUNDLE_NAME = "davmailmessages";
|
2009-04-17 05:20:11 -04:00
|
|
|
protected final String key;
|
|
|
|
private final Object[] arguments;
|
|
|
|
|
2009-04-25 09:13:48 -04:00
|
|
|
public BundleMessage(String key, Object... arguments) {
|
2009-04-17 05:20:11 -04:00
|
|
|
this.key = key;
|
|
|
|
this.arguments = arguments;
|
|
|
|
}
|
|
|
|
|
2009-04-29 17:02:06 -04:00
|
|
|
public String format() {
|
|
|
|
return format(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String format(Locale locale) {
|
|
|
|
return BundleMessage.format(locale, key, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String formatLog() {
|
|
|
|
return format(ROOT_LOCALE);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static ResourceBundle getBundle(Locale locale) {
|
2009-04-25 09:13:48 -04:00
|
|
|
if (locale == null) {
|
|
|
|
return ResourceBundle.getBundle(MESSAGE_BUNDLE_NAME);
|
|
|
|
} else {
|
|
|
|
return ResourceBundle.getBundle(MESSAGE_BUNDLE_NAME, locale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-29 17:02:06 -04:00
|
|
|
public static String format(String key, Object... arguments) {
|
|
|
|
return format(null, key, arguments);
|
2009-04-25 09:13:48 -04:00
|
|
|
}
|
|
|
|
|
2009-04-29 17:02:06 -04:00
|
|
|
public static String format(Locale locale, String key, Object... arguments) {
|
2009-04-25 09:13:48 -04:00
|
|
|
Object[] formattedArguments = null;
|
|
|
|
if (arguments != null) {
|
|
|
|
formattedArguments = new Object[arguments.length];
|
|
|
|
for (int i = 0; i < arguments.length; i++) {
|
|
|
|
if (arguments[i] instanceof BundleMessage) {
|
|
|
|
formattedArguments[i] = ((BundleMessage) arguments[i]).format(locale);
|
|
|
|
} else if (arguments[i] instanceof BundleMessageList) {
|
|
|
|
StringBuilder buffer = new StringBuilder();
|
2009-04-29 17:02:06 -04:00
|
|
|
for (BundleMessage bundleMessage : (BundleMessageList) arguments[i]) {
|
2009-04-25 09:13:48 -04:00
|
|
|
buffer.append(bundleMessage.format(locale));
|
|
|
|
}
|
|
|
|
formattedArguments[i] = buffer.toString();
|
2009-04-27 19:03:58 -04:00
|
|
|
} else if (arguments[i] instanceof DavMailException) {
|
2009-04-29 17:02:06 -04:00
|
|
|
formattedArguments[i] = ((DavMailException) arguments[i]).getMessage(locale);
|
2009-04-27 19:03:58 -04:00
|
|
|
} else if (arguments[i] instanceof Throwable) {
|
2009-04-29 17:02:06 -04:00
|
|
|
formattedArguments[i] = ((Throwable) arguments[i]).getMessage();
|
2009-04-27 19:03:58 -04:00
|
|
|
if (formattedArguments[i] == null) {
|
|
|
|
formattedArguments[i] = arguments[i].toString();
|
|
|
|
}
|
2009-04-25 09:13:48 -04:00
|
|
|
} else {
|
|
|
|
formattedArguments[i] = arguments[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MessageFormat.format(getBundle(locale).getString(key), formattedArguments);
|
2009-04-17 05:20:11 -04:00
|
|
|
}
|
|
|
|
|
2009-05-07 04:43:11 -04:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return formatLog();
|
|
|
|
}
|
|
|
|
|
2009-04-29 17:02:06 -04:00
|
|
|
public static String formatLog(String key, Object... arguments) {
|
|
|
|
return format(ROOT_LOCALE, key, arguments);
|
2009-04-17 06:32:11 -04:00
|
|
|
}
|
|
|
|
|
2009-04-29 17:02:06 -04:00
|
|
|
public static String getExceptionLogMessage(BundleMessage message, Exception e) {
|
|
|
|
return getExceptionMessage(message, e, ROOT_LOCALE);
|
2009-04-27 19:03:58 -04:00
|
|
|
}
|
|
|
|
|
2009-04-29 17:02:06 -04:00
|
|
|
public static String getExceptionMessage(BundleMessage message, Exception e) {
|
|
|
|
return getExceptionMessage(message, e, null);
|
2009-04-27 19:03:58 -04:00
|
|
|
}
|
|
|
|
|
2009-04-29 17:02:06 -04:00
|
|
|
public static String getExceptionMessage(BundleMessage message, Exception e, Locale locale) {
|
|
|
|
StringBuilder buffer = new StringBuilder();
|
|
|
|
if (message != null) {
|
|
|
|
buffer.append(message.format(locale)).append(' ');
|
|
|
|
}
|
|
|
|
if (e instanceof DavMailException) {
|
|
|
|
buffer.append(((DavMailException) e).getMessage(locale));
|
|
|
|
} else if (e.getMessage() != null) {
|
|
|
|
buffer.append(e.getMessage());
|
|
|
|
} else {
|
|
|
|
buffer.append(e.toString());
|
|
|
|
}
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class BundleMessageList extends ArrayList<BundleMessage> {
|
|
|
|
}
|
2009-04-17 05:20:11 -04:00
|
|
|
}
|