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 javax.faces.webapp;
20  
21  import javax.faces.application.Application;
22  import javax.faces.component.EditableValueHolder;
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  import javax.faces.el.ValueBinding;
26  import javax.faces.validator.Validator;
27  import javax.servlet.jsp.JspException;
28  import javax.servlet.jsp.tagext.Tag;
29  import javax.servlet.jsp.tagext.TagSupport;
30  
31  /**
32   * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
33   *
34   * @author Manfred Geiler (latest modification by $Author: skitching $)
35   * @version $Revision: 676298 $ $Date: 2008-07-13 05:31:48 -0500 (Sun, 13 Jul 2008) $
36   *
37   * @deprecated replaced by {@link ValidatorELTag}
38   */
39  public class ValidatorTag
40          extends TagSupport
41  {
42      private static final long serialVersionUID = 8794036166323016663L;
43      private String _validatorId;
44      private String _binding;
45  
46      public void setValidatorId(String validatorId)
47      {
48          _validatorId = validatorId;
49      }
50  
51      public int doStartTag()
52              throws javax.servlet.jsp.JspException
53      {
54          UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
55          if (componentTag == null)
56          {
57              throw new JspException("no parent UIComponentTag found");
58          }
59          if (!componentTag.getCreated())
60          {
61              return Tag.SKIP_BODY;
62          }
63  
64          Validator validator = createValidator();
65  
66          UIComponent component = componentTag.getComponentInstance();
67          if (component == null)
68          {
69              throw new JspException("parent UIComponentTag has no UIComponent");
70          }
71          if (!(component instanceof EditableValueHolder))
72          {
73              throw new JspException("UIComponent is no ValueHolder");
74          }
75          ((EditableValueHolder)component).addValidator(validator);
76  
77          return Tag.SKIP_BODY;
78      }
79  
80      public void release()
81      {
82          super.release();
83          _validatorId = null;
84          _binding = null;
85      }
86  
87      protected Validator createValidator()
88              throws JspException
89      {
90          FacesContext facesContext = FacesContext.getCurrentInstance();
91          Application application = facesContext.getApplication();
92          
93          if(_binding != null) {
94              ValueBinding vb = application.createValueBinding(_binding);
95              if(vb != null) {
96                  Validator validator = (Validator) vb.getValue(facesContext);
97                  if(validator != null) {
98                      return validator;
99                  }
100             }
101         }
102         
103         if (UIComponentTag.isValueReference(_validatorId))
104         {
105             ValueBinding vb = facesContext.getApplication().createValueBinding(_validatorId);
106             return application.createValidator((String)vb.getValue(facesContext));
107         }
108 
109         return application.createValidator(_validatorId);
110         
111     }
112     
113     /**
114      * 
115      * @param binding
116      * @throws javax.servlet.jsp.JspException
117      * 
118      * @deprecated
119      */
120     public void setBinding(java.lang.String binding)
121             throws javax.servlet.jsp.JspException
122     {
123         if (binding != null && !UIComponentTag.isValueReference(binding))
124         {
125             throw new IllegalArgumentException("not a valid binding: " + binding);
126         }
127         _binding = binding;
128     }
129 }