Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 69016 invoked from network); 29 Dec 2004 17:04:02 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 29 Dec 2004 17:04:02 -0000 Received: (qmail 23308 invoked by uid 500); 29 Dec 2004 17:03:58 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 23234 invoked by uid 500); 29 Dec 2004 17:03:57 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 23216 invoked by uid 500); 29 Dec 2004 17:03:57 -0000 Received: (qmail 23210 invoked by uid 99); 29 Dec 2004 17:03:57 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from minotaur.apache.org (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Wed, 29 Dec 2004 09:03:56 -0800 Received: (qmail 68975 invoked by uid 1823); 29 Dec 2004 17:03:55 -0000 Date: 29 Dec 2004 17:03:55 -0000 Message-ID: <20041229170355.68974.qmail@minotaur.apache.org> From: dflorey@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/i18n/src/java/org/apache/commons/i18n LocalizedBundle.java XMLMessageProvider.java MessageProvider.java ResourceBundleMessageProvider.java MessageManager.java X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N dflorey 2004/12/29 09:03:55 Modified: i18n/src/java/org/apache/commons/i18n LocalizedBundle.java XMLMessageProvider.java MessageProvider.java ResourceBundleMessageProvider.java MessageManager.java Log: Added support for getEntries()... Revision Changes Path 1.3 +8 -5 jakarta-commons-sandbox/i18n/src/java/org/apache/commons/i18n/LocalizedBundle.java Index: LocalizedBundle.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/i18n/src/java/org/apache/commons/i18n/LocalizedBundle.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- LocalizedBundle.java 18 Dec 2004 15:08:49 -0000 1.2 +++ LocalizedBundle.java 29 Dec 2004 17:03:55 -0000 1.3 @@ -23,16 +23,19 @@ package org.apache.commons.i18n; +import java.io.Serializable; import java.util.Locale; /** * @author Daniel Florey * - * The LocalizedBundle class represents a bundle of localized messages that + * The LocalizedBundle class represents a bundle of localized messages that * belong together. + * The LocalizedBundle class itself contains the message id and the arguments + * that might be used to include dynamic values into the message text and knows nothing * */ -public class LocalizedBundle { +public class LocalizedBundle implements Serializable { public final static String ID = "id"; public final static String ARGUMENTS = "arguments"; 1.2 +2 -3 jakarta-commons-sandbox/i18n/src/java/org/apache/commons/i18n/XMLMessageProvider.java Index: XMLMessageProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/i18n/src/java/org/apache/commons/i18n/XMLMessageProvider.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- XMLMessageProvider.java 18 Dec 2004 15:08:49 -0000 1.1 +++ XMLMessageProvider.java 29 Dec 2004 17:03:55 -0000 1.2 @@ -20,7 +20,6 @@ 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; @@ -45,9 +44,9 @@ private static Map installedMessages = new HashMap(); private static Map messages = new HashMap(); - public String getText(String id, String entry, Object[] arguments, Locale locale) throws MessageNotFoundException { + public String getText(String id, String entry, Locale locale) throws MessageNotFoundException { Message message = findMessage(id, locale); - return MessageFormat.format(message.getEntry(entry), arguments); + return message.getEntry(entry); } public Map getEntries(String id, Locale locale) throws MessageNotFoundException { 1.2 +7 -4 jakarta-commons-sandbox/i18n/src/java/org/apache/commons/i18n/MessageProvider.java Index: MessageProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/i18n/src/java/org/apache/commons/i18n/MessageProvider.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MessageProvider.java 18 Dec 2004 15:08:49 -0000 1.1 +++ MessageProvider.java 29 Dec 2004 17:03:55 -0000 1.2 @@ -23,7 +23,10 @@ package org.apache.commons.i18n; import java.util.Locale; +import java.util.Map; public interface MessageProvider { - public String getText(String id, String entry, Object[] arguments, Locale locale) throws MessageNotFoundException; + public String getText(String id, String entry, Locale locale) throws MessageNotFoundException; + + public Map getEntries(String id, Locale locale) throws MessageNotFoundException; } 1.3 +33 -4 jakarta-commons-sandbox/i18n/src/java/org/apache/commons/i18n/ResourceBundleMessageProvider.java Index: ResourceBundleMessageProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/i18n/src/java/org/apache/commons/i18n/ResourceBundleMessageProvider.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ResourceBundleMessageProvider.java 18 Dec 2004 15:23:01 -0000 1.2 +++ ResourceBundleMessageProvider.java 29 Dec 2004 17:03:55 -0000 1.3 @@ -19,11 +19,13 @@ */ package org.apache.commons.i18n; -import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.logging.Level; @@ -38,15 +40,14 @@ private static List installedResourceBundles = new ArrayList(); - public String getText(String id, String entry, Object[] arguments, Locale locale) throws MessageNotFoundException { + public String getText(String id, String entry, Locale locale) throws MessageNotFoundException { String text = null; for ( Iterator i = installedResourceBundles.iterator(); i.hasNext(); ) { String baseName = (String)i.next(); try { ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, locale); try { - text = resourceBundle.getString(id+"."+entry); - return MessageFormat.format(text, arguments); + return resourceBundle.getString(id+"."+entry); } catch ( ClassCastException e ) { // ignore all entries that are not of type String } catch ( MissingResourceException e ) { @@ -60,6 +61,34 @@ throw new MessageNotFoundException("Message with id "+id+" not found"); } + public Map getEntries(String id, Locale locale) { + String messageIdentifier = id+"."; + Map entries = null; + for ( Iterator i = installedResourceBundles.iterator(); i.hasNext(); ) { + String baseName = (String)i.next(); + try { + ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, locale); + Enumeration keys = resourceBundle.getKeys(); + while ( keys.hasMoreElements() ) { + String key = (String)keys.nextElement(); + if ( key.startsWith(messageIdentifier) ) { + if ( entries == null ) { + entries = new HashMap(); + } + entries.put(key.substring(messageIdentifier.length()), resourceBundle.getString(key)); + } + } + } catch ( MissingResourceException e ) { + logger.log(Level.WARNING, "Could not find resource bundle with base name '"+baseName+"', uninstalling it..."); + i.remove(); + } + } + if ( entries == null ) { + throw new MessageNotFoundException("No entries found for message with id "+id); + } + return entries; + } + public static void install(String baseName) { logger.log(Level.FINE, "Installing bundle with base name '"+baseName+"'"); installedResourceBundles.add(baseName); 1.4 +19 -2 jakarta-commons-sandbox/i18n/src/java/org/apache/commons/i18n/MessageManager.java Index: MessageManager.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/i18n/src/java/org/apache/commons/i18n/MessageManager.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- MessageManager.java 18 Dec 2004 15:08:49 -0000 1.3 +++ MessageManager.java 29 Dec 2004 17:03:55 -0000 1.4 @@ -19,10 +19,12 @@ */ 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; /** * @author Daniel Florey @@ -44,7 +46,8 @@ MessageNotFoundException exception = null; for ( Iterator i = messageProviders.iterator(); i.hasNext(); ) { try { - return ((MessageProvider)i.next()).getText(id, entry, arguments, locale); + String text =((MessageProvider)i.next()).getText(id, entry, locale); + return MessageFormat.format(text, arguments); } catch ( MessageNotFoundException e ) { exception = e; } @@ -54,9 +57,23 @@ public static String getText(String id, String entry, Object[] arguments, Locale locale, String defaultText) { try { - return getText(id, entry, arguments, locale); + String text = getText(id, entry, arguments, locale); + return MessageFormat.format(text, arguments); } catch ( MessageNotFoundException e ) { return defaultText; } + } + + public static Map getEntries(String id, Locale locale) throws MessageNotFoundException { + MessageNotFoundException exception = null; + for ( Iterator i = messageProviders.iterator(); i.hasNext(); ) { + try { + Map entries =((MessageProvider)i.next()).getEntries(id, locale); + return entries; + } catch ( MessageNotFoundException e ) { + exception = e; + } + } + throw exception; } } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org