com.interface21.context.support
Class AbstractNestingMessageSource

java.lang.Object
  |
  +--com.interface21.context.support.AbstractNestingMessageSource
All Implemented Interfaces:
MessageSource, NestingMessageSource
Direct Known Subclasses:
ResourceBundleMessageSource, StaticMessageSource

public abstract class AbstractNestingMessageSource
extends java.lang.Object
implements NestingMessageSource

Abstract implementation of NestingMessageSource interface, making it easy to implement custom MessageSources. Subclasses must implement the abstract resolve() method.
This class does not currently implement caching, thus subclasses can dynamically change messages over time. NOTE: Some methods of this class are based on code from Struts 1.1b3 implementation.

Author:
Rod Johnson

Constructor Summary
AbstractNestingMessageSource()
           
 
Method Summary
protected  java.lang.String escape(java.lang.String string)
          Escape any single quote characters that are included in the specified message string.
protected  java.util.Locale getDefaultLocale()
           
 java.lang.String getMessage(MessageSourceResolvable resolvable, java.util.Locale locale)
          Using all the attributes contained within the MessageSourceResolvable arg that was passed in (except for the locale attribute), try to resolve the message from the MessageSource contained within the Context. NOTE: We must throw a NoSuchMessageException on this method since at the time of calling this method we aren't able to determine if the defaultMessage attribute is null or not.
 java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.util.Locale locale)
          Try to resolve the message.
 java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.lang.String defaultMessage, java.util.Locale locale)
          Try to resolve the message.Return default message if no message was found
protected  java.lang.String localeKey(java.util.Locale locale)
          Compute and return a key to be used in caching information by a Locale.
protected  java.lang.String messageKey(java.util.Locale locale, java.lang.String key)
          Compute and return a key to be used in caching information by Locale and message key.
protected  java.lang.String messageKey(java.lang.String localeKey, java.lang.String key)
          Compute and return a key to be used in caching information by locale key and message key.
protected abstract  java.lang.String resolve(java.lang.String code, java.util.Locale locale)
          Subclasses must implement this method to resolve a message
 void setParent(MessageSource parent)
          Set the parent that will be used to try to resolve messages that this object can't resolve.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractNestingMessageSource

public AbstractNestingMessageSource()
Method Detail

setParent

public final void setParent(MessageSource parent)
Description copied from interface: NestingMessageSource
Set the parent that will be used to try to resolve messages that this object can't resolve.
Specified by:
setParent in interface NestingMessageSource
Following copied from interface: com.interface21.context.NestingMessageSource
Parameters:
parent - parent MessageSource that will be used to resolve messages that this object can't resolve. May be null, in which case no further resolution is possible.

getMessage

public final java.lang.String getMessage(java.lang.String code,
                                         java.lang.Object[] args,
                                         java.lang.String defaultMessage,
                                         java.util.Locale locale)
Try to resolve the message.Return default message if no message was found
Specified by:
getMessage in interface MessageSource
Parameters:
code - code to lookup up, such as 'calculator.noRateSet'
locale - Locale in which to do lookup
args - Array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message).
defaultMessage - String to return if the lookup fails
Returns:
a resolved message if the lookup is successful; otherwise return the default message passed as a parameter
See Also:
java.text.MessageFormat

getMessage

public java.lang.String getMessage(MessageSourceResolvable resolvable,
                                   java.util.Locale locale)
                            throws NoSuchMessageException
Using all the attributes contained within the MessageSourceResolvable arg that was passed in (except for the locale attribute), try to resolve the message from the MessageSource contained within the Context.

NOTE: We must throw a NoSuchMessageException on this method since at the time of calling this method we aren't able to determine if the defaultMessage attribute is null or not.

Specified by:
getMessage in interface MessageSource
Parameters:
resolvable - Value object storing 4 attributes required to properly resolve a message.
locale - Locale to be used as the "driver" to figuring out what message to return.
Returns:
message Resolved message.
Throws:
NoSuchMessageException - not found in any locale
See Also:
java.text.MessageFormat

getMessage

public final java.lang.String getMessage(java.lang.String code,
                                         java.lang.Object[] args,
                                         java.util.Locale locale)
                                  throws NoSuchMessageException
Try to resolve the message. Treat as an error if the message can't be found.
Specified by:
getMessage in interface MessageSource
Parameters:
code - code to lookup up, such as 'calculator.noRateSet'
locale - Locale in which to do lookup
args - Array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message).
Returns:
message
Throws:
NoSuchMessageException - not found in any locale
See Also:
java.text.MessageFormat

resolve

protected abstract java.lang.String resolve(java.lang.String code,
                                            java.util.Locale locale)
                                     throws java.lang.Exception
Subclasses must implement this method to resolve a message
Parameters:
code - code of the message to resolve
locale - locale to resolve the code for. Subclasses are encouraged to support internationalization.
Returns:
the message, or null if not found
Throws:
java.lang.Exception - if there's an error resolving the message. Note that failure to find a message for the code is not an error.

getDefaultLocale

protected java.util.Locale getDefaultLocale()

localeKey

protected java.lang.String localeKey(java.util.Locale locale)
Compute and return a key to be used in caching information by a Locale. NOTE - The locale key for the default Locale in our environment is a zero length String.
Parameters:
locale - The locale for which a key is desired

messageKey

protected java.lang.String messageKey(java.util.Locale locale,
                                      java.lang.String key)
Compute and return a key to be used in caching information by Locale and message key.
Parameters:
locale - The Locale for which this format key is calculated
key - The message key for which this format key is calculated

messageKey

protected java.lang.String messageKey(java.lang.String localeKey,
                                      java.lang.String key)
Compute and return a key to be used in caching information by locale key and message key.
Parameters:
localeKey - The locale key for which this cache key is calculated
key - The message key for which this cache key is calculated

escape

protected java.lang.String escape(java.lang.String string)
Escape any single quote characters that are included in the specified message string.
Parameters:
string - The string to be escaped


Rod Johnson and Spring contributors 2001-2003.