1 16 package org.apache.cocoon.forms.datatype.validationruleimpl; 17 18 import org.apache.cocoon.forms.FormsConstants; 19 import org.apache.cocoon.forms.util.I18nMessage; 20 import org.apache.cocoon.forms.validation.ValidationError; 21 import org.apache.oro.text.perl.Perl5Util; 22 import org.outerj.expression.ExpressionContext; 23 24 30 public class EmailValidationRule extends AbstractValidationRule { 31 32 private static final String SPECIAL_CHARS = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"; 33 private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]"; 34 private static final String QUOTED_USER = "(\"[^\"]*\")"; 35 private static final String ATOM = VALID_CHARS + '+'; 36 private static final String WORD = "(" + ATOM + "|" + QUOTED_USER + ")"; 37 38 private static final String LEGAL_ASCII_PATTERN = "/^[\\000-\\177]+$/"; 40 private static final String EMAIL_PATTERN = "/^(.+)@(.+)$/"; 41 private static final String IP_DOMAIN_PATTERN = 42 "/^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$/"; 43 44 private static final String USER_PATTERN = "/^" + WORD + "(\\." + WORD + ")*$/"; 45 private static final String DOMAIN_PATTERN = "/^" + ATOM + "(\\." + ATOM + ")*$/"; 46 private static final String ATOM_PATTERN = "/(" + ATOM + ")/"; 47 48 public ValidationError validate(Object value, ExpressionContext expressionContext) { 49 String email = (String )value; 50 51 if (isEmail(email)) 52 return null; 53 else 54 return hasFailMessage() ? getFailMessage() : new ValidationError(new I18nMessage("validation.string.invalidemail", FormsConstants.I18N_CATALOGUE)); 55 } 56 57 public boolean supportsType(Class clazz, boolean arrayType) { 58 return clazz.isAssignableFrom(String .class) && !arrayType; 59 } 60 61 boolean isEmail(String email) { 62 if (email == null) { 63 return false; 64 } 65 66 Perl5Util matchAsciiPat = new Perl5Util(); 67 if (!matchAsciiPat.match(LEGAL_ASCII_PATTERN, email)) { 68 return false; 69 } 70 71 Perl5Util emailMatcher = new Perl5Util(); 73 if (!emailMatcher.match(EMAIL_PATTERN, email)) { 74 return false; 75 } 76 77 if (email.endsWith(".")) { 78 return false; 79 } 80 81 if (!isValidUser(emailMatcher.group(1))) { 82 return false; 83 } 84 85 if (!isValidDomain(emailMatcher.group(2))) { 86 return false; 87 } 88 89 return true; 90 } 91 92 96 protected boolean isValidDomain(String domain) { 97 boolean symbolic = false; 98 Perl5Util ipAddressMatcher = new Perl5Util(); 99 100 if (ipAddressMatcher.match(IP_DOMAIN_PATTERN, domain)) { 101 if (!isValidIpAddress(ipAddressMatcher)) { 102 return false; 103 } 104 } else { 105 Perl5Util domainMatcher = new Perl5Util(); 107 symbolic = domainMatcher.match(DOMAIN_PATTERN, domain); 108 } 109 110 if (symbolic) { 111 if (!isValidSymbolicDomain(domain)) { 112 return false; 113 } 114 } else { 115 return false; 116 } 117 118 return true; 119 } 120 121 125 protected boolean isValidUser(String user) { 126 Perl5Util userMatcher = new Perl5Util(); 127 return userMatcher.match(USER_PATTERN, user); 128 } 129 130 134 protected boolean isValidIpAddress(Perl5Util ipAddressMatcher) { 135 for (int i = 1; i <= 4; i++) { 136 String ipSegment = ipAddressMatcher.group(i); 137 if (ipSegment == null || ipSegment.length() <= 0) { 138 return false; 139 } 140 141 int iIpSegment = 0; 142 143 try { 144 iIpSegment = Integer.parseInt(ipSegment); 145 } catch(NumberFormatException e) { 146 return false; 147 } 148 149 if (iIpSegment > 255) { 150 return false; 151 } 152 153 } 154 return true; 155 } 156 157 161 protected boolean isValidSymbolicDomain(String domain) { 162 String [] domainSegment = new String [10]; 163 boolean match = true; 164 int i = 0; 165 Perl5Util atomMatcher = new Perl5Util(); 166 167 while (match) { 168 match = atomMatcher.match(ATOM_PATTERN, domain); 169 if (match) { 170 domainSegment[i] = atomMatcher.group(1); 171 int l = domainSegment[i].length() + 1; 172 domain = 173 (l >= domain.length()) 174 ? "" 175 : domain.substring(l); 176 177 i++; 178 } 179 } 180 181 int len = i; 182 if (domainSegment[len - 1].length() < 2 183 || domainSegment[len - 1].length() > 4) { 184 185 return false; 186 } 187 188 if (len < 2) { 190 return false; 191 } 192 193 return true; 194 } 195 196 } 197 | Popular Tags |