Author: dflorey Date: Wed Feb 16 07:14:17 2005 New Revision: 154027 URL: http://svn.apache.org/viewcvs?view=rev&rev=154027 Log: (empty)
Added: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedRuntimeException.java jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/ErrorBundle.java jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/MessageBundle.java jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/TextBundle.java Removed: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedError.java jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedMessage.java jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedText.java Modified: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedException.java jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/MessageManager.java jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/ResourceBundleMessageProvider.java jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java Modified: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedException.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedException.java?view=diff&r1=154026&r2=154027 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedException.java (original) +++ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedException.java Wed Feb 16 07:14:17 2005 @@ -22,22 +22,29 @@ */ package org.apache.commons.i18n; +import java.text.MessageFormat; import java.util.Locale; +import org.apache.commons.i18n.bundles.ErrorBundle; + public class LocalizedException extends Exception { - private LocalizedError errorMessage; + private ErrorBundle errorMessage; - public LocalizedException(LocalizedError errorMessage, Throwable throwable) { + public LocalizedException(ErrorBundle errorMessage, Throwable throwable) { super(errorMessage.getSummary(Locale.getDefault(), throwable.getMessage()), throwable); this.errorMessage = errorMessage; } - public LocalizedException(LocalizedError errorMessage) { - super(errorMessage.getSummary(Locale.getDefault(), "no message available")); + public LocalizedException(ErrorBundle errorMessage) { + super(errorMessage.getSummary( + Locale.getDefault(), + MessageFormat.format( + MessageManager.INTERNAL_MESSAGES.getString(MessageManager.MESSAGE_ENTRY_NOT_FOUND), + new String[] { errorMessage.getId(), ErrorBundle.SUMMARY }))); this.errorMessage = errorMessage; } - public LocalizedError getErrorMessage() { + public ErrorBundle getErrorMessage() { return errorMessage; } } Added: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedRuntimeException.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedRuntimeException.java?view=auto&rev=154027 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedRuntimeException.java (added) +++ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/LocalizedRuntimeException.java Wed Feb 16 07:14:17 2005 @@ -0,0 +1,50 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons-sandbox//i18n/src/java/org/apache/commons/i18n/LocalizedException.java,v 1.1 2004/10/04 13:41:09 dflorey Exp $ + * $Revision: 1.1 $ + * $Date: 2005-02-14 20:03:07 +0100 (Mo, 14 Feb 2005) $ + * + * ==================================================================== + * + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.commons.i18n; + +import java.text.MessageFormat; +import java.util.Locale; + +import org.apache.commons.i18n.bundles.ErrorBundle; + +public class LocalizedRuntimeException extends RuntimeException { + private ErrorBundle errorMessage; + + public LocalizedRuntimeException(ErrorBundle errorMessage, Throwable throwable) { + super(errorMessage.getSummary(Locale.getDefault(), throwable.getMessage()), throwable); + this.errorMessage = errorMessage; + } + + public LocalizedRuntimeException(ErrorBundle errorMessage) { + super(errorMessage.getSummary( + Locale.getDefault(), + MessageFormat.format( + MessageManager.INTERNAL_MESSAGES.getString(MessageManager.MESSAGE_ENTRY_NOT_FOUND), + new String[] { errorMessage.getId(), ErrorBundle.SUMMARY }))); + this.errorMessage = errorMessage; + } + + public ErrorBundle getErrorMessage() { + return errorMessage; + } +} \ No newline at end of file Modified: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/MessageManager.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/MessageManager.java?view=diff&r1=154026&r2=154027 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/MessageManager.java (original) +++ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/MessageManager.java Wed Feb 16 07:14:17 2005 @@ -25,32 +25,48 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.ResourceBundle; /** * The <code>MessageManager</code> provides methods for retrieving localized - * messages and adding custom message providers. This class should not be called - * directly for other purposes than registering a custom [EMAIL PROTECTED] MessageProvider} - * or retrieving information about available message entries. + * messages and adding custom message providers. + * This class should not be called directly for other purposes than registering a custom + * [EMAIL PROTECTED] MessageProvider} or retrieving information about available + * message entries. * <p> - * To access localized messages a subclass of the [EMAIL PROTECTED] LocalizedBundle}class + * To access localized messages a subclass of the [EMAIL PROTECTED] LocalizedBundle} class * such as <code>LocalizedText </code> should be used: + * + * <pre> + * LocalizedText welcome = new LocalizedText("welcome"); + * // Get the german translacion of the retrieved welcome text + * System.out.println(welcome.getText(Locale.GERMAN)); + * </pre> + * * <p> - * <code>LocalizedText welcome = new LocalizedText("welcome"); // Using the - * default locale System.out.println(welcome.getText()); // Using some other - * locale System.out.println(welcome.getText(Locale.GERMAN));</code> - * <p> - * <code>You can call [EMAIL PROTECTED] - * 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 MessageNotFound exception that will be thrown if you - * try to access a not existing entry.</code> + * You can call [EMAIL PROTECTED] 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 [EMAIL PROTECTED] MessageNotFound} exception that will + * be thrown if you try to access a not existing entry.</p> * * @author Daniel Florey + * */ public class MessageManager { + static final String INTERNAL_MESSAGE_NOT_FOUND = "Internal I18n error: Message not found"; + + static final String MESSAGE_NOT_FOUND = "messageNotFound"; + static final String NO_MESSAGE_ENTRIES_FOUND = "noMessageEntriesFound"; + static final String MESSAGE_ENTRY_NOT_FOUND = "messageEntryNotFound"; + static final String RESOURCE_BUNDLE_NOT_FOUND = "resourceBundleNotFound"; + public static final String MESSAGE_PARSING_ERROR = "messageParsingError"; + + 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()); } Modified: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/ResourceBundleMessageProvider.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/ResourceBundleMessageProvider.java?view=diff&r1=154026&r2=154027 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/ResourceBundleMessageProvider.java (original) +++ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/ResourceBundleMessageProvider.java Wed Feb 16 07:14:17 2005 @@ -19,6 +19,7 @@ */ package org.apache.commons.i18n; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; @@ -54,11 +55,17 @@ // skip resource bundle if it is not containing the desired entry } } catch ( MissingResourceException e ) { - logger.log(Level.WARNING, "Could not find resource bundle with base name '"+baseName+"', uninstalling it..."); + logger.log( + Level.WARNING, + MessageFormat.format( + MessageManager.INTERNAL_MESSAGES.getString(MessageManager.RESOURCE_BUNDLE_NOT_FOUND), + new String[] { baseName })); i.remove(); } } - throw new MessageNotFoundException("Message with id "+id+" not found"); + throw new MessageNotFoundException(MessageFormat.format( + MessageManager.INTERNAL_MESSAGES.getString(MessageManager.NO_MESSAGE_ENTRIES_FOUND), + new String[] { id })); } public Map getEntries(String id, Locale locale) { @@ -79,23 +86,27 @@ } } } catch ( MissingResourceException e ) { - logger.log(Level.WARNING, "Could not find resource bundle with base name '"+baseName+"', uninstalling it..."); - i.remove(); + logger.log( + Level.WARNING, + MessageFormat.format( + MessageManager.INTERNAL_MESSAGES.getString(MessageManager.RESOURCE_BUNDLE_NOT_FOUND), + new String[] { baseName })); } } if ( entries == null ) { - throw new MessageNotFoundException("No entries found for message with id "+id); + throw new MessageNotFoundException(MessageFormat.format( + MessageManager.INTERNAL_MESSAGES.getString(MessageManager.NO_MESSAGE_ENTRIES_FOUND), + new String[] { id })); } return entries; } public static void install(String baseName) { - logger.log(Level.FINE, "Installing bundle with base name '"+baseName+"'"); - installedResourceBundles.add(baseName); + if ( !installedResourceBundles.contains(baseName) ) + installedResourceBundles.add(baseName); } public static void uninstall(String baseName) { - logger.log(Level.FINE, "Uninstalling bundle with base name '"+baseName+"'"); installedResourceBundles.remove(baseName); } Modified: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java?view=diff&r1=154026&r2=154027 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java (original) +++ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/XMLMessageProvider.java Wed Feb 16 07:14:17 2005 @@ -20,6 +20,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; @@ -28,11 +29,12 @@ import java.util.logging.Level; import java.util.logging.Logger; -import org.apache.commons.xmlio.in.DefaultSimpleImportHandler; -import org.apache.commons.xmlio.in.SimpleImporter; -import org.apache.commons.xmlio.in.SimplePath; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.Attributes; import org.xml.sax.InputSource; -import org.xml.sax.helpers.AttributesImpl; +import org.xml.sax.helpers.DefaultHandler; /** * @author Daniel Florey @@ -41,9 +43,11 @@ public class XMLMessageProvider implements MessageProvider { private static 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(); - + public String getText(String id, String entry, Locale locale) throws MessageNotFoundException { Message message = findMessage(id, locale); return message.getEntry(entry); @@ -55,30 +59,28 @@ } public static void install(String id, InputStream inputStream) { - logger.log(Level.FINE, "Installing messages '"+id+"'"); try { Map applicationMessages = new HashMap(); - SimpleImporter importer = new SimpleImporter(); - importer.setIncludeLeadingCDataIntoStartElementCallback(true); + SAXParser parser = factory.newSAXParser(); ConfigurationHandler handler = new ConfigurationHandler(); - importer.addSimpleImportHandler(handler); - importer.parse(new InputSource(inputStream)); + 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, "Error while parsing messages", exception); + logger.log(Level.SEVERE, + MessageFormat.format( + MessageManager.INTERNAL_MESSAGES.getString(MessageManager.MESSAGE_PARSING_ERROR), + new String[] { id }), exception); } } public static void uninstall(String id) { - logger.log(Level.FINE, "Uninstalling messages '"+id+"'"); Collection messageKeys = (Collection)installedMessages.get(id); for ( Iterator i = messageKeys.iterator(); i.hasNext(); ) { String messageKey = (String)i.next(); messages.remove(messageKey); - logger.log(Level.FINE, "Removing message with key '"+messageKey+"'"); } installedMessages.remove(id); } @@ -93,7 +95,10 @@ if (message == null) { message = lookupMessage(id, Locale.getDefault()); } - if (message == null ) throw new MessageNotFoundException("Message with id "+id+" not found"); + if (message == null ) throw new MessageNotFoundException( + MessageFormat.format( + MessageManager.INTERNAL_MESSAGES.getString(MessageManager.MESSAGE_NOT_FOUND), + new String[] { id })); return message; } @@ -112,28 +117,39 @@ return null; } - static class ConfigurationHandler extends DefaultSimpleImportHandler { + static class ConfigurationHandler extends DefaultHandler { private Map messages = new HashMap(); - private String id; + private String id, key; private Message message; + private StringBuffer cData; - public void startElement(SimplePath path, String name, AttributesImpl attributes, String leadingCDdata) { - if (path.matches("message")) { + public void startElement(String namespaceUri, String localeName, String qName, Attributes attributes) { + if (qName.matches("message")) { id = attributes.getValue("id"); - } else if (path.matches("message/locale")) { + } else if (qName.matches("locale")) { message = new Message(id); message.setLanguage(attributes.getValue("language")); message.setCountry(attributes.getValue("country")); message.setVariant(attributes.getValue("variant")); - } else if (path.matches("message/locale/entry")) { - String key = attributes.getValue("key"); - message.addEntry(key, leadingCDdata); + } else if (qName.matches("entry")) { + key = attributes.getValue("key"); + cData = new StringBuffer(); } } - - public void endElement(SimplePath path, String name) { - if (path.matches("message/locale")) { + public void characters(char[] ch, + int start, + int length) { + if ( message != null && key != null && length > 0 ) { + cData.append(ch, start, length); + } + } + + public void endElement(String namespaceUri, String localeName, String qName) { + if (qName.matches("locale")) { messages.put(message.getKey(), message); + } else if (qName.matches("entry")) { + message.addEntry(key, cData.toString()); + key = null; } } Added: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/ErrorBundle.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/ErrorBundle.java?view=auto&rev=154027 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/ErrorBundle.java (added) +++ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/ErrorBundle.java Wed Feb 16 07:14:17 2005 @@ -0,0 +1,56 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons-sandbox//i18n/src/java/org/apache/commons/i18n/LocalizedError.java,v 1.1 2004/10/04 13:41:09 dflorey Exp $ + * $Revision: 1.1 $ + * $Date: 2005-02-14 20:03:07 +0100 (Mo, 14 Feb 2005) $ + * + * ==================================================================== + * + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.commons.i18n.bundles; + +import java.util.Locale; + +import org.apache.commons.i18n.MessageNotFoundException; + +public class ErrorBundle extends MessageBundle { + public final static String SUMMARY = "summary"; + public final static String DETAILS = "details"; + + public ErrorBundle(String messageId) { + super(messageId); + } + + public ErrorBundle(String messageId, Object[] arguments) { + super(messageId, arguments); + } + + public String getSummary(Locale locale) throws MessageNotFoundException { + return getText(SUMMARY, locale); + } + + public String getSummary(Locale locale, String defaultSummary) { + return getText(SUMMARY, defaultSummary, locale); + } + + public String getDetails(Locale locale) throws MessageNotFoundException { + return getText(DETAILS, locale); + } + + public String getDetails(Locale locale, String defaultDetails) { + return getText(DETAILS, defaultDetails, locale); + } +} \ No newline at end of file Added: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/MessageBundle.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/MessageBundle.java?view=auto&rev=154027 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/MessageBundle.java (added) +++ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/MessageBundle.java Wed Feb 16 07:14:17 2005 @@ -0,0 +1,47 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons-sandbox//i18n/src/java/org/apache/commons/i18n/LocalizedMessage.java,v 1.1 2004/10/04 13:41:09 dflorey Exp $ + * $Revision: 1.1 $ + * $Date: 2005-02-14 20:03:07 +0100 (Mo, 14 Feb 2005) $ + * + * ==================================================================== + * + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.commons.i18n.bundles; + +import java.util.Locale; + +import org.apache.commons.i18n.MessageNotFoundException; + +public class MessageBundle extends TextBundle { + public static String TITLE = "title"; + + public MessageBundle(String messageId) { + super(messageId); + } + + public MessageBundle(String messageId, Object[] arguments) { + super(messageId, arguments); + } + + public String getTitle(Locale locale) throws MessageNotFoundException { + return getText(TITLE, locale); + } + + public String getTitle(Locale locale, String defaultTitle) { + return getText(TITLE, defaultTitle, locale); + } +} Added: jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/TextBundle.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/TextBundle.java?view=auto&rev=154027 ============================================================================== --- jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/TextBundle.java (added) +++ jakarta/commons/sandbox/i18n/trunk/src/java/org/apache/commons/i18n/bundles/TextBundle.java Wed Feb 16 07:14:17 2005 @@ -0,0 +1,48 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons-sandbox//i18n/src/java/org/apache/commons/i18n/LocalizedText.java,v 1.1 2004/10/04 13:41:09 dflorey Exp $ + * $Revision: 1.1 $ + * $Date: 2005-02-14 20:03:07 +0100 (Mo, 14 Feb 2005) $ + * + * ==================================================================== + * + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.commons.i18n.bundles; + +import java.util.Locale; + +import org.apache.commons.i18n.LocalizedBundle; +import org.apache.commons.i18n.MessageNotFoundException; + +public class TextBundle extends LocalizedBundle { + public final static String TEXT = "text"; + + public TextBundle(String messageId) { + super(messageId); + } + + public TextBundle(String messageId, Object[] arguments) { + super(messageId, arguments); + } + + public String getText(Locale locale) throws MessageNotFoundException { + return getText(TEXT, locale); + } + + public String getText(Locale locale, String defaultText) { + return getText(TEXT, defaultText, locale); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]