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.custom.regexprvalidator;
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  import org.apache.myfaces.validator.ValidatorBase;
28  
29  /**
30   * A custom validator for reg. expr., based upons Jakarta Commons. 
31   * 
32   * Unless otherwise specified, all attributes accept static values or EL expressions.
33   * 
34   * @JSFValidator
35   *   name = "t:validateRegExpr"
36   *   class = "org.apache.myfaces.custom.regexprvalidator.RegExprValidator"
37   *   tagClass = "org.apache.myfaces.custom.regexprvalidator.ValidateRegExprTag"
38   *   serialuidtag = "-449945949876262076L"
39   * @since 1.1.7
40   * @deprecated use myfaces commons mcv:validateRegExpr instead
41   * @author mwessendorf (latest modification by $Author: lu4242 $)
42   * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (Wed, 03 Sep 2008) $
43   */
44  
45  public abstract class AbstractRegExprValidator extends ValidatorBase {
46      /**
47       * <p>The standard converter id for this converter.</p>
48       */
49      public static final String     VALIDATOR_ID        = "org.apache.myfaces.validator.RegExpr";
50  
51      /**
52       * <p>The message identifier of the {@link FacesMessage} to be created if
53       * the regex check fails.</p>
54       */
55      public static final String REGEXPR_MESSAGE_ID = "org.apache.myfaces.Regexpr.INVALID";
56  
57      public AbstractRegExprValidator(){
58      }
59  
60      public void validate(
61          FacesContext facesContext,
62          UIComponent uiComponent,
63          Object value)
64          throws ValidatorException {
65  
66          if (facesContext == null) throw new NullPointerException("facesContext");
67          if (uiComponent == null) throw new NullPointerException("uiComponent");
68  
69          if (value == null)
70              {
71                  return;
72          }
73          Object[] args = {value.toString()};
74          if(!GenericValidator.matchRegexp(value.toString(),"^"+getPattern()+"$")){
75              throw new ValidatorException(getFacesMessage(REGEXPR_MESSAGE_ID, args));
76          }
77      }
78  
79      // -------------------------------------------------------- GETTER & SETTER
80  
81      /**
82       * the pattern, which is the base of the validation
83       * 
84       * @JSFProperty
85       *   literalOnly = "true"
86       * @return the pattern, on which a value should be validated
87       */
88      public abstract String getPattern();
89  
90      /**
91       * @param string the pattern, on which a value should be validated
92       */
93      public abstract void setPattern(String string);
94  
95  }