Clover coverage report - Code Coverage for tapestry release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:12:14 EST
file stats: LOC: 90   Methods: 5
NCLOC: 54   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Email.java 100% 100% 100% 100%
coverage
 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.TapestryUtils;
 20    import org.apache.tapestry.form.FormComponentContributorContext;
 21    import org.apache.tapestry.form.IFormComponent;
 22    import org.apache.tapestry.form.ValidationMessages;
 23    import org.apache.tapestry.util.RegexpMatcher;
 24    import org.apache.tapestry.valid.ValidationConstraint;
 25    import org.apache.tapestry.valid.ValidationStrings;
 26    import org.apache.tapestry.valid.ValidatorException;
 27   
 28    /**
 29    * Validates that the user input, a string, is an email address (by checking it against a regular
 30    * expression).
 31    *
 32    * @author Howard Lewis Ship
 33    * @since 4.0
 34    */
 35    public class Email extends BaseValidator
 36    {
 37    static final String PATTERN = "^\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,6}$";
 38   
 39    // TODO: Possible thread safety issue if the validator
 40    // is shared across threads, because the matcher
 41    // will be too.
 42    private RegexpMatcher _matcher = new RegexpMatcher();
 43   
 44  8 public Email()
 45    {
 46    }
 47   
 48  4 public Email(String initializer)
 49    {
 50  4 super(initializer);
 51    }
 52   
 53  6 public void validate(IFormComponent field, ValidationMessages messages, Object object)
 54    throws ValidatorException
 55    {
 56  6 String input = (String) object;
 57   
 58  6 if (!_matcher.matches(PATTERN, input))
 59  4 throw new ValidatorException(buildMessage(messages, field),
 60    ValidationConstraint.EMAIL_FORMAT);
 61    }
 62   
 63  8 private String buildMessage(ValidationMessages messages, IFormComponent field)
 64    {
 65  8 return messages.formatValidationMessage(
 66    getMessage(),
 67    ValidationStrings.INVALID_EMAIL,
 68    new Object[]
 69    { field.getDisplayName() });
 70    }
 71   
 72  4 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 73    FormComponentContributorContext context, IFormComponent field)
 74    {
 75  4 context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
 76   
 77  4 String pattern = _matcher.getEscapedPatternString(PATTERN);
 78  4 String message = TapestryUtils.enquote(buildMessage(context, field));
 79   
 80  4 StringBuffer buffer = new StringBuffer("function(event) { Tapestry.validate_regex(event, '");
 81  4 buffer.append(field.getClientId());
 82  4 buffer.append("', '");
 83  4 buffer.append(pattern);
 84  4 buffer.append("', ");
 85  4 buffer.append(message);
 86  4 buffer.append("); }");
 87   
 88  4 context.addSubmitHandler(buffer.toString());
 89    }
 90    }