001    /**
002     * Copyright (c) 2000-2010 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 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    }