Example usage for org.apache.commons.validator GenericValidator isBlankOrNull

List of usage examples for org.apache.commons.validator GenericValidator isBlankOrNull

Introduction

In this page you can find the example usage for org.apache.commons.validator GenericValidator isBlankOrNull.

Prototype

public static boolean isBlankOrNull(String value) 

Source Link

Document

Checks if the field isn't null and length of the field is greater than zero not including whitespace.

Usage

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if a fields value is within a range (min & max specified in
 * the vars attribute)./*from w  ww .  ja  va  2 s  .  co m*/
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return True if in range, false otherwise.
 */
public static boolean validateLongRange(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    String value = null;

    value = evaluateBean(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            String minVar = Resources.getVarValue("min", field, validator, request, true);
            String maxVar = Resources.getVarValue("max", field, validator, request, true);
            long longValue = Long.parseLong(value);
            long min = Long.parseLong(minVar);
            long max = Long.parseLong(maxVar);

            if (min > max) {
                throw new IllegalArgumentException(sysmsgs.getMessage("invalid.range", minVar, maxVar));
            }

            if (!GenericValidator.isInRange(longValue, min, max)) {
                errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

                return false;
            }
        } catch (Exception e) {
            processFailure(errors, field, "longRange", e);

            return false;
        }
    }

    return true;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if a fields value is within a range (min &amp; max specified in
 * the vars attribute).// w  ww. j av  a2s .  c  o  m
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return True if in range, false otherwise.
 */
public static boolean validateIntRange(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    String value = null;

    value = evaluateBean(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            String minVar = Resources.getVarValue("min", field, validator, request, true);
            String maxVar = Resources.getVarValue("max", field, validator, request, true);
            int min = Integer.parseInt(minVar);
            int max = Integer.parseInt(maxVar);
            int intValue = Integer.parseInt(value);

            if (min > max) {
                throw new IllegalArgumentException(sysmsgs.getMessage("invalid.range", minVar, maxVar));
            }

            if (!GenericValidator.isInRange(intValue, min, max)) {
                errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

                return false;
            }
        } catch (Exception e) {
            processFailure(errors, field, "intRange", e);

            return false;
        }
    }

    return true;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if a fields value is within a range (min &amp; max specified in
 * the vars attribute)./* w  ww . j  a v  a 2  s .c o m*/
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return True if in range, false otherwise.
 */
public static boolean validateDoubleRange(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    String value = null;

    value = evaluateBean(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            String minVar = Resources.getVarValue("min", field, validator, request, true);
            String maxVar = Resources.getVarValue("max", field, validator, request, true);
            double doubleValue = Double.parseDouble(value);
            double min = Double.parseDouble(minVar);
            double max = Double.parseDouble(maxVar);

            if (min > max) {
                throw new IllegalArgumentException(sysmsgs.getMessage("invalid.range", minVar, maxVar));
            }

            if (!GenericValidator.isInRange(doubleValue, min, max)) {
                errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

                return false;
            }
        } catch (Exception e) {
            processFailure(errors, field, "doubleRange", e);

            return false;
        }
    }

    return true;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if a fields value is within a range (min &amp; max specified in
 * the vars attribute).//from  ww  w  .java2  s . co m
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return True if in range, false otherwise.
 */
public static boolean validateFloatRange(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    String value = null;

    value = evaluateBean(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            String minVar = Resources.getVarValue("min", field, validator, request, true);
            String maxVar = Resources.getVarValue("max", field, validator, request, true);
            float floatValue = Float.parseFloat(value);
            float min = Float.parseFloat(minVar);
            float max = Float.parseFloat(maxVar);

            if (min > max) {
                throw new IllegalArgumentException(sysmsgs.getMessage("invalid.range", minVar, maxVar));
            }

            if (!GenericValidator.isInRange(floatValue, min, max)) {
                errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

                return false;
            }
        } catch (Exception e) {
            processFailure(errors, field, "floatRange", e);

            return false;
        }
    }

    return true;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if the field is a valid credit card number.
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.//  w w  w. j ava 2  s  . co m
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return true if valid, false otherwise.
 */
public static Object validateCreditCard(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    Object result = null;
    String value = null;

    value = evaluateBean(bean, field);

    if (GenericValidator.isBlankOrNull(value)) {
        return Boolean.TRUE;
    }

    result = GenericTypeValidator.formatCreditCard(value);

    if (result == null) {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
    }

    return (result == null) ? Boolean.FALSE : result;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if a field has a valid e-mail address.
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.//  w  ww.  j  a  va2s .com
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return True if valid, false otherwise.
 */
public static boolean validateEmail(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    String value = null;

    value = evaluateBean(bean, field);

    if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.isEmail(value)) {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

        return false;
    } else {
        return true;
    }
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if the field's length is less than or equal to the maximum
 * value. A <code>Null</code> will be considered an error.
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.//from  w  w  w . j a  v  a 2 s .c  om
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return True if stated conditions met.
 */
public static boolean validateMaxLength(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    String value = null;

    value = evaluateBean(bean, field);

    if (value != null) {
        try {
            String maxVar = Resources.getVarValue("maxlength", field, validator, request, true);
            int max = Integer.parseInt(maxVar);

            boolean isValid = false;
            String endLth = Resources.getVarValue("lineEndLength", field, validator, request, false);
            if (GenericValidator.isBlankOrNull(endLth)) {
                isValid = GenericValidator.maxLength(value, max);
            } else {
                isValid = GenericValidator.maxLength(value, max, Integer.parseInt(endLth));
            }

            if (!isValid) {
                errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

                return false;
            }
        } catch (Exception e) {
            processFailure(errors, field, "maxlength", e);

            return false;
        }
    }

    return true;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if the field's length is greater than or equal to the minimum
 * value. A <code>Null</code> will be considered an error.
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.// w w  w .j  a v a 2s .  c o  m
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return True if stated conditions met.
 */
public static boolean validateMinLength(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    String value = null;

    value = evaluateBean(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            String minVar = Resources.getVarValue("minlength", field, validator, request, true);
            int min = Integer.parseInt(minVar);

            boolean isValid = false;
            String endLth = Resources.getVarValue("lineEndLength", field, validator, request, false);
            if (GenericValidator.isBlankOrNull(endLth)) {
                isValid = GenericValidator.minLength(value, min);
            } else {
                isValid = GenericValidator.minLength(value, min, Integer.parseInt(endLth));
            }

            if (!isValid) {
                errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

                return false;
            }
        } catch (Exception e) {
            processFailure(errors, field, "minlength", e);

            return false;
        }
    }

    return true;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if a field has a valid url. Four optional variables can be
 * specified to configure url validation.
 *
 * <ul>/*from ww  w .j  av a2s.  com*/
 *
 * <li>Variable <code>allow2slashes</code> can be set to <code>true</code>
 * or <code>false</code> to control whether two slashes are allowed -
 * default is <code>false</code> (i.e. two slashes are NOT allowed).</li>
 *
 * <li>Variable <code>nofragments</code> can be set to <code>true</code>
 * or <code>false</code> to control whether fragments are allowed -
 * default is <code>false</code> (i.e. fragments ARE allowed).</li>
 *
 * <li>Variable <code>allowallschemes</code> can be set to
 * <code>true</code> or <code>false</code> to control if all schemes are
 * allowed - default is <code>false</code> (i.e. all schemes are NOT
 * allowed).</li>
 *
 * <li>Variable <code>schemes</code> can be set to a comma delimited list
 * of valid schemes. This value is ignored if <code>allowallschemes</code>
 * is set to <code>true</code>. Default schemes allowed are "http",
 * "https" and "ftp" if this variable is not specified.</li>
 *
 * </ul>
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return True if valid, false otherwise.
 */
public static boolean validateUrl(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    String value = null;

    value = evaluateBean(bean, field);

    if (GenericValidator.isBlankOrNull(value)) {
        return true;
    }

    // Get the options and schemes Vars
    String allowallschemesVar = Resources.getVarValue("allowallschemes", field, validator, request, false);
    boolean allowallschemes = "true".equalsIgnoreCase(allowallschemesVar);
    int options = allowallschemes ? UrlValidator.ALLOW_ALL_SCHEMES : 0;

    String allow2slashesVar = Resources.getVarValue("allow2slashes", field, validator, request, false);

    if ("true".equalsIgnoreCase(allow2slashesVar)) {
        options += UrlValidator.ALLOW_2_SLASHES;
    }

    String nofragmentsVar = Resources.getVarValue("nofragments", field, validator, request, false);

    if ("true".equalsIgnoreCase(nofragmentsVar)) {
        options += UrlValidator.NO_FRAGMENTS;
    }

    String schemesVar = allowallschemes ? null
            : Resources.getVarValue("schemes", field, validator, request, false);

    // No options or schemes - use GenericValidator as default
    if ((options == 0) && (schemesVar == null)) {
        if (GenericValidator.isUrl(value)) {
            return true;
        } else {
            errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

            return false;
        }
    }

    // Parse comma delimited list of schemes into a String[]
    String[] schemes = null;

    if (schemesVar != null) {
        StringTokenizer st = new StringTokenizer(schemesVar, ",");

        schemes = new String[st.countTokens()];

        int i = 0;

        while (st.hasMoreTokens()) {
            schemes[i++] = st.nextToken().trim();
        }
    }

    // Create UrlValidator and validate with options/schemes
    UrlValidator urlValidator = new UrlValidator(schemes, options);

    if (urlValidator.isValid(value)) {
        return true;
    } else {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

        return false;
    }
}

From source file:org.broadleafcommerce.profile.web.controller.CustomerPhoneController.java

/**
 * Creates a new phone if no customerPhoneId & phoneId are passed in; otherwise, it creates a new customerPhone object otherwise.  If they are passed in,
 *  it is assumed that there is an update.
 *
 * @param phoneNameForm/*from ww  w.j  a  va  2  s  . co  m*/
 * @param errors
 * @param request
 * @param customerPhoneId DOCUMENT ME!
 * @param phoneId DOCUMENT ME!
 *
 * @return
 */
@RequestMapping(value = "savePhone", method = { RequestMethod.GET, RequestMethod.POST })
public String savePhone(@ModelAttribute("phoneNameForm") PhoneNameForm phoneNameForm, BindingResult errors,
        HttpServletRequest request, @RequestParam(required = false) Long customerPhoneId,
        @RequestParam(required = false) Long phoneId) {
    if (GenericValidator.isBlankOrNull(phoneNameForm.getPhoneName())) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phoneName", "phoneName.required");
    }

    if (phoneId != null) {
        phoneNameForm.getPhone().setId(phoneId);
    }

    phoneFormatter.formatPhoneNumber(phoneNameForm.getPhone());
    errors.pushNestedPath("phone");
    phoneValidator.validate(phoneNameForm.getPhone(), errors);
    errors.popNestedPath();

    if (!errors.hasErrors()) {
        CustomerPhone customerPhone = (CustomerPhone) entityConfiguration
                .createEntityInstance("org.broadleafcommerce.profile.core.domain.CustomerPhone");
        customerPhone.setCustomer(customerState.getCustomer(request));
        customerPhone.setPhoneName(phoneNameForm.getPhoneName());
        customerPhone.setPhone(phoneNameForm.getPhone());

        if ((customerPhoneId != null) && (customerPhoneId > 0)) {
            customerPhone.setId(customerPhoneId);
        }

        customerPhoneValidator.validate(customerPhone, errors);

        if (!errors.hasErrors()) {
            customerPhone = customerPhoneService.saveCustomerPhone(customerPhone);
            request.setAttribute("customerPhoneId", customerPhone.getId());
            request.setAttribute("phoneId", customerPhone.getPhone().getId());
        }

        return savePhoneSuccessView;
    } else {
        return savePhoneErrorView;
    }
}