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