You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dg...@apache.org on 2003/04/29 04:15:35 UTC
cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator EmailValidator.java ValidatorUtil.java GenericValidator.java
dgraham 2003/04/28 19:15:35
Modified: validator/src/share/org/apache/commons/validator
ValidatorUtil.java GenericValidator.java
Added: validator/src/share/org/apache/commons/validator
EmailValidator.java
Log:
Factored email validation into new EmailValidator class.
Revision Changes Path
1.9 +64 -51 jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorUtil.java
Index: ValidatorUtil.java
===================================================================
RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorUtil.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ValidatorUtil.java 29 Apr 2003 00:53:43 -0000 1.8
+++ ValidatorUtil.java 29 Apr 2003 02:15:35 -0000 1.9
@@ -59,7 +59,6 @@
*
*/
-
package org.apache.commons.validator;
import java.lang.reflect.InvocationTargetException;
@@ -70,7 +69,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-
/**
* <p>Basic utility methods.</p>
*
@@ -78,21 +76,29 @@
* @author David Graham
* @version $Revision$ $Date$
*/
-public class ValidatorUtil {
+public class ValidatorUtil {
- /**
- * Logger.
- */
- protected static Log log = LogFactory.getLog(ValidatorUtil.class);
-
- /**
- * <p>Replace part of a <code>String</code> with another value.</p>
- *
- * @param value <code>String</code> to perform the replacement on.
- * @param key The name of the constant.
- * @param replaceValue The value of the constant.
- */
- public static String replace(String value, String key, String replaceValue) {
+ /**
+ * Delimiter to put around a regular expression following Perl 5 syntax.
+ */
+ public final static String REGEXP_DELIMITER = "/";
+
+ /**
+ * Logger.
+ */
+ protected static Log log = LogFactory.getLog(ValidatorUtil.class);
+
+ /**
+ * <p>Replace part of a <code>String</code> with another value.</p>
+ *
+ * @param value <code>String</code> to perform the replacement on.
+ * @param key The name of the constant.
+ * @param replaceValue The value of the constant.
+ */
+ public static String replace(
+ String value,
+ String key,
+ String replaceValue) {
if (value == null || key == null || replaceValue == null) {
return value;
}
@@ -102,7 +108,7 @@
if (pos < 0) {
return value;
}
-
+
int length = value.length();
int start = pos;
int end = pos + key.length();
@@ -119,41 +125,41 @@
}
return value;
- }
+ }
- /**
- * Convenience method for getting a value from a bean property as a
- * <code>String</code>.
- */
- public static String getValueAsString(Object bean, String property) {
- Object value = null;
+ /**
+ * Convenience method for getting a value from a bean property as a
+ * <code>String</code>.
+ */
+ public static String getValueAsString(Object bean, String property) {
+ Object value = null;
- try {
+ try {
value = PropertyUtils.getProperty(bean, property);
-
+
} catch (IllegalAccessException e) {
- log.error(e.getMessage(), e);
+ log.error(e.getMessage(), e);
} catch (InvocationTargetException e) {
- log.error(e.getMessage(), e);
+ log.error(e.getMessage(), e);
} catch (NoSuchMethodException e) {
- log.error(e.getMessage(), e);
- }
-
- return (value != null ? value.toString() : null);
- }
-
- /**
- * Makes a deep copy of a <code>FastHashMap</code> if the values
- * are <code>String</code>, <code>Msg</code>, <code>Arg</code>,
- * or <code>Var</code>. Otherwise it is a shallow copy.
- *
- * @param map <code>FastHashMap</code> to copy.
- * @return FastHashMap A copy of the <code>FastHashMap</code> that was
- * passed in.
- */
- public static FastHashMap copyFastHashMap(FastHashMap map) {
- FastHashMap results = new FastHashMap();
-
+ log.error(e.getMessage(), e);
+ }
+
+ return (value != null ? value.toString() : null);
+ }
+
+ /**
+ * Makes a deep copy of a <code>FastHashMap</code> if the values
+ * are <code>String</code>, <code>Msg</code>, <code>Arg</code>,
+ * or <code>Var</code>. Otherwise it is a shallow copy.
+ *
+ * @param map <code>FastHashMap</code> to copy.
+ * @return FastHashMap A copy of the <code>FastHashMap</code> that was
+ * passed in.
+ */
+ public static FastHashMap copyFastHashMap(FastHashMap map) {
+ FastHashMap results = new FastHashMap();
+
Iterator i = map.keySet().iterator();
while (i.hasNext()) {
String key = (String) i.next();
@@ -178,5 +184,12 @@
return results;
}
-
+
+ /**
+ * Adds a '/' on either side of the regular expression.
+ */
+ public static String getDelimitedRegExp(String regexp) {
+ return (REGEXP_DELIMITER + regexp + REGEXP_DELIMITER);
+ }
+
}
1.15 +17 -136 jakarta-commons/validator/src/share/org/apache/commons/validator/GenericValidator.java
Index: GenericValidator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericValidator.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- GenericValidator.java 29 Apr 2003 01:45:43 -0000 1.14
+++ GenericValidator.java 29 Apr 2003 02:15:35 -0000 1.15
@@ -69,8 +69,7 @@
import org.apache.oro.text.perl.Perl5Util;
/**
- * <p>This class contains basic methods for
- * performing validations.</p>
+ * <p>This class contains basic methods for performing validations.</p>
*
* @author David Winterfeldt
* @author James Turner
@@ -81,10 +80,10 @@
public class GenericValidator implements Serializable {
/**
- * Delimiter to put around a regular expression
- * following Perl 5 syntax.
+ * Delimiter to put around a regular expression following Perl 5 syntax.
+ * @deprecated Use ValidatorUtil.REGEXP_DELIMITER instead.
*/
- public final static String REGEXP_DELIM = "/";
+ public final static String REGEXP_DELIM = ValidatorUtil.REGEXP_DELIMITER;
/**
* <p>Checks if the field isn't null and length of the field is greater than zero not
@@ -322,138 +321,19 @@
/**
* <p>Checks if a field has a valid e-mail address.</p>
- * <p>Based on a script by Sandeep V. Tamhankar (stamhankar@hotmail.com),
- * http://javascript.internet.com</p>
*
- * @param value The value validation is being performed on.
+ * @param value The value validation is being performed on.
*/
- public static boolean isEmail(String value) {
- boolean bValid = true;
-
- try {
- String specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
- String validChars = "[^\\s" + specialChars + "]";
- String quotedUser = "(\"[^\"]*\")";
- String atom = validChars + '+';
- String word = "(" + atom + "|" + quotedUser + ")";
-
- // Each pattern must be surrounded by /
- String legalAsciiPat = getDelimittedRegexp("^[\\000-\\177]+$");
- String emailPat = getDelimittedRegexp("^(.+)@(.+)$");
- String ipDomainPat =
- getDelimittedRegexp("^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$");
- String userPat = getDelimittedRegexp("^" + word + "(\\." + word + ")*$");
- String domainPat =
- getDelimittedRegexp("^" + atom + "(\\." + atom + ")*$");
- String atomPat = getDelimittedRegexp("(" + atom + ")");
-
- Perl5Util matchEmailPat = new Perl5Util();
- Perl5Util matchUserPat = new Perl5Util();
- Perl5Util matchIPPat = new Perl5Util();
- Perl5Util matchDomainPat = new Perl5Util();
- Perl5Util matchAtomPat = new Perl5Util();
- Perl5Util matchAsciiPat = new Perl5Util();
-
- boolean ipAddress = false;
- boolean symbolic = false;
-
- if (!matchAsciiPat.match(legalAsciiPat, value)) {
- return false;
- }
-
- // Check the whole email address structure
- bValid = matchEmailPat.match(emailPat, value);
-
- if (value.endsWith(".")) {
- bValid = false;
- }
-
- // Check the user component of the email address
- if (bValid) {
-
- String user = matchEmailPat.group(1);
-
- // See if "user" is valid
- bValid = matchUserPat.match(userPat, user);
- }
-
- // Check the domain component of the email address
- if (bValid) {
- String domain = matchEmailPat.group(2);
-
- // check if domain is IP address or symbolic
- ipAddress = matchIPPat.match(ipDomainPat, domain);
-
- if (ipAddress) {
- // this is an IP address so check components
- for (int i = 1; i <= 4; i++) {
- String ipSegment = matchIPPat.group(i);
- if (ipSegment != null && ipSegment.length() > 0) {
- int iIpSegment = 0;
- try {
- iIpSegment = Integer.parseInt(ipSegment);
- } catch (Exception e) {
- bValid = false;
- }
-
- if (iIpSegment > 255) {
- bValid = false;
- }
- } else {
- bValid = false;
- }
- }
- } else {
- // Domain is symbolic name
- symbolic = matchDomainPat.match(domainPat, domain);
- }
-
- if (symbolic) {
- // this is a symbolic domain so check components
- String[] domainSegment = new String[10];
- boolean match = true;
- int i = 0;
- int l = 0;
-
- while (match) {
- match = matchAtomPat.match(atomPat, domain);
- if (match) {
- domainSegment[i] = matchAtomPat.group(1);
- 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) {
- bValid = false;
- }
-
- // Make sure there's a host name preceding the domain.
- if (len < 2) {
- bValid = false;
- }
-
- } else {
- bValid = false;
- }
- }
- } catch (Exception e) {
- bValid = false;
- }
-
- return bValid;
- }
+ public static boolean isEmail(String value) {
+ return EmailValidator.getInstance().isEmail(value);
+ }
/**
* <p>Checks if the value's length is less than or equal to the max.</p>
*
* @param value The value validation is being performed on.
* @param max The maximum length.
- */
+ */
public static boolean maxLength(String value, int max) {
return (value.length() <= max);
}
@@ -463,15 +343,16 @@
*
* @param value The value validation is being performed on.
* @param min The minimum length.
- */
+ */
public static boolean minLength(String value, int min) {
return (value.length() >= min);
}
/**
* Adds a '/' on either side of the regular expression.
- */
+ * @deprecated use ValidatorUtil.getDelimitedRegExp() instead.
+ */
protected static String getDelimittedRegexp(String regexp) {
- return (REGEXP_DELIM + regexp + REGEXP_DELIM);
+ return ValidatorUtil.getDelimitedRegExp(regexp);
}
}
1.1 jakarta-commons/validator/src/share/org/apache/commons/validator/EmailValidator.java
Index: EmailValidator.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/EmailValidator.java,v 1.1 2003/04/29 02:15:35 dgraham Exp $
* $Revision: 1.1 $
* $Date: 2003/04/29 02:15:35 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.validator;
import org.apache.oro.text.perl.Perl5Util;
/**
* <p>Perform email validations.</p>
* <p>
* This class is a Singleton; you can retrieve the instance via the getInstance() method.
* </p>
* <p>
* Based on a script by <a href="mailto:stamhankar@hotmail.com">Sandeep V. Tamhankar</a>
* http://javascript.internet.com
* </p>
*
* @author David Winterfeldt
* @author James Turner
* @author <a href="mailto:husted@apache.org">Ted Husted</a>
* @author David Graham
* @version $Revision: 1.1 $ $Date: 2003/04/29 02:15:35 $
*/
public class EmailValidator {
/**
* Singleton instance of this class.
*/
private static final EmailValidator instance =
new EmailValidator();
/**
* Returns the Singleton instance of this validator.
*/
public static EmailValidator getInstance() {
return instance;
}
/**
* Protected constructor for subclasses to use.
*/
protected EmailValidator() {
super();
}
/**
* <p>Checks if a field has a valid e-mail address.</p>
*
* @param value The value validation is being performed on.
*/
public boolean isEmail(String value) {
boolean isValid = true;
try {
String specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
String validChars = "[^\\s" + specialChars + "]";
String quotedUser = "(\"[^\"]*\")";
String atom = validChars + '+';
String word = "(" + atom + "|" + quotedUser + ")";
// Each pattern must be surrounded by /
String legalAsciiPat =
ValidatorUtil.getDelimitedRegExp("^[\\000-\\177]+$");
String emailPat = ValidatorUtil.getDelimitedRegExp("^(.+)@(.+)$");
String ipDomainPat =
ValidatorUtil.getDelimitedRegExp(
"^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$");
String userPat =
ValidatorUtil.getDelimitedRegExp(
"^" + word + "(\\." + word + ")*$");
String domainPat =
ValidatorUtil.getDelimitedRegExp(
"^" + atom + "(\\." + atom + ")*$");
String atomPat = ValidatorUtil.getDelimitedRegExp("(" + atom + ")");
Perl5Util matchEmailPat = new Perl5Util();
Perl5Util matchUserPat = new Perl5Util();
Perl5Util matchIPPat = new Perl5Util();
Perl5Util matchDomainPat = new Perl5Util();
Perl5Util matchAtomPat = new Perl5Util();
Perl5Util matchAsciiPat = new Perl5Util();
boolean ipAddress = false;
boolean symbolic = false;
if (!matchAsciiPat.match(legalAsciiPat, value)) {
return false;
}
// Check the whole email address structure
isValid = matchEmailPat.match(emailPat, value);
if (value.endsWith(".")) {
isValid = false;
}
// Check the user component of the email address
if (isValid) {
String user = matchEmailPat.group(1);
// See if "user" is valid
isValid = matchUserPat.match(userPat, user);
}
// Check the domain component of the email address
if (isValid) {
String domain = matchEmailPat.group(2);
// check if domain is IP address or symbolic
ipAddress = matchIPPat.match(ipDomainPat, domain);
if (ipAddress) {
// this is an IP address so check components
for (int i = 1; i <= 4; i++) {
String ipSegment = matchIPPat.group(i);
if (ipSegment != null && ipSegment.length() > 0) {
int iIpSegment = 0;
try {
iIpSegment = Integer.parseInt(ipSegment);
} catch (Exception e) {
isValid = false;
}
if (iIpSegment > 255) {
isValid = false;
}
} else {
isValid = false;
}
}
} else {
// Domain is symbolic name
symbolic = matchDomainPat.match(domainPat, domain);
}
if (symbolic) {
// this is a symbolic domain so check components
String[] domainSegment = new String[10];
boolean match = true;
int i = 0;
int l = 0;
while (match) {
match = matchAtomPat.match(atomPat, domain);
if (match) {
domainSegment[i] = matchAtomPat.group(1);
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) {
isValid = false;
}
// Make sure there's a host name preceding the domain.
if (len < 2) {
isValid = false;
}
} else {
isValid = false;
}
}
} catch (Exception e) {
isValid = false;
}
return isValid;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org