dgraham 2003/07/01 20:42:09 Modified: src/share/org/apache/struts/util MessageResources.java Log: Fixed confusing formatting. Revision Changes Path 1.18 +82 -122 jakarta-struts/src/share/org/apache/struts/util/MessageResources.java Index: MessageResources.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/MessageResources.java,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- MessageResources.java 2 Jul 2003 03:34:04 -0000 1.17 +++ MessageResources.java 2 Jul 2003 03:42:09 -0000 1.18 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2001 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -57,20 +57,18 @@ * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * - */ - + */ package org.apache.struts.util; - import java.io.Serializable; import java.text.MessageFormat; import java.util.HashMap; import java.util.Locale; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - /** * General purpose abstract class that describes an API for retrieving * Locale-sensitive messages from underlying resource locations of an @@ -93,10 +91,8 @@ * @author Craig R. McClanahan * @version $Revision$ $Date$ */ - public abstract class MessageResources implements Serializable { - // ------------------------------------------------------------- Properties /** @@ -104,7 +100,6 @@ */ protected static Log log = LogFactory.getLog(MessageResources.class); - /** * The configuration parameter used to initialize this MessageResources. */ @@ -118,13 +113,11 @@ return (this.config); } - /** * The default Locale for our environment. */ protected Locale defaultLocale = Locale.getDefault(); - /** * The <code>MessageResourcesFactory</code> that created this instance. */ @@ -138,14 +131,12 @@ return (this.factory); } - /** * The set of previously created MessageFormat objects, keyed by the * key computed in <code>messageKey()</code>. */ protected HashMap formats = new HashMap(); - /** * Indicate is a <code>null</code> is returned instead of an error message string * when an unknown Locale or key is requested. @@ -171,10 +162,8 @@ this.returnNull = returnNull; } - // ----------------------------------------------------------- Constructors - /** * Construct a new MessageResources according to the specified parameters. * @@ -187,7 +176,6 @@ } - /** * Construct a new MessageResources according to the specified parameters. * @@ -195,8 +183,10 @@ * @param config The configuration parameter for this MessageResources * @param returnNull The returnNull property we should initialize with */ - public MessageResources(MessageResourcesFactory factory, String config, - boolean returnNull) { + public MessageResources( + MessageResourcesFactory factory, + String config, + boolean returnNull) { super(); this.factory = factory; @@ -205,11 +195,8 @@ } - // --------------------------------------------------------- Public Methods - - /** * Returns a text message for the specified key, for the default Locale. * @@ -217,11 +204,10 @@ */ public String getMessage(String key) { - return (getMessage((Locale) null, key)); + return this.getMessage((Locale) null, key); } - /** * Returns a text message after parametric replacement of the specified * parameter placeholders. @@ -231,11 +217,10 @@ */ public String getMessage(String key, Object args[]) { - return (getMessage((Locale) null, key, args)); + return this.getMessage((Locale) null, key, args); } - /** * Returns a text message after parametric replacement of the specified * parameter placeholders. @@ -245,11 +230,10 @@ */ public String getMessage(String key, Object arg0) { - return (getMessage((Locale) null, key, arg0)); + return this.getMessage((Locale) null, key, arg0); } - /** * Returns a text message after parametric replacement of the specified * parameter placeholders. @@ -260,11 +244,10 @@ */ public String getMessage(String key, Object arg0, Object arg1) { - return (getMessage((Locale) null, key, arg0, arg1)); + return this.getMessage((Locale) null, key, arg0, arg1); } - /** * Returns a text message after parametric replacement of the specified * parameter placeholders. @@ -274,14 +257,12 @@ * @param arg1 The replacement for placeholder {1} in the message * @param arg2 The replacement for placeholder {2} in the message */ - public String getMessage(String key, Object arg0, Object arg1, - Object arg2) { + public String getMessage(String key, Object arg0, Object arg1, Object arg2) { - return (getMessage((Locale) null, key, arg0, arg1, arg2)); + return this.getMessage((Locale) null, key, arg0, arg1, arg2); } - /** * Returns a text message after parametric replacement of the specified * parameter placeholders. @@ -292,14 +273,17 @@ * @param arg2 The replacement for placeholder {2} in the message * @param arg3 The replacement for placeholder {3} in the message */ - public String getMessage(String key, Object arg0, Object arg1, - Object arg2, Object arg3) { + public String getMessage( + String key, + Object arg0, + Object arg1, + Object arg2, + Object arg3) { - return (getMessage((Locale) null, key, arg0, arg1, arg2, arg3)); + return this.getMessage((Locale) null, key, arg0, arg1, arg2, arg3); } - /** * Returns a text message for the specified key, for the default Locale. * A null string result will be returned by this method if no relevant @@ -315,7 +299,6 @@ */ public abstract String getMessage(Locale locale, String key); - /** * Returns a text message after parametric replacement of the specified * parameter placeholders. A null string result will be returned by @@ -328,30 +311,31 @@ */ public String getMessage(Locale locale, String key, Object args[]) { - // Cache MessageFormat instances as they are accessed - if (locale == null) + // Cache MessageFormat instances as they are accessed + if (locale == null) { locale = defaultLocale; - MessageFormat format = null; - String formatKey = messageKey(locale, key); - synchronized (formats) { - format = (MessageFormat) formats.get(formatKey); - if (format == null) { - String formatString = getMessage(locale, key); - if (formatString == null) { - if (returnNull) - return (null); - else - return ("???" + formatKey + "???"); - } - format = new MessageFormat(escape(formatString)); - formats.put(formatKey, format); - } + } - } - return (format.format(args)); + MessageFormat format = null; + String formatKey = messageKey(locale, key); - } + synchronized (formats) { + format = (MessageFormat) formats.get(formatKey); + if (format == null) { + String formatString = getMessage(locale, key); + + if (formatString == null) { + return returnNull ? null : ("???" + formatKey + "???"); + } + + format = new MessageFormat(escape(formatString)); + formats.put(formatKey, format); + } + } + + return format.format(args); + } /** * Returns a text message after parametric replacement of the specified @@ -364,14 +348,9 @@ * @param arg0 The replacement for placeholder {0} in the message */ public String getMessage(Locale locale, String key, Object arg0) { - - Object args[] = new Object[1]; - args[0] = arg0; - return (getMessage(locale, key, args)); - + return this.getMessage(locale, key, new Object[] { arg0 }); } - /** * Returns a text message after parametric replacement of the specified * parameter placeholders. A null string result will never be returned @@ -383,17 +362,10 @@ * @param arg0 The replacement for placeholder {0} in the message * @param arg1 The replacement for placeholder {1} in the message */ - public String getMessage(Locale locale, - String key, Object arg0, Object arg1) { - - Object args[] = new Object[2]; - args[0] = arg0; - args[1] = arg1; - return (getMessage(locale, key, args)); - + public String getMessage(Locale locale, String key, Object arg0, Object arg1) { + return this.getMessage(locale, key, new Object[] { arg0, arg1 }); } - /** * Returns a text message after parametric replacement of the specified * parameter placeholders. A null string result will never be returned @@ -406,19 +378,16 @@ * @param arg1 The replacement for placeholder {1} in the message * @param arg2 The replacement for placeholder {2} in the message */ - public String getMessage(Locale locale, - String key, Object arg0, Object arg1, - Object arg2) { - - Object args[] = new Object[3]; - args[0] = arg0; - args[1] = arg1; - args[2] = arg2; - return (getMessage(locale, key, args)); + public String getMessage( + Locale locale, + String key, + Object arg0, + Object arg1, + Object arg2) { + return this.getMessage(locale, key, new Object[] { arg0, arg1, arg2 }); } - /** * Returns a text message after parametric replacement of the specified * parameter placeholders. A null string result will never be returned @@ -443,7 +412,6 @@ return this.getMessage(locale, key, new Object[] { arg0, arg1, arg2, arg3 }); } - /** * Return <code>true</code> if there is a defined message for the specified * key in the system default locale. @@ -452,11 +420,10 @@ */ public boolean isPresent(String key) { - return (isPresent(null, key)); + return this.isPresent(null, key); } - /** * Return <code>true</code> if there is a defined message for the specified * key in the specified Locale. @@ -468,18 +435,20 @@ public boolean isPresent(Locale locale, String key) { String message = getMessage(locale, key); - if (message == null) - return (false); - else if (message.startsWith("???") && message.endsWith("???")) - return (false); // FIXME - Only valid for default implementation - else - return (true); - } + if (message == null) { + return false; + } else if (message.startsWith("???") && message.endsWith("???")) { + return false; // FIXME - Only valid for default implementation - // ------------------------------------------------------ Protected Methods + } else { + return true; + } + } + + // ------------------------------------------------------ Protected Methods /** * Escape any single quote characters that are included in the specified @@ -489,20 +458,26 @@ */ protected String escape(String string) { - if ((string == null) || (string.indexOf('\'') < 0)) - return (string); + if ((string == null) || (string.indexOf('\'') < 0)) { + return string; + } + int n = string.length(); StringBuffer sb = new StringBuffer(n); + for (int i = 0; i < n; i++) { char ch = string.charAt(i); - if (ch == '\'') + + if (ch == '\'') { sb.append('\''); + } + sb.append(ch); } - return (sb.toString()); - } + return sb.toString(); + } /** * Compute and return a key to be used in caching information by a Locale. @@ -512,17 +487,9 @@ * @param locale The locale for which a key is desired */ protected String localeKey(Locale locale) { - - if (locale == null) - return (""); - // else if (locale.equals(defaultLocale)) - // return (""); - else - return (locale.toString()); - + return (locale == null) ? "" : locale.toString(); } - /** * Compute and return a key to be used in caching information * by Locale and message key. @@ -536,7 +503,6 @@ } - /** * Compute and return a key to be used in caching information * by locale key and message key. @@ -550,33 +516,29 @@ } - // --------------------------------------------------------- Static Methods - /** * The default MessageResourcesFactory used to create MessageResources * instances. */ protected static MessageResourcesFactory defaultFactory = null; - /** * Create and return an instance of <code>MessageResources</code> for the * created by the default <code>MessageResourcesFactory</code>. * * @param config Configuration parameter for this message bundle. */ - public synchronized static MessageResources - getMessageResources(String config) { + public synchronized static MessageResources getMessageResources(String config) { - if (defaultFactory == null) + if (defaultFactory == null) { defaultFactory = MessageResourcesFactory.createFactory(); - return defaultFactory.createResources(config); + } + return defaultFactory.createResources(config); } - /** * Log a message to the Writer that has been configured for our use. * @@ -586,7 +548,6 @@ log.debug(message); } - /** * Log a message and exception to the Writer that has been configured * for our use. @@ -597,6 +558,5 @@ public void log(String message, Throwable throwable) { log.debug(message, throwable); } - }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]