Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 204   Methods: 16
NCLOC: 103   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
EmailValidator.java 35.7% 40.5% 50% 41.8%
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.Locale;
 19   
 import java.util.Map;
 20   
 
 21   
 import org.apache.tapestry.IMarkupWriter;
 22   
 import org.apache.tapestry.IRequestCycle;
 23   
 import org.apache.tapestry.form.IFormComponent;
 24   
 
 25   
 /**
 26   
  * Simple validation of email strings, to enforce required, and minimum length (maximum length is
 27   
  * enforced in the client browser, by setting a maximum input length on the text field).
 28   
  * 
 29   
  * @author Malcolm Edgar
 30   
  * @since 2.3
 31   
  */
 32   
 
 33   
 public class EmailValidator extends BaseValidator
 34   
 {
 35   
     private int _minimumLength;
 36   
 
 37   
     private String _minimumLengthMessage;
 38   
 
 39   
     private String _invalidEmailFormatMessage;
 40   
 
 41   
     private String _scriptPath = "/org/apache/tapestry/valid/EmailValidator.script";
 42   
 
 43  5
     public EmailValidator()
 44   
     {
 45   
     }
 46   
 
 47   
     /**
 48   
      * Initializes the EmailValidator with properties defined by the initializer.
 49   
      * 
 50   
      * @since 4.0
 51   
      */
 52   
 
 53  0
     public EmailValidator(String initializer)
 54   
     {
 55  0
         super(initializer);
 56   
     }
 57   
 
 58  0
     public String toString(IFormComponent field, Object value)
 59   
     {
 60  0
         if (value == null)
 61  0
             return null;
 62   
 
 63  0
         return value.toString();
 64   
     }
 65   
 
 66  5
     public Object toObject(IFormComponent field, String input) throws ValidatorException
 67   
     {
 68  5
         if (checkRequired(field, input))
 69  0
             return null;
 70   
 
 71  5
         if (_minimumLength > 0 && input.length() < _minimumLength)
 72  2
             throw new ValidatorException(buildMinimumLengthMessage(field),
 73   
                     ValidationConstraint.MINIMUM_WIDTH);
 74   
 
 75  3
         if (!isValidEmail(input))
 76  2
             throw new ValidatorException(buildInvalidEmailFormatMessage(field),
 77   
                     ValidationConstraint.EMAIL_FORMAT);
 78   
 
 79  1
         return input;
 80   
     }
 81   
 
 82  0
     public int getMinimumLength()
 83   
     {
 84  0
         return _minimumLength;
 85   
     }
 86   
 
 87  2
     public void setMinimumLength(int minimumLength)
 88   
     {
 89  2
         _minimumLength = minimumLength;
 90   
     }
 91   
 
 92  0
     public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
 93   
             IRequestCycle cycle)
 94   
     {
 95  0
         if (!isClientScriptingEnabled())
 96  0
             return;
 97   
 
 98  0
         Map symbols = new HashMap();
 99   
 
 100  0
         Locale locale = field.getPage().getLocale();
 101  0
         String displayName = field.getDisplayName();
 102   
 
 103  0
         if (isRequired())
 104  0
             symbols.put("requiredMessage", buildRequiredMessage(field));
 105   
 
 106  0
         if (_minimumLength > 0)
 107  0
             symbols.put("minimumLengthMessage", buildMinimumLengthMessage(field));
 108   
 
 109  0
         String pattern = getPattern(getInvalidEmailFormatMessage(), "invalid-email-format", locale);
 110   
 
 111  0
         symbols.put("emailFormatMessage", formatString(pattern, displayName));
 112   
 
 113  0
         processValidatorScript(_scriptPath, cycle, field, symbols);
 114   
     }
 115   
 
 116  0
     public String getScriptPath()
 117   
     {
 118  0
         return _scriptPath;
 119   
     }
 120   
 
 121   
     /**
 122   
      * Allows a developer to use the existing validation logic with a different client-side script.
 123   
      * This is often sufficient to allow application-specific error presentation (perhaps by using
 124   
      * DHTML to update the content of a &lt;span&gt; tag, or to use a more sophisticated pop-up
 125   
      * window than <code>window.alert()</code>).
 126   
      */
 127   
 
 128  0
     public void setScriptPath(String scriptPath)
 129   
     {
 130  0
         _scriptPath = scriptPath;
 131   
     }
 132   
 
 133   
     /**
 134   
      * Return true if the email format is valid.
 135   
      * 
 136   
      * @param email
 137   
      *            the email string to validate
 138   
      * @return true if the email format is valid
 139   
      */
 140   
 
 141  3
     protected boolean isValidEmail(String email)
 142   
     {
 143  3
         int atIndex = email.indexOf('@');
 144   
 
 145  3
         return !((atIndex <= 0) || (atIndex == email.length() - 1));
 146   
     }
 147   
 
 148   
     /** @since 3.0 */
 149   
 
 150  0
     public String getInvalidEmailFormatMessage()
 151   
     {
 152  0
         return _invalidEmailFormatMessage;
 153   
     }
 154   
 
 155   
     /** @since 3.0 */
 156   
 
 157  0
     public String getMinimumLengthMessage()
 158   
     {
 159  0
         return _minimumLengthMessage;
 160   
     }
 161   
 
 162   
     /**
 163   
      * Overrides the <code>invalid-email-format</code> bundle key. Parameter {0} is the display
 164   
      * name of the field.
 165   
      * 
 166   
      * @since 3.0
 167   
      */
 168   
 
 169  1
     public void setInvalidEmailFormatMessage(String string)
 170   
     {
 171  1
         _invalidEmailFormatMessage = string;
 172   
     }
 173   
 
 174   
     /**
 175   
      * Overrides the <code>field-too-short</code> bundle key. Parameter {0} is the minimum length.
 176   
      * Parameter {1} is the display name of the field.
 177   
      * 
 178   
      * @since 3.0
 179   
      */
 180  1
     public void setMinimumLengthMessage(String string)
 181   
     {
 182  1
         _minimumLengthMessage = string;
 183   
     }
 184   
 
 185   
     /** @since 3.0 */
 186   
 
 187  2
     protected String buildMinimumLengthMessage(IFormComponent field)
 188   
     {
 189  2
         String pattern = getPattern(_minimumLengthMessage, "field-too-short", field.getPage()
 190   
                 .getLocale());
 191   
 
 192  2
         return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName());
 193   
     }
 194   
 
 195   
     /** @since 3.0 */
 196   
 
 197  2
     protected String buildInvalidEmailFormatMessage(IFormComponent field)
 198   
     {
 199  2
         String pattern = getPattern(_invalidEmailFormatMessage, "invalid-email-format", field
 200   
                 .getPage().getLocale());
 201   
 
 202  2
         return formatString(pattern, field.getDisplayName());
 203   
     }
 204   
 }