001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }