List of usage examples for org.apache.commons.validator.util ValidatorUtils getValueAsString
public static String getValueAsString(Object bean, String property)
String
. 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 ww w . j a v a2s . 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, 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.FieldChecks.java
/** * @param bean/*from ww w. j a v a2 s . c om*/ * @param field * @return */ private static String evaluateBean(Object bean, Field field) { String value; if (isString(bean)) { value = (String) bean; } else { value = ValidatorUtils.getValueAsString(bean, field.getProperty()); } return value; }
From source file:org.apache.struts.validator.TestValidWhen.java
/** * Parse the expression returning the result * * @param test Test expression//from www.j a v a 2s.co m * @param bean Test Bean * @param index index value * @param property Bean property */ private boolean doParse(String test, Object bean, int index, String property) throws Exception { if (bean == null) { throw new NullPointerException("Bean is null for property '" + property + "'"); } String value = String.class.isInstance(bean) ? (String) bean : ValidatorUtils.getValueAsString(bean, property); ValidWhenLexer lexer = new ValidWhenLexer(new StringReader(test)); ValidWhenParser parser = new ValidWhenParser(lexer); parser.setForm(bean); parser.setIndex(index); parser.setValue(value); parser.expression(); return parser.getResult(); }
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 w ww. ja va 2s . 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 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.apache.struts.validator.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 w w w .jav a 2s. c om*/ 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) <= OCTAL_LITERAL)) && (LA(4) == RBRACKET) && (LA(5) == IDENTIFIER)) { identifier(); match(LBRACKET); integer(); 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) <= OCTAL_LITERAL)) && (LA(4) == RBRACKET) && (LA(5) == LBRACKET)) { identifier(); match(LBRACKET); integer(); 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()); } }
From source file:org.hyperic.hq.ui.validator.IdenticalValidator.java
/** * Validates if two fields are equal in terms of String's equal() * function.//from www . j a va 2 s. c o m * * 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. */ 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.hyperic.util.validator.common.CommonValidatorUtil.java
/** * Validates the principal (Username) field based on default regular * expression.//from w w w. j a v a 2s. c om * * @param bean The bean containing the field to validate. * @param field The Field property info from the mapping file. * @return boolean If the field contains valid data <code>true</code> * is returned, otherwise <code>false</code>. */ // note to-do: accept non-default regexp (as property arg?) public static boolean validatePrincipal(Object bean, Field field) { boolean valid = false; // validate the length and characters. String userName = ValidatorUtils.getValueAsString(bean, field.getProperty()); if ((userName != null) && (GenericValidator.matchRegexp(userName, USERNAME_VALID_REGEXP))) valid = true; return valid; }
From source file:org.hyperic.util.validator.common.CommonValidatorUtil.java
/** * Validates a password field which restricts the length * between PASSWORD_MIN_LENGTH and PASSWORD_MAX_LENGTH * * @param bean containing the fields to validate. * @param Field object containing the property resource info. * /* www . j a v a2 s .c om*/ */ public static boolean validatePassword(Object bean, Field field) { boolean valid = false; // fetch the text to validate and enforce String pwd = ValidatorUtils.getValueAsString(bean, field.getProperty()); if ((pwd != null) && (GenericValidator.minLength(pwd, PASSWORD_MIN_LENGTH)) && (GenericValidator.maxLength(pwd, PASSWORD_MAX_LENGTH))) valid = true; return valid; }
From source file:org.hyperic.util.validator.common.CommonValidatorUtil.java
/** * Validates a password verification field which requires that the * value exactly match the String value of the bean property * referenced by property argument 1 (probably "password"). * * @param bean containing the fields to validate. * @param Field object containing the property resource info. * /*from ww w .j a va2s.c om*/ */ public static boolean validatePasswordVerification(Object bean, Field field) { boolean valid = false; String pwdv = ValidatorUtils.getValueAsString(bean, field.getProperty()); String pwd = ValidatorUtils.getValueAsString(bean, field.getArg(1).getKey()); // compare the pw verification field to the pw field. if ((pwd != null && pwdv != null) && (pwdv.compareTo(pwd) == 0)) valid = true; return valid; }
From source file:org.hyperic.util.validator.common.CommonValidatorUtil.java
/** * Conditional validation method.//from ww w. j a v a 2 s. c om * @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; }