📄 validationutilities.java
字号:
/* @LICENSE_COPYRIGHT@ */package net.sf.irunninglog.validation;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.commons.validator.Field;import org.apache.commons.validator.ValidatorAction;import org.apache.commons.validator.util.ValidatorUtils;import net.sf.irunninglog.util.DTO;import net.sf.irunninglog.util.Utilities;/** * Utility class that proived helper methods related to object validation. * Among other thigns, this class provides a mechanism for executing * validations without using any common-validator objects. The various * <code>execuetXXXValidation</code> methods provide a wrapper around the * <code>Validations</code> methods, and makes performing validations easier. * * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a> * @version $Revision: 1.1.1.1 $ $Date: 2005/06/23 01:49:05 $ * @since iRunningLog 1.0 * @see Validations */public final class ValidationUtilities { /** <code>Log</code> instance for this class. */ private static final Log LOG = LogFactory.getLog(ValidationUtilities.class); /** Error key for required field errors. */ public static final String ERROR_REQUIRED_FIELD = "E0001"; /** Error key for minimum length errors. */ public static final String ERROR_MIN_LENGTH = "E0002"; /** Error key for maximum length errors. */ public static final String ERROR_MAX_LENGTH = "E0003"; /** Error key for invalid date errors. */ public static final String ERROR_INVALID_DATE = "E0004"; /** Error key for invalid date errors. */ public static final String ERROR_INVALID_VALUE = "E0005"; /** Error key for invalid email errors. */ public static final String ERROR_INVALID_EMAIL = "E0006"; /** Error key for maximum value errors. */ public static final String ERROR_MIN_VALUE = "E0007"; /** Error key for minimum value errors. */ public static final String ERROR_MAX_VALUE = "E0008"; /** Error key for invalid number (decimal) errors. */ public static final String ERROR_INVALID_NUMBER = "E0009"; /** Error key for invalid Boolean errors. */ public static final String ERROR_INVALID_BOOLEAN = "E0010"; /** * Error key for when a field's value is required based on the value of * another field. **/ public static final String ERROR_REQUIRED_DEPENDENT_FIELD = "E0011"; /** * Error key for when a field's value is forbidden based on the value of * another field. **/ public static final String ERROR_FORBIDDEN_DEPENDENT_FIELD = "E0012"; /** Error key for invalid time errors. */ public static final String ERROR_INVALID_TIME = "E0013"; /** Error key for when a Shoe is marked as both Retired and Default. */ public static final String ERROR_INVALID_SHOE_FLAGS = "E0014"; /** Utility class - does not expose a constructor. */ private ValidationUtilities() { } /** * Method used to extract a string value from a bean. This method will * use the parameter bean and property values to extract the <code> * String</code> containted by the bean for the specified property. If the * property is a <code>String[]</code> or <code>Collection</code> and it * is empty, an empty <code>String</code> ("") is returned. * * @param bean The bean from which to extract a value. This can be any * Java bean, or a <code>DTO</code> * @param property The property to be extracted from the bean. * @return The value contained within the bean for the given property, or * <code>null</code> if there was a problem extracting the property * @see DTO */ public static String getValueAsString(Object bean, String property) { if (LOG.isDebugEnabled()) { LOG.debug("getValueAsString: Attempting to get the value of '" + property + "' as a String from the following object " + bean); } String value; if (bean instanceof DTO) { LOG.debug("getValueAsString: The object is a DTO"); value = ((DTO) bean).getValue(property); value = (value == null) ? "" : value; } else { value = ValidatorUtils.getValueAsString(bean, property); } if (LOG.isDebugEnabled()) { LOG.debug("getValueAsString: The value of '" + property + "' is '" + value + "'"); } return value; } /** * Perform a required field validation. This method will use the supplied * parameters to contruct the appropriate commons-validator objects and * then use the <code>Validations.validateRequired</code> method to * perform the actual validation. * * @param bean The bean containing the property to be validated * @param fieldName The name of the property to be evaluated * @param errors List to which any errors should be added * @return True if the validation succeeded, false otherwise * @see Validations#validateRequired(DTO, ValidatorAction, Field, List) */ public static boolean executeRequiredFieldValidation(DTO bean, String fieldName, List errors) { Field field = getNewValidationField(fieldName); ValidatorAction action = getNewValidatorAction(ERROR_REQUIRED_FIELD); if (LOG.isDebugEnabled()) { LOG.debug("executeRequiredFieldValidation: Forwarding validation" + " of field '" + fieldName + "' to Validations"); } boolean success = Validations.validateRequired(bean, action, field, errors); if (LOG.isDebugEnabled()) { LOG.debug("executeRequiredFieldValidation: Result of the" + " validation is '" + success + "'"); } return success; } /** * Perform a max length validation. This method will use the supplied * parameters to contruct the appropriate commons-validator objects and * then use the <code>Validations.validateMaxLength</code> method to * perform the actual validation. * * @param bean The bean containing the property to be validated * @param fieldName The name of the property to be evaluated * @param errors List to which any errors should be added * @return True if the validation succeeded, false otherwise * @see Validations#validateMaxLength(DTO, ValidatorAction, Field, List) */ public static boolean executeMaxLengthValidation(DTO bean, String fieldName, List errors) { Field field = getNewValidationField(fieldName); ValidatorAction action = getNewValidatorAction(ERROR_MAX_LENGTH); if (LOG.isDebugEnabled()) { LOG.debug("executeMaxLengthValidation: Forwarding validation of" + " field '" + fieldName + "' to Validations"); } boolean success = Validations.validateMaxLength(bean, action, field, errors); if (LOG.isDebugEnabled()) { LOG.debug("executeMaxLengthValidation: Result of the validation" + " is '" + success + "'"); } return success; } /** * Perform a minimum length validation. This method will use the supplied * parameters to contruct the appropriate commons-validator objects and * then use the <code>Validations.validateMinLength</code> method to * perform the actual validation. * * @param bean The bean containing the property to be validated * @param fieldName The name of the property to be evaluated * @param errors List to which any errors should be added * @return True if the validation succeeded, false otherwise * @see Validations#validateMinLength(DTO, ValidatorAction, Field, List) */ public static boolean executeMinLengthValidation(DTO bean, String fieldName, List errors) { Field field = getNewValidationField(fieldName); ValidatorAction action = getNewValidatorAction(ERROR_MIN_LENGTH); if (LOG.isDebugEnabled()) { LOG.debug("executeMinLengthValidation: Forwarding validation of" + " field '" + fieldName + "' to Validations"); } boolean success = Validations.validateMinLength(bean, action, field, errors); if (LOG.isDebugEnabled()) { LOG.debug("executeMinLengthValidation: Result of the validation" + " is '" + success + "'"); } return success; } /** * Perform a maximum value validation. This method will use the supplied * parameters to contruct the appropriate commons-validator objects and * then use the <code>Validations.validateMaxValue</code> method to * perform the actual validation. * * @param bean The bean containing the property to be validated * @param fieldName The name of the property to be evaluated * @param errors List to which any errors should be added * @return True if the validation succeeded, false otherwise * @see Validations#validateMaxValue(DTO, ValidatorAction, Field, List) */ public static boolean executeMaxValueValidation(DTO bean, String fieldName, List errors) { Field field = getNewValidationField(fieldName); ValidatorAction action = getNewValidatorAction(ERROR_MAX_VALUE); if (LOG.isDebugEnabled()) { LOG.debug("executeMaxValueValidation: Forwarding validation of" + " field '" + fieldName + "' to Validations"); } boolean success = Validations.validateMaxValue(bean, action, field, errors); if (LOG.isDebugEnabled()) { LOG.debug("executeMaxValueValidation: Result of the validation" + " is '" + success + "'"); } return success; } /** * Perform a minimum value validation. This method will use the supplied * parameters to contruct the appropriate commons-validator objects and * then use the <code>Validations.validateMinValue</code> method to * perform the actual validation. * * @param bean The bean containing the property to be validated * @param fieldName The name of the property to be evaluated * @param errors List to which any errors should be added * @return True if the validation succeeded, false otherwise * @see Validations#validateMinValue(DTO, ValidatorAction, Field, List) */ public static boolean executeMinValueValidation(DTO bean, String fieldName, List errors) { Field field = getNewValidationField(fieldName); ValidatorAction action = getNewValidatorAction(ERROR_MIN_VALUE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -