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