001    // Copyright 2007, 2008 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.internal.services;
016    
017    import org.apache.tapestry5.FieldValidator;
018    import org.apache.tapestry5.MarkupWriter;
019    import org.apache.tapestry5.ValidationException;
020    
021    import java.util.List;
022    
023    /**
024     * Aggregates together a number of field validator instances as a single unit.
025     */
026    public final class CompositeFieldValidator implements FieldValidator
027    {
028        private final FieldValidator[] validators;
029    
030        public CompositeFieldValidator(List<FieldValidator> validators)
031        {
032            this.validators = validators.toArray(new FieldValidator[validators.size()]);
033        }
034    
035        @SuppressWarnings("unchecked")
036        public void validate(Object value) throws ValidationException
037        {
038            for (FieldValidator fv : validators)
039                fv.validate(value);
040        }
041    
042        public void render(MarkupWriter writer)
043        {
044            for (FieldValidator fv : validators)
045                fv.render(writer);
046        }
047    
048        public boolean isRequired()
049        {
050            for (FieldValidator fv : validators)
051            {
052                if (fv.isRequired()) return true;
053            }
054    
055            return false;
056        }
057    
058    }