KickJava   Java API By Example, From Geeks To Geeks.

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


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.component.UIComponent;
19 import javax.faces.context.FacesContext;
20 import javax.faces.el.ValueBinding;
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.tagext.Tag JavaDoc;
23 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
24
25 /**
26  * @author Manfred Geiler (latest modification by $Author: mwessendorf $)
27  * @version $Revision: 1.4 $ $Date: 2004/07/01 22:00:54 $
28  */

29 public class AttributeTag
30         extends TagSupport JavaDoc
31 {
32     private String JavaDoc _name;
33     private String JavaDoc _value;
34
35     public void setName(String JavaDoc name)
36     {
37         _name = name;
38     }
39
40     public void setValue(String JavaDoc value)
41     {
42         _value = value;
43     }
44
45     public int doStartTag() throws JspException JavaDoc
46     {
47         UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
48         if (componentTag == null)
49         {
50             throw new JspException JavaDoc("no parent UIComponentTag found");
51         }
52         UIComponent component = componentTag.getComponentInstance();
53         if (component == null)
54         {
55             throw new JspException JavaDoc("parent UIComponentTag has no UIComponent");
56         }
57         String JavaDoc name = getName();
58         if (component.getAttributes().get(name) == null)
59         {
60             component.getAttributes().put(name, getValue());
61         }
62         return Tag.SKIP_BODY;
63     }
64
65     public void release()
66     {
67         super.release();
68         _name = null;
69         _value = null;
70     }
71
72
73     private String JavaDoc getName()
74     {
75         if (UIComponentTag.isValueReference(_name))
76         {
77             FacesContext facesContext = FacesContext.getCurrentInstance();
78             ValueBinding vb = facesContext.getApplication().createValueBinding(_name);
79             return (String JavaDoc)vb.getValue(facesContext);
80         }
81         else
82         {
83             return _name;
84         }
85     }
86
87     private String JavaDoc getValue()
88     {
89         if (UIComponentTag.isValueReference(_value))
90         {
91             FacesContext facesContext = FacesContext.getCurrentInstance();
92             ValueBinding vb = facesContext.getApplication().createValueBinding(_value);
93             return (String JavaDoc)vb.getValue(facesContext);
94         }
95         else
96         {
97             return _value;
98         }
99     }
100
101 }
102
Popular Tags