private <T extends ValidationListener> int compare(ValidationContext<T> context, Comparable<Object> constraintValue, Object currentValue) { int r; if (constraintValue.getClass().isAssignableFrom(currentValue.getClass())) { r = constraintValue.compareTo(context.getPropertyValue()); } else if (currentValue instanceof Number) { double dv = ((Number) currentValue).doubleValue(); double mdv = ((Number) constraintValue).doubleValue(); r = mdv > dv ? 1 : -1; } else { r = String.valueOf(constraintValue).compareTo(String.valueOf(currentValue)); } return r; }
protected <T extends ValidationListener> void validateMaxLength(ValidationContext<T> context) { Integer maxLength = (Integer) context.getMetaProperty().getFeature(Features.Property.MAX_LENGTH); if (maxLength == null) return; if (context.getPropertyValue() == null) return; final Object value = context.getPropertyValue(); int length = 0; if (value instanceof String) { length = ((String) value).length(); } else if (value instanceof Collection<?>) { length = ((Collection<?>) value).size(); } if (length > maxLength) { context.getListener().addError(MAX_LENGTH, context); } }
protected <T extends ValidationListener> void validateMinLength(ValidationContext<T> context) { Integer maxLength = (Integer) context.getMetaProperty().getFeature(Features.Property.MIN_LENGTH); if (maxLength == null) return; if (context.getPropertyValue() == null) return; final Object value = context.getPropertyValue(); int length = 0; if (value instanceof String) { length = ((String) value).length(); } else if (value instanceof Collection<?>) { length = ((Collection<?>) value).size(); } if (length < maxLength) { context.getListener().addError(MIN_LENGTH, context); } }
@Override public <T extends ValidationListener> void validate(ValidationContext<T> context) { if (context.getPropertyValue() == null) return; if (!EMailValidationUtils.isValid(context.getPropertyValue(), getPattern())) { context.getListener().addError(Reasons.EMAIL_ADDRESS, context); } }
protected <T extends ValidationListener> void validateMinValue(ValidationContext<T> context) { @SuppressWarnings("unchecked") Comparable<Object> minValue = (Comparable<Object>) context.getMetaProperty().getFeature(MIN_VALUE); if (minValue == null || context.getPropertyValue() == null) return; if (compare(context, minValue, context.getPropertyValue()) > 0) { context.getListener().addError(MIN_VALUE, context); } }
protected <T extends ValidationListener> void validateMaxValue(ValidationContext<T> context) { @SuppressWarnings("unchecked") Comparable<Object> maxValue = (Comparable<Object>) context.getMetaProperty().getFeature(MAX_VALUE); if (maxValue == null || context.getPropertyValue() == null) return; if (compare(context, maxValue, context.getPropertyValue()) < 0) { context.getListener().addError(MAX_VALUE, context); } }
protected <T extends ValidationListener> void validateTimeLag(ValidationContext<T> context) { String lag = (String) context.getMetaProperty().getFeature(TIME_LAG); if (lag == null) return; if (context.getPropertyValue() == null) return; long date = ((Date) context.getPropertyValue()).getTime(); long now = System.currentTimeMillis(); if (XMLMetaValue.TIMELAG_Future.equals(lag)) { if (date < now) { context.getListener().addError(TIME_LAG, context); } } else if (XMLMetaValue.TIMELAG_Past.equals(lag)) { if (date > now) { context.getListener().addError(TIME_LAG, context); } } else { throw new IllegalArgumentException("unknown timelag " + lag + " at " + context); } }
protected <T extends ValidationListener> void validateRegExp(ValidationContext<T> context) { final MetaProperty meta = context.getMetaProperty(); final String regExp = (String) meta.getFeature(REG_EXP); if (regExp == null) return; if (context.getPropertyValue() == null) return; final String value = String.valueOf(context.getPropertyValue()); try { Pattern pattern = (Pattern) meta.getFeature(REG_EXP_PATTERN); if (pattern == null) { pattern = Pattern.compile(regExp); meta.putFeature(REG_EXP_PATTERN, pattern); } if (!pattern.matcher(value).matches()) { context.getListener().addError(REG_EXP, context); } } catch (PatternSyntaxException e) { throw new IllegalArgumentException("regular expression malformed. regexp " + regExp + " at " + context, e); } }
protected <T extends ValidationListener> void validateMandatory(ValidationContext<T> context) { if (context.getMetaProperty().isMandatory()) { if (context.getPropertyValue() == null) { context.getListener().addError(MANDATORY, context); } } }
} else { if (context.getMetaProperty() == null) value = context.getBean(); else value = context.getPropertyValue(); message = messageTemplate; if (propPath == null)