Coverage Report - org.apache.tapestry.form.validator.Email
 
Classes in this File Line Coverage Branch Coverage Complexity
Email
0%
0/17
0%
0/4
1.6
 
 1  
 // Copyright 2005 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.tapestry.form.validator;
 16  
 
 17  
 import org.apache.tapestry.IMarkupWriter;
 18  
 import org.apache.tapestry.IRequestCycle;
 19  
 import org.apache.tapestry.form.FormComponentContributorContext;
 20  
 import org.apache.tapestry.form.IFormComponent;
 21  
 import org.apache.tapestry.form.ValidationMessages;
 22  
 import org.apache.tapestry.json.JSONLiteral;
 23  
 import org.apache.tapestry.json.JSONObject;
 24  
 import org.apache.tapestry.valid.ValidationConstants;
 25  
 import org.apache.tapestry.valid.ValidationConstraint;
 26  
 import org.apache.tapestry.valid.ValidationStrings;
 27  
 import org.apache.tapestry.valid.ValidatorException;
 28  
 
 29  
 /**
 30  
  * Validates that the user input, a string, is an email address (by checking it against a regular
 31  
  * expression).
 32  
  * 
 33  
  * @author Howard Lewis Ship
 34  
  * @since 4.0
 35  
  */
 36  
 public class Email extends BaseValidator
 37  
 {
 38  
     public static final String TLD_PATTERN = "arpa|aero|biz|com|coop|edu|gov|info|int|mil|museum|name|net|"
 39  
             + "org|pro|travel|xxx|jobs|mobi|post|"
 40  
             + "ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|"
 41  
             + "bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|"
 42  
             + "ec|ee|eg|er|eu|es|et|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|"
 43  
             + "gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kr|kw|ky|kz|"
 44  
             + "la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|"
 45  
             + "my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|"
 46  
             + "re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sk|sl|sm|sn|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|"
 47  
             + "tn|to|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw";
 48  
     public static final String DOMAIN_PATTERN = "([0-9a-z]([-0-9a-z]{0,61}[0-9a-z])?\\.)+" + "(" + TLD_PATTERN + ")";
 49  
     public static final String USERNAME_PATTERN = "([-/!\\#$*?=_+&'\\da-z]+[.])*[-/!\\#$*?=_+&'\\da-z]+";
 50  
     public static final String PATTERN = "^(?i)"+ USERNAME_PATTERN + "@" + "(" + DOMAIN_PATTERN + ")$";
 51  
 
 52  0
     private static final java.util.regex.Pattern PATTERN_COMPILED = java.util.regex.Pattern.compile(PATTERN);
 53  
 
 54  
     public Email()
 55  0
     {
 56  0
     }
 57  
     
 58  
     public Email(String initializer)
 59  
     {
 60  0
         super(initializer);
 61  0
     }
 62  
     
 63  
     public void validate(IFormComponent field, ValidationMessages messages, Object object)
 64  
             throws ValidatorException
 65  
     {
 66  0
         String input = (String) object;
 67  
 
 68  0
         if ( !PATTERN_COMPILED.matcher(input).matches() )
 69  0
             throw new ValidatorException(buildMessage(messages, field),
 70  
                     ValidationConstraint.EMAIL_FORMAT);
 71  0
     }
 72  
 
 73  
     private String buildMessage(ValidationMessages messages, IFormComponent field)
 74  
     {
 75  0
         return messages.formatValidationMessage(
 76  
                 getMessage(),
 77  
                 ValidationStrings.INVALID_EMAIL,
 78  
                 new Object[]
 79  
                 { field.getDisplayName() });
 80  
     }
 81  
     
 82  
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 83  
             FormComponentContributorContext context, IFormComponent field)
 84  
     {        
 85  0
         JSONObject profile = context.getProfile();
 86  
         
 87  0
         if (!profile.has(ValidationConstants.CONSTRAINTS)) {
 88  0
             profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
 89  
         }
 90  0
         JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
 91  
         
 92  0
         accumulateProperty(cons, field.getClientId(),
 93  
                 new JSONLiteral("[tapestry.form.validation.isEmailAddress,false,true]"));
 94  
         
 95  0
         accumulateProfileProperty(field, profile, 
 96  
                 ValidationConstants.CONSTRAINTS, buildMessage(context, field));
 97  0
     }
 98  
 }