org.springmodules.validation.bean.rule
Class AbstractValidationRule

java.lang.Object
  extended by org.springmodules.validation.bean.rule.AbstractValidationRule
All Implemented Interfaces:
ValidationRule
Direct Known Subclasses:
ConditionReferenceValidationRule, DateInTheFutureValidationRule, DateInThePastValidationRule, EmailValidationRule, ExpressionValidationRule, InstantInTheFutureValidationRule, InstantInThePastValidationRule, LengthValidationRule, MaxLengthValidationRule, MaxSizeValidationRule, MaxValidationRule, MinLengthValidationRule, MinSizeValidationRule, MinValidationRule, NotBlankValidationRule, NotEmptyValidationRule, NotNullValidationRule, RangeValidationRule, RegExpValidationRule, SizeValidationRule, ValidationMethodValidationRule

public abstract class AbstractValidationRule
extends Object
implements ValidationRule

A super class for all specific validation rules.

Author:
Uri Boness

Constructor Summary
protected AbstractValidationRule(String defaultErrorCode)
          Constructs a new AbstractValidationRule with a default error code.
protected AbstractValidationRule(String defaultErrorCode, ErrorArgumentsResolver defaultErrorArgumentsResolver)
          Constructs a new AbstractValidationRule with a default error code and error argument resolver.
 
Method Summary
protected static boolean checkContexts(String[] contextTokens)
           
protected static ErrorArgumentsResolver createErrorArgumentsResolver(Object arg)
          A helper method for sub-classes helping with the creation of error argument resolvers.
protected static ErrorArgumentsResolver createErrorArgumentsResolver(Object[] arguments)
          A helper method for sub-classes helping with the creation of error argument resolvers.
protected static ErrorArgumentsResolver createErrorArgumentsResolver(Object arg1, Object arg2)
          A helper method for sub-classes helping with the creation of error argument resolvers.
protected static ErrorArgumentsResolver createErrorArgumentsResolver(Object arg1, Object arg2, Object arg3)
          A helper method for sub-classes helping with the creation of error argument resolvers.
 String getDefaultErrorMessage()
          Returns the default error message that can be used in case no error message is bound to the error code of the rule.
 Object[] getErrorArguments(Object obj)
          Returns the error arguments for this validation rule based on the given validated object.
 String getErrorCode()
          Return the error code of this validation rule.
 boolean isApplicable(Object obj)
          Checks if this validation rule is applicable on a given object by performing the following two steps: If the object is null and supportsNullValues() returns false then returning false.
 void setApplicabilityCondition(Condition applicabilityCondition)
          Sets the applicability condition that along with the supportsNullValues() method determines whether this condition is applicable on a given object or not.
 void setContextTokens(String[] contextTokens)
          Sets the validation contexts names in which this validation rule is applicable.
 void setDefaultErrorMessage(String defaultErrorMessage)
          Sets the default error message of this validation rule.
 void setErrorArgumentsResolver(ErrorArgumentsResolver errorArgumentsResolver)
          Sets the ErrorArgumentsResolver that will be used by this validation to extract the error arguments based on the validated object.
 void setErrorCode(String errorCode)
          Sets the error code of this validation rule.
protected  boolean supportsNullValues()
          Determines whether this validation rule supports null values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springmodules.validation.bean.rule.ValidationRule
getCondition
 

Constructor Detail

AbstractValidationRule

protected AbstractValidationRule(String defaultErrorCode)
Constructs a new AbstractValidationRule with a default error code. By default, the error arguments of this validation rule will only return the validated object itself.

Parameters:
defaultErrorCode -

AbstractValidationRule

protected AbstractValidationRule(String defaultErrorCode,
                                 ErrorArgumentsResolver defaultErrorArgumentsResolver)
Constructs a new AbstractValidationRule with a default error code and error argument resolver.

Parameters:
defaultErrorCode - The default error code for this validation rule.
defaultErrorArgumentsResolver - The argument resolver for this validation rule.
Method Detail

isApplicable

public boolean isApplicable(Object obj)
Checks if this validation rule is applicable on a given object by performing the following two steps:
  1. If the object is null and supportsNullValues() returns false then returning false.
  2. Returning whatever the applicablity condition check returns for the given object (setApplicabilityCondition(org.springmodules.validation.util.condition.Condition))
  3. Specified by:
    isApplicable in interface ValidationRule
    Parameters:
    obj - The object to be validated
    Returns:
    True if this validation rule is applicable on the given obj, false otherwise.
    See Also:
    ValidationRule.isApplicable(Object)

getErrorCode

public String getErrorCode()
Description copied from interface: ValidationRule
Return the error code of this validation rule. This method cannot return null.

Specified by:
getErrorCode in interface ValidationRule
Returns:
The error code of this validation rule.
See Also:
ValidationRule.getErrorCode()

getErrorArguments

public Object[] getErrorArguments(Object obj)
Returns the error arguments for this validation rule based on the given validated object. The arguments are determined by the associated ErrorArgumentsResolver.

Specified by:
getErrorArguments in interface ValidationRule
Parameters:
obj - The validated object.
Returns:
The arguments that apply to the error code of this validation rule.
See Also:
setErrorArgumentsResolver(org.springmodules.validation.bean.rule.resolver.ErrorArgumentsResolver), ValidationRule.getErrorArguments(Object)

getDefaultErrorMessage

public String getDefaultErrorMessage()
Description copied from interface: ValidationRule
Returns the default error message that can be used in case no error message is bound to the error code of the rule. This method can return null to indicate that no default message exists.

Specified by:
getDefaultErrorMessage in interface ValidationRule
Returns:
The default error message of this validation rule.
See Also:
ValidationRule.getDefaultErrorMessage()

supportsNullValues

protected boolean supportsNullValues()
Determines whether this validation rule supports null values. This method by default returns false, any sub-class that supports null values should override this method and return true.

Returns:
whether this validation rule supports null values.

setErrorCode

public void setErrorCode(String errorCode)
Sets the error code of this validation rule.

Parameters:
errorCode - The error code of this validation rule.

setDefaultErrorMessage

public void setDefaultErrorMessage(String defaultErrorMessage)
Sets the default error message of this validation rule.

Parameters:
defaultErrorMessage - The default error message of this validation rule.

setErrorArgumentsResolver

public void setErrorArgumentsResolver(ErrorArgumentsResolver errorArgumentsResolver)
Sets the ErrorArgumentsResolver that will be used by this validation to extract the error arguments based on the validated object.

Parameters:
errorArgumentsResolver - The error argument resolver that will be used by this validator.

setApplicabilityCondition

public void setApplicabilityCondition(Condition applicabilityCondition)
Sets the applicability condition that along with the supportsNullValues() method determines whether this condition is applicable on a given object or not.

Parameters:
applicabilityCondition - The applicability condition that will be used by this validation rule when deciding whether it is applicable on a given object.

setContextTokens

public void setContextTokens(String[] contextTokens)
Sets the validation contexts names in which this validation rule is applicable.

Parameters:
contextTokens - The validation context names in which this validation rule is applicable. null represents applicability in any context (even if one doesn't exsit).

createErrorArgumentsResolver

protected static ErrorArgumentsResolver createErrorArgumentsResolver(Object arg)
A helper method for sub-classes helping with the creation of error argument resolvers. The resolver created by this method will return an array that contains the validated object and the given arg as the error arguments.

Parameters:
arg - The error argument to be returned along with the validated object.
Returns:
The created error arguments resolver.

createErrorArgumentsResolver

protected static ErrorArgumentsResolver createErrorArgumentsResolver(Object arg1,
                                                                     Object arg2)
A helper method for sub-classes helping with the creation of error argument resolvers. The resolver created by this method will return an array that contains the validated object and the given arguments as the error arguments.

Parameters:
arg1 - The error argument to be returned in position {1}
arg2 - The error argument to be returned in position {2}
Returns:
The created error arguments resolver.

createErrorArgumentsResolver

protected static ErrorArgumentsResolver createErrorArgumentsResolver(Object arg1,
                                                                     Object arg2,
                                                                     Object arg3)
A helper method for sub-classes helping with the creation of error argument resolvers. The resolver created by this method will return an array that contains the validated object and the given arguments as the error arguments.

Parameters:
arg1 - The error argument to be returned in position {1}
arg2 - The error argument to be returned in position {2}
arg3 - The error argument to be returned in position {3}
Returns:
The created error arguments resolver.

createErrorArgumentsResolver

protected static ErrorArgumentsResolver createErrorArgumentsResolver(Object[] arguments)
A helper method for sub-classes helping with the creation of error argument resolvers. The resolver created by this method will return an array that contains the validated object and the given arguments as the error arguments.

Parameters:
arguments - The error arguments that will be return along with the validated object, starting in position {1}.
Returns:
The created error arguments resolver.

checkContexts

protected static boolean checkContexts(String[] contextTokens)


Copyright © 2009. All Rights Reserved.