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