Coverage Report - org.apache.tapestry5.validator.Email
 
Classes in this File Line Coverage Branch Coverage Complexity
Email
75%
6/8
100%
2/2
0
 
 1  
 // Copyright 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.Pattern;
 24  
 
 25  
 /**
 26  
  * A validator that checks if a given string is well-formed email address. This validator is not configurable.
 27  
  */
 28  0
 public class Email extends AbstractValidator<Void, String>
 29  
 {
 30  
     private static final String ATOM = "[^\\x00-\\x1F^\\(^\\)^\\<^\\>^\\@^\\,^\\;^\\:^\\\\^\\\"^\\.^\\[^\\]^\\s]";
 31  
 
 32  
     private static final String DOMAIN = "(" + ATOM + "+(\\." + ATOM + "+)*";
 33  
 
 34  
     private static final String IP_DOMAIN = "\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\]";
 35  
 
 36  2
     private static final Pattern PATTERN = Pattern
 37  
             .compile("^" + ATOM + "+(\\." + ATOM + "+)*@" + DOMAIN + "|" + IP_DOMAIN + ")$", Pattern.CASE_INSENSITIVE);
 38  
 
 39  
     public Email()
 40  
     {
 41  6
         super(null, String.class, "invalid-email");
 42  6
     }
 43  
 
 44  
     public void render(Field field, Void constraintValue, MessageFormatter formatter, MarkupWriter markupWriter,
 45  
                        FormSupport formSupport)
 46  
     {
 47  0
     }
 48  
 
 49  
     private String buildMessage(MessageFormatter formatter, Field field)
 50  
     {
 51  4
         return formatter.format(field.getLabel());
 52  
     }
 53  
 
 54  
     public void validate(Field field, Void constraintValue, MessageFormatter formatter, String value)
 55  
             throws ValidationException
 56  
     {
 57  6
         if (!PATTERN.matcher(value).matches()) throw new ValidationException(buildMessage(formatter, field));
 58  2
     }
 59  
 }