1 /*
2 * Copyright (c) 2002-2006 by OpenSymphony
3 * All rights reserved.
4 */
5 package com.opensymphony.xwork2;
6
7 import com.opensymphony.xwork2.util.LocalizedTextUtil;
8 import com.opensymphony.xwork2.util.ValueStack;
9
10 import java.util;
11
12
13 /**
14 * Default TextProvider implementation.
15 *
16 * @author Jason Carreira
17 * @author Rainer Hermanns
18 */
19 public class TextProviderSupport implements ResourceBundleTextProvider {
20
21 private Class clazz;
22 private LocaleProvider localeProvider;
23 private ResourceBundle bundle;
24
25 /**
26 * Default constructor
27 */
28 public TextProviderSupport() {
29 }
30
31 /**
32 * Constructor.
33 *
34 * @param clazz a clazz to use for reading the resource bundle.
35 * @param provider a locale provider.
36 */
37 public TextProviderSupport(Class clazz, LocaleProvider provider) {
38 this.clazz = clazz;
39 this.localeProvider = provider;
40 }
41
42 /**
43 * Constructor.
44 *
45 * @param bundle the resource bundle.
46 * @param provider a locale provider.
47 */
48 public TextProviderSupport(ResourceBundle bundle, LocaleProvider provider) {
49 this.bundle = bundle;
50 this.localeProvider = provider;
51 }
52
53 /**
54 * @param bundle the resource bundle.
55 */
56 public void setBundle(ResourceBundle bundle) {
57 this.bundle = bundle;
58 }
59
60 /**
61 * @param clazz a clazz to use for reading the resource bundle.
62 */
63 public void setClazz(Class clazz) {
64 this.clazz = clazz;
65 }
66
67
68 /**
69 * @param localeProvider a locale provider.
70 */
71 public void setLocaleProvider(LocaleProvider localeProvider) {
72 this.localeProvider = localeProvider;
73 }
74
75 /**
76 * Get a text from the resource bundles associated with this action.
77 * The resource bundles are searched, starting with the one associated
78 * with this particular action, and testing all its superclasses' bundles.
79 * It will stop once a bundle is found that contains the given text. This gives
80 * a cascading style that allow global texts to be defined for an application base
81 * class.
82 *
83 * @param key name of text to be found
84 * @return value of named text
85 */
86 public String getText(String key) {
87 return getText(key, key, Collections.EMPTY_LIST);
88 }
89
90 /**
91 * Get a text from the resource bundles associated with this action.
92 * The resource bundles are searched, starting with the one associated
93 * with this particular action, and testing all its superclasses' bundles.
94 * It will stop once a bundle is found that contains the given text. This gives
95 * a cascading style that allow global texts to be defined for an application base
96 * class. If no text is found for this text name, the default value is returned.
97 *
98 * @param key name of text to be found
99 * @param defaultValue the default value which will be returned if no text is found
100 * @return value of named text
101 */
102 public String getText(String key, String defaultValue) {
103 return getText(key, defaultValue, Collections.EMPTY_LIST);
104 }
105
106 /**
107 * Get a text from the resource bundles associated with this action.
108 * The resource bundles are searched, starting with the one associated
109 * with this particular action, and testing all its superclasses' bundles.
110 * It will stop once a bundle is found that contains the given text. This gives
111 * a cascading style that allow global texts to be defined for an application base
112 * class. If no text is found for this text name, the default value is returned.
113 *
114 * @param key name of text to be found
115 * @param defaultValue the default value which will be returned if no text is found
116 * @return value of named text
117 */
118 public String getText(String key, String defaultValue, String arg) {
119 List args = new ArrayList();
120 args.add(arg);
121 return getText(key, defaultValue, args);
122 }
123
124 /**
125 * Get a text from the resource bundles associated with this action.
126 * The resource bundles are searched, starting with the one associated
127 * with this particular action, and testing all its superclasses' bundles.
128 * It will stop once a bundle is found that contains the given text. This gives
129 * a cascading style that allow global texts to be defined for an application base
130 * class. If no text is found for this text name, the default value is returned.
131 *
132 * @param key name of text to be found
133 * @param args a List of args to be used in a MessageFormat message
134 * @return value of named text
135 */
136 public String getText(String key, List args) {
137 return getText(key, key, args);
138 }
139
140 /**
141 * Get a text from the resource bundles associated with this action.
142 * The resource bundles are searched, starting with the one associated
143 * with this particular action, and testing all its superclasses' bundles.
144 * It will stop once a bundle is found that contains the given text. This gives
145 * a cascading style that allow global texts to be defined for an application base
146 * class. If no text is found for this text name, the default value is returned.
147 *
148 * @param key name of text to be found
149 * @param args an array of args to be used in a MessageFormat message
150 * @return value of named text
151 */
152 public String getText(String key, String[] args) {
153 return getText(key, key, args);
154 }
155
156 /**
157 * Get a text from the resource bundles associated with this action.
158 * The resource bundles are searched, starting with the one associated
159 * with this particular action, and testing all its superclasses' bundles.
160 * It will stop once a bundle is found that contains the given text. This gives
161 * a cascading style that allow global texts to be defined for an application base
162 * class. If no text is found for this text name, the default value is returned.
163 *
164 * @param key name of text to be found
165 * @param defaultValue the default value which will be returned if no text is found
166 * @param args a List of args to be used in a MessageFormat message
167 * @return value of named text
168 */
169 public String getText(String key, String defaultValue, List args) {
170 Object[] argsArray = ((args != null && !args.equals(Collections.EMPTY_LIST)) ? args.toArray() : null);
171 if (clazz != null) {
172 return LocalizedTextUtil.findText(clazz, key, getLocale(), defaultValue, argsArray);
173 } else {
174 return LocalizedTextUtil.findText(bundle, key, getLocale(), defaultValue, argsArray);
175 }
176 }
177
178 /**
179 * Get a text from the resource bundles associated with this action.
180 * The resource bundles are searched, starting with the one associated
181 * with this particular action, and testing all its superclasses' bundles.
182 * It will stop once a bundle is found that contains the given text. This gives
183 * a cascading style that allow global texts to be defined for an application base
184 * class. If no text is found for this text name, the default value is returned.
185 *
186 * @param key name of text to be found
187 * @param defaultValue the default value which will be returned if no text is found
188 * @param args an array of args to be used in a MessageFormat message
189 * @return value of named text
190 */
191 public String getText(String key, String defaultValue, String[] args) {
192 if (clazz != null) {
193 return LocalizedTextUtil.findText(clazz, key, getLocale(), defaultValue, args);
194 } else {
195 return LocalizedTextUtil.findText(bundle, key, getLocale(), defaultValue, args);
196 }
197 }
198
199 /**
200 * Gets a message based on a key using the supplied args, as defined in
201 * {@link java.text.MessageFormat}, or, if the message is not found, a supplied
202 * default value is returned. Instead of using the value stack in the ActionContext
203 * this version of the getText() method uses the provided value stack.
204 *
205 * @param key the resource bundle key that is to be searched for
206 * @param defaultValue the default value which will be returned if no message is found
207 * @param args a list args to be used in a {@link java.text.MessageFormat} message
208 * @param stack the value stack to use for finding the text
209 * @return the message as found in the resource bundle, or defaultValue if none is found
210 */
211 public String getText(String key, String defaultValue, List args, ValueStack stack) {
212 Object[] argsArray = ((args != null) ? args.toArray() : null);
213 Locale locale = null;
214 if (stack == null){
215 locale = getLocale();
216 }else{
217 locale = (Locale) stack.getContext().get(ActionContext.LOCALE);
218 }
219 if (locale == null) {
220 locale = getLocale();
221 }
222 if (clazz != null) {
223 return LocalizedTextUtil.findText(clazz, key, locale, defaultValue, argsArray, stack);
224 } else {
225 return LocalizedTextUtil.findText(bundle, key, locale, defaultValue, argsArray, stack);
226 }
227 }
228
229
230 /**
231 * Gets a message based on a key using the supplied args, as defined in
232 * {@link java.text.MessageFormat}, or, if the message is not found, a supplied
233 * default value is returned. Instead of using the value stack in the ActionContext
234 * this version of the getText() method uses the provided value stack.
235 *
236 * @param key the resource bundle key that is to be searched for
237 * @param defaultValue the default value which will be returned if no message is found
238 * @param args an array args to be used in a {@link java.text.MessageFormat} message
239 * @param stack the value stack to use for finding the text
240 * @return the message as found in the resource bundle, or defaultValue if none is found
241 */
242 public String getText(String key, String defaultValue, String[] args, ValueStack stack) {
243 Locale locale = null;
244 if (stack == null){
245 locale = getLocale();
246 }else{
247 locale = (Locale) stack.getContext().get(ActionContext.LOCALE);
248 }
249 if (locale == null) {
250 locale = getLocale();
251 }
252 if (clazz != null) {
253 return LocalizedTextUtil.findText(clazz, key, locale, defaultValue, args, stack);
254 } else {
255 return LocalizedTextUtil.findText(bundle, key, locale, defaultValue, args, stack);
256 }
257
258 }
259
260 /**
261 * Get the named bundle.
262 * <p/>
263 * You can override the getLocale() methodName to change the behaviour of how
264 * to choose locale for the bundles that are returned. Typically you would
265 * use the TextProvider interface to get the users configured locale, or use
266 * your own methodName to allow the user to select the locale and store it in
267 * the session (by using the SessionAware interface).
268 *
269 * @param aBundleName bundle name
270 * @return a resource bundle
271 */
272 public ResourceBundle getTexts(String aBundleName) {
273 return LocalizedTextUtil.findResourceBundle(aBundleName, getLocale());
274 }
275
276 /**
277 * Get the resource bundle associated with this action.
278 * This will be based on the actual subclass that is used.
279 *
280 * @return resouce bundle
281 */
282 public ResourceBundle getTexts() {
283 if (clazz != null) {
284 return getTexts(clazz.getName());
285 }
286 return bundle;
287 }
288
289 /**
290 * Get's the locale from the localeProvider.
291 *
292 * @return the locale from the localeProvider.
293 */
294 private Locale getLocale() {
295 return localeProvider.getLocale();
296 }
297 }