Coverage Report - org.apache.tapestry.valid.StringValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
StringValidator
0%
0/31
0%
0/18
2.167
 
 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
 26  
  * (maximum length is enforced in the client browser, by setting a maximum input
 27  
  * length on the text field).
 28  
  * 
 29  
  * @author Howard Lewis Ship
 30  
  * @since 1.0.8
 31  
  */
 32  
 
 33  
 public class StringValidator extends BaseValidator
 34  
 {
 35  
 
 36  
     private int _minimumLength;
 37  
 
 38  
     private String _minimumLengthMessage;
 39  
 
 40  
     /** @since 2.2 * */
 41  
 
 42  0
     private String _scriptPath = "/org/apache/tapestry/valid/StringValidator.script";
 43  
 
 44  
     public StringValidator()
 45  0
     {
 46  0
     }
 47  
 
 48  
     /**
 49  
      * Initializes the StringValidator with properties defined by the
 50  
      * initializer.
 51  
      * 
 52  
      * @since 4.0
 53  
      */
 54  
 
 55  
     public StringValidator(String initializer)
 56  
     {
 57  0
         super(initializer);
 58  0
     }
 59  
 
 60  
     public String toString(IFormComponent field, Object value)
 61  
     {
 62  0
         if (value == null) return null;
 63  
 
 64  0
         return value.toString();
 65  
     }
 66  
 
 67  
     public Object toObject(IFormComponent field, String input)
 68  
         throws ValidatorException
 69  
     {
 70  0
         if (checkRequired(field, input)) return null;
 71  
 
 72  0
         if (_minimumLength > 0 && input.length() < _minimumLength)
 73  0
             throw new ValidatorException(buildMinimumLengthMessage(field),
 74  
                     ValidationConstraint.MINIMUM_WIDTH);
 75  
 
 76  0
         return input;
 77  
     }
 78  
 
 79  
     public int getMinimumLength()
 80  
     {
 81  0
         return _minimumLength;
 82  
     }
 83  
 
 84  
     public void setMinimumLength(int minimumLength)
 85  
     {
 86  0
         _minimumLength = minimumLength;
 87  0
     }
 88  
 
 89  
     /**
 90  
      * @since 2.2
 91  
      */
 92  
 
 93  
     public void renderValidatorContribution(IFormComponent field,
 94  
             IMarkupWriter writer, IRequestCycle cycle)
 95  
     {
 96  0
         if (!isClientScriptingEnabled()) return;
 97  
 
 98  0
         if (!(isRequired() || _minimumLength > 0)) return;
 99  
 
 100  0
         Map symbols = new HashMap();
 101  
 
 102  0
         if (isRequired())
 103  0
             symbols.put("requiredMessage", buildRequiredMessage(field));
 104  
 
 105  0
         if (_minimumLength > 0)
 106  0
             symbols.put("minimumLengthMessage",
 107  
                     buildMinimumLengthMessage(field));
 108  
 
 109  0
         processValidatorScript(_scriptPath, cycle, field, symbols);
 110  0
     }
 111  
 
 112  
     /**
 113  
      * @since 2.2
 114  
      */
 115  
 
 116  
     public String getScriptPath()
 117  
     {
 118  0
         return _scriptPath;
 119  
     }
 120  
 
 121  
     /**
 122  
      * Allows a developer to use the existing validation logic with a different
 123  
      * client-side script. This is often sufficient to allow
 124  
      * application-specific error presentation (perhaps by using DHTML to update
 125  
      * the content of a &lt;span&gt; tag, or to use a more sophisticated pop-up
 126  
      * window than <code>window.alert()</code>).
 127  
      * 
 128  
      * @since 2.2
 129  
      */
 130  
 
 131  
     public void setScriptPath(String scriptPath)
 132  
     {
 133  0
         _scriptPath = scriptPath;
 134  0
     }
 135  
 
 136  
     /** @since 3.0 */
 137  
     public String getMinimumLengthMessage()
 138  
     {
 139  0
         return _minimumLengthMessage;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Overrides the <code>field-too-short</code> bundle key. Parameter {0} is
 144  
      * the minimum length. Parameter {1} is the display name of the field.
 145  
      * 
 146  
      * @since 3.0
 147  
      */
 148  
 
 149  
     public void setMinimumLengthMessage(String string)
 150  
     {
 151  0
         _minimumLengthMessage = string;
 152  0
     }
 153  
 
 154  
     /** @since 3.0 */
 155  
 
 156  
     protected String buildMinimumLengthMessage(IFormComponent field)
 157  
     {
 158  0
         String pattern = getPattern(_minimumLengthMessage, "field-too-short",
 159  
                 field.getPage().getLocale());
 160  
 
 161  0
         return formatString(pattern, Integer.toString(_minimumLength), field
 162  
                 .getDisplayName());
 163  
     }
 164  
 
 165  
 }