Class 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's isValid() method the PROP_SHORT_EVAL property will be checked. If it is defined, it overrides the flag set using the setShortEvaluation(boolean) method.

    Version:
    $Id: ChainValidator.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Field Detail

      • PROP_SHORT_EVAL

        public static final String PROP_SHORT_EVAL
        Constant for the short evaluation property.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ChainValidator

        public ChainValidator()
        Creates a new instance of ChainValidator.
    • Method Detail

      • isShortEvaluation

        public boolean isShortEvaluation()
        Returns the shortEvaluation flag.
        Returns:
        the flag for short evaluation
      • setShortEvaluation

        public void setShortEvaluation​(boolean shortEvaluation)
        Sets the shortEvaluation flag. This flag controls the behavior of the isValid() 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 the TransformerContext passed to the isValid() 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.
        Specified by:
        isValid in interface Validator
        Parameters:
        o - the object to be validated
        ctx - the transformer context
        Returns:
        an object with the results of the validation
      • getContextForChildValidator

        protected TransformerContext getContextForChildValidator​(int index,
                                                                 TransformerContext ctx)
        Returns the TransformerContext 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 validator
        ctx - 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 the isValid() method. This implementation checks whether the PROP_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