1 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 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 public EmailValidator() 44 { 45 } 46 47 52 53 public EmailValidator(String initializer) 54 { 55 super(initializer); 56 } 57 58 public String toString(IFormComponent field, Object value) 59 { 60 if (value == null) 61 return null; 62 63 return value.toString(); 64 } 65 66 public Object toObject(IFormComponent field, String input) throws ValidatorException 67 { 68 if (checkRequired(field, input)) 69 return null; 70 71 if (_minimumLength > 0 && input.length() < _minimumLength) 72 throw new ValidatorException(buildMinimumLengthMessage(field), 73 ValidationConstraint.MINIMUM_WIDTH); 74 75 if (!isValidEmail(input)) 76 throw new ValidatorException(buildInvalidEmailFormatMessage(field), 77 ValidationConstraint.EMAIL_FORMAT); 78 79 return input; 80 } 81 82 public int getMinimumLength() 83 { 84 return _minimumLength; 85 } 86 87 public void setMinimumLength(int minimumLength) 88 { 89 _minimumLength = minimumLength; 90 } 91 92 public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer, 93 IRequestCycle cycle) 94 { 95 if (!isClientScriptingEnabled()) 96 return; 97 98 Map symbols = new HashMap (); 99 100 Locale locale = field.getPage().getLocale(); 101 String displayName = field.getDisplayName(); 102 103 if (isRequired()) 104 symbols.put("requiredMessage", buildRequiredMessage(field)); 105 106 if (_minimumLength > 0) 107 symbols.put("minimumLengthMessage", buildMinimumLengthMessage(field)); 108 109 String pattern = getPattern(getInvalidEmailFormatMessage(), "invalid-email-format", locale); 110 111 symbols.put("emailFormatMessage", formatString(pattern, displayName)); 112 113 processValidatorScript(_scriptPath, cycle, field, symbols); 114 } 115 116 public String getScriptPath() 117 { 118 return _scriptPath; 119 } 120 121 127 128 public void setScriptPath(String scriptPath) 129 { 130 _scriptPath = scriptPath; 131 } 132 133 140 141 protected boolean isValidEmail(String email) 142 { 143 int atIndex = email.indexOf('@'); 144 145 return !((atIndex <= 0) || (atIndex == email.length() - 1)); 146 } 147 148 149 150 public String getInvalidEmailFormatMessage() 151 { 152 return _invalidEmailFormatMessage; 153 } 154 155 156 157 public String getMinimumLengthMessage() 158 { 159 return _minimumLengthMessage; 160 } 161 162 168 169 public void setInvalidEmailFormatMessage(String string) 170 { 171 _invalidEmailFormatMessage = string; 172 } 173 174 180 public void setMinimumLengthMessage(String string) 181 { 182 _minimumLengthMessage = string; 183 } 184 185 186 187 protected String buildMinimumLengthMessage(IFormComponent field) 188 { 189 String pattern = getPattern(_minimumLengthMessage, "field-too-short", field.getPage() 190 .getLocale()); 191 192 return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName()); 193 } 194 195 196 197 protected String buildInvalidEmailFormatMessage(IFormComponent field) 198 { 199 String pattern = getPattern(_invalidEmailFormatMessage, "invalid-email-format", field 200 .getPage().getLocale()); 201 202 return formatString(pattern, field.getDisplayName()); 203 } 204 } | Popular Tags |