Coverage Report - org.apache.tapestry.form.validator.Email
 
Classes in this File Line Coverage Branch Coverage Complexity
Email
100% 
100% 
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.util.RegexpMatcher;
 25  
 import org.apache.tapestry.valid.ValidationConstants;
 26  
 import org.apache.tapestry.valid.ValidationConstraint;
 27  
 import org.apache.tapestry.valid.ValidationStrings;
 28  
 import org.apache.tapestry.valid.ValidatorException;
 29  
 
 30  
 /**
 31  
  * Validates that the user input, a string, is an email address (by checking it against a regular
 32  
  * expression).
 33  
  * 
 34  
  * @author Howard Lewis Ship
 35  
  * @since 4.0
 36  
  */
 37  
 public class Email extends BaseValidator
 38  
 {
 39  
     public static final String PATTERN = "^[A-Za-z0-9]+([-_\\.]*[A-Za-z0-9]+)*@[A-Za-z0-9]+([-_\\.]*[A-Za-z0-9]+)*(\\.[_A-Za-z]{2,6})$";
 40  
     
 41  
     // TODO: Possible thread safety issue if the validator
 42  
     // is shared across threads, because the matcher
 43  
     // will be too.
 44  
     
 45  7
     private RegexpMatcher _matcher = new RegexpMatcher();
 46  
     
 47  
     public Email()
 48  5
     {
 49  5
     }
 50  
     
 51  
     public Email(String initializer)
 52  
     {
 53  2
         super(initializer);
 54  2
     }
 55  
     
 56  
     public void validate(IFormComponent field, ValidationMessages messages, Object object)
 57  
             throws ValidatorException
 58  
     {
 59  5
         String input = (String) object;
 60  
 
 61  5
         if (!_matcher.matches(PATTERN, input))
 62  2
             throw new ValidatorException(buildMessage(messages, field),
 63  
                     ValidationConstraint.EMAIL_FORMAT);
 64  3
     }
 65  
 
 66  
     private String buildMessage(ValidationMessages messages, IFormComponent field)
 67  
     {
 68  4
         return messages.formatValidationMessage(
 69  
                 getMessage(),
 70  
                 ValidationStrings.INVALID_EMAIL,
 71  
                 new Object[]
 72  
                 { field.getDisplayName() });
 73  
     }
 74  
     
 75  
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 76  
             FormComponentContributorContext context, IFormComponent field)
 77  
     {
 78  2
         context.addInitializationScript(field, "dojo.require(\"dojo.validate.web\");");
 79  
         
 80  2
         JSONObject profile = context.getProfile();
 81  
         
 82  2
         if (!profile.has(ValidationConstants.CONSTRAINTS)) {
 83  2
             profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
 84  
         }
 85  2
         JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
 86  
         
 87  2
         accumulateProperty(cons, field.getClientId(),
 88  
                 new JSONLiteral("[dojo.validate.isEmailAddress,false,true]"));
 89  
         
 90  2
         accumulateProfileProperty(field, profile, 
 91  
                 ValidationConstants.CONSTRAINTS, buildMessage(context, field));
 92  2
     }
 93  
 }