1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.tag.jsf;
20
21 import java.io.IOException;
22
23 import javax.el.ValueExpression;
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.ValueHolder;
26 import javax.faces.context.FacesContext;
27 import javax.faces.convert.Converter;
28 import javax.faces.view.ValueHolderAttachedObjectHandler;
29 import javax.faces.view.facelets.ComponentHandler;
30 import javax.faces.view.facelets.ConverterHandler;
31 import javax.faces.view.facelets.FaceletContext;
32 import javax.faces.view.facelets.MetaRuleset;
33 import javax.faces.view.facelets.TagAttribute;
34 import javax.faces.view.facelets.TagException;
35 import javax.faces.view.facelets.TagHandlerDelegate;
36
37 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
38 import org.apache.myfaces.view.facelets.tag.MetaRulesetImpl;
39
40
41
42
43
44
45
46
47
48
49
50
51 public class ConverterTagHandlerDelegate extends TagHandlerDelegate implements ValueHolderAttachedObjectHandler
52 {
53 private ConverterHandler _delegate;
54
55 public ConverterTagHandlerDelegate(ConverterHandler delegate)
56 {
57 _delegate = delegate;
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 @Override
78 public void apply(FaceletContext ctx, UIComponent parent) throws IOException
79 {
80
81 if (!ComponentHandler.isNew(parent))
82 {
83 return;
84 }
85 if (parent instanceof ValueHolder)
86 {
87 applyAttachedObject(ctx.getFacesContext(), parent);
88 }
89 else if (UIComponent.isCompositeComponent(parent))
90 {
91 if (getFor() == null)
92 {
93 throw new TagException(_delegate.getTag(), "is nested inside a composite component"
94 + " but does not have a for attribute.");
95 }
96 FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx);
97 mctx.addAttachedObjectHandler(parent, _delegate);
98 }
99 else
100 {
101 throw new TagException(_delegate.getTag(),
102 "Parent not composite component or an instance of ValueHolder: " + parent);
103 }
104 }
105
106
107
108
109
110
111
112
113 protected Converter createConverter(FaceletContext ctx)
114 {
115 if (_delegate.getConverterId(ctx) == null)
116 {
117 throw new TagException(_delegate.getTag(),
118 "Default behavior invoked of requiring a converter-id passed in the "
119 + "constructor, must override ConvertHandler(ConverterConfig)");
120 }
121 return ctx.getFacesContext().getApplication().createConverter(_delegate.getConverterId(ctx));
122 }
123
124 @Override
125 public MetaRuleset createMetaRuleset(Class type)
126 {
127 return new MetaRulesetImpl(_delegate.getTag(), type).ignore("binding").ignore("for");
128 }
129
130 public void applyAttachedObject(FacesContext context, UIComponent parent)
131 {
132
133 FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(
134 FaceletContext.FACELET_CONTEXT_KEY);
135
136
137 ValueHolder vh = (ValueHolder) parent;
138 ValueExpression ve = null;
139 Converter c = null;
140 if (_delegate.getBinding() != null)
141 {
142 ve = _delegate.getBinding().getValueExpression(faceletContext, Converter.class);
143 c = (Converter) ve.getValue(faceletContext);
144 }
145 if (c == null)
146 {
147 c = this.createConverter(faceletContext);
148 if (ve != null)
149 {
150 ve.setValue(faceletContext, c);
151 }
152 }
153 if (c == null)
154 {
155 throw new TagException(_delegate.getTag(), "No Converter was created");
156 }
157 _delegate.setAttributes(faceletContext, c);
158 vh.setConverter(c);
159 Object lv = vh.getLocalValue();
160 FacesContext faces = faceletContext.getFacesContext();
161 if (lv instanceof String)
162 {
163 vh.setValue(c.getAsObject(faces, parent, (String) lv));
164 }
165 }
166
167 public String getFor()
168 {
169 TagAttribute forAttribute = _delegate.getTagAttribute("for");
170
171 if (forAttribute == null)
172 {
173 return null;
174 }
175 else
176 {
177 return forAttribute.getValue();
178 }
179 }
180 }