1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.faces.validator;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  
28  import java.util.Locale;
29  
30  import javax.faces.application.FacesMessage;
31  import javax.faces.component.StateHolder;
32  import javax.faces.component.UIComponent;
33  import javax.faces.context.ExternalContext;
34  import javax.faces.context.FacesContext;
35  import javax.faces.validator.ValidatorException;
36  
37  import org.apache.commons.validator.EmailValidator;
38  
39  /**
40   * <a href="EmailAddressValidator.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Neil Griffin
43   */
44  public class EmailAddressValidator
45      implements StateHolder, javax.faces.validator.Validator {
46  
47      public void restoreState(FacesContext facesContext, Object obj) {
48      }
49  
50      public Object saveState(FacesContext facesContext) {
51          return null;
52      }
53  
54      public void validate(
55              FacesContext facesContext, UIComponent uiComponent, Object obj)
56          throws ValidatorException {
57  
58          ExternalContext externalContext = facesContext.getExternalContext();
59  
60          Locale locale = externalContext.getRequestLocale();
61  
62          if (obj instanceof String) {
63              String emailAddress = (String)obj;
64  
65              if (Validator.isNotNull(emailAddress)) {
66                  if (!EmailValidator.getInstance().isValid(emailAddress)) {
67                      String summary = LanguageUtil.get(
68                          locale, "please-enter-a-valid-email-address");
69  
70                      FacesMessage facesMessage = new FacesMessage(
71                          FacesMessage.SEVERITY_ERROR, summary, null);
72  
73                      throw new ValidatorException(facesMessage);
74                  }
75              }
76          }
77          else {
78              String summary = LanguageUtil.format(
79                  locale,
80                  "validator-expected-type-string,-but-instead-received-type-x",
81                  obj.getClass().getName());
82  
83              FacesMessage facesMessage = new FacesMessage(
84                  FacesMessage.SEVERITY_ERROR, summary, null);
85  
86              throw new ValidatorException(facesMessage);
87          }
88      }
89  
90      public boolean isTransient() {
91          return _transient;
92      }
93  
94      public void setTransient(boolean value) {
95          _transient = value;
96      }
97  
98      private boolean _transient;
99  
100 }