base.email.EmailAddressParse Maven / Gradle / Ivy
/**
* Creative commons Attribution-NonCommercial license.
*
* http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB
*
* NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
*/
package base.email;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailAddressParse {
/**
* Create a list of validated email addresses from a user entered, possibly badly formed string.
* getError()
*
* @param line
* @return List of validated email addresses.
* @see base.email.EmailAddressParse#getError()
*/
public EmailAddress[] parse(String line) {
error = null;
List parts = new ArrayList();
String part = "";
int mode = 0;
char escapeType = 0;
for(int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
switch(mode) {
case 0 :
if(c == ' ' || c == '\t' || c == '\n' || c == ',') {
if(part.length() > 0) {
parts.add(part);
}
part = "";
continue;
}
if(c == '<' || c == '"' || c == '\'') {
if(c == '<' && part.length() > 0) {
parts.add(part);
part = "";
}
escapeType = c;
if(c == '<') {
escapeType='>';
}
mode = 1;
continue;
}
part = part+c;
break;
case 1 :
if(c == escapeType) {
if(c == '>' && part.length() > 0) {
parts.add(part);
part = "";
}
mode = 0;
continue;
}
part = part+c;
break;
}
}
if(part.length() > 0) {
parts.add(part);
}
List addresses = new ArrayList();
String name = "";
for(String bit : parts) {
if(bit.contains("@")) {
if(name == "") {
addresses.add(new EmailAddress(bit));
} else {
addresses.add(new EmailAddress(name, bit));
}
name = "";
} else {
if(name == "") {
name = bit;
} else {
name = name + " " + bit;
}
}
}
if(name.length() > 0) {
error = "Name \"" + name + "\" appears without an associated email address.";
}
return addresses.toArray(new EmailAddress[0]);
}
/*
* Here are the pre-compiled regular expressions used to aide
* validation of email addresses.
*/
private static Pattern invalidNameCharacters = Pattern.compile(".*([\\\\\\/\\*\\&\\(\\)\\!\\#\\$\\%\\^\\~\\`\\{\\}\\;\\:\\\"\\,\\<\\>\\?\\[\\]\\=\\|]).*");
private static Pattern partCheck = Pattern.compile("([^@]+)@([^@]+)");
private static Pattern numberCheck = Pattern.compile(".*\\d.*");
private static Pattern invalidDomainNameCharacters = Pattern.compile(".*([\\\\\\/\\*\\&\\(\\)\\!\\#\\$\\%\\^\\~\\`\\{\\}\\;\\:\\\"\\,\\<\\>\\?\\[\\]\\'\\=\\|]).*");
/**
* Contains an error if a problem occurred with the most recent
* call to the email validating function isValid().
*/
private String error = null;
/**
* Describes any problem with parsing email addresses, or null null
* if no problem occurred. Invoking getError() resets the error
* back to null.
*
* @return String containing the error message
*/
public String getError() {
String e2 = error;
error = null;
return e2;
}
/**
* Test the validity of an email address, returning true if
* the email address is valid. You can follow this call with a call
* to getError() to discover the problem with the email address.
*
* @param email Email address to check
* @return True if valid, false if invalid
* @see base.email.EmailAddressParse#getError()
*/
public boolean isValid(String email) {
if(email == null) {
error = "Email address not provided.";
return false;
}
if(email.length() < 6) {
error="This is not long enough to be a valid e-mail address.";
return false;
}
Matcher m = partCheck.matcher(email);
if(!m.matches()) {
error = "Email address must contain a single @ in the middle";
return false;
}
String name = m.group(1);
String domain = m.group(2);
String[] domainParts = domain.split("\\.");
if(domainParts.length <= 1) {
error="Your email domain is incomplete.\n";
return false;
}
if(domainParts[domainParts.length-1].length() < 2) {
error = "Email domain appears to be invalid.";
return false;
}
if(domainParts[domainParts.length-1].length() > 4) {
error = "Email domain appears to be invalid.";
return false;
}
m = numberCheck.matcher(domainParts[domainParts.length-1]);
if(m.matches()) {
error = "Email domain appears to be invalid.";
return false;
}
m = invalidDomainNameCharacters.matcher(domain);
if(m.matches()) {
error = "The " + m.group(1) + " character may not be used in the domain name part of an email address.";
return false;
}
m = invalidNameCharacters.matcher(name);
if(m.matches()) {
error = "The "+m.group(1)+" character may not be used in the name part of an email address.";
return false;
}
if(email.indexOf("@") < 0) {
error="Email address must contain the @ symbol.";
return false;
}
if(email.indexOf(" ") >= 0) {
error = "Email address should not contain a space.";
return false;
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy