001: /*
002: * Copyright (c) 2002-2007 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork.validator;
006:
007: import com.opensymphony.xwork.TextProvider;
008: import com.opensymphony.xwork.util.OgnlValueStack;
009:
010: import java.util.*;
011:
012: import org.apache.commons.logging.LogFactory;
013: import org.apache.commons.logging.Log;
014:
015: /**
016: * This is a composite {@link TextProvider} that takes in an array or {@link List} of {@link TextProvider}s, it will
017: * consult each of them in order to get a composite result. To know how each method behaves, please refer to the
018: * javadoc for each methods.
019: *
020: * @author tmjee
021: * @version $Date$ $Id$
022: */
023: public class CompositeTextProvider implements TextProvider {
024:
025: private static final Log LOG = LogFactory
026: .getLog(CompositeTextProvider.class);
027:
028: private List textProviders = new ArrayList();
029:
030: /**
031: * Instantiates a {@link CompositeTextProvider} with some predefined <code>textProviders</code>.
032: * @param textProviders
033: */
034: public CompositeTextProvider(List textProviders) {
035: for (Iterator i = textProviders.iterator(); i.hasNext();) {
036: Object obj = i.next();
037: if (obj instanceof TextProvider) {
038: this .textProviders.add(obj);
039: } else {
040: LOG
041: .warn("object ["
042: + obj
043: + "] provided in list ["
044: + textProviders
045: + "] is not a TextProvider instance, ignoring it");
046: }
047: }
048: }
049:
050: /**
051: * Instantiates a {@link CompositeTextProvider} with some predefined <code>textProviders</code>.
052: * @param textProviders
053: */
054: public CompositeTextProvider(TextProvider[] textProviders) {
055: this (Arrays.asList(textProviders));
056: }
057:
058: /**
059: * @see {@link com.opensymphony.xwork.TextProvider#hasKey(String)}
060: * It will consult each individual {@link TextProvider}s and return true if either one of the
061: * {@link TextProvider} has such a <code>key></code> else false.
062: *
063: * @param key
064: * @return
065: */
066: public boolean hasKey(String key) {
067: // if there's a key in either text providers we are ok, else try the next text provider
068: for (Iterator i = textProviders.iterator(); i.hasNext();) {
069: TextProvider _textProvider = (TextProvider) i.next();
070: if (_textProvider.hasKey(key)) {
071: return true;
072: }
073: }
074: return false;
075: }
076:
077: /**
078: * It will consult each {@link TextProvider}s and return the first valid message for this
079: * <code>key</code>
080: * @see {@link com.opensymphony.xwork.TextProvider#getText(String)}
081: * @param key
082: * @return
083: */
084: public String getText(String key) {
085: return getText(key, key, Collections.EMPTY_LIST);
086: }
087:
088: /**
089: * It will consult each {@link TextProvider}s and return the first valid message for this
090: * <code>key</code> before returning <code>defaultValue</code> if every else fails.
091: * @see {@link com.opensymphony.xwork.TextProvider#getText(String, String)}
092: * @param key
093: * @param defaultValue
094: * @return
095: */
096: public String getText(String key, String defaultValue) {
097: return getText(key, defaultValue, Collections.EMPTY_LIST);
098: }
099:
100: /**
101: * It will consult each {@link TextProvider}s and return the first valid message for this
102: * <code>key</code>, before returining <code>defaultValue</code>
103: * if every else fails.
104: * @see {@link com.opensymphony.xwork.TextProvider#getText(String, String, String)}
105: * @param key
106: * @param defaultValue
107: * @param obj
108: * @return
109: */
110: public String getText(String key, String defaultValue,
111: final String obj) {
112: return getText(key, defaultValue, new ArrayList() {
113: {
114: add(obj);
115: }
116: });
117: }
118:
119: /**
120: * It will consult each {@link TextProvider}s and return the first valid message for this
121: * <code>key</code>.
122: * @see {@link com.opensymphony.xwork.TextProvider#getText(String, java.util.List)}
123: * @param key
124: * @param args
125: * @return
126: */
127: public String getText(String key, List args) {
128: return getText(key, key, args);
129: }
130:
131: /**
132: * It will consult each {@link TextProvider}s and return the first valid message for this
133: * <code>key</code>.
134: * @see {@link com.opensymphony.xwork.TextProvider#getText(String, String[])}
135: * @param key
136: * @param args
137: * @return
138: */
139: public String getText(String key, String[] args) {
140: return getText(key, key, args);
141: }
142:
143: /**
144: * It will consult each {@link TextProvider}s and return the first valid message for this
145: * <code>key</code>, before returining <code>defaultValue</code>
146: * @see {@link com.opensymphony.xwork.TextProvider#getText#getText(String, String, java.util.List)}
147: * @param key
148: * @param defaultValue
149: * @param args
150: * @return
151: */
152: public String getText(String key, String defaultValue, List args) {
153: // if there's one text provider that gives us a msg not the same as defaultValue
154: // for this key, we are ok, else try the next
155: // text provider
156: for (Iterator i = textProviders.iterator(); i.hasNext();) {
157: TextProvider _textProvider = (TextProvider) i.next();
158: String msg = _textProvider.getText(key, defaultValue, args);
159: if (msg != null && (!msg.equals(defaultValue))) {
160: return msg;
161: }
162: }
163: return defaultValue;
164: }
165:
166: /**
167: * It will consult each {@link TextProvider}s and return the first valid message for this
168: * <code>key</code>, before returining <code>defaultValue</code>.
169: * @see {@link com.opensymphony.xwork.TextProvider#getText(String, String, String[])}
170: * @param key
171: * @param defaultValue
172: * @param args
173: * @return
174: */
175: public String getText(String key, String defaultValue, String[] args) {
176: // if there's one text provider that gives us a msg not the same as defaultValue
177: // for this key, we are ok, else try the next
178: // text provider
179: for (Iterator i = textProviders.iterator(); i.hasNext();) {
180: TextProvider _textProvider = (TextProvider) i.next();
181: String msg = _textProvider.getText(key, defaultValue, args);
182: if (msg != null && (!msg.equals(defaultValue))) {
183: return msg;
184: }
185: }
186: return defaultValue;
187: }
188:
189: /**
190: * It will consult each {@link TextProvider}s and return the first valid message for this
191: * <code>key</code>, before returining <code>defaultValue</code>
192: *
193: * @see {@link com.opensymphony.xwork.TextProvider#getText(String, String, java.util.List, com.opensymphony.xwork.util.OgnlValueStack)}
194: * @param key
195: * @param defaultValue
196: * @param args
197: * @param stack
198: * @return
199: */
200: public String getText(String key, String defaultValue, List args,
201: OgnlValueStack stack) {
202: // if there's one text provider that gives us a msg not the same as defaultValue
203: // for this key, we are ok, else try the next
204: // text provider
205: for (Iterator i = textProviders.iterator(); i.hasNext();) {
206: TextProvider _textProvider = (TextProvider) i.next();
207: String msg = _textProvider.getText(key, defaultValue, args,
208: stack);
209: if (msg != null && (!msg.equals(defaultValue))) {
210: return msg;
211: }
212: }
213: return defaultValue;
214: }
215:
216: /**
217: * It will consult each {@link TextProvider}s and return the first valid message for this
218: * <code>key</code>, before returining <code>defaultValue</code>
219: * @see {@link com.opensymphony.xwork.TextProvider#getText(String, String, String[], com.opensymphony.xwork.util.OgnlValueStack)}
220: * @param key
221: * @param defaultValue
222: * @param args
223: * @param stack
224: * @return
225: */
226: public String getText(String key, String defaultValue,
227: String[] args, OgnlValueStack stack) {
228: // if there's one text provider that gives us a msg not the same as defaultValue
229: // for this key, we are ok, else try the next
230: // text provider
231: for (Iterator i = textProviders.iterator(); i.hasNext();) {
232: TextProvider _textProvider = (TextProvider) i.next();
233: String msg = _textProvider.getText(key, defaultValue, args,
234: stack);
235: if (msg != null && (!msg.equals(defaultValue))) {
236: return msg;
237: }
238: }
239: return defaultValue;
240: }
241:
242: /**
243: * It will consult each {@link TextProvider}s and return the first non-null {@link ResourceBundle}.
244: * @see {@link TextProvider#getTexts(String)}
245: * @param bundleName
246: * @return
247: */
248: public ResourceBundle getTexts(String bundleName) {
249: // if there's one text provider that gives us a non-null resource bunlde for this bundleName, we are ok, else try the next
250: // text provider
251: for (Iterator i = textProviders.iterator(); i.hasNext();) {
252: TextProvider _textProvider = (TextProvider) i.next();
253: ResourceBundle bundle = _textProvider.getTexts(bundleName);
254: if (bundle != null) {
255: return bundle;
256: }
257: }
258: return null;
259: }
260:
261: /**
262: * It will consult each {@link com.opensymphony.xwork.TextProvider}s and return the first non-null {@link ResourceBundle}.
263: * @see {@link TextProvider#getTexts()}
264: * @return
265: */
266: public ResourceBundle getTexts() {
267: // if there's one text provider that gives us a non-null resource bundle, we are ok, else try the next
268: // text provider
269: for (Iterator i = textProviders.iterator(); i.hasNext();) {
270: TextProvider _textProvider = (TextProvider) i.next();
271: ResourceBundle bundle = _textProvider.getTexts();
272: if (bundle != null) {
273: return bundle;
274: }
275: }
276: return null;
277: }
278: }
|