Clover coverage report - Code Coverage for tapestry release 4.0-rc-2
Coverage timestamp: Sat Dec 17 2005 09:39:46 PST
file stats: LOC: 96   Methods: 6
NCLOC: 58   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Pattern.java 100% 100% 83.3% 96%
coverage 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 a user input string against a regular expression pattern.
 30    *
 31    * @author Howard Lewis Ship
 32    * @since 4.0
 33    * @see org.apache.tapestry.util.RegexpMatcher
 34    */
 35    public class Pattern extends BaseValidator
 36    {
 37    // It is expectd that each Pattern instance will be used by a single component instance,
 38    // and therefore be restricted to a single thread.
 39   
 40    private RegexpMatcher _matcher = new RegexpMatcher();
 41   
 42    private String _pattern;
 43   
 44  0 public Pattern()
 45    {
 46    }
 47   
 48  5 public Pattern(String initializer)
 49    {
 50  5 super(initializer);
 51    }
 52   
 53  3 public void validate(IFormComponent field, ValidationMessages messages, Object object)
 54    throws ValidatorException
 55    {
 56  3 String input = (String) object;
 57   
 58  3 if (!_matcher.matches(_pattern, input))
 59  2 throw new ValidatorException(buildMessage(messages, field),
 60    ValidationConstraint.PATTERN_MISMATCH);
 61    }
 62   
 63  4 private String buildMessage(ValidationMessages messages, IFormComponent field)
 64    {
 65  4 return messages.formatValidationMessage(
 66    getMessage(),
 67    ValidationStrings.REGEX_MISMATCH,
 68    new Object[]
 69    { field.getDisplayName() });
 70    }
 71   
 72  2 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 73    FormComponentContributorContext context, IFormComponent field)
 74    {
 75  2 context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
 76   
 77  2 String pattern = _matcher.getEscapedPatternString(_pattern);
 78  2 String message = buildMessage(context, field);
 79   
 80  2 StringBuffer buffer = new StringBuffer("function(event) { Tapestry.validate_regex(event, '");
 81  2 buffer.append(field.getClientId());
 82  2 buffer.append("', '");
 83  2 buffer.append(pattern);
 84  2 buffer.append("', ");
 85  2 buffer.append(TapestryUtils.enquote(message));
 86  2 buffer.append("); }");
 87   
 88  2 context.addSubmitHandler(buffer.toString());
 89    }
 90   
 91  5 public void setPattern(String pattern)
 92    {
 93  5 _pattern = pattern;
 94    }
 95   
 96    }