001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork;
006:
007: import com.opensymphony.xwork.util.LocalizedTextUtil;
008: import com.opensymphony.xwork.util.OgnlValueStack;
009:
010: import java.util.*;
011:
012: /**
013: * Default TextProvider implementation.
014: *
015: * @author Jason Carreira
016: * @author Rainer Hermanns
017: */
018: public class TextProviderSupport implements TextProvider {
019:
020: private Class clazz;
021: private LocaleProvider localeProvider;
022: private ResourceBundle bundle;
023:
024: /**
025: * Constructor.
026: *
027: * @param clazz a clazz to use for reading the resource bundle.
028: * @param provider a locale provider.
029: */
030: public TextProviderSupport(Class clazz, LocaleProvider provider) {
031: this .clazz = clazz;
032: this .localeProvider = provider;
033: }
034:
035: /**
036: * Constructor.
037: *
038: * @param bundle the resource bundle.
039: * @param provider a locale provider.
040: */
041: public TextProviderSupport(ResourceBundle bundle,
042: LocaleProvider provider) {
043: this .bundle = bundle;
044: this .localeProvider = provider;
045: }
046:
047: /**
048: * Checks if a key is available in the resource bundles associated with this action.
049: * The resource bundles are searched, starting with the one associated
050: * with this particular action, and testing all its superclasses' bundles.
051: * It will stop once a bundle is found that contains the given text. This gives
052: * a cascading style that allow global texts to be defined for an application base
053: * class.
054: */
055: public boolean hasKey(String key) {
056: String message = null;
057: OgnlValueStack stack = ActionContext.getContext()
058: .getValueStack();
059: if (clazz != null) {
060: message = LocalizedTextUtil.findText(clazz, key,
061: getLocale(), null, new Object[0], stack, false);
062: } else {
063: message = LocalizedTextUtil.findText(bundle, key,
064: getLocale(), null, new Object[0], stack, false);
065: }
066: return message == null ? false : true;
067: }
068:
069: /**
070: * Get a text from the resource bundles associated with this action.
071: * The resource bundles are searched, starting with the one associated
072: * with this particular action, and testing all its superclasses' bundles.
073: * It will stop once a bundle is found that contains the given text. This gives
074: * a cascading style that allow global texts to be defined for an application base
075: * class.
076: *
077: * @param key name of text to be found
078: * @return value of named text
079: */
080: public String getText(String key) {
081: return getText(key, key, Collections.EMPTY_LIST);
082: }
083:
084: /**
085: * Get a text from the resource bundles associated with this action.
086: * The resource bundles are searched, starting with the one associated
087: * with this particular action, and testing all its superclasses' bundles.
088: * It will stop once a bundle is found that contains the given text. This gives
089: * a cascading style that allow global texts to be defined for an application base
090: * class. If no text is found for this text name, the default value is returned.
091: *
092: * @param key name of text to be found
093: * @param defaultValue the default value which will be returned if no text is found
094: * @return value of named text
095: */
096: public String getText(String key, String defaultValue) {
097: return getText(key, defaultValue, Collections.EMPTY_LIST);
098: }
099:
100: /**
101: * Get a text from the resource bundles associated with this action.
102: * The resource bundles are searched, starting with the one associated
103: * with this particular action, and testing all its superclasses' bundles.
104: * It will stop once a bundle is found that contains the given text. This gives
105: * a cascading style that allow global texts to be defined for an application base
106: * class. If no text is found for this text name, the default value is returned.
107: *
108: * @param key name of text to be found
109: * @param defaultValue the default value which will be returned if no text is found
110: * @return value of named text
111: */
112: public String getText(String key, String defaultValue, String arg) {
113: List args = new ArrayList();
114: args.add(arg);
115: return getText(key, defaultValue, args);
116: }
117:
118: /**
119: * Get a text from the resource bundles associated with this action.
120: * The resource bundles are searched, starting with the one associated
121: * with this particular action, and testing all its superclasses' bundles.
122: * It will stop once a bundle is found that contains the given text. This gives
123: * a cascading style that allow global texts to be defined for an application base
124: * class. If no text is found for this text name, the default value is returned.
125: *
126: * @param key name of text to be found
127: * @param args a List of args to be used in a MessageFormat message
128: * @return value of named text
129: */
130: public String getText(String key, List args) {
131: return getText(key, key, args);
132: }
133:
134: /**
135: * Get a text from the resource bundles associated with this action.
136: * The resource bundles are searched, starting with the one associated
137: * with this particular action, and testing all its superclasses' bundles.
138: * It will stop once a bundle is found that contains the given text. This gives
139: * a cascading style that allow global texts to be defined for an application base
140: * class. If no text is found for this text name, the default value is returned.
141: *
142: * @param key name of text to be found
143: * @param args an array of args to be used in a MessageFormat message
144: * @return value of named text
145: */
146: public String getText(String key, String[] args) {
147: return getText(key, key, args);
148: }
149:
150: /**
151: * Get a text from the resource bundles associated with this action.
152: * The resource bundles are searched, starting with the one associated
153: * with this particular action, and testing all its superclasses' bundles.
154: * It will stop once a bundle is found that contains the given text. This gives
155: * a cascading style that allow global texts to be defined for an application base
156: * class. If no text is found for this text name, the default value is returned.
157: *
158: * @param key name of text to be found
159: * @param defaultValue the default value which will be returned if no text is found
160: * @param args a List of args to be used in a MessageFormat message
161: * @return value of named text
162: */
163: public String getText(String key, String defaultValue, List args) {
164: Object[] argsArray = ((args != null && !args
165: .equals(Collections.EMPTY_LIST)) ? args.toArray()
166: : null);
167: if (clazz != null) {
168: return LocalizedTextUtil.findText(clazz, key, getLocale(),
169: defaultValue, argsArray);
170: } else {
171: return LocalizedTextUtil.findText(bundle, key, getLocale(),
172: defaultValue, argsArray);
173: }
174: }
175:
176: /**
177: * Get a text from the resource bundles associated with this action.
178: * The resource bundles are searched, starting with the one associated
179: * with this particular action, and testing all its superclasses' bundles.
180: * It will stop once a bundle is found that contains the given text. This gives
181: * a cascading style that allow global texts to be defined for an application base
182: * class. If no text is found for this text name, the default value is returned.
183: *
184: * @param key name of text to be found
185: * @param defaultValue the default value which will be returned if no text is found
186: * @param args an array of args to be used in a MessageFormat message
187: * @return value of named text
188: */
189: public String getText(String key, String defaultValue, String[] args) {
190: if (clazz != null) {
191: return LocalizedTextUtil.findText(clazz, key, getLocale(),
192: defaultValue, args);
193: } else {
194: return LocalizedTextUtil.findText(bundle, key, getLocale(),
195: 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,
212: OgnlValueStack stack) {
213: Object[] argsArray = ((args != null) ? args.toArray() : null);
214: Locale locale = (Locale) stack.getContext().get(
215: ActionContext.LOCALE);
216: if (locale == null) {
217: locale = getLocale();
218: }
219: if (clazz != null) {
220: return LocalizedTextUtil.findText(clazz, key, locale,
221: defaultValue, argsArray, stack);
222: } else {
223: return LocalizedTextUtil.findText(bundle, key, locale,
224: defaultValue, argsArray, stack);
225: }
226: }
227:
228: /**
229: * Gets a message based on a key using the supplied args, as defined in
230: * {@link java.text.MessageFormat}, or, if the message is not found, a supplied
231: * default value is returned. Instead of using the value stack in the ActionContext
232: * this version of the getText() method uses the provided value stack.
233: *
234: * @param key the resource bundle key that is to be searched for
235: * @param defaultValue the default value which will be returned if no message is found
236: * @param args an array args to be used in a {@link java.text.MessageFormat} message
237: * @param stack the value stack to use for finding the text
238: * @return the message as found in the resource bundle, or defaultValue if none is found
239: */
240: public String getText(String key, String defaultValue,
241: String[] args, OgnlValueStack stack) {
242: Locale locale = (Locale) stack.getContext().get(
243: ActionContext.LOCALE);
244: if (locale == null) {
245: locale = getLocale();
246: }
247: if (clazz != null) {
248: return LocalizedTextUtil.findText(clazz, key, locale,
249: defaultValue, args, stack);
250: } else {
251: return LocalizedTextUtil.findText(bundle, key, locale,
252: defaultValue, args, stack);
253: }
254:
255: }
256:
257: /**
258: * Get the named bundle.
259: * <p/>
260: * You can override the getLocale() methodName to change the behaviour of how
261: * to choose locale for the bundles that are returned. Typically you would
262: * use the TextProvider interface to get the users configured locale, or use
263: * your own methodName to allow the user to select the locale and store it in
264: * the session (by using the SessionAware interface).
265: *
266: * @param aBundleName bundle name
267: * @return a resource bundle
268: */
269: public ResourceBundle getTexts(String aBundleName) {
270: return LocalizedTextUtil.findResourceBundle(aBundleName,
271: getLocale());
272: }
273:
274: /**
275: * Get the resource bundle associated with this action.
276: * This will be based on the actual subclass that is used.
277: *
278: * @return resouce bundle
279: */
280: public ResourceBundle getTexts() {
281: if (clazz != null) {
282: return getTexts(clazz.getName());
283: }
284: return bundle;
285: }
286:
287: /**
288: * Get's the locale from the localeProvider.
289: *
290: * @return the locale from the localeProvider.
291: */
292: private Locale getLocale() {
293: return localeProvider.getLocale();
294: }
295: }
|