Clover coverage report - Code Coverage for tapestry release 4.0-alpha-2
Coverage timestamp: Thu May 5 2005 09:57:44 EDT
file stats: LOC: 151   Methods: 11
NCLOC: 73   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
StringValidator.java 71.4% 80.8% 72.7% 76.5%
coverage coverage
 1   
 // Copyright 2004, 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.valid;
 16   
 
 17   
 import java.util.HashMap;
 18   
 import java.util.Map;
 19   
 
 20   
 import org.apache.tapestry.IMarkupWriter;
 21   
 import org.apache.tapestry.IRequestCycle;
 22   
 import org.apache.tapestry.form.IFormComponent;
 23   
 
 24   
 /**
 25   
  * Simple validation of strings, to enforce required, and minimum length (maximum length is enforced
 26   
  * in the client browser, by setting a maximum input length on the text field).
 27   
  * 
 28   
  * @author Howard Lewis Ship
 29   
  * @since 1.0.8
 30   
  */
 31   
 
 32   
 public class StringValidator extends BaseValidator
 33   
 {
 34   
     private int _minimumLength;
 35   
 
 36   
     private String _minimumLengthMessage;
 37   
 
 38   
     /** @since 2.2 * */
 39   
 
 40   
     private String _scriptPath = "/org/apache/tapestry/valid/StringValidator.script";
 41   
 
 42  10
     public StringValidator()
 43   
     {
 44   
     }
 45   
 
 46  5
     public String toString(IFormComponent field, Object value)
 47   
     {
 48  5
         if (value == null)
 49  3
             return null;
 50   
 
 51  2
         return value.toString();
 52   
     }
 53   
 
 54  11
     public Object toObject(IFormComponent field, String input) throws ValidatorException
 55   
     {
 56  11
         if (checkRequired(field, input))
 57  2
             return null;
 58   
 
 59  6
         if (_minimumLength > 0 && input.length() < _minimumLength)
 60  1
             throw new ValidatorException(buildMinimumLengthMessage(field),
 61   
                     ValidationConstraint.MINIMUM_WIDTH);
 62   
 
 63  5
         return input;
 64   
     }
 65   
 
 66  8
     public int getMinimumLength()
 67   
     {
 68  8
         return _minimumLength;
 69   
     }
 70   
 
 71  5
     public void setMinimumLength(int minimumLength)
 72   
     {
 73  5
         _minimumLength = minimumLength;
 74   
     }
 75   
 
 76   
     /**
 77   
      * @since 2.2
 78   
      */
 79   
 
 80  4
     public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
 81   
             IRequestCycle cycle)
 82   
     {
 83  4
         if (!isClientScriptingEnabled())
 84  0
             return;
 85   
 
 86  4
         if (!(isRequired() || _minimumLength > 0))
 87  0
             return;
 88   
 
 89  4
         Map symbols = new HashMap();
 90   
 
 91  4
         if (isRequired())
 92  4
             symbols.put("requiredMessage", buildRequiredMessage(field));
 93   
 
 94  4
         if (_minimumLength > 0)
 95  4
             symbols.put("minimumLengthMessage", buildMinimumLengthMessage(field));
 96   
 
 97  4
         processValidatorScript(_scriptPath, cycle, field, symbols);
 98   
     }
 99   
 
 100   
     /**
 101   
      * @since 2.2
 102   
      */
 103   
 
 104  0
     public String getScriptPath()
 105   
     {
 106  0
         return _scriptPath;
 107   
     }
 108   
 
 109   
     /**
 110   
      * Allows a developer to use the existing validation logic with a different client-side script.
 111   
      * This is often sufficient to allow application-specific error presentation (perhaps by using
 112   
      * DHTML to update the content of a &lt;span&gt; tag, or to use a more sophisticated pop-up
 113   
      * window than <code>window.alert()</code>).
 114   
      * 
 115   
      * @since 2.2
 116   
      */
 117   
 
 118  0
     public void setScriptPath(String scriptPath)
 119   
     {
 120  0
         _scriptPath = scriptPath;
 121   
     }
 122   
 
 123   
     /** @since 3.0 */
 124  0
     public String getMinimumLengthMessage()
 125   
     {
 126  0
         return _minimumLengthMessage;
 127   
     }
 128   
 
 129   
     /**
 130   
      * Overrides the <code>field-too-short</code> bundle key. Parameter {0} is the minimum length.
 131   
      * Parameter {1} is the display name of the field.
 132   
      * 
 133   
      * @since 3.0
 134   
      */
 135   
 
 136  1
     public void setMinimumLengthMessage(String string)
 137   
     {
 138  1
         _minimumLengthMessage = string;
 139   
     }
 140   
 
 141   
     /** @since 3.0 */
 142   
 
 143  5
     protected String buildMinimumLengthMessage(IFormComponent field)
 144   
     {
 145  5
         String pattern = getPattern(_minimumLengthMessage, "field-too-short", field.getPage()
 146   
                 .getLocale());
 147   
 
 148  5
         return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName());
 149   
     }
 150   
 
 151   
 }