/* * Copyright (c) 2002-2006 by OpenSymphony * All rights reserved. */ package com.opensymphony.xwork.validator.validators; import com.opensymphony.xwork.ActionContext; import com.opensymphony.xwork.TextProvider; import com.opensymphony.xwork.util.OgnlValueStack; import com.opensymphony.xwork.validator.*; import java.util.Collection; /** * * The VisitorFieldValidator allows you to forward validation to object * properties of your action using the object's own validation files. This * allows you to use the ModelDriven development pattern and manage your * validations for your models in one place, where they belong, next to your * model classes. The VisitorFieldValidator can handle either simple Object * properties, Collections of Objects, or Arrays. * *

* * *

* * *
 * 
 *    <validators>
 *        <!-- Plain Validator Syntax -->
 *        <validator type="visitor">
 *            <param name="fieldName">user</param>
 *            <param name="context">myContext</param>
 *            <param name="appendPrefix">true</param>
 *        </validator>
 *        
 *        <!-- Field Validator Syntax -->
 *        <field name="user">
 *           <field-validator type="visitor">
 *              <param name="context">myContext</param>
 *              <param name="appendPrefix">true</param>
 *           </field-validator>
 *        </field>
 *    </validators>
 * 
 * 
* * *

In the example above, if the acion's getUser() method return User object, WebWork * will look for User-myContext-validation.xml for the validators. Since appednPrefix is true, * every field name will be prefixed with 'user' such that if the actual field name for 'name' is * 'user.name'

* * * * * @author Jason Carreira * @author Rainer Hermanns * @version $Date: 2007-03-30 04:47:15 +0200 (Fr, 30 Mrz 2007) $ $Id: VisitorFieldValidator.java 1396 2007-03-30 02:47:15Z tm_jee $ */ public class VisitorFieldValidator extends FieldValidatorSupport { private String context; private boolean appendPrefix = true; /** * Sets whether the field name of this field validator should be prepended to the field name of * the visited field to determine the full field name when an error occurs. The default is * true. */ public void setAppendPrefix(boolean appendPrefix) { this.appendPrefix = appendPrefix; } /** * Flags whether the field name of this field validator should be prepended to the field name of * the visited field to determine the full field name when an error occurs. The default is * true. */ public boolean isAppendPrefix() { return appendPrefix; } public void setContext(String context) { this.context = context; } public String getContext() { return context; } public void validate(Object object) throws ValidationException { String fieldName = getFieldName(); Object value = this.getFieldValue(fieldName, object); if (value == null) { log.warn("The visited object is null, VisitorValidator will not be able to handle validation properly. Please make sure the visited object is not null for VisitorValidator to function properly"); return; } OgnlValueStack stack = ActionContext.getContext().getValueStack(); stack.push(object); String visitorContext = (context == null) ? ActionContext.getContext().getName() : context; if (value instanceof Collection) { Collection coll = (Collection) value; Object[] array = coll.toArray(); validateArrayElements(array, fieldName, visitorContext); } else if (value instanceof Object[]) { Object[] array = (Object[]) value; validateArrayElements(array, fieldName, visitorContext); } else { validateObject(fieldName, value, visitorContext); } stack.pop(); } private void validateArrayElements(Object[] array, String fieldName, String visitorContext) throws ValidationException { if ( array == null) { return; } for (int i = 0; i < array.length; i++) { Object o = array[i]; validateObject(fieldName + "[" + i + "]", o, visitorContext); } } private void validateObject(String fieldName, Object o, String visitorContext) throws ValidationException { OgnlValueStack stack = ActionContext.getContext().getValueStack(); stack.push(o); ValidatorContext validatorContext = getValidatorContext(); if (o instanceof TextProvider) { validatorContext = new DelegatingValidatorContext( validatorContext, (TextProvider)o, validatorContext); } if (appendPrefix) { validatorContext = new AppendingValidatorContext( validatorContext, fieldName, getMessage(o)); } ActionValidatorManagerFactory.getInstance().validate(o, visitorContext, validatorContext); stack.pop(); } private class AppendingValidatorContext extends DelegatingValidatorContext { String field; String message; public AppendingValidatorContext(ValidatorContext parent, String field, String message) { super(parent, parent, parent); this.field = field; this.message = message; } /** * Translates a simple field name into a full field name in Ognl syntax * * @param fieldName * @return field name in OGNL syntax */ public String getFullFieldName(String fieldName) { return field + "." + fieldName; } public void addActionError(String anErrorMessage) { super.addFieldError(field, message + anErrorMessage); } public void addFieldError(String fieldName, String errorMessage) { super.addFieldError(getFullFieldName(fieldName), message + errorMessage); } } }