KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > validationruleimpl > EmailValidationRule


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
25  * <p>ValidationRule that checks that a string is an email address.</p>
26  * <p>Based on code copied from org.apache.commons.validator.EmailValidator.</p>
27  *
28  * @version $Id: EmailValidationRule.java 326838 2005-10-20 06:26:53Z sylvain $
29  */

30 public class EmailValidationRule extends AbstractValidationRule {
31
32     private static final String JavaDoc SPECIAL_CHARS = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
33     private static final String JavaDoc VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]";
34     private static final String JavaDoc QUOTED_USER = "(\"[^\"]*\")";
35     private static final String JavaDoc ATOM = VALID_CHARS + '+';
36     private static final String JavaDoc WORD = "(" + ATOM + "|" + QUOTED_USER + ")";
37
38     // Each pattern must be surrounded by /
39
private static final String JavaDoc LEGAL_ASCII_PATTERN = "/^[\\000-\\177]+$/";
40     private static final String JavaDoc EMAIL_PATTERN = "/^(.+)@(.+)$/";
41     private static final String JavaDoc IP_DOMAIN_PATTERN =
42             "/^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$/";
43
44     private static final String JavaDoc USER_PATTERN = "/^" + WORD + "(\\." + WORD + ")*$/";
45     private static final String JavaDoc DOMAIN_PATTERN = "/^" + ATOM + "(\\." + ATOM + ")*$/";
46     private static final String JavaDoc ATOM_PATTERN = "/(" + ATOM + ")/";
47
48     public ValidationError validate(Object JavaDoc value, ExpressionContext expressionContext) {
49         String JavaDoc email = (String JavaDoc)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 JavaDoc clazz, boolean arrayType) {
58         return clazz.isAssignableFrom(String JavaDoc.class) && !arrayType;
59     }
60
61     boolean isEmail(String JavaDoc 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         // Check the whole email address structure
72
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     /**
93      * Returns true if the domain component of an email address is valid.
94      * @param domain being validatied.
95      */

96     protected boolean isValidDomain(String JavaDoc 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             // Domain is symbolic name
106
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     /**
122      * Returns true if the user component of an email address is valid.
123      * @param user being validated
124      */

125     protected boolean isValidUser(String JavaDoc user) {
126         Perl5Util userMatcher = new Perl5Util();
127         return userMatcher.match(USER_PATTERN, user);
128     }
129
130     /**
131      * Validates an IP address. Returns true if valid.
132      * @param ipAddressMatcher Pattren matcher
133      */

134     protected boolean isValidIpAddress(Perl5Util ipAddressMatcher) {
135         for (int i = 1; i <= 4; i++) {
136             String JavaDoc 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 JavaDoc e) {
146                 return false;
147             }
148
149             if (iIpSegment > 255) {
150                 return false;
151             }
152
153         }
154         return true;
155     }
156
157     /**
158      * Validates a symbolic domain name. Returns true if it's valid.
159      * @param domain symbolic domain name
160      */

161     protected boolean isValidSymbolicDomain(String JavaDoc domain) {
162         String JavaDoc[] domainSegment = new String JavaDoc[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         // Make sure there's a host name preceding the domain.
189
if (len < 2) {
190             return false;
191         }
192
193         return true;
194     }
195
196 }
197
Popular Tags