Top Description Fields Constructors Methods
org.springframework.context.support

public abstract Class MessageSourceSupport

extends Object
Class Inheritance
Known Direct Subclasses
org.springframework.context.support.AbstractMessageSource, org.springframework.context.support.DelegatingMessageSource
Imports
java.text.MessageFormat, java.util.Locale, .Map, java.util.concurrent.ConcurrentHashMap, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.util.ObjectUtils

Base class for message source implementations, providing support infrastructure such as java.text.MessageFormat handling but not implementing concrete methods defined in the org.springframework.context.MessageSource.

AbstractMessageSource derives from this class, providing concrete getMessage implementations that delegate to a central template method for message code resolution.

Author
Juergen Hoeller
Since
2.5.5

Field Summary

Modifier and TypeField and Description
private boolean
private static final MessageFormat
protected final Log
logger

Logger available to subclasses.

private final Map<String, Map<Locale, MessageFormat>>
messageFormatsPerMessage

Cache to hold already generated MessageFormats per message.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected MessageFormat

Returns:

the MessageFormat instance
createMessageFormat
(String
the message to create a MessageFormat for
msg
,
Locale
the Locale to create a MessageFormat for
locale
)

Create a MessageFormat for the given message and Locale.

protected String

Returns:

the formatted message (with resolved arguments)
formatMessage
(String
the message to format
msg
,
Object[]
array of arguments that will be filled in for params within the message, or null if none
args
,
Locale
the Locale used for formatting
locale
)

Format the given message String, using cached MessageFormats.

protected boolean
isAlwaysUseMessageFormat()

Return whether to always apply the MessageFormat rules, parsing even messages without arguments.

protected String

Returns:

the rendered default message (with resolved arguments)
renderDefaultMessage
(String
the passed-in default message String
defaultMessage
,
Object[]
array of arguments that will be filled in for params within the message, or null if none.
args
,
Locale
the Locale used for formatting
locale
)

Render the given default message String.

protected Object[]

Returns:

the resolved argument array
resolveArguments
(Object[]
the original argument array
args
,
Locale
the Locale to resolve against
locale
)

Template method for resolving argument objects.

public void
setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat)

Set whether to always apply the MessageFormat rules, parsing even messages without arguments.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

alwaysUseMessageFormatback to summary
private boolean alwaysUseMessageFormat
INVALID_MESSAGE_FORMATback to summary
private static final MessageFormat INVALID_MESSAGE_FORMAT
loggerback to summary
protected final Log logger

Logger available to subclasses.

messageFormatsPerMessageback to summary
private final Map<String, Map<Locale, MessageFormat>> messageFormatsPerMessage

Cache to hold already generated MessageFormats per message. Used for passed-in default messages. MessageFormats for resolved codes are cached on a specific basis in subclasses.

Constructor Detail

MessageSourceSupportback to summary
public MessageSourceSupport()

Method Detail

createMessageFormatback to summary
protected MessageFormat createMessageFormat(String msg, Locale locale)

Create a MessageFormat for the given message and Locale.

Parameters
msg:String

the message to create a MessageFormat for

locale:Locale

the Locale to create a MessageFormat for

Returns:MessageFormat

the MessageFormat instance

formatMessageback to summary
protected String formatMessage(String msg, Object[] args, Locale locale)

Format the given message String, using cached MessageFormats. By default invoked for passed-in default messages, to resolve any argument placeholders found in them.

Parameters
msg:String

the message to format

args:Object[]

array of arguments that will be filled in for params within the message, or null if none

locale:Locale

the Locale used for formatting

Returns:String

the formatted message (with resolved arguments)

isAlwaysUseMessageFormatback to summary
protected boolean isAlwaysUseMessageFormat()

Return whether to always apply the MessageFormat rules, parsing even messages without arguments.

renderDefaultMessageback to summary
protected String renderDefaultMessage(String defaultMessage, Object[] args, Locale locale)

Render the given default message String. The default message is passed in as specified by the caller and can be rendered into a fully formatted default message shown to the user.

The default implementation passes the String to formatMessage, resolving any argument placeholders found in them. Subclasses may override this method to plug in custom processing of default messages.

Parameters
defaultMessage:String

the passed-in default message String

args:Object[]

array of arguments that will be filled in for params within the message, or null if none.

locale:Locale

the Locale used for formatting

Returns:String

the rendered default message (with resolved arguments)

See Also
formatMessage(String, Object[], java.util.Locale)
resolveArgumentsback to summary
protected Object[] resolveArguments(Object[] args, Locale locale)

Template method for resolving argument objects.

The default implementation simply returns the given argument array as-is. Can be overridden in subclasses in order to resolve special argument types.

Parameters
args:Object[]

the original argument array

locale:Locale

the Locale to resolve against

Returns:Object[]

the resolved argument array

setAlwaysUseMessageFormatback to summary
public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat)

Set whether to always apply the MessageFormat rules, parsing even messages without arguments.

Default is false: Messages without arguments are by default returned as-is, without parsing them through MessageFormat. Set this to true to enforce MessageFormat for all messages, expecting all message texts to be written with MessageFormat escaping.

For example, MessageFormat expects a single quote to be escaped as two adjacent single quotes ("''"). If your message texts are all written with such escaping, even when not defining argument placeholders, you need to set this flag to true. Otherwise, only message texts with actual arguments are supposed to be written with MessageFormat escaping.

See Also
java.text.MessageFormat