org.springmodules.validation.bean
Class BeanValidator

java.lang.Object
  extended by org.springmodules.validation.bean.RuleBasedValidator
      extended by org.springmodules.validation.bean.BeanValidator
All Implemented Interfaces:
Validator

public class BeanValidator
extends RuleBasedValidator

An AbstractTypeSpecificValidator implementation that applies all validation rules on a bean of a specific type, based on an appropriate BeanValidationConfiguration. The validation configuration is loaded per bean type by the configured BeanValidationConfigurationLoader.

Author:
Uri Boness

Constructor Summary
BeanValidator()
          Constructs a new BeanValidator.
BeanValidator(BeanValidationConfigurationLoader configurationLoader)
          Constructs a new BeanValidator for the given bean class using the given validation configuration loader.
 
Method Summary
protected  void applyBeanValidation(BeanValidationConfiguration configuration, Object obj, Errors errors)
          Applying the validation rules listed in the given validation configuration on the given object, and registering all validation errors with the given Errors object.
protected  void applyCustomValidator(BeanValidationConfiguration configuration, Object obj, Errors errors)
          Applies the custom validator of the given configuration (if one exists) on the given object.
protected  void applyGlobalValidationRules(BeanValidationConfiguration configuration, Object obj, Errors errors)
          Applies the global validation rules as listed in the given validation configuration on the given object, and registering all global validation errors with the given Errors.
protected  void applyPropertiesValidationRules(BeanValidationConfiguration configuration, Object obj, Errors errors)
          Applies the property validation rules as listed in the given validation configuration on the given object, and registering all property validation errors with the given Errors.
 void setConfigurationLoader(BeanValidationConfigurationLoader configurationLoader)
          Sets the bean validation configuration loader this validator will use to load the bean validation configurations.
 void setErrorCodeConverter(ErrorCodeConverter errorCodeConverter)
          Sets the error code converter this validator will use to resolve the error codes to be registered with the Errors object.
 void setShortCircuitFieldValidation(boolean shortCircuitFieldValidation)
          Determines whether field validation will be short-ciruite, that is, if multiple validation rules are defined on a field, the first rule to fail will stop the validation process for that field.
 boolean supports(Class clazz)
          This validator supports only those classes that are supported by the validation configuration loader it uses.
 void validate(Object obj, Errors errors)
          Applies all validation rules as defined in the BeanValidationConfiguration retrieved for the given bean from the configured BeanValidationConfigurationLoader.
protected  void validateAndShortCircuitRules(ValidationRule[] rules, String propertyName, Object obj, Errors errors)
          Applying the given validation rules on the given property of the given object.
protected  void validateArrayProperty(Object root, Object array, String propertyName, Errors errors, Set validatedObjects)
          Validates the elements of the given array property.
protected  void validatedSubBean(Object root, Object subBean, String propertyName, Errors errors, Set validatedObjects)
          Validates the given nested property bean (sub-bean).
protected  void validateListOrSetProperty(Object root, Collection collection, String propertyName, Errors errors, Set validatedObjects)
          Validates the elements of the given list or set property.
protected  void validateMapProperty(Object root, Map map, String propertyName, Errors errors, Set validatedObjects)
          Validates the elements within the given map property.
protected  void validateObjectGraphConstraints(Object root, Object obj, Errors errors, Set validatedObjects)
          The heart of this validator.
protected  BeanWrapper wrapBean(Object bean)
          Wraps the given bean in a BeanWrapper.
 
Methods inherited from class org.springmodules.validation.bean.RuleBasedValidator
addGlobalRule, addGlobalRule, addGlobalRule, addGlobalRule, addGlobalRule, addPropertyGlobalRule, addPropertyRule, addPropertyRule, addPropertyRule, addPropertyRule, setExtraGlobalVadlidationRules, setExtraPropertyValidationRules
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanValidator

public BeanValidator()
Constructs a new BeanValidator. By default the SimpleBeanValidationConfigurationLoader is used as the bean validation configuration loader.


BeanValidator

public BeanValidator(BeanValidationConfigurationLoader configurationLoader)
Constructs a new BeanValidator for the given bean class using the given validation configuration loader.

Parameters:
configurationLoader - The BeanValidationConfigurationLoader that is used to load the bean validation configuration.
Method Detail

supports

public boolean supports(Class clazz)
This validator supports only those classes that are supported by the validation configuration loader it uses.

Specified by:
supports in interface Validator
Overrides:
supports in class RuleBasedValidator
See Also:
RuleBasedValidator.supports(Class), BeanValidationConfigurationLoader.supports(Class)

validate

public void validate(Object obj,
                     Errors errors)
Applies all validation rules as defined in the BeanValidationConfiguration retrieved for the given bean from the configured BeanValidationConfigurationLoader.

Specified by:
validate in interface Validator
Overrides:
validate in class RuleBasedValidator
See Also:
Validator.validate(Object, org.springframework.validation.Errors)

setErrorCodeConverter

public void setErrorCodeConverter(ErrorCodeConverter errorCodeConverter)
Sets the error code converter this validator will use to resolve the error codes to be registered with the Errors object.

Parameters:
errorCodeConverter - The error code converter this validator will use to resolve the error different error codes.

setConfigurationLoader

public void setConfigurationLoader(BeanValidationConfigurationLoader configurationLoader)
Sets the bean validation configuration loader this validator will use to load the bean validation configurations.

Parameters:
configurationLoader - The loader this validator will use to load the bean validation configurations.

setShortCircuitFieldValidation

public void setShortCircuitFieldValidation(boolean shortCircuitFieldValidation)
Determines whether field validation will be short-ciruite, that is, if multiple validation rules are defined on a field, the first rule to fail will stop the validation process for that field. By default the field validation will be short-circuited.

Parameters:
shortCircuitFieldValidation -

validateObjectGraphConstraints

protected void validateObjectGraphConstraints(Object root,
                                              Object obj,
                                              Errors errors,
                                              Set validatedObjects)
The heart of this validator. This is a recursive method that validates the given object (object) under the context of the given object graph root (root). The validation rules to be applied are loaded using the configured BeanValidationConfigurationLoader. All errors are registered with the given Errors object under the context of the object graph root.

Parameters:
root - The root of the object graph.
obj - The object to be validated
errors - The Errors instance where the validation errors will be registered.
validatedObjects - keeps track of all the validated objects (to prevent revalidating the same objects when a circular relationship exists).

wrapBean

protected BeanWrapper wrapBean(Object bean)
Wraps the given bean in a BeanWrapper.

Parameters:
bean - The bean to be wraped.
Returns:
The bean wrapper that wraps the given bean.

validateArrayProperty

protected void validateArrayProperty(Object root,
                                     Object array,
                                     String propertyName,
                                     Errors errors,
                                     Set validatedObjects)
Validates the elements of the given array property.

Parameters:
root - The root of the object graph that is being validated.
array - The given array.
propertyName - The name of the array property.
errors - The Errors instance where all validation errors will be registered.
validatedObjects - A registry of all objects that were already validated.

validateListOrSetProperty

protected void validateListOrSetProperty(Object root,
                                         Collection collection,
                                         String propertyName,
                                         Errors errors,
                                         Set validatedObjects)
Validates the elements of the given list or set property.

Parameters:
root - The root of the object graph that is being validated.
collection - The given list or set.
propertyName - The name of the array property.
errors - The Errors instance where all validation errors will be registered.
validatedObjects - A registry of all objects that were already validated.

validateMapProperty

protected void validateMapProperty(Object root,
                                   Map map,
                                   String propertyName,
                                   Errors errors,
                                   Set validatedObjects)
Validates the elements within the given map property.

Parameters:
root - The root of the object graph that is being validated.
map - The given map or set.
propertyName - The name of the array property.
errors - The Errors instance where all validation errors will be registered.
validatedObjects - A registry of all objects that were already validated.

validatedSubBean

protected void validatedSubBean(Object root,
                                Object subBean,
                                String propertyName,
                                Errors errors,
                                Set validatedObjects)
Validates the given nested property bean (sub-bean).

Parameters:
root - The root of the object graph that is being validated.
subBean - The given nested property value (the sub-bean).
propertyName - The name of the array property.
errors - The Errors instance where all validation errors will be registered.
validatedObjects - A registry of all objects that were already validated.

applyBeanValidation

protected void applyBeanValidation(BeanValidationConfiguration configuration,
                                   Object obj,
                                   Errors errors)
Applying the validation rules listed in the given validation configuration on the given object, and registering all validation errors with the given Errors object.

Parameters:
configuration - The bean validation configuration that define the validation rules to be applied.
obj - The validated object.
errors - The Errors instance where the validation error will be registered.

applyGlobalValidationRules

protected void applyGlobalValidationRules(BeanValidationConfiguration configuration,
                                          Object obj,
                                          Errors errors)
Applies the global validation rules as listed in the given validation configuration on the given object, and registering all global validation errors with the given Errors.

Parameters:
configuration - The bean validation configuration that holds all the global validation rules.
obj - The validated object.
errors - The Errors instance where all global validation errors will be registered.

applyPropertiesValidationRules

protected void applyPropertiesValidationRules(BeanValidationConfiguration configuration,
                                              Object obj,
                                              Errors errors)
Applies the property validation rules as listed in the given validation configuration on the given object, and registering all property validation errors with the given Errors.

Parameters:
configuration - The bean validation configuration that holds all the property validation rules.
obj - The validated object.
errors - The Errors instance where all property validation errors will be registered (see Errors.rejectValue(String, String)).

validateAndShortCircuitRules

protected void validateAndShortCircuitRules(ValidationRule[] rules,
                                            String propertyName,
                                            Object obj,
                                            Errors errors)
Applying the given validation rules on the given property of the given object. The validation stops as soon as one of the validation rules produces validation errors. This errors are then registered with the given {@link Errors) instance.

Parameters:
rules - The validation rules that should be applied on the given property of the given object.
propertyName - The name of the property to be validated.
obj - The validated object.
errors - The Errors instance where the validation errors will be registered.

applyCustomValidator

protected void applyCustomValidator(BeanValidationConfiguration configuration,
                                    Object obj,
                                    Errors errors)
Applies the custom validator of the given configuration (if one exists) on the given object.

Parameters:
configuration - The configuration from which the custom validator will be taken from.
obj - The object to be validated.
errors - The Errors instance where all validation errors will be registered.


Copyright © 2009. All Rights Reserved.