Rfc822Validatorpublic class Rfc822Validator extends Object implements AutoCompleteTextView.ValidatorThis class works as a Validator for AutoCompleteTextView for
email addresses. If a token does not appear to be a valid address,
it is trimmed of characters that cannot legitimately appear in one
and has the specified domain name added. It is meant for use with
{@link Rfc822Token} and {@link Rfc822Tokenizer}. |
Fields Summary |
---|
private static final Pattern | EMAIL_ADDRESS_PATTERN | private String | mDomain |
Constructors Summary |
---|
public Rfc822Validator(String domain)Constructs a new validator that uses the specified domain name as
the default when none is specified.
mDomain = domain;
|
Methods Summary |
---|
public java.lang.CharSequence | fixText(java.lang.CharSequence cs){@inheritDoc}
// Return an empty string if the email address only contains spaces, \n or \t
if (TextUtils.getTrimmedLength(cs) == 0) return "";
Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(cs);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < tokens.length; i++) {
String text = tokens[i].getAddress();
int index = text.indexOf('@");
if (index < 0) {
// If there is no @, just append the domain of the account
tokens[i].setAddress(removeIllegalCharacters(text) + "@" + mDomain);
} else {
// Otherwise, remove the illegal characters on both sides of the '@'
String fix = removeIllegalCharacters(text.substring(0, index));
String domain = removeIllegalCharacters(text.substring(index + 1));
tokens[i].setAddress(fix + "@" + (domain.length() != 0 ? domain : mDomain));
}
sb.append(tokens[i].toString());
if (i + 1 < tokens.length) {
sb.append(", ");
}
}
return sb;
| public boolean | isValid(java.lang.CharSequence text){@inheritDoc}
Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(text);
return tokens.length == 1 &&
EMAIL_ADDRESS_PATTERN.
matcher(tokens[0].getAddress()).matches();
| private java.lang.String | removeIllegalCharacters(java.lang.String s)
StringBuilder result = new StringBuilder();
int length = s.length();
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
/*
* An RFC822 atom can contain any ASCII printing character
* except for periods and any of the following punctuation.
* A local-part can contain multiple atoms, concatenated by
* periods, so do allow periods here.
*/
if (c <= ' " || c > '~") {
continue;
}
if (c == '(" || c == ')" || c == '<" || c == '>" ||
c == '@" || c == '," || c == ';" || c == ':" ||
c == '\\" || c == '"" || c == '[" || c == ']") {
continue;
}
result.append(c);
}
return result.toString();
|
|