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