1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.faces.validator;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.util.Validator;
19  
20  import java.util.Locale;
21  
22  import javax.faces.application.FacesMessage;
23  import javax.faces.component.StateHolder;
24  import javax.faces.component.UIComponent;
25  import javax.faces.context.ExternalContext;
26  import javax.faces.context.FacesContext;
27  import javax.faces.validator.ValidatorException;
28  
29  import org.apache.commons.validator.EmailValidator;
30  
31  /**
32   * <a href="EmailAddressValidator.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Neil Griffin
35   */
36  public class EmailAddressValidator
37      implements StateHolder, javax.faces.validator.Validator {
38  
39      public void restoreState(FacesContext facesContext, Object obj) {
40      }
41  
42      public Object saveState(FacesContext facesContext) {
43          return null;
44      }
45  
46      public void validate(
47              FacesContext facesContext, UIComponent uiComponent, Object obj)
48          throws ValidatorException {
49  
50          ExternalContext externalContext = facesContext.getExternalContext();
51  
52          Locale locale = externalContext.getRequestLocale();
53  
54          if (obj instanceof String) {
55              String emailAddress = (String)obj;
56  
57              if (Validator.isNotNull(emailAddress)) {
58                  if (!EmailValidator.getInstance().isValid(emailAddress)) {
59                      String summary = LanguageUtil.get(
60                          locale, "please-enter-a-valid-email-address");
61  
62                      FacesMessage facesMessage = new FacesMessage(
63                          FacesMessage.SEVERITY_ERROR, summary, null);
64  
65                      throw new ValidatorException(facesMessage);
66                  }
67              }
68          }
69          else {
70              String summary = LanguageUtil.format(
71                  locale,
72                  "validator-expected-type-string,-but-instead-received-type-x",
73                  obj.getClass().getName());
74  
75              FacesMessage facesMessage = new FacesMessage(
76                  FacesMessage.SEVERITY_ERROR, summary, null);
77  
78              throw new ValidatorException(facesMessage);
79          }
80      }
81  
82      public boolean isTransient() {
83          return _transient;
84      }
85  
86      public void setTransient(boolean value) {
87          _transient = value;
88      }
89  
90      private boolean _transient;
91  
92  }