KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > support > MessageSourceAccessor


1 /*
2  * Copyright 2002-2005 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
17 package org.springframework.context.support;
18
19 import java.util.Locale JavaDoc;
20
21 import org.springframework.context.MessageSource;
22 import org.springframework.context.MessageSourceResolvable;
23 import org.springframework.context.NoSuchMessageException;
24 import org.springframework.context.i18n.LocaleContextHolder;
25
26 /**
27  * Helper class for easy access to messages from a MessageSource,
28  * providing various overloaded getMessage methods.
29  *
30  * <p>Available from ApplicationObjectSupport, but also reusable
31  * as a standalone helper to delegate to in application objects.
32  *
33  * @author Juergen Hoeller
34  * @since 23.10.2003
35  * @see ApplicationObjectSupport#getMessageSourceAccessor
36  */

37 public class MessageSourceAccessor {
38
39     private final MessageSource messageSource;
40
41     private final Locale JavaDoc defaultLocale;
42
43     /**
44      * Create a new MessageSourceAccessor, using LocaleContextHolder's locale
45      * as default locale.
46      * @param messageSource the MessageSource to wrap
47      * @see org.springframework.context.i18n.LocaleContextHolder#getLocale()
48      */

49     public MessageSourceAccessor(MessageSource messageSource) {
50         this.messageSource = messageSource;
51         this.defaultLocale = null;
52     }
53
54     /**
55      * Create a new MessageSourceAccessor, using the given default locale.
56      * @param messageSource the MessageSource to wrap
57      * @param defaultLocale the default locale to use for message access
58      */

59     public MessageSourceAccessor(MessageSource messageSource, Locale JavaDoc defaultLocale) {
60         this.messageSource = messageSource;
61         this.defaultLocale = defaultLocale;
62     }
63
64     /**
65      * Return the default locale to use if no explicit locale has been given.
66      * <p>The default implementation returns the default locale passed into the
67      * corresponding constructor, or LocaleContextHolder's locale as fallback.
68      * Can be overridden in subclasses.
69      * @see #MessageSourceAccessor(org.springframework.context.MessageSource, java.util.Locale)
70      * @see org.springframework.context.i18n.LocaleContextHolder#getLocale()
71      */

72     protected Locale JavaDoc getDefaultLocale() {
73         return (this.defaultLocale != null ? this.defaultLocale : LocaleContextHolder.getLocale());
74     }
75
76     /**
77      * Retrieve the message for the given code and the default Locale.
78      * @param code code of the message
79      * @param defaultMessage String to return if the lookup fails
80      * @return the message
81      */

82     public String JavaDoc getMessage(String JavaDoc code, String JavaDoc defaultMessage) {
83         return this.messageSource.getMessage(code, null, defaultMessage, getDefaultLocale());
84     }
85
86     /**
87      * Retrieve the message for the given code and the given Locale.
88      * @param code code of the message
89      * @param defaultMessage String to return if the lookup fails
90      * @param locale Locale in which to do lookup
91      * @return the message
92      */

93     public String JavaDoc getMessage(String JavaDoc code, String JavaDoc defaultMessage, Locale JavaDoc locale) {
94         return this.messageSource.getMessage(code, null, defaultMessage, locale);
95     }
96
97     /**
98      * Retrieve the message for the given code and the default Locale.
99      * @param code code of the message
100      * @param args arguments for the message, or <code>null</code> if none
101      * @param defaultMessage String to return if the lookup fails
102      * @return the message
103      */

104     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args, String JavaDoc defaultMessage) {
105         return this.messageSource.getMessage(code, args, defaultMessage, getDefaultLocale());
106     }
107
108     /**
109      * Retrieve the message for the given code and the given Locale.
110      * @param code code of the message
111      * @param args arguments for the message, or <code>null</code> if none
112      * @param defaultMessage String to return if the lookup fails
113      * @param locale Locale in which to do lookup
114      * @return the message
115      */

116     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args, String JavaDoc defaultMessage, Locale JavaDoc locale) {
117         return this.messageSource.getMessage(code, args, defaultMessage, locale);
118     }
119
120     /**
121      * Retrieve the message for the given code and the default Locale.
122      * @param code code of the message
123      * @return the message
124      * @throws org.springframework.context.NoSuchMessageException if not found
125      */

126     public String JavaDoc getMessage(String JavaDoc code) throws NoSuchMessageException {
127         return this.messageSource.getMessage(code, null, getDefaultLocale());
128     }
129
130     /**
131      * Retrieve the message for the given code and the given Locale.
132      * @param code code of the message
133      * @param locale Locale in which to do lookup
134      * @return the message
135      * @throws org.springframework.context.NoSuchMessageException if not found
136      */

137     public String JavaDoc getMessage(String JavaDoc code, Locale JavaDoc locale) throws NoSuchMessageException {
138         return this.messageSource.getMessage(code, null, locale);
139     }
140
141     /**
142      * Retrieve the message for the given code and the default Locale.
143      * @param code code of the message
144      * @param args arguments for the message, or <code>null</code> if none
145      * @return the message
146      * @throws org.springframework.context.NoSuchMessageException if not found
147      */

148     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args) throws NoSuchMessageException {
149         return this.messageSource.getMessage(code, args, getDefaultLocale());
150     }
151
152     /**
153      * Retrieve the message for the given code and the given Locale.
154      * @param code code of the message
155      * @param args arguments for the message, or <code>null</code> if none
156      * @param locale Locale in which to do lookup
157      * @return the message
158      * @throws org.springframework.context.NoSuchMessageException if not found
159      */

160     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args, Locale JavaDoc locale) throws NoSuchMessageException {
161         return this.messageSource.getMessage(code, args, locale);
162     }
163
164     /**
165      * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance)
166      * in the default Locale.
167      * @param resolvable the MessageSourceResolvable
168      * @return the message
169      * @throws org.springframework.context.NoSuchMessageException if not found
170      */

171     public String JavaDoc getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException {
172         return this.messageSource.getMessage(resolvable, getDefaultLocale());
173     }
174
175     /**
176      * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance)
177      * in the given Locale.
178      * @param resolvable the MessageSourceResolvable
179      * @param locale Locale in which to do lookup
180      * @return the message
181      * @throws org.springframework.context.NoSuchMessageException if not found
182      */

183     public String JavaDoc getMessage(MessageSourceResolvable resolvable, Locale JavaDoc locale) throws NoSuchMessageException {
184         return this.messageSource.getMessage(resolvable, locale);
185     }
186
187 }
188
Popular Tags