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.taglib.core;
20  
21  import javax.el.ELContext;
22  import javax.el.ValueExpression;
23  import javax.faces.application.Application;
24  import javax.faces.context.FacesContext;
25  import javax.faces.validator.RegexValidator;
26  import javax.faces.validator.Validator;
27  import javax.faces.webapp.ValidatorELTag;
28  import javax.servlet.jsp.JspException;
29  
30  /**
31   * JSP Tag class for {@link javax.faces.validator.RegexValidator}.
32   *
33   * @author Jan-Kees van Andel
34   * @since 2.0
35   */
36  public class ValidateRegexTag extends ValidatorELTag
37  {
38      private static final long serialVersionUID = 8363913774859484811L;
39  
40      private ValueExpression _pattern;
41  
42      private ValueExpression _binding;
43  
44      @Override
45      protected Validator createValidator() throws JspException
46      {
47          FacesContext fc = FacesContext.getCurrentInstance();
48          ELContext elc = fc.getELContext();
49          if (_binding != null)
50          {
51              Object validator;
52              try
53              {
54                  validator = _binding.getValue(elc);
55              }
56              catch (Exception e)
57              {
58                  throw new JspException("Error while creating the Validator", e);
59              }
60              if (validator instanceof RegexValidator)
61              {
62                  return (Validator)validator;
63              }
64          }
65          if (null != _pattern)
66          {
67              Application appl = fc.getApplication();
68              RegexValidator validator = (RegexValidator) appl.createValidator(RegexValidator.VALIDATOR_ID);
69              String pattern = (String)_pattern.getValue(elc);
70              validator.setPattern(pattern);
71  
72              if (_binding != null)
73              {
74                  _binding.setValue(elc, validator);
75              }
76  
77              return validator;
78          }
79          else
80          {
81              throw new AssertionError("pattern may not be null");
82          }
83      }
84  
85      public ValueExpression getBinding()
86      {
87          return _binding;
88      }
89  
90      public void setBinding(ValueExpression binding)
91      {
92          _binding = binding;
93      }
94  
95      public ValueExpression getPattern()
96      {
97          return _pattern;
98      }
99  
100     public void setPattern(ValueExpression pattern)
101     {
102         _pattern = pattern;
103     }
104 
105     @Override
106     public void release()
107     {
108         _pattern = null;
109         _binding = null;
110     }
111 }