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.util.Locale;
  18. import org.springframework.context.MessageSource;
  19. import org.springframework.context.MessageSourceResolvable;
  20. import org.springframework.context.NoSuchMessageException;
  21. /**
  22. * Helper class for easy access to messages from a MessageSource,
  23. * providing various overloaded getMessage methods.
  24. *
  25. * <p>Available from ApplicationObjectSupport, but also reusable
  26. * as a standalone helper to delegate to in application objects.
  27. *
  28. * @author Juergen Hoeller
  29. * @since 23.10.2003
  30. * @see ApplicationObjectSupport#getMessageSourceAccessor
  31. */
  32. public class MessageSourceAccessor {
  33. private final MessageSource messageSource;
  34. private final Locale defaultLocale;
  35. /**
  36. * Create a new MessageSourceAccessor, using the system Locale
  37. * as default Locale.
  38. * @param messageSource the MessageSource to wrap
  39. */
  40. public MessageSourceAccessor(MessageSource messageSource) {
  41. this.messageSource = messageSource;
  42. this.defaultLocale = Locale.getDefault();
  43. }
  44. /**
  45. * Create a new MessageSourceAccessor, using the given default locale.
  46. * @param messageSource the MessageSource to wrap
  47. * @param defaultLocale the default locale to use for message access
  48. */
  49. public MessageSourceAccessor(MessageSource messageSource, Locale defaultLocale) {
  50. this.messageSource = messageSource;
  51. this.defaultLocale = defaultLocale;
  52. }
  53. /**
  54. * Retrieve the message for the given code and the default Locale.
  55. * @param code code of the message
  56. * @param defaultMessage String to return if the lookup fails
  57. * @return the message
  58. */
  59. public String getMessage(String code, String defaultMessage) {
  60. return this.messageSource.getMessage(code, null, defaultMessage, this.defaultLocale);
  61. }
  62. /**
  63. * Retrieve the message for the given code and the given Locale.
  64. * @param code code of the message
  65. * @param defaultMessage String to return if the lookup fails
  66. * @param locale Locale in which to do lookup
  67. * @return the message
  68. */
  69. public String getMessage(String code, String defaultMessage, Locale locale) {
  70. return this.messageSource.getMessage(code, null, defaultMessage, locale);
  71. }
  72. /**
  73. * Retrieve the message for the given code and the default Locale.
  74. * @param code code of the message
  75. * @param args arguments for the message, or null if none
  76. * @param defaultMessage String to return if the lookup fails
  77. * @return the message
  78. */
  79. public String getMessage(String code, Object[] args, String defaultMessage) {
  80. return this.messageSource.getMessage(code, args, defaultMessage, this.defaultLocale);
  81. }
  82. /**
  83. * Retrieve the message for the given code and the given Locale.
  84. * @param code code of the message
  85. * @param args arguments for the message, or null if none
  86. * @param defaultMessage String to return if the lookup fails
  87. * @param locale Locale in which to do lookup
  88. * @return the message
  89. */
  90. public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
  91. return this.messageSource.getMessage(code, args, defaultMessage, locale);
  92. }
  93. /**
  94. * Retrieve the message for the given code and the default Locale.
  95. * @param code code of the message
  96. * @return the message
  97. * @throws org.springframework.context.NoSuchMessageException if not found
  98. */
  99. public String getMessage(String code) throws NoSuchMessageException {
  100. return this.messageSource.getMessage(code, null, this.defaultLocale);
  101. }
  102. /**
  103. * Retrieve the message for the given code and the given Locale.
  104. * @param code code of the message
  105. * @param locale Locale in which to do lookup
  106. * @return the message
  107. * @throws org.springframework.context.NoSuchMessageException if not found
  108. */
  109. public String getMessage(String code, Locale locale) throws NoSuchMessageException {
  110. return this.messageSource.getMessage(code, null, locale);
  111. }
  112. /**
  113. * Retrieve the message for the given code and the default Locale.
  114. * @param code code of the message
  115. * @param args arguments for the message, or null if none
  116. * @return the message
  117. * @throws org.springframework.context.NoSuchMessageException if not found
  118. */
  119. public String getMessage(String code, Object[] args) throws NoSuchMessageException {
  120. return this.messageSource.getMessage(code, args, this.defaultLocale);
  121. }
  122. /**
  123. * Retrieve the message for the given code and the given Locale.
  124. * @param code code of the message
  125. * @param args arguments for the message, or null if none
  126. * @param locale Locale in which to do lookup
  127. * @return the message
  128. * @throws org.springframework.context.NoSuchMessageException if not found
  129. */
  130. public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
  131. return this.messageSource.getMessage(code, args, locale);
  132. }
  133. /**
  134. * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance)
  135. * in the default Locale.
  136. * @param resolvable the MessageSourceResolvable
  137. * @return the message
  138. * @throws org.springframework.context.NoSuchMessageException if not found
  139. */
  140. public String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException {
  141. return this.messageSource.getMessage(resolvable, this.defaultLocale);
  142. }
  143. /**
  144. * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance)
  145. * in the given Locale.
  146. * @param resolvable the MessageSourceResolvable
  147. * @param locale Locale in which to do lookup
  148. * @return the message
  149. * @throws org.springframework.context.NoSuchMessageException if not found
  150. */
  151. public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
  152. return this.messageSource.getMessage(resolvable, locale);
  153. }
  154. }