Author: ugo
Date: Wed Oct 13 09:10:36 2004
New Revision: 54744
Removed:
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/lib/commons-validator-1.1.3.jar
Modified:
cocoon/branches/BRANCH_2_1_X/gump.xml
cocoon/branches/BRANCH_2_1_X/lib/jars.xml
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/validationruleimpl/EmailValidationRule.java
Log:
Removing Commons Validator and keeping just the necessary code
Modified: cocoon/branches/BRANCH_2_1_X/gump.xml
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/gump.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/gump.xml Wed Oct 13 09:10:36 2004
@@ -767,7 +767,6 @@
<depend project="cocoon" inherit="all"/>
<depend project="xreporter-expression"/>
<depend project="jakarta-oro"/>
- <depend project="commons-validator"/>
<depend project="cocoon-block-xsp" type="samples"/>
<work nested="build/cocoon-@@DATE@@/blocks/forms/test"/>
Modified: cocoon/branches/BRANCH_2_1_X/lib/jars.xml
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/lib/jars.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/lib/jars.xml Wed Oct 13 09:10:36 2004
@@ -1182,12 +1182,4 @@
<homepage>http://jakarta.apache.org/tomcat/</homepage>
</file>
- <file>
- <title>Jakarta Commons Validator</title>
- <description>Validation rules for user input.</description>
- <used-by>Cocoon Forms</used-by>
- <lib>forms/lib/commons-validator-1.1.3.jar</lib>
- <homepage>http://jakarta.apache.org/commons/validator/</homepage>
- </file>
-
</jars>
Modified:
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/validationruleimpl/EmailValidationRule.java
==============================================================================
---
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/validationruleimpl/EmailValidationRule.java
(original)
+++
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/validationruleimpl/EmailValidationRule.java
Wed Oct 13 09:10:36 2004
@@ -18,16 +18,33 @@
import org.apache.cocoon.forms.Constants;
import org.apache.cocoon.forms.util.I18nMessage;
import org.apache.cocoon.forms.validation.ValidationError;
-import org.apache.commons.validator.EmailValidator;
+import org.apache.oro.text.perl.Perl5Util;
import org.outerj.expression.ExpressionContext;
/**
- * ValidationRule that checks that a string is an email address.
+ * <p>ValidationRule that checks that a string is an email address.</p>
+ * <p>Based on code copied from
org.apache.commons.validator.EmailValidator.</p>
*
* @version $Id$
*/
public class EmailValidationRule extends AbstractValidationRule {
+ private static final String SPECIAL_CHARS =
"\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
+ private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]";
+ private static final String QUOTED_USER = "(\"[^\"]*\")";
+ private static final String ATOM = VALID_CHARS + '+';
+ private static final String WORD = "(" + ATOM + "|" + QUOTED_USER + ")";
+
+ // Each pattern must be surrounded by /
+ private static final String LEGAL_ASCII_PATTERN = "/^[\\000-\\177]+$/";
+ private static final String EMAIL_PATTERN = "/^(.+)@(.+)$/";
+ private static final String IP_DOMAIN_PATTERN =
+ "/^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$/";
+
+ private static final String USER_PATTERN = "/^" + WORD + "(\\." + WORD +
")*$/";
+ private static final String DOMAIN_PATTERN = "/^" + ATOM + "(\\." + ATOM +
")*$/";
+ private static final String ATOM_PATTERN = "/(" + ATOM + ")/";
+
public ValidationError validate(Object value, ExpressionContext
expressionContext) {
String email = (String)value;
@@ -42,7 +59,138 @@
}
boolean isEmail(String email) {
- EmailValidator ev = EmailValidator.getInstance();
- return ev.isValid(email);
+ if (email == null) {
+ return false;
+ }
+
+ Perl5Util matchAsciiPat = new Perl5Util();
+ if (!matchAsciiPat.match(LEGAL_ASCII_PATTERN, email)) {
+ return false;
+ }
+
+ // Check the whole email address structure
+ Perl5Util emailMatcher = new Perl5Util();
+ if (!emailMatcher.match(EMAIL_PATTERN, email)) {
+ return false;
+ }
+
+ if (email.endsWith(".")) {
+ return false;
+ }
+
+ if (!isValidUser(emailMatcher.group(1))) {
+ return false;
+ }
+
+ if (!isValidDomain(emailMatcher.group(2))) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Returns true if the domain component of an email address is valid.
+ * @param domain being validatied.
+ */
+ protected boolean isValidDomain(String domain) {
+ boolean symbolic = false;
+ Perl5Util ipAddressMatcher = new Perl5Util();
+
+ if (ipAddressMatcher.match(IP_DOMAIN_PATTERN, domain)) {
+ if (!isValidIpAddress(ipAddressMatcher)) {
+ return false;
+ }
+ } else {
+ // Domain is symbolic name
+ Perl5Util domainMatcher = new Perl5Util();
+ symbolic = domainMatcher.match(DOMAIN_PATTERN, domain);
+ }
+
+ if (symbolic) {
+ if (!isValidSymbolicDomain(domain)) {
+ return false;
+ }
+ } else {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Returns true if the user component of an email address is valid.
+ * @param user being validated
+ */
+ protected boolean isValidUser(String user) {
+ Perl5Util userMatcher = new Perl5Util();
+ return userMatcher.match(USER_PATTERN, user);
}
+
+ /**
+ * Validates an IP address. Returns true if valid.
+ * @param ipAddressMatcher Pattren matcher
+ */
+ protected boolean isValidIpAddress(Perl5Util ipAddressMatcher) {
+ for (int i = 1; i <= 4; i++) {
+ String ipSegment = ipAddressMatcher.group(i);
+ if (ipSegment == null || ipSegment.length() <= 0) {
+ return false;
+ }
+
+ int iIpSegment = 0;
+
+ try {
+ iIpSegment = Integer.parseInt(ipSegment);
+ } catch(NumberFormatException e) {
+ return false;
+ }
+
+ if (iIpSegment > 255) {
+ return false;
+ }
+
+ }
+ return true;
+ }
+
+ /**
+ * Validates a symbolic domain name. Returns true if it's valid.
+ * @param domain symbolic domain name
+ */
+ protected boolean isValidSymbolicDomain(String domain) {
+ String[] domainSegment = new String[10];
+ boolean match = true;
+ int i = 0;
+ Perl5Util atomMatcher = new Perl5Util();
+
+ while (match) {
+ match = atomMatcher.match(ATOM_PATTERN, domain);
+ if (match) {
+ domainSegment[i] = atomMatcher.group(1);
+ int l = domainSegment[i].length() + 1;
+ domain =
+ (l >= domain.length())
+ ? ""
+ : domain.substring(l);
+
+ i++;
+ }
+ }
+
+ int len = i;
+ if (domainSegment[len - 1].length() < 2
+ || domainSegment[len - 1].length() > 4) {
+
+ return false;
+ }
+
+ // Make sure there's a host name preceding the domain.
+ if (len < 2) {
+ return false;
+ }
+
+ return true;
+ }
+
}