001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.context.support;
018:
019: import java.util.Locale;
020:
021: import org.springframework.context.MessageSource;
022: import org.springframework.context.MessageSourceResolvable;
023: import org.springframework.context.NoSuchMessageException;
024: import org.springframework.context.i18n.LocaleContextHolder;
025:
026: /**
027: * Helper class for easy access to messages from a MessageSource,
028: * providing various overloaded getMessage methods.
029: *
030: * <p>Available from ApplicationObjectSupport, but also reusable
031: * as a standalone helper to delegate to in application objects.
032: *
033: * @author Juergen Hoeller
034: * @since 23.10.2003
035: * @see ApplicationObjectSupport#getMessageSourceAccessor
036: */
037: public class MessageSourceAccessor {
038:
039: private final MessageSource messageSource;
040:
041: private final Locale defaultLocale;
042:
043: /**
044: * Create a new MessageSourceAccessor, using LocaleContextHolder's locale
045: * as default locale.
046: * @param messageSource the MessageSource to wrap
047: * @see org.springframework.context.i18n.LocaleContextHolder#getLocale()
048: */
049: public MessageSourceAccessor(MessageSource messageSource) {
050: this .messageSource = messageSource;
051: this .defaultLocale = null;
052: }
053:
054: /**
055: * Create a new MessageSourceAccessor, using the given default locale.
056: * @param messageSource the MessageSource to wrap
057: * @param defaultLocale the default locale to use for message access
058: */
059: public MessageSourceAccessor(MessageSource messageSource,
060: Locale defaultLocale) {
061: this .messageSource = messageSource;
062: this .defaultLocale = defaultLocale;
063: }
064:
065: /**
066: * Return the default locale to use if no explicit locale has been given.
067: * <p>The default implementation returns the default locale passed into the
068: * corresponding constructor, or LocaleContextHolder's locale as fallback.
069: * Can be overridden in subclasses.
070: * @see #MessageSourceAccessor(org.springframework.context.MessageSource, java.util.Locale)
071: * @see org.springframework.context.i18n.LocaleContextHolder#getLocale()
072: */
073: protected Locale getDefaultLocale() {
074: return (this .defaultLocale != null ? this .defaultLocale
075: : LocaleContextHolder.getLocale());
076: }
077:
078: /**
079: * Retrieve the message for the given code and the default Locale.
080: * @param code code of the message
081: * @param defaultMessage String to return if the lookup fails
082: * @return the message
083: */
084: public String getMessage(String code, String defaultMessage) {
085: return this .messageSource.getMessage(code, null,
086: defaultMessage, getDefaultLocale());
087: }
088:
089: /**
090: * Retrieve the message for the given code and the given Locale.
091: * @param code code of the message
092: * @param defaultMessage String to return if the lookup fails
093: * @param locale Locale in which to do lookup
094: * @return the message
095: */
096: public String getMessage(String code, String defaultMessage,
097: Locale locale) {
098: return this .messageSource.getMessage(code, null,
099: defaultMessage, locale);
100: }
101:
102: /**
103: * Retrieve the message for the given code and the default Locale.
104: * @param code code of the message
105: * @param args arguments for the message, or <code>null</code> if none
106: * @param defaultMessage String to return if the lookup fails
107: * @return the message
108: */
109: public String getMessage(String code, Object[] args,
110: String defaultMessage) {
111: return this .messageSource.getMessage(code, args,
112: defaultMessage, getDefaultLocale());
113: }
114:
115: /**
116: * Retrieve the message for the given code and the given Locale.
117: * @param code code of the message
118: * @param args arguments for the message, or <code>null</code> if none
119: * @param defaultMessage String to return if the lookup fails
120: * @param locale Locale in which to do lookup
121: * @return the message
122: */
123: public String getMessage(String code, Object[] args,
124: String defaultMessage, Locale locale) {
125: return this .messageSource.getMessage(code, args,
126: defaultMessage, locale);
127: }
128:
129: /**
130: * Retrieve the message for the given code and the default Locale.
131: * @param code code of the message
132: * @return the message
133: * @throws org.springframework.context.NoSuchMessageException if not found
134: */
135: public String getMessage(String code) throws NoSuchMessageException {
136: return this .messageSource.getMessage(code, null,
137: getDefaultLocale());
138: }
139:
140: /**
141: * Retrieve the message for the given code and the given Locale.
142: * @param code code of the message
143: * @param locale Locale in which to do lookup
144: * @return the message
145: * @throws org.springframework.context.NoSuchMessageException if not found
146: */
147: public String getMessage(String code, Locale locale)
148: throws NoSuchMessageException {
149: return this .messageSource.getMessage(code, null, locale);
150: }
151:
152: /**
153: * Retrieve the message for the given code and the default Locale.
154: * @param code code of the message
155: * @param args arguments for the message, or <code>null</code> if none
156: * @return the message
157: * @throws org.springframework.context.NoSuchMessageException if not found
158: */
159: public String getMessage(String code, Object[] args)
160: throws NoSuchMessageException {
161: return this .messageSource.getMessage(code, args,
162: getDefaultLocale());
163: }
164:
165: /**
166: * Retrieve the message for the given code and the given Locale.
167: * @param code code of the message
168: * @param args arguments for the message, or <code>null</code> if none
169: * @param locale Locale in which to do lookup
170: * @return the message
171: * @throws org.springframework.context.NoSuchMessageException if not found
172: */
173: public String getMessage(String code, Object[] args, Locale locale)
174: throws NoSuchMessageException {
175: return this .messageSource.getMessage(code, args, locale);
176: }
177:
178: /**
179: * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance)
180: * in the default Locale.
181: * @param resolvable the MessageSourceResolvable
182: * @return the message
183: * @throws org.springframework.context.NoSuchMessageException if not found
184: */
185: public String getMessage(MessageSourceResolvable resolvable)
186: throws NoSuchMessageException {
187: return this .messageSource.getMessage(resolvable,
188: getDefaultLocale());
189: }
190:
191: /**
192: * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance)
193: * in the given Locale.
194: * @param resolvable the MessageSourceResolvable
195: * @param locale Locale in which to do lookup
196: * @return the message
197: * @throws org.springframework.context.NoSuchMessageException if not found
198: */
199: public String getMessage(MessageSourceResolvable resolvable,
200: Locale locale) throws NoSuchMessageException {
201: return this.messageSource.getMessage(resolvable, locale);
202: }
203:
204: }
|