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; |
20 | |
|
21 | |
import java.io.FileNotFoundException; |
22 | |
import java.io.IOException; |
23 | |
import java.net.URL; |
24 | |
import java.util.Collection; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import javax.el.ELException; |
29 | |
import javax.el.ValueExpression; |
30 | |
import javax.faces.FacesException; |
31 | |
import javax.faces.component.UIComponent; |
32 | |
import javax.faces.view.facelets.FaceletContext; |
33 | |
import javax.faces.view.facelets.FaceletException; |
34 | |
import javax.faces.view.facelets.TagAttribute; |
35 | |
import javax.faces.view.facelets.TagConfig; |
36 | |
import javax.faces.view.facelets.TagException; |
37 | |
import javax.faces.view.facelets.TagHandler; |
38 | |
import org.apache.myfaces.util.ExternalSpecifications; |
39 | |
|
40 | |
import org.apache.myfaces.view.facelets.AbstractFaceletContext; |
41 | |
import org.apache.myfaces.view.facelets.ELExpressionCacheMode; |
42 | |
import org.apache.myfaces.view.facelets.FaceletCompositionContext; |
43 | |
import org.apache.myfaces.view.facelets.TemplateClient; |
44 | |
import org.apache.myfaces.view.facelets.TemplateContext; |
45 | |
import org.apache.myfaces.view.facelets.el.FaceletStateValueExpression; |
46 | |
import org.apache.myfaces.view.facelets.el.FaceletStateValueExpressionUEL; |
47 | |
import org.apache.myfaces.view.facelets.impl.TemplateContextImpl; |
48 | |
import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport; |
49 | |
import org.apache.myfaces.view.facelets.tag.jsf.FaceletState; |
50 | |
import org.apache.myfaces.view.facelets.tag.ui.DefineHandler; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
final class UserTagHandler extends TagHandler implements TemplateClient, ComponentContainerHandler |
60 | |
{ |
61 | |
|
62 | |
protected final TagAttribute[] _vars; |
63 | |
|
64 | |
protected final URL _location; |
65 | |
|
66 | |
protected final Map<String, DefineHandler> _handlers; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public UserTagHandler(TagConfig config, URL location) |
72 | |
{ |
73 | 0 | super(config); |
74 | 0 | this._vars = this.tag.getAttributes().getAll(); |
75 | 0 | this._location = location; |
76 | |
|
77 | 0 | Collection<DefineHandler> defines = TagHandlerUtils.findNextByType(nextHandler, DefineHandler.class); |
78 | 0 | if (defines.isEmpty()) |
79 | |
{ |
80 | 0 | _handlers = null; |
81 | |
} |
82 | |
else |
83 | |
{ |
84 | 0 | _handlers = new HashMap<String, DefineHandler>(); |
85 | 0 | for (DefineHandler handler : defines) |
86 | |
{ |
87 | 0 | _handlers.put(handler.getName(), handler); |
88 | 0 | } |
89 | |
} |
90 | 0 | } |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, |
102 | |
ELException |
103 | |
{ |
104 | 0 | AbstractFaceletContext actx = (AbstractFaceletContext) ctx; |
105 | |
|
106 | |
try |
107 | |
{ |
108 | 0 | String[] names = null; |
109 | 0 | ValueExpression[] values = null; |
110 | 0 | if (this._vars.length > 0) |
111 | |
{ |
112 | 0 | names = new String[_vars.length]; |
113 | 0 | values = new ValueExpression[_vars.length]; |
114 | 0 | for (int i = 0; i < _vars.length; i++) |
115 | |
{ |
116 | 0 | names[i] = _vars[i].getLocalName(); |
117 | 0 | values[i] = _vars[i].getValueExpression(ctx, Object.class); |
118 | |
} |
119 | |
} |
120 | 0 | actx.pushTemplateContext(new TemplateContextImpl()); |
121 | 0 | actx.pushClient(this); |
122 | 0 | FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx); |
123 | 0 | String uniqueId = fcc.startComponentUniqueIdSection(); |
124 | |
try |
125 | |
{ |
126 | 0 | if (this._vars.length > 0) |
127 | |
{ |
128 | 0 | if (ELExpressionCacheMode.alwaysRecompile.equals(actx.getELExpressionCacheMode())) |
129 | |
{ |
130 | 0 | FaceletState faceletState = ComponentSupport.getFaceletState(ctx, parent, true); |
131 | 0 | for (int i = 0; i < this._vars.length; i++) |
132 | |
{ |
133 | |
|
134 | 0 | faceletState.putBinding(uniqueId, names[i], values[i]); |
135 | |
ValueExpression ve; |
136 | 0 | if (ExternalSpecifications.isUnifiedELAvailable()) |
137 | |
{ |
138 | 0 | ve = new FaceletStateValueExpressionUEL(uniqueId, names[i]); |
139 | |
} |
140 | |
else |
141 | |
{ |
142 | 0 | ve = new FaceletStateValueExpression(uniqueId, names[i]); |
143 | |
} |
144 | 0 | actx.getTemplateContext().setParameter(names[i], ve); |
145 | |
} |
146 | 0 | } |
147 | |
else |
148 | |
{ |
149 | 0 | for (int i = 0; i < this._vars.length; i++) |
150 | |
{ |
151 | 0 | ((AbstractFaceletContext) ctx).getTemplateContext().setParameter(names[i], values[i]); |
152 | |
} |
153 | |
} |
154 | |
} |
155 | |
|
156 | |
|
157 | 0 | if (!ELExpressionCacheMode.alwaysRecompile.equals(actx.getELExpressionCacheMode())) |
158 | |
{ |
159 | 0 | actx.getTemplateContext().setAllowCacheELExpressions(false); |
160 | |
} |
161 | 0 | ctx.includeFacelet(parent, this._location); |
162 | |
} |
163 | |
finally |
164 | |
{ |
165 | 0 | fcc.endComponentUniqueIdSection(); |
166 | 0 | } |
167 | |
} |
168 | 0 | catch (FileNotFoundException e) |
169 | |
{ |
170 | 0 | throw new TagException(this.tag, e.getMessage()); |
171 | |
} |
172 | |
finally |
173 | |
{ |
174 | |
|
175 | |
|
176 | 0 | actx.popClient(this); |
177 | 0 | actx.popTemplateContext(); |
178 | |
|
179 | 0 | } |
180 | 0 | } |
181 | |
|
182 | |
public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException, |
183 | |
FaceletException, ELException |
184 | |
{ |
185 | 0 | if (name != null) |
186 | |
{ |
187 | 0 | if (this._handlers == null) |
188 | |
{ |
189 | 0 | return false; |
190 | |
} |
191 | 0 | DefineHandler handler = (DefineHandler) this._handlers.get(name); |
192 | 0 | if (handler != null) |
193 | |
{ |
194 | 0 | AbstractFaceletContext actx = (AbstractFaceletContext) ctx; |
195 | 0 | TemplateContext itc = actx.popTemplateContext(); |
196 | |
try |
197 | |
{ |
198 | 0 | handler.applyDefinition(ctx, parent); |
199 | |
} |
200 | |
finally |
201 | |
{ |
202 | 0 | actx.pushTemplateContext(itc); |
203 | 0 | } |
204 | 0 | return true; |
205 | |
} |
206 | |
else |
207 | |
{ |
208 | 0 | return false; |
209 | |
} |
210 | |
} |
211 | |
else |
212 | |
{ |
213 | 0 | AbstractFaceletContext actx = (AbstractFaceletContext) ctx; |
214 | 0 | TemplateContext itc = actx.popTemplateContext(); |
215 | |
try |
216 | |
{ |
217 | 0 | this.nextHandler.apply(ctx, parent); |
218 | |
} |
219 | |
finally |
220 | |
{ |
221 | 0 | actx.pushTemplateContext(itc); |
222 | 0 | } |
223 | 0 | return true; |
224 | |
} |
225 | |
} |
226 | |
|
227 | |
} |