All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springmodules.validation.bean.RuleBasedValidator Maven / Gradle / Ivy

There is a newer version: 0.8a
Show newest version
/*
 * Copyright 2004-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springmodules.validation.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springmodules.validation.bean.rule.DefaultValidationRule;
import org.springmodules.validation.bean.rule.PropertyValidationRule;
import org.springmodules.validation.bean.rule.ValidationRule;
import org.springmodules.validation.util.condition.Condition;

/**
 * A {@link org.springframework.validation.Validator} implementation which uses {@link org.springmodules.validation.bean.rule.ValidationRule}'s to define its
 * validation execution. There are two types of validation rules this validator accepts:
 * 
    *
  • * Global Rules - Rules that apply on the validated objects and where all validation errors are registered * globaly with the {@link Errors} object (i.e. {@link Errors#reject(String)}). *
  • *
  • * Property Rules - Rules that apply on specific properties of the validated objects and where all validation * errors are registered with the {@link Errors} object under the context of those properties * (i.e. {@link Errors#rejectValue(String, String)}). *
  • *
* * @author Uri Boness */ public class RuleBasedValidator implements Validator { // a list of global ValidationRule's - List private List globalRules; // maps a list of ValidationRule's to propertyNames - Map> private Map rulesByProperty; /** * Contrusts a new RuleBasedValidator for the given type. After contruction, this validator will initially hold * no rules. */ public RuleBasedValidator() { globalRules = new ArrayList(); rulesByProperty = new HashMap(); } /** * This validator supports all classes. Any object can be validated by this validator as long as the validation rules * apply to it. * * @see Validator#supports(Class) */ public boolean supports(Class clazz) { return true; } /** * Validates the given object and registers all validation errors with the given errors object. The validation * is done by applying all validation rules associated with this validator on the given object. * * @see org.springframework.validation.Validator#validate(Object, org.springframework.validation.Errors) */ public void validate(Object obj, Errors errors) { // validating using the registered global rules for (Iterator iter = globalRules.iterator(); iter.hasNext();) { ValidationRule rule = (ValidationRule)iter.next(); if (rule.isApplicable(obj) && !rule.getCondition().check(obj)) { errors.reject(rule.getErrorCode(), rule.getErrorArguments(obj), rule.getDefaultErrorMessage()); } } // validating using the registered field rules for (Iterator names = rulesByProperty.keySet().iterator(); names.hasNext();) { String propertyName = (String)names.next(); List rules = (List)rulesByProperty.get(propertyName); for (Iterator iter = rules.iterator(); iter.hasNext();) { ValidationRule rule = (ValidationRule)iter.next(); if (rule.isApplicable(obj) && !rule.getCondition().check(obj)) { errors.rejectValue(propertyName, rule.getErrorCode(), rule.getErrorArguments(obj), rule.getDefaultErrorMessage()); } } } } //====== Setters to support JavaBean based configuration environment (e.g. spring's application context =========== /** * Sets extra global validation rules for this validator. * * @param globalRules The extra global validation rules to be added to this validator. */ public void setExtraGlobalVadlidationRules(ValidationRule[] globalRules) { for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy