001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork.validator;
006:
007: import com.opensymphony.xwork.*;
008: import com.opensymphony.xwork.util.OgnlValueStack;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: import java.util.Collection;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Locale;
017: import java.util.Map;
018: import java.util.ResourceBundle;
019:
020: /**
021: * A default implementation of the {@link ValidatorContext} interface.
022: *
023: * @author Jason Carreira
024: * @author Rainer Hermanns
025: */
026: public class DelegatingValidatorContext implements ValidatorContext {
027:
028: private LocaleProvider localeProvider;
029: private TextProvider textProvider;
030: private ValidationAware validationAware;
031:
032: /**
033: * Creates a new validation context given a ValidationAware object, and a text and locale provider. These objects
034: * are used internally to set errors and get and set error text.
035: */
036: public DelegatingValidatorContext(ValidationAware validationAware,
037: TextProvider textProvider, LocaleProvider localeProvider) {
038: this .textProvider = textProvider;
039: this .validationAware = validationAware;
040: this .localeProvider = localeProvider;
041: }
042:
043: /**
044: * Creates a new validation context given an object - usually an Action. The internal objects
045: * (validation aware instance and a locale and text provider) are created based on the given action.
046: *
047: * @param object the object to use for validation (usually an Action).
048: */
049: public DelegatingValidatorContext(Object object) {
050: this .localeProvider = makeLocaleProvider(object);
051: this .validationAware = makeValidationAware(object);
052: this .textProvider = makeTextProvider(object, localeProvider);
053: }
054:
055: /**
056: * Create a new validation context given a Class definition. The locale provider, text provider and
057: * the validation context are created based on the class.
058: *
059: * @param clazz the class to initialize the context with.
060: */
061: public DelegatingValidatorContext(Class clazz) {
062: localeProvider = new ActionContextLocaleProvider();
063: textProvider = new TextProviderSupport(clazz, localeProvider);
064: validationAware = new LoggingValidationAware(clazz);
065: }
066:
067: public void setActionErrors(Collection errorMessages) {
068: validationAware.setActionErrors(errorMessages);
069: }
070:
071: public Collection getActionErrors() {
072: return validationAware.getActionErrors();
073: }
074:
075: public void setActionMessages(Collection messages) {
076: validationAware.setActionMessages(messages);
077: }
078:
079: public Collection getActionMessages() {
080: return validationAware.getActionMessages();
081: }
082:
083: public void setFieldErrors(Map errorMap) {
084: validationAware.setFieldErrors(errorMap);
085: }
086:
087: public Map getFieldErrors() {
088: return validationAware.getFieldErrors();
089: }
090:
091: public String getFullFieldName(String fieldName) {
092: return fieldName;
093: }
094:
095: public Locale getLocale() {
096: return localeProvider.getLocale();
097: }
098:
099: public boolean hasKey(String key) {
100: return textProvider.hasKey(key);
101: }
102:
103: public String getText(String aTextName) {
104: return textProvider.getText(aTextName);
105: }
106:
107: public String getText(String aTextName, String defaultValue) {
108: return textProvider.getText(aTextName, defaultValue);
109: }
110:
111: public String getText(String aTextName, String defaultValue,
112: String obj) {
113: return textProvider.getText(aTextName, defaultValue, obj);
114: }
115:
116: public String getText(String aTextName, List args) {
117: return textProvider.getText(aTextName, args);
118: }
119:
120: public String getText(String key, String[] args) {
121: return textProvider.getText(key, args);
122: }
123:
124: public String getText(String aTextName, String defaultValue,
125: List args) {
126: return textProvider.getText(aTextName, defaultValue, args);
127: }
128:
129: public String getText(String key, String defaultValue, String[] args) {
130: return textProvider.getText(key, defaultValue, args);
131: }
132:
133: public ResourceBundle getTexts(String aBundleName) {
134: return textProvider.getTexts(aBundleName);
135: }
136:
137: public String getText(String key, String defaultValue, List args,
138: OgnlValueStack stack) {
139: return textProvider.getText(key, defaultValue, args, stack);
140: }
141:
142: public String getText(String key, String defaultValue,
143: String[] args, OgnlValueStack stack) {
144: return textProvider.getText(key, defaultValue, args, stack);
145: }
146:
147: public ResourceBundle getTexts() {
148: return textProvider.getTexts();
149: }
150:
151: public void addActionError(String anErrorMessage) {
152: validationAware.addActionError(anErrorMessage);
153: }
154:
155: public void addActionMessage(String aMessage) {
156: validationAware.addActionMessage(aMessage);
157: }
158:
159: public void addFieldError(String fieldName, String errorMessage) {
160: validationAware.addFieldError(fieldName, errorMessage);
161: }
162:
163: public boolean hasActionErrors() {
164: return validationAware.hasActionErrors();
165: }
166:
167: public boolean hasActionMessages() {
168: return validationAware.hasActionMessages();
169: }
170:
171: public boolean hasErrors() {
172: return validationAware.hasErrors();
173: }
174:
175: public boolean hasFieldErrors() {
176: return validationAware.hasFieldErrors();
177: }
178:
179: public static TextProvider makeTextProvider(Object object,
180: LocaleProvider localeProvider) {
181: if (object instanceof TextProvider) {
182: return (TextProvider) object;
183: } else {
184: // the object argument passed through here will most probably be an ActionSupport decendant which does
185: // implements TextProvider.
186: if ((object != null) && (object instanceof TextProvider)) {
187: return new CompositeTextProvider(new TextProvider[] {
188: ((TextProvider) object),
189: new TextProviderSupport(object.getClass(),
190: localeProvider) });
191: } else {
192: return new TextProviderSupport(object.getClass(),
193: localeProvider);
194: }
195: }
196: }
197:
198: protected static LocaleProvider makeLocaleProvider(Object object) {
199: if (object instanceof LocaleProvider) {
200: return (LocaleProvider) object;
201: } else {
202: return new ActionContextLocaleProvider();
203: }
204: }
205:
206: protected static ValidationAware makeValidationAware(Object object) {
207: if (object instanceof ValidationAware) {
208: return (ValidationAware) object;
209: } else {
210: return new LoggingValidationAware(object);
211: }
212: }
213:
214: protected void setTextProvider(TextProvider textProvider) {
215: this .textProvider = textProvider;
216: }
217:
218: protected TextProvider getTextProvider() {
219: return textProvider;
220: }
221:
222: protected void setValidationAware(ValidationAware validationAware) {
223: this .validationAware = validationAware;
224: }
225:
226: protected ValidationAware getValidationAware() {
227: return validationAware;
228: }
229:
230: /**
231: * An implementation of LocaleProvider which gets the locale from the action context.
232: */
233: private static class ActionContextLocaleProvider implements
234: LocaleProvider {
235: public Locale getLocale() {
236: return ActionContext.getContext().getLocale();
237: }
238: }
239:
240: /**
241: * An implementation of ValidationAware which logs errors and messages.
242: */
243: private static class LoggingValidationAware implements
244: ValidationAware {
245:
246: private Log log;
247:
248: public LoggingValidationAware(Class clazz) {
249: log = LogFactory.getLog(clazz);
250: }
251:
252: public LoggingValidationAware(Object obj) {
253: log = LogFactory.getLog(obj.getClass());
254: }
255:
256: public void setActionErrors(Collection errorMessages) {
257: for (Iterator iterator = errorMessages.iterator(); iterator
258: .hasNext();) {
259: String s = (String) iterator.next();
260: addActionError(s);
261: }
262: }
263:
264: public Collection getActionErrors() {
265: return null;
266: }
267:
268: public void setActionMessages(Collection messages) {
269: for (Iterator iterator = messages.iterator(); iterator
270: .hasNext();) {
271: String s = (String) iterator.next();
272: addActionMessage(s);
273: }
274: }
275:
276: public Collection getActionMessages() {
277: return null;
278: }
279:
280: public void setFieldErrors(Map errorMap) {
281: for (Iterator iterator = errorMap.entrySet().iterator(); iterator
282: .hasNext();) {
283: Map.Entry entry = (Map.Entry) iterator.next();
284: addFieldError((String) entry.getKey(), (String) entry
285: .getValue());
286: }
287: }
288:
289: public Map getFieldErrors() {
290: return null;
291: }
292:
293: public void addActionError(String anErrorMessage) {
294: log.error("Validation error: " + anErrorMessage);
295: }
296:
297: public void addActionMessage(String aMessage) {
298: log.info("Validation Message: " + aMessage);
299: }
300:
301: public void addFieldError(String fieldName, String errorMessage) {
302: log.error("Validation error for " + fieldName + ":"
303: + errorMessage);
304: }
305:
306: public boolean hasActionErrors() {
307: return false;
308: }
309:
310: public boolean hasActionMessages() {
311: return false;
312: }
313:
314: public boolean hasErrors() {
315: return false;
316: }
317:
318: public boolean hasFieldErrors() {
319: return false;
320: }
321: }
322: }
|