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.UIComponent;
23  import javax.faces.component.ValueHolder;
24  import javax.faces.context.FacesContext;
25  import javax.faces.convert.Converter;
26  import javax.faces.el.ValueBinding;
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 ConverterELTag}
35   */
36  public class ConverterTag extends TagSupport
37  {
38      private static final long serialVersionUID = -6168345066829108081L;
39      private String _converterId;
40      private String _binding;
41  
42      public ConverterTag()
43      {
44          super();
45      }
46  
47      public void setConverterId(String converterId)
48      {
49          _converterId = converterId;
50      }
51  
52      @Override
53      public int doStartTag() throws JspException
54      {
55  
56          UIComponentClassicTagBase componentTag =
57                  UIComponentClassicTagBase.getParentUIComponentClassicTagBase(pageContext);
58  
59          if (componentTag == null)
60          {
61              throw new JspException("no parent UIComponentTag found");
62          }
63          if (!componentTag.getCreated())
64          {
65              return Tag.SKIP_BODY;
66          }
67  
68          Converter converter = createConverter();
69  
70          UIComponent component = componentTag.getComponentInstance();
71          if (component == null)
72          {
73              throw new JspException("parent UIComponentTag has no UIComponent");
74          }
75          if (!(component instanceof ValueHolder))
76          {
77              throw new JspException("UIComponent is no ValueHolder");
78          }
79          ((ValueHolder)component).setConverter(converter);
80  
81          return Tag.SKIP_BODY;
82      }
83  
84      @Override
85      public void release()
86      {
87          super.release();
88          _converterId = null;
89          _binding = null;
90      }
91  
92      /**
93       * @throws JspException  
94       */
95      protected Converter createConverter() throws JspException
96      {
97          FacesContext facesContext = FacesContext.getCurrentInstance();
98          Application application = facesContext.getApplication();
99  
100         if (_binding != null)
101         {
102             ValueBinding vb = application.createValueBinding(_binding);
103             if (vb != null)
104             {
105                 Converter converter = (Converter)vb.getValue(facesContext);
106                 if (converter != null)
107                 {
108                     return converter;
109                 }
110             }
111         }
112 
113         if (UIComponentTag.isValueReference(_converterId))
114         {
115             ValueBinding vb = facesContext.getApplication().createValueBinding(_converterId);
116             return application.createConverter((String)vb.getValue(facesContext));
117         }
118 
119         return application.createConverter(_converterId);
120 
121     }
122 
123     /**
124      * @throws JspException  
125      */
126     public void setBinding(String binding) throws JspException
127     {
128         _binding = binding;
129     }
130 }