Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.temp.shared.validators;

import com.temp.shared.ValueValidator;
import org.litesoft.core.util.externalization.E13nData;
import com.temp.shared.utils.StringUtils;

/**
 * Email Address Validator, that requires at a minimum that an email address
 * consists of at least: an addressee, an at sign ('@'), and a domain.
 *
 * Specifically, no spaces or control characters are allowed, addresses can not
 * be empty, and the domain must consist of at least two levels separated by
 * periods ('.').
 *
 * The resulting format for the email is:<br />
 * <ul>
 * (addressee)@[((domain).)*](secondary domain).(primary domain)
 * </ul>
 *
 * @author georgs
 */
public class EmailValidator implements ValueValidator<String> {

    public enum ErrorTemplateIdCode {
        EmailEmpty, //
        //
        EmailNoAtSign, //
        EmailNoSpacesAllowed, //
        EmailNoCtrlCharsAllowed, //
        //
        EmailNoSecondaryDomain, //
        EmailInvalidDomainFormat, //
        //
        EmailNoAddressee, //
        EmailInvalidAddresseeFormat
    }

    public static final EmailValidator INSTANCE = new EmailValidator();

    protected EmailValidator() {
    }

    public boolean isValid(String email) {
        return (null == checkValue(email));
    }

    @Override
    public E13nData checkValue(String email) {
        email = StringUtils.noEmpty(email);
        if (email == null) {
            return new E13nData(ErrorTemplateIdCode.EmailEmpty);
        }
        int at = email.indexOf(' ');
        if (at != -1) {
            return new E13nData(ErrorTemplateIdCode.EmailNoSpacesAllowed);
        }
        at = StringUtils.indexOfControlCharacter(email);
        if (at != -1) {
            return new E13nData(ErrorTemplateIdCode.EmailNoCtrlCharsAllowed);
        }
        at = email.indexOf('@');
        if (at == -1) {
            return new E13nData(ErrorTemplateIdCode.EmailNoAtSign);
        }
        String addressee = email.substring(0, at);
        String domain = email.substring(at + 1);
        if (domain.indexOf('.') == -1) {
            return new E13nData(ErrorTemplateIdCode.EmailNoSecondaryDomain);
        }
        if (!domainOK(domain)) {
            return new E13nData(ErrorTemplateIdCode.EmailInvalidDomainFormat);
        }
        if (addressee.length() == 0) {
            return new E13nData(ErrorTemplateIdCode.EmailNoAddressee);
        }
        return addresseeOK(addressee) ? null : new E13nData(ErrorTemplateIdCode.EmailInvalidAddresseeFormat);
    }

    protected boolean domainOK(String domain) {
        int from = 0;
        for (int at; -1 != (at = domain.indexOf('.', from)); from = at + 1) {
            if (!subDomainOK(domain.substring(from, at))) {
                return false;
            }
        }
        return subDomainOK(domain.substring(from));
    }

    /**
     * Check that the Sub-Domain is OK.
     *
     * TODO: this should be evolved to have the "real" rules!
     *
     * @param subDomain
     *            !null
     */
    protected boolean subDomainOK(String subDomain) {
        return (subDomain.length() != 0);
    }

    /**
     * Check that the Addressee is OK.
     *
     * TODO: this should be evolved to have the "real" rules!
     *
     * @param addressee
     *            !null
     */
    protected boolean addresseeOK(String addressee) {
        return true;
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/shared/validators/EmailValidator.java

Diff revisions: vs.
Revision Author Commited Message
724 Diff Diff GeorgeS picture GeorgeS Mon 11 Jun, 2012 00:47:26 +0000
626 GeorgeS picture GeorgeS Wed 11 Apr, 2012 19:39:41 +0000