1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.webapp;
20
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.el.ValueBinding;
24 import javax.servlet.jsp.JspException;
25 import javax.servlet.jsp.tagext.Tag;
26 import javax.servlet.jsp.tagext.TagSupport;
27
28
29
30
31
32
33
34 public class AttributeTag
35 extends TagSupport
36 {
37 private static final long serialVersionUID = 3147657100171678632L;
38 private String _name;
39 private String _value;
40
41
42
43
44
45 public void setName(String name)
46 {
47 _name = name;
48 }
49
50
51
52
53
54 public void setValue(String value)
55 {
56 _value = value;
57 }
58
59
60
61
62 public int doStartTag() throws JspException
63 {
64 UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
65 if (componentTag == null)
66 {
67 throw new JspException("no parent UIComponentTag found");
68 }
69 UIComponent component = componentTag.getComponentInstance();
70 if (component == null)
71 {
72 throw new JspException("parent UIComponentTag has no UIComponent");
73 }
74 String name = getName();
75 if (component.getAttributes().get(name) == null)
76 {
77 Object value = getValue();
78
79 if(value != null)
80 component.getAttributes().put(name, value);
81 }
82 return Tag.SKIP_BODY;
83 }
84
85
86
87
88 public void release()
89 {
90 super.release();
91 _name = null;
92 _value = null;
93 }
94
95
96 private String getName()
97 {
98 if (UIComponentTag.isValueReference(_name))
99 {
100 FacesContext facesContext = FacesContext.getCurrentInstance();
101 ValueBinding vb = facesContext.getApplication().createValueBinding(_name);
102 return (String)vb.getValue(facesContext);
103 }
104
105 return _name;
106 }
107
108 private Object getValue()
109 {
110 if (UIComponentTag.isValueReference(_value))
111 {
112 FacesContext facesContext = FacesContext.getCurrentInstance();
113 ValueBinding vb = facesContext.getApplication().createValueBinding(_value);
114 return vb.getValue(facesContext);
115 }
116
117 return _value;
118 }
119
120 }