Diff of /jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java
Parent Directory
| Revision Log
| Patch
--- jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java 2005/05/06 10:19:21 168589
+++ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java 2005/05/06 10:36:38 168590
@@ -21,9 +21,7 @@ package org.apache.commons.i18n;
import java.io.InputStream;
import java.text.MessageFormat;
-import java.util.Collection;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
@@ -41,13 +39,32 @@ import org.xml.sax.helpers.DefaultHandle
*
*/
public class XMLMessageProvider implements MessageProvider {
- private static Logger logger = Logger.getLogger(XMLMessageProvider.class.getName());
+ private static final Logger logger = Logger.getLogger(XMLMessageProvider.class.getName());
private static SAXParserFactory factory = SAXParserFactory.newInstance();
- private static Map installedMessages = new HashMap();
- private static Map messages = new HashMap();
-
+ private final String id;
+
+ private Map messages = new HashMap();
+
+ public XMLMessageProvider(String id, InputStream inputStream) {
+ this.id = id;
+ try {
+ Map applicationMessages = new HashMap();
+ SAXParser parser = factory.newSAXParser();
+ ConfigurationHandler handler = new ConfigurationHandler();
+ parser.parse(new InputSource(inputStream), handler);
+ Map parsedMessages = handler.getMessages();
+ applicationMessages.putAll(parsedMessages);
+ messages.putAll(applicationMessages);
+ } catch (Exception exception) {
+ logger.log(Level.SEVERE,
+ MessageFormat.format(
+ MessageManager.INTERNAL_MESSAGES.getString(MessageManager.MESSAGE_PARSING_ERROR),
+ new String[] { id }), exception);
+ }
+ }
+
/* (non-Javadoc)
* @see org.apache.commons.i18n.MessageProvider#getText(java.lang.String, java.lang.String, java.util.Locale)
*/
@@ -72,33 +89,14 @@ public class XMLMessageProvider implemen
* @param inputStream providing the messages in the required XML format
*/
public static void install(String id, InputStream inputStream) {
- try {
- Map applicationMessages = new HashMap();
- SAXParser parser = factory.newSAXParser();
- ConfigurationHandler handler = new ConfigurationHandler();
- parser.parse(new InputSource(inputStream), handler);
- Map parsedMessages = handler.getMessages();
- applicationMessages.putAll(parsedMessages);
- messages.putAll(applicationMessages);
- installedMessages.put(id, applicationMessages.keySet());
- } catch (Exception exception) {
- logger.log(Level.SEVERE,
- MessageFormat.format(
- MessageManager.INTERNAL_MESSAGES.getString(MessageManager.MESSAGE_PARSING_ERROR),
- new String[] { id }), exception);
- }
+ MessageManager.addMessageProvider(id, new XMLMessageProvider(id, inputStream));
}
/**
* @param id unique identifier for the messages to uninstall
*/
public static void uninstall(String id) {
- Collection messageKeys = (Collection)installedMessages.get(id);
- for ( Iterator i = messageKeys.iterator(); i.hasNext(); ) {
- String messageKey = (String)i.next();
- messages.remove(messageKey);
- }
- installedMessages.remove(id);
+ MessageManager.removeMessageProvider(id);
}
/**
@@ -110,7 +108,7 @@ public class XMLMessageProvider implemen
install(id, inputStream);
}
- private static Message findMessage(String id, Locale locale) {
+ private Message findMessage(String id, Locale locale) {
Message message = lookupMessage(id, locale);
if (message == null) {
message = lookupMessage(id, Locale.getDefault());
@@ -122,13 +120,8 @@ public class XMLMessageProvider implemen
return message;
}
- private static Message lookupMessage(String id, Locale locale) {
- StringBuffer keyBuffer = new StringBuffer(64);
- keyBuffer.append(id);
- if (locale.getLanguage() != null) keyBuffer.append("_" + locale.getLanguage());
- if (locale.getCountry() != null) keyBuffer.append("_" + locale.getCountry());
- if (locale.getVariant() != null) keyBuffer.append("_" + locale.getVariant());
- String key = keyBuffer.toString();
+ private Message lookupMessage(String id, Locale locale) {
+ String key = id + '_' + locale.toString();
if (messages.containsKey(key)) return (Message)messages.get(key);
while (key.lastIndexOf('_') > 0) {
key = key.substring(0, key.lastIndexOf('_'));
@@ -137,7 +130,7 @@ public class XMLMessageProvider implemen
return null;
}
- static class ConfigurationHandler extends DefaultHandler {
+ class ConfigurationHandler extends DefaultHandler {
private String id, key;
private Message message;
private StringBuffer cData;
@@ -178,7 +171,8 @@ public class XMLMessageProvider implemen
}
static class Message {
- private String id, language, country, variant;
+ private final String id;
+ private String language, country, variant;
private Map entries = new HashMap();
public Message(String id) {
@@ -210,12 +204,9 @@ public class XMLMessageProvider implemen
}
public String getKey() {
- StringBuffer key = new StringBuffer(64);
- key.append(id);
- if (language != null) key.append("_" + language);
- if (country != null) key.append("_" + country);
- if (variant != null) key.append("_" + variant);
- return key.toString();
+ return id + '_' + new Locale((language != null) ? language : "",
+ (country != null) ? country : "",
+ (variant != null) ? variant : "").toString();
}
}
}
\ No newline at end of file