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.internal.beaneditor.EnvironmentMessages; 021import org.apache.tapestry5.ioc.AnnotationProvider; 022import org.apache.tapestry5.ioc.Messages; 023import static org.apache.tapestry5.ioc.internal.util.CollectionFactory.newList; 024import org.apache.tapestry5.services.FieldValidatorDefaultSource; 025import org.apache.tapestry5.services.FieldValidatorSource; 026import org.apache.tapestry5.services.ValidationConstraintGenerator; 027import org.apache.tapestry5.services.Environment; 028 029import java.util.List; 030import java.util.Locale; 031 032public 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}