View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    *
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    *
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.constraint;
14  
15  import java.util.regex.Pattern;
16  
17  import net.sf.oval.ConstraintTarget;
18  import net.sf.oval.Validator;
19  import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
20  import net.sf.oval.context.OValContext;
21  import net.sf.oval.exception.OValException;
22  
23  /**
24   * @author Sebastian Thomschke
25   */
26  public class EmailCheck extends AbstractAnnotationCheck<Email>
27  {
28  	private static final long serialVersionUID = 1L;
29  
30  	private static final String SPECIAL_CHARACTERS = "'\\(\\)\\-\\.`";
31  	private static final String ASCII = "\\w " + SPECIAL_CHARACTERS;
32  	private static final String ASCII_WITHOUT_COMMA = "[" + ASCII + "]+";
33  	private static final String ASCII_WITH_COMMA = "\"[" + ASCII + ",]+\"";
34  	private static final String ASCII_WITH_QUESTION_MARK_AND_EQUALS = "[" + ASCII + "\\?\\=]+";
35  	private static final String MIME_ENCODED = "\\=\\?" + ASCII_WITH_QUESTION_MARK_AND_EQUALS + "\\?\\=";
36  	private static final String NAME = "(" + ASCII_WITHOUT_COMMA + "|" + ASCII_WITH_COMMA + "|" + MIME_ENCODED + ")";
37  
38  	private static final String EMAIL_BASE_PATTERN = "['_A-Za-z0-9-&+]+(\\.['_A-Za-z0-9-&+]+)*[.]{0,1}@([A-Za-z0-9-])+(\\.[A-Za-z0-9-]+)*((\\.[A-Za-z0-9]{2,})|(\\.[A-Za-z0-9]{2,}\\.[A-Za-z0-9]{2,}))";
39  
40  	private static final Pattern EMAIL_PATTERN = Pattern.compile("^" + EMAIL_BASE_PATTERN + "$");
41  
42  	private static final Pattern EMAIL_WITH_PERSONAL_NAME_PATTERN = Pattern.compile("^(" + EMAIL_BASE_PATTERN + "|" + NAME + " +<"
43  			+ EMAIL_BASE_PATTERN + ">)$");
44  
45  	private boolean allowPersonalName;
46  
47  	/**
48  	 * {@inheritDoc}
49  	 */
50  	@Override
51  	public void configure(final Email constraintAnnotation)
52  	{
53  		super.configure(constraintAnnotation);
54  		setAllowPersonalName(constraintAnnotation.allowPersonalName());
55  	}
56  
57  	/**
58  	 * {@inheritDoc}
59  	 */
60  	@Override
61  	protected ConstraintTarget[] getAppliesToDefault()
62  	{
63  		return new ConstraintTarget[]{ConstraintTarget.VALUES};
64  	}
65  
66  	/**
67  	 * @return the allowPersonalName
68  	 */
69  	public boolean isAllowPersonalName()
70  	{
71  		return allowPersonalName;
72  	}
73  
74  	/**
75  	 * {@inheritDoc}
76  	 */
77  	public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
78  			final Validator validator) throws OValException
79  	{
80  		if (valueToValidate == null) return true;
81  
82  		if (allowPersonalName) return EMAIL_WITH_PERSONAL_NAME_PATTERN.matcher(valueToValidate.toString()).matches();
83  		return EMAIL_PATTERN.matcher(valueToValidate.toString()).matches();
84  	}
85  
86  	/**
87  	 * @param allowPersonalName the allowPersonalName to set
88  	 */
89  	public void setAllowPersonalName(final boolean allowPersonalName)
90  	{
91  		this.allowPersonalName = allowPersonalName;
92  	}
93  }