001    /**
002     * Copyright (c) 2000-2010 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 void restoreState(FacesContext facesContext, Object obj) {
038            }
039    
040            public Object saveState(FacesContext facesContext) {
041                    return null;
042            }
043    
044            public void validate(
045                            FacesContext facesContext, UIComponent uiComponent, Object obj)
046                    throws ValidatorException {
047    
048                    ExternalContext externalContext = facesContext.getExternalContext();
049    
050                    Locale locale = externalContext.getRequestLocale();
051    
052                    if (obj instanceof String) {
053                            String emailAddress = (String)obj;
054    
055                            if (Validator.isNotNull(emailAddress)) {
056                                    if (!EmailValidator.getInstance().isValid(emailAddress)) {
057                                            String summary = LanguageUtil.get(
058                                                    locale, "please-enter-a-valid-email-address");
059    
060                                            FacesMessage facesMessage = new FacesMessage(
061                                                    FacesMessage.SEVERITY_ERROR, summary, null);
062    
063                                            throw new ValidatorException(facesMessage);
064                                    }
065                            }
066                    }
067                    else {
068                            String summary = LanguageUtil.format(
069                                    locale,
070                                    "validator-expected-type-string,-but-instead-received-type-x",
071                                    obj.getClass().getName());
072    
073                            FacesMessage facesMessage = new FacesMessage(
074                                    FacesMessage.SEVERITY_ERROR, summary, null);
075    
076                            throw new ValidatorException(facesMessage);
077                    }
078            }
079    
080            public boolean isTransient() {
081                    return _transient;
082            }
083    
084            public void setTransient(boolean value) {
085                    _transient = value;
086            }
087    
088            private boolean _transient;
089    
090    }