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.Validator;
26  import javax.faces.webapp.ValidatorELTag;
27  import javax.servlet.jsp.JspException;
28  
29  /**
30   * Basic Validator implementation.
31   *
32   * @author Andreas Berger (latest modification by $Author: skitching $)
33   * @version $Revision: 684465 $ $Date: 2008-08-10 06:38:21 -0500 (Sun, 10 Aug 2008) $
34   * @since 1.2
35   */
36  public class ValidatorTag
37          extends ValidatorELTag
38  {
39      private ValueExpression _validatorId;
40      private ValueExpression _binding;
41      private String _validatorIdString = null;
42  
43      public void setValidatorId(ValueExpression validatorId)
44      {
45          _validatorId = validatorId;
46      }
47  
48      public void setBinding(ValueExpression binding)
49      {
50          _binding = binding;
51      }
52  
53      /**
54       * Use this method to specify the validatorId programmatically.
55       *
56       * @param validatorIdString
57       */
58      public void setValidatorIdString(String validatorIdString)
59      {
60          _validatorIdString = validatorIdString;
61      }
62  
63      public void release()
64      {
65          super.release();
66          _validatorId = null;
67          _binding = null;
68          _validatorIdString = null;
69      }
70  
71      protected Validator createValidator() throws javax.servlet.jsp.JspException
72      {
73          FacesContext facesContext = FacesContext.getCurrentInstance();
74          ELContext elContext = facesContext.getELContext();
75          if (null != _binding)
76          {
77              Object validator;
78              try
79              {
80                  validator = _binding.getValue(elContext);
81              } catch (Exception e)
82              {
83                  throw new JspException("Error while creating the Validator", e);
84              }
85              if (validator instanceof Validator)
86              {
87                  return (Validator) validator;
88              }
89          }
90          Application application = facesContext.getApplication();
91          Validator validator = null;
92          try
93          {
94              // first check if an ValidatorId was set by a method
95              if (null != _validatorIdString)
96              {
97                  validator = application.createValidator(_validatorIdString);
98              } else if (null != _validatorId)
99              {
100                 String validatorId = (String) _validatorId.getValue(elContext);
101                 validator = application.createValidator(validatorId);
102             }
103         } catch (Exception e)
104         {
105             throw new JspException("Error while creating the Validator", e);
106         }
107 
108         if (null != validator)
109         {
110             if (null != _binding)
111             {
112                 _binding.setValue(elContext, validator);
113             }
114             return validator;
115         }
116         throw new JspException("validatorId and/or binding must be specified");
117     }
118 
119 }