001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.taglib.faces.validator;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.util.Locale;
021    
022    import javax.faces.application.FacesMessage;
023    import javax.faces.component.StateHolder;
024    import javax.faces.component.UIComponent;
025    import javax.faces.context.ExternalContext;
026    import javax.faces.context.FacesContext;
027    import javax.faces.validator.ValidatorException;
028    
029    import org.apache.commons.validator.EmailValidator;
030    
031    /**
032     * @author Neil Griffin
033     */
034    public class EmailAddressValidator
035            implements StateHolder, javax.faces.validator.Validator {
036    
037            public boolean isTransient() {
038                    return _transient;
039            }
040    
041            public void restoreState(FacesContext facesContext, Object obj) {
042            }
043    
044            public Object saveState(FacesContext facesContext) {
045                    return null;
046            }
047    
048            public void setTransient(boolean value) {
049                    _transient = value;
050            }
051    
052            public void validate(
053                            FacesContext facesContext, UIComponent uiComponent, Object obj)
054                    throws ValidatorException {
055    
056                    ExternalContext externalContext = facesContext.getExternalContext();
057    
058                    Locale locale = externalContext.getRequestLocale();
059    
060                    if (obj instanceof String) {
061                            String emailAddress = (String)obj;
062    
063                            if (Validator.isNotNull(emailAddress)) {
064                                    if (!EmailValidator.getInstance().isValid(emailAddress)) {
065                                            String summary = LanguageUtil.get(
066                                                    locale, "please-enter-a-valid-email-address");
067    
068                                            FacesMessage facesMessage = new FacesMessage(
069                                                    FacesMessage.SEVERITY_ERROR, summary, null);
070    
071                                            throw new ValidatorException(facesMessage);
072                                    }
073                            }
074                    }
075                    else {
076                            String summary = LanguageUtil.format(
077                                    locale,
078                                    "validator-expected-type-string,-but-instead-received-type-x",
079                                    obj.getClass().getName());
080    
081                            FacesMessage facesMessage = new FacesMessage(
082                                    FacesMessage.SEVERITY_ERROR, summary, null);
083    
084                            throw new ValidatorException(facesMessage);
085                    }
086            }
087    
088            private boolean _transient;
089    
090    }