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   * @deprecated replaced by {@link ValidatorELTag}
35   */
36  public class ValidatorTag extends TagSupport
37  {
38      private static final long serialVersionUID = 8794036166323016663L;
39      private String _validatorId;
40      private String _binding;
41  
42      public void setValidatorId(String validatorId)
43      {
44          _validatorId = validatorId;
45      }
46  
47      @Override
48      public int doStartTag() throws JspException
49      {
50          UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
51          if (componentTag == null)
52          {
53              throw new JspException("no parent UIComponentTag found");
54          }
55          if (!componentTag.getCreated())
56          {
57              return Tag.SKIP_BODY;
58          }
59  
60          Validator validator = createValidator();
61  
62          UIComponent component = componentTag.getComponentInstance();
63          if (component == null)
64          {
65              throw new JspException("parent UIComponentTag has no UIComponent");
66          }
67          if (!(component instanceof EditableValueHolder))
68          {
69              throw new JspException("UIComponent is no ValueHolder");
70          }
71          ((EditableValueHolder)component).addValidator(validator);
72  
73          return Tag.SKIP_BODY;
74      }
75  
76      @Override
77      public void release()
78      {
79          super.release();
80          _validatorId = null;
81          _binding = null;
82      }
83  
84      /**
85       * @throws JspException  
86       */
87      protected Validator createValidator() throws JspException
88      {
89          FacesContext facesContext = FacesContext.getCurrentInstance();
90          Application application = facesContext.getApplication();
91  
92          if (_binding != null)
93          {
94              ValueBinding vb = application.createValueBinding(_binding);
95              if (vb != null)
96              {
97                  Validator validator = (Validator)vb.getValue(facesContext);
98                  if (validator != null)
99                  {
100                     return validator;
101                 }
102             }
103         }
104 
105         if (UIComponentTag.isValueReference(_validatorId))
106         {
107             ValueBinding vb = facesContext.getApplication().createValueBinding(_validatorId);
108             return application.createValidator((String)vb.getValue(facesContext));
109         }
110 
111         return application.createValidator(_validatorId);
112 
113     }
114 
115     /**
116      * 
117      * @param binding
118      * @throws javax.servlet.jsp.JspException
119      * 
120      * @deprecated
121      */
122     public void setBinding(java.lang.String binding) throws javax.servlet.jsp.JspException
123     {
124         if (binding != null && !UIComponentTag.isValueReference(binding))
125         {
126             throw new IllegalArgumentException("not a valid binding: " + binding);
127         }
128         _binding = binding;
129     }
130 }