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
015package org.apache.tapestry5.internal.services;
016
017import org.apache.tapestry5.ComponentResources;
018import org.apache.tapestry5.Field;
019import org.apache.tapestry5.FieldValidator;
020import org.apache.tapestry5.commons.AnnotationProvider;
021import org.apache.tapestry5.commons.Messages;
022import org.apache.tapestry5.internal.beaneditor.EnvironmentMessages;
023import org.apache.tapestry5.services.FieldValidatorDefaultSource;
024import org.apache.tapestry5.services.FieldValidatorSource;
025import org.apache.tapestry5.services.ValidationConstraintGenerator;
026import org.apache.tapestry5.services.Environment;
027
028import static org.apache.tapestry5.commons.util.CollectionFactory.newList;
029
030import java.util.List;
031import java.util.Locale;
032
033public class FieldValidatorDefaultSourceImpl implements FieldValidatorDefaultSource
034{
035    private final ValidationConstraintGenerator validationConstraintGenerator;
036
037    private final FieldValidatorSource fieldValidatorSource;
038
039    public FieldValidatorDefaultSourceImpl(
040            ValidationConstraintGenerator validationConstraintGenerator,
041            FieldValidatorSource fieldValidatorSource)
042    {
043        this.validationConstraintGenerator = validationConstraintGenerator;
044        this.fieldValidatorSource = fieldValidatorSource;
045    }
046
047    public FieldValidator createDefaultValidator(Field field, String overrideId,
048                                                 Messages overrideMessages, Locale locale, Class propertyType,
049                                                 AnnotationProvider propertyAnnotations)
050    {
051        List<FieldValidator> validators = newList();
052
053        for (String constraint : validationConstraintGenerator.buildConstraints(
054                propertyType,
055                propertyAnnotations))
056        {
057            int equalsx = constraint.indexOf('=');
058
059            String validatorType = equalsx > 0 ? constraint.substring(0, equalsx) : constraint;
060            String constraintValue = equalsx > 0 ? constraint.substring(equalsx + 1) : null;
061
062            FieldValidator validator = fieldValidatorSource.createValidator(
063                    field,
064                    validatorType,
065                    constraintValue,
066                    overrideId,
067                    overrideMessages,
068                    locale);
069
070            validators.add(validator);
071        }
072        return validators.size() == 1 ? validators.get(0) : new CompositeFieldValidator(validators);
073    }
074
075    public FieldValidator createDefaultValidator(ComponentResources resources, String parameterName)
076    {
077        Class propertyType = resources.getBoundType(parameterName);
078
079        if (propertyType == null) return null;
080
081        Field field = (Field) resources.getComponent();
082
083        return createDefaultValidator(field, resources.getId(), resources.getContainerMessages(), resources.getLocale(),
084                                      propertyType, resources.getAnnotationProvider(parameterName));
085    }
086}