KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > webapp > ConverterTag


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package javax.faces.webapp;
17
18 import javax.faces.application.Application;
19 import javax.faces.component.UIComponent;
20 import javax.faces.component.ValueHolder;
21 import javax.faces.context.FacesContext;
22 import javax.faces.convert.Converter;
23 import javax.faces.el.ValueBinding;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.tagext.Tag JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 /**
29  * @author Manfred Geiler (latest modification by $Author: mwessendorf $)
30  * @version $Revision: 1.4 $ $Date: 2004/07/01 22:00:53 $
31  */

32 public class ConverterTag
33         extends TagSupport JavaDoc
34 {
35     private String JavaDoc _converterId;
36
37     public ConverterTag()
38     {
39         super();
40     }
41
42     public void setConverterId(String JavaDoc converterId)
43     {
44         _converterId = converterId;
45     }
46
47     public int doStartTag()
48             throws JspException JavaDoc
49     {
50         UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
51         if (componentTag == null)
52         {
53             throw new JspException JavaDoc("no parent UIComponentTag found");
54         }
55         if (!componentTag.getCreated())
56         {
57             return Tag.SKIP_BODY;
58         }
59
60         Converter converter = createConverter();
61
62         UIComponent component = componentTag.getComponentInstance();
63         if (component == null)
64         {
65             throw new JspException JavaDoc("parent UIComponentTag has no UIComponent");
66         }
67         if (!(component instanceof ValueHolder))
68         {
69             throw new JspException JavaDoc("UIComponent is no ValueHolder");
70         }
71         ((ValueHolder)component).setConverter(converter);
72
73         return Tag.SKIP_BODY;
74     }
75
76     public void release()
77     {
78         super.release();
79         _converterId = null;
80     }
81
82     protected Converter createConverter()
83             throws JspException JavaDoc
84     {
85         FacesContext facesContext = FacesContext.getCurrentInstance();
86         Application application = facesContext.getApplication();
87         if (UIComponentTag.isValueReference(_converterId))
88         {
89             ValueBinding vb = facesContext.getApplication().createValueBinding(_converterId);
90             return application.createConverter((String JavaDoc)vb.getValue(facesContext));
91         }
92         else
93         {
94             return application.createConverter(_converterId);
95         }
96     }
97 }
98
Popular Tags