View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.commons.validator;
20  
21  import javax.faces.application.FacesMessage;
22  import javax.faces.component.UIComponent;
23  import javax.faces.context.FacesContext;
24  import javax.faces.validator.ValidatorException;
25  
26  import org.apache.commons.validator.GenericValidator;
27  
28  
29  /**
30   * A custom validator for email address format, based upons Jakarta Commons.
31   * 
32   * Unless otherwise specified, all attributes accept static values or EL expressions.
33   * 
34   * @JSFValidator
35   *   name = "mcv:validateEmail"
36   *   tagClass = "org.apache.myfaces.commons.validator.ValidateEmailTag"
37   *   serialuidtag = "6041422002721046221L"
38   *   
39   * @author mwessendorf (latest modification by $Author: skitching $)
40   * @version $Revision: 673801 $ $Date: 2008-07-03 15:58:48 -0500 (Thu, 03 Jul 2008) $
41   */
42  public class EmailValidator extends ValidatorBase {
43  
44      /**
45       * <p>The standard converter id for this converter.</p>
46       */
47      public static final String     VALIDATOR_ID        = "org.apache.myfaces.commons.validator.Email";
48      /**
49       * <p>The message identifier of the {@link FacesMessage} to be created if
50       * the maximum length check fails.</p>
51       */
52      public static final String EMAIL_MESSAGE_ID = "org.apache.myfaces.commons.validator.Email.INVALID";
53  
54      public EmailValidator(){
55      }
56  
57      /**
58       * methode that validates an email-address.
59       * it uses the commons-validator
60       */
61      public void validate(
62          FacesContext facesContext,
63          UIComponent uiComponent,
64          Object value)
65          throws ValidatorException {
66  
67  
68              if (facesContext == null) throw new NullPointerException("facesContext");
69              if (uiComponent == null) throw new NullPointerException("uiComponent");
70  
71              if (value == null)
72              {
73                  return;
74              }
75              if (!GenericValidator.isEmail(value.toString().trim())) {
76                  Object[] args = {value.toString()};
77                  throw new ValidatorException(getFacesMessage(EMAIL_MESSAGE_ID, args));
78              }
79  
80      }
81  
82  }