Example usage for org.apache.commons.validator.util ValidatorUtils getValueAsString

List of usage examples for org.apache.commons.validator.util ValidatorUtils getValueAsString

Introduction

In this page you can find the example usage for org.apache.commons.validator.util ValidatorUtils getValueAsString.

Prototype

public static String getValueAsString(Object bean, String property) 

Source Link

Document

Convenience method for getting a value from a bean property as a String.

Usage

From source file:org.rhq.enterprise.gui.legacy.validator.IdenticalValidator.java

/**
 * Validates if two fields are equal in terms of String's equal() function. Example of use: <code><field
 * property="password" depends="identical"> <arg0 key="password.displayName"/> <arg1
 * key="passwordConfirm.displayName"/> <var><var-name>secondProperty</var-name> <var-value>password2</var-value>
 * </var> </field></code>
 *
 * @return Returns true if the fields property and property2 are identical.
 *//*from  w ww  . j a v a  2 s .c  o  m*/
public boolean validate(Object bean, ValidatorAction va, Field field, ActionMessages msgs,
        HttpServletRequest request) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    String sProperty2 = field.getVarValue("secondProperty");
    String value2 = ValidatorUtils.getValueAsString(bean, sProperty2);

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

        return false;
    }

    if (!value.equals(value2)) {
        ActionMessage msg = Resources.getActionMessage(request, va, field);
        msgs.add(field.getKey(), msg);
        return false;
    }

    return true;
}

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

private static String toString(Object bean, Field field) {
    String value = null;/*from w  ww . ja  va 2  s. c o m*/
    if (isString(bean)) {
        value = (String) bean;
    } else {
        value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    }
    return value;
}

From source file:org.seasar.struts.validator.S2FieldChecks.java

/**
 * ??????//from w  w  w. java  2s .  c  o  m
 * 
 * @param bean
 *            JavaBeans
 * @param field
 *            
 * @return 
 */
protected static String getValueAsString(Object bean, Field field) {
    if (isString(bean)) {
        return (String) bean;
    }
    return ValidatorUtils.getValueAsString(bean, field.getProperty());
}

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./*  w  w w. j a v  a2 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 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./* w  w  w . j ava  2s . c o 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;
}

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

/**
 * Extracts the value of the given bean. If the bean is <code>null</code>, the returned value is also <code>null</code>.
 * If the bean is a <code>String</code> then the bean itself is returned. In all other cases, the <code>ValidatorUtils</code>
 * class is used to extract the bean value using the <code>Field</code> object supplied.
 *
 * @see ValidatorUtils#getValueAsString(Object, String)
 *///from  ww w . java 2 s . c  om
protected static String extractValue(Object bean, Field field) {
    String value = null;

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

    return value;
}

From source file:org.springmodules.commons.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.//  ww  w .ja v a  2  s  .c o 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 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, Errors 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 = field.getVarValue("test");
    if (test == null) {
        String msg = "ValidWhen Error 'test' parameter is missing for field ' " + field.getKey() + "'";
        errors.rejectValue(field.getKey(), msg);
        log.error(msg);
        return false;
    }

    // Create the Lexer
    ValidWhenLexer lexer = null;
    try {
        lexer = new ValidWhenLexer(new StringReader(test));
    } catch (Exception ex) {
        String msg = "ValidWhenLexer Error for field ' " + field.getKey() + "' - " + ex;
        errors.rejectValue(field.getKey(), msg);
        log.error(msg);
        log.debug(msg, ex);
        return false;
    }

    // Create the Parser
    ValidWhenParser parser = null;
    try {
        parser = new ValidWhenParser(lexer);
    } catch (Exception ex) {
        String msg = "ValidWhenParser Error for field ' " + field.getKey() + "' - " + ex;
        errors.rejectValue(field.getKey(), msg);
        log.error(msg);
        log.debug(msg, ex);
        return false;
    }

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

    try {
        parser.expression();
        valid = parser.getResult();

    } catch (Exception ex) {
        String msg = "ValidWhen Error for field ' " + field.getKey() + "' - " + ex;
        errors.rejectValue(field.getKey(), msg);
        log.error(msg);
        log.debug(msg, ex);

        return false;
    }

    if (!valid) {
        FieldChecks.rejectValue(errors, field, va);
        return false;
    }

    return true;
}

From source file:org.springmodules.validation.commons.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 ww . ja v  a  2  s.  c o 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 = FieldChecks.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(FieldChecks.FIELD_TEST_NULL)) {
            thisRequired = (dependVal != null) && (dependVal.length() > 0);
        }

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

        if (dependTest.equals(FieldChecks.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)) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        } else {
            return true;
        }
    }
    return true;
}

From source file:org.springmodules.validation.commons.FieldChecks.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   w w w . j  ava  2 s.  com*/
 * @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.
 * @return <code>true</code> if meets stated requirements,
 *         <code>false</code> otherwise.
 */
public static boolean validateValidWhen(Object bean, ValidatorAction va, Field field, Errors errors,
        Validator validator) {

    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 = field.getVarValue("test");
    if (test == null) {
        String msg = "ValidWhen Error 'test' parameter is missing for field ' " + field.getKey() + "'";
        errors.rejectValue(field.getKey(), msg);
        log.error(msg);
        return false;
    }

    // Create the Lexer
    ValidWhenLexer lexer = null;
    try {
        lexer = new ValidWhenLexer(new StringReader(test));
    } catch (Exception ex) {
        String msg = "ValidWhenLexer Error for field ' " + field.getKey() + "' - " + ex;
        errors.rejectValue(field.getKey(), msg);
        log.error(msg);
        log.debug(msg, ex);
        return false;
    }

    // Create the Parser
    ValidWhenParser parser = null;
    try {
        parser = new ValidWhenParser(lexer);
    } catch (Exception ex) {
        String msg = "ValidWhenParser Error for field ' " + field.getKey() + "' - " + ex;
        errors.rejectValue(field.getKey(), msg);
        log.error(msg);
        log.debug(msg, ex);
        return false;
    }

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

    try {
        parser.expression();
        valid = parser.getResult();

    } catch (Exception ex) {
        String msg = "ValidWhen Error for field ' " + field.getKey() + "' - " + ex;
        errors.rejectValue(field.getKey(), msg);
        log.error(msg);
        log.debug(msg, ex);

        return false;
    }

    if (!valid) {
        rejectValue(errors, field, va);
        return false;
    }

    return true;
}

From source file:org.springmodules.validation.commons.validwhen.ValidWhenParser.java

public final void field() throws RecognitionException, TokenStreamException {

    if ((LA(1) == IDENTIFIER) && (LA(2) == LBRACKET) && (LA(3) == RBRACKET) && (LA(4) == IDENTIFIER)) {
        identifier();/*from   ww w  .  j ava 2 s . co m*/
        match(LBRACKET);
        match(RBRACKET);
        identifier();

        Object i2 = argStack.pop();
        Object i1 = argStack.pop();
        argStack.push(ValidatorUtils.getValueAsString(form, i1 + "[" + index + "]" + i2));

    } else if ((LA(1) == IDENTIFIER) && (LA(2) == LBRACKET)
            && (LA(3) == DECIMAL_LITERAL || LA(3) == HEX_LITERAL) && (LA(4) == RBRACKET)
            && (LA(5) == IDENTIFIER)) {
        identifier();
        match(LBRACKET);
        number();
        match(RBRACKET);
        identifier();

        Object i5 = argStack.pop();
        Object i4 = argStack.pop();
        Object i3 = argStack.pop();
        argStack.push(ValidatorUtils.getValueAsString(form, i3 + "[" + i4 + "]" + i5));

    } else if ((LA(1) == IDENTIFIER) && (LA(2) == LBRACKET)
            && (LA(3) == DECIMAL_LITERAL || LA(3) == HEX_LITERAL) && (LA(4) == RBRACKET)
            && (LA(5) == LBRACKET)) {
        identifier();
        match(LBRACKET);
        number();
        match(RBRACKET);
        match(LBRACKET);

        Object i7 = argStack.pop();
        Object i6 = argStack.pop();
        argStack.push(ValidatorUtils.getValueAsString(form, i6 + "[" + i7 + "]"));

    } else if ((LA(1) == IDENTIFIER) && (LA(2) == LBRACKET) && (LA(3) == RBRACKET)
            && (_tokenSet_0.member(LA(4)))) {
        identifier();
        match(LBRACKET);
        match(RBRACKET);

        Object i8 = argStack.pop();
        argStack.push(ValidatorUtils.getValueAsString(form, i8 + "[" + index + "]"));

    } else if ((LA(1) == IDENTIFIER) && (_tokenSet_0.member(LA(2)))) {
        identifier();

        Object i9 = argStack.pop();
        argStack.push(ValidatorUtils.getValueAsString(form, (String) i9));

    } else {
        throw new NoViableAltException(LT(1), getFilename());
    }

}