
base.email.EmailAddressParse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of base Show documentation
Show all versions of base Show documentation
A collection of basic java utility classes that provide basic features for a standalone/simple JEE application. Backed by a Cassandra, MySQL, or SQLite database, it provides, web page templates, user and group management, and a searchable online audit log of all user activity.
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
package base.email;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Parse a text string into a set of EmailAddress objects. If not possible,
* the getError()
method can be used to determine the cause.
*/
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