⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 validations.java

📁 A Java web application, based on Struts and Hibernate, that serves as an online running log. Users m
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* @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.EmailValidator;import org.apache.commons.validator.Field;import org.apache.commons.validator.GenericValidator;import org.apache.commons.validator.ValidatorAction;import net.sf.irunninglog.canonical.CanonicalUtilities;import net.sf.irunninglog.util.ConversionException;import net.sf.irunninglog.util.Conversions;import net.sf.irunninglog.util.DTO;import net.sf.irunninglog.util.Utilities;/** * Class responsible for performing all validations within the application. * This class can be used to perform validations from any layer of the * application, as it is based on the commons-validator validation framework. * * <p/> * * The current set of validations are aimed at validating <code>DTO</code> * objects based on a field's name.  The methods make use of the * <code>CanonicalUtilities</code> class to resolve validation values using the * application's property files. * * @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 CanonicalUtilities */public final class Validations {    /** <code>Log</code> instance for this class. */    private static final Log LOG = LogFactory.getLog(Validations.class);    /** Utility class - does not expose a constructor. */    private Validations() { }    /**     * Ensure that a field contains a valid boolean value.  A valid boolean is     * either a value that can be converted to a boolean by the     * <code>Conversions.stringToBoolean()</code> method, or a null/empty value     *     * @param bean The transfer object containing the field to be validated     * @param action <code>ValidatorAction</code> containing information used     *               to perform the validation and/or constuct an error message     * @param field Object containing the name of the field to be validated     * @param errors List to which any errors should be added     * @return True if the field is valid, false otherwise     */    public static boolean validateBoolean(DTO bean,                                          ValidatorAction action,                                          Field field,                                          List errors) {        if (LOG.isDebugEnabled()) {            LOG.debug("validateBoolean: Validating using this bean " + bean);            LOG.debug("validateBoolean: Validating using this action "                      + action);            LOG.debug("validateBoolean: Validating using this field " + field);            LOG.debug("validateBoolean: Validating using this list " + errors);        }        if (bean == null || action == null || field == null || errors == null) {            LOG.error("validateBoolean invoked with null parameter(s)");            throw new IllegalArgumentException("validateBoolean invoked with"                                               + " null parameter(s)");        }        String canonicalId = bean.getCanonicalId();        String fieldName = field.getProperty();        String value = ValidationUtilities.getValueAsString(bean, fieldName);        String label = CanonicalUtilities.getLabel(canonicalId, fieldName);        String [] args = new String [] {label};        if (LOG.isDebugEnabled()) {            LOG.debug("validateBoolean: Validating field '" + fieldName                      + "' with a value of '" + value + "'");        }        if (Utilities.isBlank(value)) {            if (LOG.isDebugEnabled()) {                LOG.debug("validateBoolean: Field value is blank, returning"                          + " true");            }            return true;        }        try {            Conversions.stringToBoolean(value);            if (LOG.isDebugEnabled()) {                LOG.debug("validateBoolean: Value is a valid boolean, returning"                          + " true");            }            return true;        } catch (ConversionException ex) {            ValidationError error = new ValidationError(action.getMsg(), args);            if (LOG.isDebugEnabled()) {                LOG.debug("validateBoolean: Value is not a valid boolean, "                          + "returning false and adding this error to the list "                          + error);            }            errors.add(error);            return false;        }    }    /**     * Ensure that a field contains a valid decimal value.  A valid decimal is     * either a value that can be converted to a decimal by the     * <code>Conversions.stringToDecimal()</code> method, or a null/empty value     *     * @param bean The transfer object containing the field to be validated     * @param action <code>ValidatorAction</code> containing information used     *               to perform the validation and/or constuct an error message     * @param field Object containing the name of the field to be validated     * @param errors List to which any errors should be added     * @return True if the field is valid, false otherwise     */    public static boolean validateDecimal(DTO bean,                                          ValidatorAction action,                                          Field field,                                          List errors) {        if (LOG.isDebugEnabled()) {            LOG.debug("validateDecimal: Validating using this bean " + bean);            LOG.debug("validateDecimal: Validating using this action "                      + action);            LOG.debug("validateDecimal: Validating using this field " + field);            LOG.debug("validateDecimal: Validating using this list " + errors);        }        if (bean == null || action == null || field == null || errors == null) {            LOG.error("validateDecimal invoked with null parameter(s)");            throw new IllegalArgumentException("validateDecimal invoked with"                                               + " null parameter(s)");        }        String canonicalId = bean.getCanonicalId();        String fieldName = field.getProperty();        String value = ValidationUtilities.getValueAsString(bean, fieldName);        String label = CanonicalUtilities.getLabel(canonicalId, fieldName);        String [] args = new String [] {label};        if (LOG.isDebugEnabled()) {            LOG.debug("validateDecimal: Validating field '" + fieldName                      + "' with a value of '" + value + "'");        }        if (Utilities.isBlank(value)) {            if (LOG.isDebugEnabled()) {                LOG.debug("validateDecimal: Field value is blank, returning"                          + " true");            }            return true;        }        try {            Conversions.stringToDecimal(value);            if (LOG.isDebugEnabled()) {                LOG.debug("validateDecimal: Value is a valid boolean, returning"                          + " true");            }            return true;        } catch (ConversionException ex) {            ValidationError error = new ValidationError(action.getMsg(), args);            if (LOG.isDebugEnabled()) {                LOG.debug("validateDecimal: Value is not a valid boolean, "                          + "returning false and adding this error to the list "                          + error);            }            errors.add(error);            return false;        }    }    /**     * Ensure that a field's value is not empty.  An empty field is one whose     * value is either<code>null</code> or comprised only of spaces.     *     * @param bean The transfer object containing the field to be validated     * @param action <code>ValidatorAction</code> containing information used     *               to perform the validation and/or constuct an error message     * @param field Object containing the name of the field to be validated     * @param errors List to which any errors should be added     * @return True if the field is valid, false otherwise     */    public static boolean validateRequired(DTO bean,                                           ValidatorAction action,                                           Field field,                                           List errors) {        if (LOG.isDebugEnabled()) {            LOG.debug("validateRequired: Validating using this bean " + bean);            LOG.debug("validateRequired: Validating using this action "                      + action);            LOG.debug("validateRequired: Validating using this field " + field);            LOG.debug("validateRequired: Validating using this list " + errors);        }        if (bean == null || action == null || field == null || errors == null) {            LOG.error("validateRequired invoked with null parameter(s)");            throw new IllegalArgumentException("validateRequired invoked with"                                               + " null parameter(s)");        }        String canonicalId = bean.getCanonicalId();        String fieldName = field.getProperty();        String value = ValidationUtilities.getValueAsString(bean, fieldName);        String label = CanonicalUtilities.getLabel(canonicalId, fieldName);        String [] args = new String [] {label};        if (LOG.isDebugEnabled()) {            LOG.debug("validateRequired: Validating field '" + fieldName                      + "' with a value of '" + value + "'");        }        if (Utilities.isBlank(value)) {            ValidationError error = new ValidationError(action.getMsg(), args);            if (LOG.isDebugEnabled()) {                LOG.debug("validateRequired: Value is blank, returning false"                          + " and adding this error to the list " + error);            }            errors.add(error);            return false;        } else {            if (LOG.isDebugEnabled()) {                LOG.debug("validateRequired: Value not blank, returning true");            }            return true;        }    }    /**     * Ensure that a field's length does not exceed a certain limit.     *     * @param bean The transfer object containing the field to be validated     * @param action <code>ValidatorAction</code> containing information used     *               to perform the validation and/or constuct an error message     * @param field Object containing the name of the field to be validated     * @param errors List to which any errors should be added     * @return True if the field is valid, false otherwise     */    public static boolean validateMaxLength(DTO bean,                                            ValidatorAction action,                                            Field field,                                            List errors) {        if (LOG.isDebugEnabled()) {            LOG.debug("validateMaxLength: Validating using this bean " + bean);            LOG.debug("validateMaxLength: Validating using this action "                      + action);            LOG.debug("validateMaxLength: Validating using this field "                      + field);            LOG.debug("validateMaxLength: Validating using this list "                      + errors);        }        if (bean == null || action == null || field == null || errors == null) {            LOG.error("validateMaxLength invoked with null parameter(s)");            throw new IllegalArgumentException("validateMaxLength invoked with"                                               + " null parameter(s)");        }        String canonicalId = bean.getCanonicalId();        String fieldName = field.getProperty();        String value = ValidationUtilities.getValueAsString(bean, fieldName);        String label = CanonicalUtilities.getLabel(canonicalId, fieldName);        int length = CanonicalUtilities.getMaxLength(canonicalId, fieldName);        String [] args = new String [] {label, String.valueOf(length)};        if (LOG.isDebugEnabled()) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -