1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.context.support;
  17. import java.text.MessageFormat;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Locale;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.springframework.context.HierarchicalMessageSource;
  24. import org.springframework.context.MessageSource;
  25. import org.springframework.context.MessageSourceResolvable;
  26. import org.springframework.context.NoSuchMessageException;
  27. /**
  28. * Abstract implementation of HierarchicalMessageSource interface,
  29. * making it easy to implement a custom MessageSource.
  30. * Subclasses must implement the abstract resolveCode method.
  31. *
  32. * <p>Supports not only MessageSourceResolvables as primary messages
  33. * but also resolution of message arguments that are in turn
  34. * MessageSourceResolvables themselves.
  35. *
  36. * <p>This class does not implement caching, thus subclasses can
  37. * dynamically change messages over time. Subclasses are encouraged
  38. * to cache their messages in a modification-aware fashion.
  39. *
  40. * @author Rod Johnson
  41. * @author Juergen Hoeller
  42. * @see #resolveCode
  43. */
  44. public abstract class AbstractMessageSource implements HierarchicalMessageSource {
  45. protected final Log logger = LogFactory.getLog(getClass());
  46. private MessageSource parentMessageSource;
  47. private boolean useCodeAsDefaultMessage = false;
  48. public void setParentMessageSource(MessageSource parent) {
  49. this.parentMessageSource = parent;
  50. }
  51. public MessageSource getParentMessageSource() {
  52. return parentMessageSource;
  53. }
  54. /**
  55. * Set whether to use the message code as default message instead of
  56. * throwing a NoSuchMessageException. Useful for development and debugging.
  57. * Default is false.
  58. * <p>Note: In case of a MessageSourceResolvable with multiple codes
  59. * (like a FieldError) and a MessageSource that has a parent MessageSource,
  60. * do <i>not</i> activate "useCodeAsDefaultMessage" in the <i>parent</i>:
  61. * Else, you'll get the first code returned as message by the parent,
  62. * without attempts to check further codes.
  63. * @see #getMessage(String, Object[], Locale)
  64. * @see org.springframework.validation.FieldError
  65. */
  66. public void setUseCodeAsDefaultMessage(boolean useCodeAsDefaultMessage) {
  67. this.useCodeAsDefaultMessage = useCodeAsDefaultMessage;
  68. }
  69. public final String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
  70. String msg = getMessageInternal(code, args, locale);
  71. if (msg != null) {
  72. return msg;
  73. }
  74. if (defaultMessage == null && this.useCodeAsDefaultMessage) {
  75. return code;
  76. }
  77. return defaultMessage;
  78. }
  79. public final String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
  80. String msg = getMessageInternal(code, args, locale);
  81. if (msg != null) {
  82. return msg;
  83. }
  84. if (this.useCodeAsDefaultMessage) {
  85. return code;
  86. }
  87. throw new NoSuchMessageException(code, locale);
  88. }
  89. public final String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
  90. String[] codes = resolvable.getCodes();
  91. if (codes == null) {
  92. throw new NoSuchMessageException(null, locale);
  93. }
  94. for (int i = 0; i < codes.length; i++) {
  95. String msg = getMessageInternal(codes[i], resolvable.getArguments(), locale);
  96. if (msg != null) {
  97. return msg;
  98. }
  99. }
  100. if (resolvable.getDefaultMessage() != null) {
  101. return resolvable.getDefaultMessage();
  102. }
  103. if (this.useCodeAsDefaultMessage && codes.length > 0) {
  104. return codes[0];
  105. }
  106. throw new NoSuchMessageException(codes.length > 0 ? codes[codes.length - 1] : null, locale);
  107. }
  108. /**
  109. * Resolve the given code and arguments as message in the given Locale,
  110. * returning null if not found. Does <i>not</i> fall back to the code
  111. * as default message. Invoked by getMessage methods.
  112. * @param code the code to lookup up, such as 'calculator.noRateSet'. Users of
  113. * this class are encouraged to base message names on the relevant fully
  114. * qualified class name, thus avoiding conflict and ensuring maximum clarity.
  115. * @param args array of arguments that will be filled in for params within
  116. * the message (params look like "{0}", "{1,date}", "{2,time}" within a message),
  117. * or null if none.
  118. * @param locale the Locale in which to do the lookup
  119. * @return the resolved message, or null if not found
  120. * @see #getMessage(String, Object[], String, Locale)
  121. * @see #getMessage(String, Object[], Locale)
  122. * @see #getMessage(MessageSourceResolvable, Locale)
  123. * @see #setUseCodeAsDefaultMessage
  124. */
  125. protected String getMessageInternal(String code, Object[] args, Locale locale) {
  126. if (code == null) {
  127. return null;
  128. }
  129. if (locale == null) {
  130. locale = Locale.getDefault();
  131. }
  132. MessageFormat messageFormat = resolveCode(code, locale);
  133. if (messageFormat != null) {
  134. return messageFormat.format(resolveArguments(args, locale));
  135. }
  136. else if (this.parentMessageSource != null) {
  137. // check parent MessageSource, returning null if not found there
  138. try {
  139. return this.parentMessageSource.getMessage(code, args, locale);
  140. }
  141. catch (NoSuchMessageException ex) {
  142. return null;
  143. }
  144. }
  145. else {
  146. return null;
  147. }
  148. }
  149. /**
  150. * Search through the given array of objects, find any
  151. * MessageSourceResolvable objects and resolve them.
  152. * <p>Allows for messages to have MessageSourceResolvables as arguments.
  153. * @param args array of arguments for a message
  154. * @param locale the locale to resolve through
  155. * @return an array of arguments with any MessageSourceResolvables resolved
  156. */
  157. protected Object[] resolveArguments(Object[] args, Locale locale) {
  158. if (args == null) {
  159. return new Object[0];
  160. }
  161. List resolvedArgs = new ArrayList();
  162. for (int i = 0; i < args.length; i++) {
  163. if (args[i] instanceof MessageSourceResolvable) {
  164. resolvedArgs.add(getMessage((MessageSourceResolvable) args[i],
  165. locale));
  166. }
  167. else {
  168. resolvedArgs.add(args[i]);
  169. }
  170. }
  171. return resolvedArgs.toArray(new Object[resolvedArgs.size()]);
  172. }
  173. /**
  174. * Create a MessageFormat for the given message and Locale.
  175. * <p>This implementation creates an empty MessageFormat first,
  176. * populating it with Locale and pattern afterwards, to stay
  177. * compatible with J2SE 1.3.
  178. * @param msg the message to create a MessageFormat for
  179. * @param locale the Locale to create a MessageFormat for
  180. * @return the MessageFormat instance
  181. */
  182. protected MessageFormat createMessageFormat(String msg, Locale locale) {
  183. if (logger.isDebugEnabled()) {
  184. logger.debug("Creating MessageFormat for pattern [" + msg + "] and locale '" + locale + "'");
  185. }
  186. MessageFormat messageFormat = new MessageFormat("");
  187. messageFormat.setLocale(locale);
  188. messageFormat.applyPattern(msg);
  189. return messageFormat;
  190. }
  191. /**
  192. * Subclasses must implement this method to resolve a message.
  193. * <p>Returns a MessageFormat instance rather than a message String,
  194. * to allow for appropriate caching of MessageFormats in subclasses.
  195. * @param code the code of the message to resolve
  196. * @param locale the Locale to resolve the code for
  197. * (subclasses are encouraged to support internationalization)
  198. * @return the MessageFormat for the message, or null if not found
  199. */
  200. protected abstract MessageFormat resolveCode(String code, Locale locale);
  201. }