Coverage Report - org.apache.tapestry.form.validator.Pattern
 
Classes in this File Line Coverage Branch Coverage Complexity
Pattern
0%
0/21
0%
0/4
1.429
 
 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  
 import org.apache.oro.text.regex.Perl5Compiler;
 29  
 
 30  
 /**
 31  
  * Validates a user input string against a regular expression pattern.
 32  
  * 
 33  
  * @author Howard Lewis Ship
 34  
  * @since 4.0
 35  
  * @see org.apache.tapestry.util.RegexpMatcher
 36  
  */
 37  
 public class Pattern extends BaseValidator
 38  
 {
 39  
     // It is expectd that each Pattern instance will be used by a single component instance,
 40  
     // and therefore be restricted to a single thread.
 41  
 
 42  
     private String _pattern;
 43  
     private String _quotedPattern;
 44  
     private java.util.regex.Pattern _compiledPattern;
 45  
 
 46  
     public Pattern()
 47  0
     {
 48  0
     }
 49  
 
 50  
     public Pattern(String initializer)
 51  
     {
 52  0
         super(initializer);
 53  0
     }
 54  
 
 55  
     public void validate(IFormComponent field, ValidationMessages messages, Object object)
 56  
             throws ValidatorException
 57  
     {
 58  0
         String input = object.toString();
 59  
 
 60  0
         if (! _compiledPattern.matcher(input).matches() )
 61  0
             throw new ValidatorException(buildMessage(messages, field),
 62  
                     ValidationConstraint.PATTERN_MISMATCH);
 63  0
     }
 64  
 
 65  
     private String buildMessage(ValidationMessages messages, IFormComponent field)
 66  
     {
 67  0
         return messages.formatValidationMessage(
 68  
                 getMessage(),
 69  
                 ValidationStrings.PATTERN_MISMATCH,
 70  
                 new Object[]
 71  
                 {field.getDisplayName(), _pattern });
 72  
     }
 73  
     
 74  
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 75  
             FormComponentContributorContext context, IFormComponent field)
 76  
     {
 77  0
        JSONObject profile = context.getProfile();
 78  
         
 79  0
         if (!profile.has(ValidationConstants.CONSTRAINTS)) {
 80  0
             profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
 81  
         }
 82  0
         JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
 83  
         
 84  0
         accumulateProperty(cons, field.getClientId(), 
 85  
                 new JSONLiteral("[tapestry.form.validation.isValidPattern,\""
 86  
                         + _quotedPattern + "\"]"));
 87  
         
 88  0
         accumulateProfileProperty(field, profile, 
 89  
                 ValidationConstants.CONSTRAINTS, buildMessage(context, field));
 90  0
     }
 91  
 
 92  
     public void setPattern(String pattern)
 93  
     {
 94  0
         _pattern = pattern;
 95  0
         _compiledPattern = java.util.regex.Pattern.compile(pattern);
 96  0
         _quotedPattern =  Perl5Compiler.quotemeta(_pattern);
 97  0
     }
 98  
 
 99  
     public String getPattern()
 100  
     {
 101  0
         return _pattern;
 102  
     }
 103  
 }