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: 193   Methods: 15
NCLOC: 99   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% 41.7% 53.3% 43.1%
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  0
     public String toString(IFormComponent field, Object value)
 48   
     {
 49  0
         if (value == null)
 50  0
             return null;
 51   
 
 52  0
         return value.toString();
 53   
     }
 54   
 
 55  5
     public Object toObject(IFormComponent field, String input) throws ValidatorException
 56   
     {
 57  5
         if (checkRequired(field, input))
 58  0
             return null;
 59   
 
 60  5
         if (_minimumLength > 0 && input.length() < _minimumLength)
 61  2
             throw new ValidatorException(buildMinimumLengthMessage(field),
 62   
                     ValidationConstraint.MINIMUM_WIDTH);
 63   
 
 64  3
         if (!isValidEmail(input))
 65  2
             throw new ValidatorException(buildInvalidEmailFormatMessage(field),
 66   
                     ValidationConstraint.EMAIL_FORMAT);
 67   
 
 68  1
         return input;
 69   
     }
 70   
 
 71  0
     public int getMinimumLength()
 72   
     {
 73  0
         return _minimumLength;
 74   
     }
 75   
 
 76  2
     public void setMinimumLength(int minimumLength)
 77   
     {
 78  2
         _minimumLength = minimumLength;
 79   
     }
 80   
 
 81  0
     public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
 82   
             IRequestCycle cycle)
 83   
     {
 84  0
         if (!isClientScriptingEnabled())
 85  0
             return;
 86   
 
 87  0
         Map symbols = new HashMap();
 88   
 
 89  0
         Locale locale = field.getPage().getLocale();
 90  0
         String displayName = field.getDisplayName();
 91   
 
 92  0
         if (isRequired())
 93  0
             symbols.put("requiredMessage", buildRequiredMessage(field));
 94   
 
 95  0
         if (_minimumLength > 0)
 96  0
             symbols.put("minimumLengthMessage", buildMinimumLengthMessage(field));
 97   
 
 98  0
         String pattern = getPattern(getInvalidEmailFormatMessage(), "invalid-email-format", locale);
 99   
 
 100  0
         symbols.put("emailFormatMessage", formatString(pattern, displayName));
 101   
 
 102  0
         processValidatorScript(_scriptPath, cycle, field, symbols);
 103   
     }
 104   
 
 105  0
     public String getScriptPath()
 106   
     {
 107  0
         return _scriptPath;
 108   
     }
 109   
 
 110   
     /**
 111   
      * Allows a developer to use the existing validation logic with a different client-side script.
 112   
      * This is often sufficient to allow application-specific error presentation (perhaps by using
 113   
      * DHTML to update the content of a &lt;span&gt; tag, or to use a more sophisticated pop-up
 114   
      * window than <code>window.alert()</code>).
 115   
      */
 116   
 
 117  0
     public void setScriptPath(String scriptPath)
 118   
     {
 119  0
         _scriptPath = scriptPath;
 120   
     }
 121   
 
 122   
     /**
 123   
      * Return true if the email format is valid.
 124   
      * 
 125   
      * @param email
 126   
      *            the email string to validate
 127   
      * @return true if the email format is valid
 128   
      */
 129   
 
 130  3
     protected boolean isValidEmail(String email)
 131   
     {
 132  3
         int atIndex = email.indexOf('@');
 133   
 
 134  3
         return !((atIndex <= 0) || (atIndex == email.length() - 1));
 135   
     }
 136   
 
 137   
     /** @since 3.0 */
 138   
 
 139  0
     public String getInvalidEmailFormatMessage()
 140   
     {
 141  0
         return _invalidEmailFormatMessage;
 142   
     }
 143   
 
 144   
     /** @since 3.0 */
 145   
 
 146  0
     public String getMinimumLengthMessage()
 147   
     {
 148  0
         return _minimumLengthMessage;
 149   
     }
 150   
 
 151   
     /**
 152   
      * Overrides the <code>invalid-email-format</code> bundle key. Parameter {0} is the display
 153   
      * name of the field.
 154   
      * 
 155   
      * @since 3.0
 156   
      */
 157   
 
 158  1
     public void setInvalidEmailFormatMessage(String string)
 159   
     {
 160  1
         _invalidEmailFormatMessage = string;
 161   
     }
 162   
 
 163   
     /**
 164   
      * Overrides the <code>field-too-short</code> bundle key. Parameter {0} is the minimum length.
 165   
      * Parameter {1} is the display name of the field.
 166   
      * 
 167   
      * @since 3.0
 168   
      */
 169  1
     public void setMinimumLengthMessage(String string)
 170   
     {
 171  1
         _minimumLengthMessage = string;
 172   
     }
 173   
 
 174   
     /** @since 3.0 */
 175   
 
 176  2
     protected String buildMinimumLengthMessage(IFormComponent field)
 177   
     {
 178  2
         String pattern = getPattern(_minimumLengthMessage, "field-too-short", field.getPage()
 179   
                 .getLocale());
 180   
 
 181  2
         return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName());
 182   
     }
 183   
 
 184   
     /** @since 3.0 */
 185   
 
 186  2
     protected String buildInvalidEmailFormatMessage(IFormComponent field)
 187   
     {
 188  2
         String pattern = getPattern(_invalidEmailFormatMessage, "invalid-email-format", field
 189   
                 .getPage().getLocale());
 190   
 
 191  2
         return formatString(pattern, field.getDisplayName());
 192   
     }
 193   
 }