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  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspAttribute;
30  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspTag;
31  
32  /**
33   * Creates a validator and associates it with the nearest parent UIComponent. 
34   * <p>
35   *  During the validation phase (or the apply-request-values
36   * phase for immediate components), if the associated component has any
37   * submitted value and the conversion of that value to the required
38   * type has succeeded then the specified validator type is
39   * invoked to test the validity of the converted value.
40   * </p>
41   * <p>
42   * Commonly associated with an h:inputText entity, but may be applied to
43   * any input component.
44   * </p>
45   * <p>
46   * Some validators may allow the component to use attributes to define
47   * component-specific validation constraints; see the f:attribute tag.
48   * See also the "validator" attribute of all input components, which
49   * allows a component to specify an arbitrary validation &lt;i&gt;method&lt;/i&gt;
50   * (rather than a registered validation type, as this tag does).
51   * </p>
52   * <p>
53   * Unless otherwise specified, all attributes accept static values
54   * or EL expressions.
55   * </p>
56   *
57   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
58   * @version $Revision: 693358 $ $Date: 2008-09-08 22:54:29 -0500 (Mon, 08 Sep 2008) $
59   */
60  @JSFJspTag(name="f:validator",bodyContent="empty")
61  public class ValidatorImplTag
62          extends ValidatorELTag
63  {
64      private ValueExpression _validatorId;
65      private ValueExpression _binding;
66      private String _validatorIdString = null;
67  
68      /**
69       * The registered ID of the desired Validator.
70       */
71      @JSFJspAttribute(
72              rtexprvalue=true,
73              className="java.lang.String")    
74      public void setValidatorId(ValueExpression validatorId)
75      {
76          _validatorId = validatorId;
77      }
78  
79      /**
80       *  A ValueExpression that evaluates to an implementation of
81       * the javax.faces.validator.Validator interface. 
82       */
83      @JSFJspAttribute(
84              rtexprvalue=true,
85              className="javax.faces.validator.Validator")
86      public void setBinding(ValueExpression binding)
87      {
88          _binding = binding;
89      }
90  
91      /**
92       * Use this method to specify the validatorId programmatically.
93       *
94       * @param validatorIdString
95       */
96      public void setValidatorIdString(String validatorIdString)
97      {
98          _validatorIdString = validatorIdString;
99      }
100 
101     public void release()
102     {
103         super.release();
104         _validatorId = null;
105         _binding = null;
106         _validatorIdString = null;
107     }
108     
109     protected Validator createValidator() throws javax.servlet.jsp.JspException
110     {
111 
112         if (_validatorIdString != null)
113         {
114             return this.createClassicValidator();
115         }        
116         if (_validatorId != null && _validatorId.isLiteralText())
117         {
118             return this.createClassicValidator();
119         }        
120         
121         return new DelegateValidator(_validatorId, _binding,
122                 _validatorIdString);
123     }    
124 
125     protected Validator createClassicValidator() throws javax.servlet.jsp.JspException
126     {
127         FacesContext facesContext = FacesContext.getCurrentInstance();
128         ELContext elContext = facesContext.getELContext();
129         if (null != _binding)
130         {
131             Object validator;
132             try
133             {
134                 validator = _binding.getValue(elContext);
135             } catch (Exception e)
136             {
137                 throw new JspException("Error while creating the Validator", e);
138             }
139             if (validator instanceof Validator)
140             {
141                 return (Validator) validator;
142             }
143         }
144         Application application = facesContext.getApplication();
145         Validator validator = null;
146         try
147         {
148             // first check if an ValidatorId was set by a method
149             if (null != _validatorIdString)
150             {
151                 validator = application.createValidator(_validatorIdString);
152             } else if (null != _validatorId)
153             {
154                 String validatorId = (String) _validatorId.getValue(elContext);
155                 validator = application.createValidator(validatorId);
156             }
157         } catch (Exception e)
158         {
159             throw new JspException("Error while creating the Validator", e);
160         }
161 
162         if (null != validator)
163         {
164             if (null != _binding)
165             {
166                 _binding.setValue(elContext, validator);
167             }
168             return validator;
169         }
170         throw new JspException("validatorId and/or binding must be specified");
171     }
172 
173 }