Class ChainValidator
- java.lang.Object
-
- net.sf.jguiraffe.transform.ChainValidator
-
- All Implemented Interfaces:
Validator
public class ChainValidator extends Object implements Validator
A special
Validator
implementation that allows combining multiple primitive validators.An instance of this class can be initialized with an arbitrary number of child validators. In its implementation of the
isValid()
method the child validators are invoked one after the other. Configuration options control whether validation should stop when the first validation error was detected or whether in any case all child validators are to be invoked (in the latter case really all error messages caused by validation errors can be collected.)In the
TransformerContext
passed to this validator'sisValid()
method thePROP_SHORT_EVAL
property will be checked. If it is defined, it overrides the flag set using thesetShortEvaluation(boolean)
method.- Version:
- $Id: ChainValidator.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Field Summary
Fields Modifier and Type Field Description static String
PROP_SHORT_EVAL
Constant for the short evaluation property.
-
Constructor Summary
Constructors Constructor Description ChainValidator()
Creates a new instance ofChainValidator
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addChildValidator(Validator child)
Adds a child validator to this chain validator.void
addChildValidator(Validator child, Map<String,Object> props)
Adds a child validator to this chain validator and sets special properties for the new child.protected boolean
doShortEvaluation(TransformerContext ctx)
Checks whether short evaluation should be performed by theisValid()
method.Validator
getChildValidator(int index)
Returns the child validator at the given index.protected TransformerContext
getContextForChildValidator(int index, TransformerContext ctx)
Returns theTransformerContext
to be used for the specified child validator.boolean
isShortEvaluation()
Returns theshortEvaluation
flag.ValidationResult
isValid(Object o, TransformerContext ctx)
Validates the passed in object.void
setShortEvaluation(boolean shortEvaluation)
Sets theshortEvaluation
flag.int
size()
Returns the number of child validators.
-
-
-
Field Detail
-
PROP_SHORT_EVAL
public static final String PROP_SHORT_EVAL
Constant for the short evaluation property.- See Also:
- Constant Field Values
-
-
Method Detail
-
isShortEvaluation
public boolean isShortEvaluation()
Returns theshortEvaluation
flag.- Returns:
- the flag for short evaluation
-
setShortEvaluation
public void setShortEvaluation(boolean shortEvaluation)
Sets theshortEvaluation
flag. This flag controls the behavior of theisValid()
method in case of validation errors. If it is set, validation is aborted when the first child validator detects an error. Otherwise all child validators will be invoked and the results are collected.- Parameters:
shortEvaluation
- the value of the flag
-
addChildValidator
public void addChildValidator(Validator child)
Adds a child validator to this chain validator.- Parameters:
child
- the child validator to be added (must not be null)- Throws:
IllegalArgumentException
- if the validator to be added is null
-
addChildValidator
public void addChildValidator(Validator child, Map<String,Object> props)
Adds a child validator to this chain validator and sets special properties for the new child. If defined, these properties will be available through theTransformerContext
passed to theisValid()
method of the child.- Parameters:
child
- the child validator to be added (must not be null)props
- a map with properties for the new child validator- Throws:
IllegalArgumentException
- if the validator to be added is null
-
size
public int size()
Returns the number of child validators.- Returns:
- the number of child validators
-
getChildValidator
public Validator getChildValidator(int index)
Returns the child validator at the given index. The index is 0-based and can be in the range 0 <=index
<size()
.- Parameters:
index
- the index of the child validator- Returns:
- the child at this index
- Throws:
IndexOutOfBoundsException
- if the index is invalid
-
isValid
public ValidationResult isValid(Object o, TransformerContext ctx)
Validates the passed in object. This implementation delegates to the child validators.
-
getContextForChildValidator
protected TransformerContext getContextForChildValidator(int index, TransformerContext ctx)
Returns theTransformerContext
to be used for the specified child validator. If the child was added with custom properties, a specialized context will be created allowing access to these properties. Otherwise the default context will be returned.- Parameters:
index
- the index of the child validatorctx
- the original context- Returns:
- a context for this child validator
- Throws:
IndexOutOfBoundsException
- if the index is invalid
-
doShortEvaluation
protected boolean doShortEvaluation(TransformerContext ctx)
Checks whether short evaluation should be performed by theisValid()
method. This implementation checks whether thePROP_SHORT_EVAL
property is defined in the passed in context. If not,isShortEvaluation()
will be called.- Parameters:
ctx
- the current transformer context- Returns:
- a flag whether short evaluation should be performed
-
-