/**
 * Created on 2008-6-19 01:00:18
 */
package cn.net.openid.jos.web.validation;

import java.util.regex.Pattern;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import cn.net.openid.jos.domain.Email;
import cn.net.openid.jos.web.MessageCodes;

/**
 * @author Sutra Zhou
 */
public class EmailValidator implements Validator {
	private Pattern emailAddressPattern;

	public void setEmailAddressPattern(String emailAddressPattern) {
		this.emailAddressPattern = Pattern.compile(emailAddressPattern);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@SuppressWarnings("unchecked")
	public boolean supports(Class clazz) {
		return Email.class.isAssignableFrom(clazz);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.validation.Validator#validate(java.lang.Object,
	 * org.springframework.validation.Errors)
	 */
	public void validate(Object target, Errors errors) {
		Email email = (Email) target;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address",
				"required", "Field is required.");
		if (!errors.hasFieldErrors("address")
				&& !emailAddressPattern.matcher(email.getAddress()).matches()) {
			errors.rejectValue("address", MessageCodes.Email.Error.ADDRESS,
					new String[] { email.getAddress() },
					"\"{0}\" is not a valid e-mail address.");
		}
	}
}

@@ -106,24 +106,109 @@
 x.Pattern;%0A%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import org.s
@@ -241,16 +241,16 @@
 Errors;%0A
-
 import o
@@ -510,16 +510,89 @@
 dator %7B%0A
+%09private static final Log log = LogFactory.getLog(EmailValidator.class);%0A
 %09private
@@ -752,18 +752,172 @@
 sPattern
-);
+.trim());%0A%09%09if (log.isDebugEnabled()) %7B%0A%09%09%09log.debug(%22setEmailAddressPattern, emailAddressPattern.pattern: %22%0A%09%09%09%09%09+ this.emailAddressPattern.pattern());%0A%09%09%7D
 %0A%09%7D%0A%0A%09/*

Trim email address pattern.