Diff of /jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/MessageManager.java
Parent Directory
| Revision Log
| Patch
--- jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/MessageManager.java 2005/05/06 10:19:21 168589
+++ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/MessageManager.java 2005/05/06 10:36:38 168590
@@ -20,12 +20,7 @@
package org.apache.commons.i18n;
import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.ResourceBundle;
+import java.util.*;
/**
* The <code>MessageManager</code> provides methods for retrieving localized
@@ -46,7 +41,7 @@ import java.util.ResourceBundle;
* <p>
* You can call {@link MessageManager#getText(String,String,Object[],Locale) getText} directly,
* but if you do so, you have to ensure that the given entry key really
- * exists and to deal with the {@link MessageNotFound} exception that will
+ * exists and to deal with the {@link MessageNotFoundException} exception that will
* be thrown if you try to access a not existing entry.</p>
*
*/
@@ -61,28 +56,33 @@ public class MessageManager {
public static final ResourceBundle INTERNAL_MESSAGES = ResourceBundle.getBundle("messages", Locale.getDefault());
- private static List messageProviders = new ArrayList();
-
- static {
- // Add default message providers
- messageProviders.add(new XMLMessageProvider());
- messageProviders.add(new ResourceBundleMessageProvider());
- }
+ private static Map messageProviders = new LinkedHashMap();
/**
* Add a custom <code>{@link MessageProvider}</code> to the
* <code>MessageManager</code>. It will be incorporated in later calls of
* the {@link MessageManager#getText(String,String,Object[],Locale) getText}
* or {@link #getEntries(String,Locale) getEntries}methods.
- *
+ *
+ * @param providerId Id of the provider used for uninstallation and
+ * qualified naming.
* @param messageProvider
* The <code>MessageProvider</code> to be added.
*/
- public static void addMessageProvider(MessageProvider messageProvider) {
- messageProviders.add(messageProvider);
+ public static void addMessageProvider(String providerId, MessageProvider messageProvider) {
+ messageProviders.put(providerId, messageProvider);
}
/**
+ * Remove custom <code>{@link MessageProvider}</code> from the
+ * <code>MessageManager</code>. Used for tearing down unit tests.
+ *
+ * @param providerId The ID of the provider to remove.
+ */
+ static void removeMessageProvider(String providerId) {
+ messageProviders.remove(providerId);
+ }
+ /**
* Iterates over all registered message providers in order to find the given
* entry in the requested message bundle.
*
@@ -104,8 +104,10 @@ public class MessageManager {
*/
public static String getText(String id, String entry, Object[] arguments,
Locale locale) throws MessageNotFoundException {
+ if(messageProviders.isEmpty())
+ throw new MessageNotFoundException("No MessageProvider registered");
MessageNotFoundException exception = null;
- for (Iterator i = messageProviders.iterator(); i.hasNext();) {
+ for (Iterator i = messageProviders.values().iterator(); i.hasNext();) {
try {
String text = ((MessageProvider) i.next()).getText(id, entry,
locale);
@@ -140,10 +142,9 @@ public class MessageManager {
public static String getText(String id, String entry, Object[] arguments,
Locale locale, String defaultText) {
try {
- String text = getText(id, entry, arguments, locale);
- return MessageFormat.format(text, arguments);
+ return getText(id, entry, arguments, locale);
} catch (MessageNotFoundException e) {
- return defaultText;
+ return MessageFormat.format(defaultText, arguments);
}
}
@@ -155,12 +156,12 @@ public class MessageManager {
*/
public static Map getEntries(String id, Locale locale)
throws MessageNotFoundException {
+ if(messageProviders.isEmpty())
+ throw new MessageNotFoundException("No MessageProvider registered");
MessageNotFoundException exception = null;
- for (Iterator i = messageProviders.iterator(); i.hasNext();) {
+ for (Iterator i = messageProviders.values().iterator(); i.hasNext();) {
try {
- Map entries = ((MessageProvider) i.next()).getEntries(id,
- locale);
- return entries;
+ return ((MessageProvider) i.next()).getEntries(id, locale);
} catch (MessageNotFoundException e) {
exception = e;
}