001// Copyright 2007 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.beaneditor; 016 017import org.apache.tapestry5.beaneditor.Validate; 018import org.apache.tapestry5.commons.AnnotationProvider; 019import org.apache.tapestry5.services.ValidationConstraintGenerator; 020 021import java.util.Arrays; 022import java.util.List; 023import java.util.regex.Pattern; 024 025/** 026 * Checks for the {@link Validate} annotation, and extracts its value to form the result. 027 */ 028public class ValidateAnnotationConstraintGenerator implements ValidationConstraintGenerator 029{ 030 031 static final String VALIDATOR_PATTERN="(?<!\\\\)\\s*,\\s*(?!([0-9]*\\}))"; 032 033 private final Pattern validatorPattern; 034 035 public ValidateAnnotationConstraintGenerator() 036 { 037 validatorPattern = Pattern.compile(VALIDATOR_PATTERN); 038 } 039 040 public List<String> buildConstraints(Class propertyType, AnnotationProvider annotationProvider) 041 { 042 Validate annotation = annotationProvider.getAnnotation(Validate.class); 043 044 if (annotation == null) 045 return null; 046 047 //TAP5-520: Commas within regular expressions like {n,m} or {n,} or a\,b . 048 //We use Negative Lookahead to avoid matching the case a\,b . 049 //We use Positive Lookahead to avoid matching cases {n,m} and {n,}. 050 //http://www.regular-expressions.info/lookaround.html 051 return Arrays.asList(validatorPattern.split(annotation.value())); 052 } 053 054}