Coverage Report - org.apache.tapestry5.validator.Regexp
 
Classes in this File Line Coverage Branch Coverage Complexity
Regexp
100%
9/9
100%
2/2
0
 
 1  
 // Copyright 2007, 2008 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry5.validator;
 16  
 
 17  
 import org.apache.tapestry5.Field;
 18  
 import org.apache.tapestry5.MarkupWriter;
 19  
 import org.apache.tapestry5.ValidationException;
 20  
 import org.apache.tapestry5.ioc.MessageFormatter;
 21  
 import org.apache.tapestry5.services.FormSupport;
 22  
 
 23  
 import java.util.regex.Matcher;
 24  
 import java.util.regex.Pattern;
 25  
 
 26  10
 public class Regexp extends AbstractValidator<Pattern, String>
 27  
 {
 28  
     public Regexp()
 29  
     {
 30  6
         super(Pattern.class, String.class, "regexp");
 31  6
     }
 32  
 
 33  
     private String buildMessage(MessageFormatter formatter, Field field, Pattern constraintValue)
 34  
     {
 35  8
         return formatter.format(constraintValue.toString(), field.getLabel());
 36  
     }
 37  
 
 38  
     public void render(Field field, Pattern constraintValue, MessageFormatter formatter, MarkupWriter writer,
 39  
                        FormSupport formSupport)
 40  
     {
 41  6
         formSupport.addValidation(field, "regexp", buildMessage(formatter, field, constraintValue),
 42  
                                   constraintValue.pattern());
 43  6
     }
 44  
 
 45  
     public void validate(Field field, Pattern constraintValue, MessageFormatter formatter, String value)
 46  
             throws ValidationException
 47  
     {
 48  8
         Matcher matcher = constraintValue.matcher(value);
 49  
 
 50  8
         if (!matcher.matches()) throw new ValidationException(buildMessage(formatter, field, constraintValue));
 51  6
     }
 52  
 
 53  
 }