Example usage for org.apache.commons.validator Validator getParameterValue

List of usage examples for org.apache.commons.validator Validator getParameterValue

Introduction

In this page you can find the example usage for org.apache.commons.validator Validator getParameterValue.

Prototype

public Object getParameterValue(String parameterClassName) 

Source Link

Document

Returns the value of the specified parameter that will be used during the processing of validations.

Usage

From source file:org.agnitas.service.csv.validator.FieldsValidator.java

public static boolean checkRange(Object bean, Field field, Validator validator) {
    final String value = getValueAsString(bean, field);
    final ImportProfile profile = (ImportProfile) validator
            .getParameterValue("org.agnitas.beans.ImportProfile");
    final ColumnMapping currentColumn = getColumnMappingForCurentField(field, profile);
    final Integer length = Integer.valueOf(field.getVarValue("maxLength"));
    if (currentColumn != null && currentColumn.getMandatory()) {
        return !GenericValidator.isBlankOrNull(value) && checkMaxStringLength(value, length);
    } else {//from w  ww.  j a  v a2s. com
        return currentColumn == null || checkMaxStringLength(value, length);
    }
}

From source file:org.agnitas.service.csv.validator.FieldsValidator.java

/**
 * Checks if the field can be successfully converted to a <code>double</code>.
 *
 * @param value The value validation is being performed on.
 * @return boolean If the field can be successfully converted
 *         to a <code>double</code> <code>true</code> is returned.
 *         Otherwise <code>false</code>.
 *//*from  w  w  w  . ja v  a  2 s. c  o m*/
public static boolean validateDouble(Object bean, Field field, Validator validator) {
    String value = getValueAsString(bean, field);
    final ImportProfile profile = (ImportProfile) validator
            .getParameterValue("org.agnitas.beans.ImportProfile");
    final ColumnMapping currentColumn = getColumnMappingForCurentField(field, profile);
    if (currentColumn != null && currentColumn.getMandatory()) {
        return !GenericValidator.isBlankOrNull(value) && GenericValidator.isDouble(value);
    } else {
        return currentColumn == null || GenericValidator.isDouble(value);
    }
}

From source file:org.agnitas.service.csv.validator.FieldsValidator.java

/**
 * Checks if the field can be successfully converted to a <code>date</code>.
 *
 * @param value The value validation is being performed on.
 * @return boolean If the field can be successfully converted
 *         to a <code>date</code> <code>true</code> is returned.
 *         Otherwise <code>false</code>.
 *///from   w ww.ja v a 2 s.  c  om
public static boolean validateDate(Object bean, Field field, Validator validator) {
    String value = getValueAsString(bean, field);
    final ImportProfile profile = (ImportProfile) validator
            .getParameterValue("org.agnitas.beans.ImportProfile");
    final ColumnMapping currentColumn = getColumnMappingForCurentField(field, profile);
    if (currentColumn != null && currentColumn.getMandatory()) {
        return !GenericValidator.isBlankOrNull(value)
                && GenericValidator.isDate(value, DateFormat.getValue(profile.getDateFormat()), true);
    } else {
        return currentColumn == null
                || GenericValidator.isDate(value, DateFormat.getValue(profile.getDateFormat()), true);
    }
}

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

/**
 * Checks if the field isn't null based on the values of other fields.
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.//from  www . j  a v a 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 meets stated requirements, false otherwise.
 */
public static boolean validateRequiredIf(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    Object form = validator.getParameterValue(org.apache.commons.validator.Validator.BEAN_PARAM);
    String value = null;
    boolean required = false;

    value = evaluateBean(bean, field);

    int i = 0;
    String fieldJoin = "AND";

    if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) {
        fieldJoin = field.getVarValue("fieldJoin");
    }

    if (fieldJoin.equalsIgnoreCase("AND")) {
        required = true;
    }

    while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) {
        String dependProp = field.getVarValue("field[" + i + "]");
        String dependTest = field.getVarValue("fieldTest[" + i + "]");
        String dependTestValue = field.getVarValue("fieldValue[" + i + "]");
        String dependIndexed = field.getVarValue("fieldIndexed[" + i + "]");

        if (dependIndexed == null) {
            dependIndexed = "false";
        }

        String dependVal = null;
        boolean thisRequired = false;

        if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
            String key = field.getKey();

            if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) {
                String ind = key.substring(0, key.indexOf(".") + 1);

                dependProp = ind + dependProp;
            }
        }

        dependVal = ValidatorUtils.getValueAsString(form, dependProp);

        if (dependTest.equals(FIELD_TEST_NULL)) {
            if ((dependVal != null) && (dependVal.length() > 0)) {
                thisRequired = false;
            } else {
                thisRequired = true;
            }
        }

        if (dependTest.equals(FIELD_TEST_NOTNULL)) {
            if ((dependVal != null) && (dependVal.length() > 0)) {
                thisRequired = true;
            } else {
                thisRequired = false;
            }
        }

        if (dependTest.equals(FIELD_TEST_EQUAL)) {
            thisRequired = dependTestValue.equalsIgnoreCase(dependVal);
        }

        if (fieldJoin.equalsIgnoreCase("AND")) {
            required = required && thisRequired;
        } else {
            required = required || thisRequired;
        }

        i++;
    }

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

            return false;
        } else {
            return true;
        }
    }

    return true;
}

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

/**
 * Get the value of a variable./*from w w w  .  ja  v a 2s.co  m*/
 *
 * @param varName   The variable name
 * @param field     the validator Field
 * @param validator The Validator
 * @param request   the servlet request
 * @param required  Whether the variable is mandatory
 * @return The variable's value
 */
public static String getVarValue(String varName, Field field, Validator validator, HttpServletRequest request,
        boolean required) {
    Var var = field.getVar(varName);

    if (var == null) {
        String msg = sysmsgs.getMessage("variable.missing", varName);

        if (required) {
            throw new IllegalArgumentException(msg);
        }

        if (log.isDebugEnabled()) {
            log.debug(field.getProperty() + ": " + msg);
        }

        return null;
    }

    ServletContext application = (ServletContext) validator.getParameterValue(SERVLET_CONTEXT_PARAM);

    return getVarValue(var, application, request, required);
}

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

/**
 * Gets the <code>ActionMessage</code> based on the
 * <code>ValidatorAction</code> message and the <code>Field</code>'s arg
 * objects./*from   ww  w. ja  v  a  2s.c  o  m*/
 *
 * @param validator the Validator
 * @param request   the servlet request
 * @param va        Validator action
 * @param field     the validator Field
 */
public static ActionMessage getActionMessage(Validator validator, HttpServletRequest request,
        ValidatorAction va, Field field) {
    Msg msg = field.getMessage(va.getName());

    if ((msg != null) && !msg.isResource()) {
        return new ActionMessage(msg.getKey(), false);
    }

    String msgKey = null;
    String msgBundle = null;

    if (msg == null) {
        msgKey = va.getMsg();
    } else {
        msgKey = msg.getKey();
        msgBundle = msg.getBundle();
    }

    if ((msgKey == null) || (msgKey.length() == 0)) {
        return new ActionMessage("??? " + va.getName() + "." + field.getProperty() + " ???", false);
    }

    ServletContext application = (ServletContext) validator.getParameterValue(SERVLET_CONTEXT_PARAM);
    MessageResources messages = getMessageResources(application, request, msgBundle);
    Locale locale = RequestUtils.getUserLocale(request, null);

    Arg[] args = field.getArgs(va.getName());
    String[] argValues = getArgValues(application, request, messages, locale, args);

    ActionMessage actionMessage = null;

    if (msgBundle == null) {
        actionMessage = new ActionMessage(msgKey, argValues);
    } else {
        String message = messages.getMessage(locale, msgKey, argValues);

        actionMessage = new ActionMessage(message, false);
    }

    return actionMessage;
}

From source file:org.apache.struts.validator.validwhen.ValidWhen.java

/**
 * Checks if the field matches the boolean expression specified in
 * <code>test</code> parameter.
 *
 * @param bean    The bean validation is being performed on.
 * @param va      The <code>ValidatorAction</code> that is currently being
 *                performed.//from   ww w. j a v  a2  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 request Current request object.
 * @return <code>true</code> if meets stated requirements,
 *         <code>false</code> otherwise.
 */
public static boolean validateValidWhen(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    Object form = validator.getParameterValue(Validator.BEAN_PARAM);
    String value = null;
    boolean valid = false;
    int index = -1;

    if (field.isIndexed()) {
        String key = field.getKey();

        final int leftBracket = key.indexOf("[");
        final int rightBracket = key.indexOf("]");

        if ((leftBracket > -1) && (rightBracket > -1)) {
            index = Integer.parseInt(key.substring(leftBracket + 1, rightBracket));
        }
    }

    if (isString(bean)) {
        value = (String) bean;
    } else {
        value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    }

    String test = null;

    try {
        test = Resources.getVarValue("test", field, validator, request, true);
    } catch (IllegalArgumentException ex) {
        String logErrorMsg = sysmsgs.getMessage("validation.failed", "validwhen", field.getProperty(),
                ex.toString());

        log.error(logErrorMsg);

        String userErrorMsg = sysmsgs.getMessage("system.error");

        errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));

        return false;
    }

    // Create the Lexer
    ValidWhenLexer lexer = null;

    try {
        lexer = new ValidWhenLexer(new StringReader(test));
    } catch (Exception ex) {
        String logErrorMsg = "ValidWhenLexer Error for field ' " + field.getKey() + "' - " + ex;

        log.error(logErrorMsg);

        String userErrorMsg = sysmsgs.getMessage("system.error");

        errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));

        return false;
    }

    // Create the Parser
    ValidWhenParser parser = null;

    try {
        parser = new ValidWhenParser(lexer);
    } catch (Exception ex) {
        String logErrorMsg = "ValidWhenParser Error for field ' " + field.getKey() + "' - " + ex;

        log.error(logErrorMsg);

        String userErrorMsg = sysmsgs.getMessage("system.error");

        errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));

        return false;
    }

    parser.setForm(form);
    parser.setIndex(index);
    parser.setValue(value);

    try {
        parser.expression();
        valid = parser.getResult();
    } catch (Exception ex) {
        String logErrorMsg = "ValidWhen Error for field ' " + field.getKey() + "' - " + ex;

        log.error(logErrorMsg);

        String userErrorMsg = sysmsgs.getMessage("system.error");

        errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));

        return false;
    }

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

        return false;
    }

    return true;
}

From source file:org.hyperic.util.validator.common.CommonValidatorUtil.java

/**
 * Conditional validation method.//from   w w w  . ja va  2s  . co  m
 * @param bean to be tested
 * @param bean's field to be tested.
 * @param current validator
 * @return true if condition satisfied, false otherwise.
 */
public static boolean validateRequiredIf(Object bean, Field field, Validator validator) {
    Object form = validator.getParameterValue(Validator.BEAN_PARAM);
    String value = null;
    boolean required = false;
    if (isString(bean)) {
        value = (String) bean;
    } else {
        value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    }
    int i = 0;
    String fieldJoin = "AND";
    if (!GenericValidator.isBlankOrNull(field.getVarValue("field-join"))) {
        fieldJoin = field.getVarValue("field-join");
    }
    if (fieldJoin.equalsIgnoreCase("AND")) {
        required = true;
    }
    while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) {
        String dependProp = field.getVarValue("field[" + i + "]");
        String dependTest = field.getVarValue("field-test[" + i + "]");
        String dependTestValue = field.getVarValue("field-value[" + i + "]");
        String dependIndexed = field.getVarValue("field-indexed[" + i + "]");
        if (dependIndexed == null)
            dependIndexed = "false";
        String dependVal = null;
        boolean this_required = false;
        if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
            String key = field.getKey();
            if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) {
                String ind = key.substring(0, key.indexOf(".") + 1);
                dependProp = ind + dependProp;
            }
        }
        dependVal = ValidatorUtils.getValueAsString(form, dependProp);
        if (dependTest.equals(FIELD_TEST_NULL)) {
            if ((dependVal != null) && (dependVal.length() > 0)) {
                this_required = false;
            } else {
                this_required = true;
            }
        }
        if (dependTest.equals(FIELD_TEST_NOTNULL)) {
            if ((dependVal != null) && (dependVal.length() > 0)) {
                this_required = true;
            } else {
                this_required = false;
            }
        }
        if (dependTest.equals(FIELD_TEST_EQUAL)) {
            this_required = dependTestValue.equalsIgnoreCase(dependVal);
        }
        if (fieldJoin.equalsIgnoreCase("AND")) {
            required = required && this_required;
        } else {
            required = required || this_required;
        }
        i++;
    }
    if (required) {
        if ((value != null) && (value.length() > 0)) {
            return true;
        } else {
            return false;
        }
    }
    return true;
}

From source file:org.sgrp.singer.validator.ArrayFieldChecks.java

/**
 * Checks if the field isn't null based on the values of other fields.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed.//from   w w  w. ja va 2  s.  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 meets stated requirements, false otherwise.
 */
public static boolean validateRequiredIf(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        org.apache.commons.validator.Validator validator, HttpServletRequest request) {

    Object form = validator.getParameterValue(org.apache.commons.validator.Validator.BEAN_PARAM);
    String[] value = null;
    boolean required = false;

    if (isStringArray(bean)) {
        value = (String[]) bean;
    } else {
        value = new String[0];
    }

    int i = 0;
    String fieldJoin = "AND";
    if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) {
        fieldJoin = field.getVarValue("fieldJoin");
    }

    if (fieldJoin.equalsIgnoreCase("AND")) {
        required = true;
    }

    while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) {
        String dependProp = field.getVarValue("field[" + i + "]");
        String dependTest = field.getVarValue("fieldTest[" + i + "]");
        String dependTestValue = field.getVarValue("fieldValue[" + i + "]");
        String dependIndexed = field.getVarValue("fieldIndexed[" + i + "]");

        if (dependIndexed == null) {
            dependIndexed = "false";
        }

        String dependVal = null;
        boolean thisRequired = false;
        if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
            String key = field.getKey();
            if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) {
                String ind = key.substring(0, key.indexOf(".") + 1);
                dependProp = ind + dependProp;
            }
        }

        dependVal = ValidatorUtils.getValueAsString(form, dependProp);
        if (dependTest.equals(FIELD_TEST_NULL)) {
            if ((dependVal != null) && (dependVal.length() > 0)) {
                thisRequired = false;
            } else {
                thisRequired = true;
            }
        }

        if (dependTest.equals(FIELD_TEST_NOTNULL)) {
            if ((dependVal != null) && (dependVal.length() > 0)) {
                thisRequired = true;
            } else {
                thisRequired = false;
            }
        }

        if (dependTest.equals(FIELD_TEST_EQUAL)) {
            thisRequired = dependTestValue.equalsIgnoreCase(dependVal);
        }

        if (fieldJoin.equalsIgnoreCase("AND")) {
            required = required && thisRequired;
        } else {
            required = required || thisRequired;
        }

        i++;
    }

    if (required) {

        for (i = 0; i < value.length; i++) {
            if (GenericValidator.isBlankOrNull(value[i])) {
                errors.add(field.getKey(), Resources.getActionMessage(request, va, field));

                return false;

            }
        }
    }
    return true;
}

From source file:org.springmodules.commons.validator.FieldChecks.java

/**
 * Checks if the field isn't null based on the values of other fields.
 *
 * @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 ava 2 s. co m*/
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</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 meets stated requirements, false otherwise.
 */
public static boolean validateRequiredIf(Object bean, ValidatorAction va, Field field, Errors errors,
        org.apache.commons.validator.Validator validator) {

    Object form = validator.getParameterValue(org.apache.commons.validator.Validator.BEAN_PARAM);

    boolean required = false;

    String value = extractValue(bean, field);

    int i = 0;
    String fieldJoin = "AND";
    if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) {
        fieldJoin = field.getVarValue("fieldJoin");
    }

    if (fieldJoin.equalsIgnoreCase("AND")) {
        required = true;
    }

    while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) {
        String dependProp = field.getVarValue("field[" + i + "]");
        String dependTest = field.getVarValue("fieldTest[" + i + "]");
        String dependTestValue = field.getVarValue("fieldValue[" + i + "]");
        String dependIndexed = field.getVarValue("fieldIndexed[" + i + "]");

        if (dependIndexed == null) {
            dependIndexed = "false";
        }

        String dependVal = null;
        boolean thisRequired = false;
        if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
            String key = field.getKey();
            if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) {
                String ind = key.substring(0, key.indexOf(".") + 1);
                dependProp = ind + dependProp;
            }
        }

        dependVal = ValidatorUtils.getValueAsString(form, dependProp);
        if (dependTest.equals(FIELD_TEST_NULL)) {
            if ((dependVal != null) && (dependVal.length() > 0)) {
                thisRequired = false;
            } else {
                thisRequired = true;
            }
        }

        if (dependTest.equals(FIELD_TEST_NOTNULL)) {
            if ((dependVal != null) && (dependVal.length() > 0)) {
                thisRequired = true;
            } else {
                thisRequired = false;
            }
        }

        if (dependTest.equals(FIELD_TEST_EQUAL)) {
            thisRequired = dependTestValue.equalsIgnoreCase(dependVal);
        }

        if (fieldJoin.equalsIgnoreCase("AND")) {
            required = required && thisRequired;
        } else {
            required = required || thisRequired;
        }

        i++;
    }

    if (required) {
        if (GenericValidator.isBlankOrNull(value)) {
            rejectValue(errors, field, va);
            return false;
        } else {
            return true;
        }
    }
    return true;
}