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.ui;
20
21 import java.io.IOException;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27
28 import javax.el.ELException;
29 import javax.faces.FacesException;
30 import javax.faces.application.StateManager;
31 import javax.faces.component.UIComponent;
32 import javax.faces.event.PhaseId;
33 import javax.faces.view.facelets.FaceletContext;
34 import javax.faces.view.facelets.FaceletException;
35 import javax.faces.view.facelets.TagAttribute;
36 import javax.faces.view.facelets.TagConfig;
37 import javax.faces.view.facelets.TagHandler;
38
39 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
40 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
41 import org.apache.myfaces.view.facelets.AbstractFaceletContext;
42 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
43 import org.apache.myfaces.view.facelets.TemplateClient;
44 import org.apache.myfaces.view.facelets.tag.ComponentContainerHandler;
45 import org.apache.myfaces.view.facelets.tag.TagHandlerUtils;
46 import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 @JSFFaceletTag(name="ui:decorate")
62 public final class DecorateHandler extends TagHandler implements TemplateClient, ComponentContainerHandler
63 {
64
65
66 private static final Logger log = Logger.getLogger(DecorateHandler.class.getName());
67
68
69
70
71
72 @JSFFaceletAttribute(
73 name="template",
74 className="javax.el.ValueExpression",
75 deferredValueType="java.lang.String")
76 private final TagAttribute _template;
77
78 private final Map<String, DefineHandler> _handlers;
79
80 private final ParamHandler[] _params;
81
82
83
84
85 public DecorateHandler(TagConfig config)
86 {
87 super(config);
88 _template = getRequiredAttribute("template");
89 _handlers = new HashMap<String, DefineHandler>();
90
91 for (DefineHandler handler : TagHandlerUtils.findNextByType(nextHandler, DefineHandler.class))
92 {
93 _handlers.put(handler.getName(), handler);
94 if (log.isLoggable(Level.FINE))
95 {
96 log.fine(tag + " found Define[" + handler.getName() + "]");
97 }
98 }
99
100 Collection<ParamHandler> params = TagHandlerUtils.findNextByType(nextHandler, ParamHandler.class);
101 if (!params.isEmpty())
102 {
103 int i = 0;
104 _params = new ParamHandler[params.size()];
105 for (ParamHandler handler : params)
106 {
107 _params[i++] = handler;
108 }
109 }
110 else
111 {
112 _params = null;
113 }
114 }
115
116
117
118
119
120
121
122 public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
123 ELException
124 {
125
126
127
128
129
130
131
132
133
134
135
136 AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
137 actx.pushClient(this);
138
139 if (_params != null)
140 {
141
142
143 for (int i = 0; i < _params.length; i++)
144 {
145 _params[i].apply(ctx, parent);
146 }
147 }
148
149 FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
150 String path;
151 boolean markInitialState = false;
152 if (!_template.isLiteral())
153 {
154 String uniqueId = fcc.startComponentUniqueIdSection();
155
156 String restoredPath = (String) ComponentSupport.restoreInitialTagState(ctx, fcc, parent, uniqueId);
157 if (restoredPath != null)
158 {
159
160
161 if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId()))
162 {
163 path = this._template.getValue(ctx);
164 if (path == null || path.length() == 0)
165 {
166 return;
167 }
168 if (!path.equals(restoredPath))
169 {
170 markInitialState = true;
171 }
172 }
173 else
174 {
175 path = restoredPath;
176 }
177 }
178 else
179 {
180
181 path = this._template.getValue(ctx);
182 }
183 ComponentSupport.saveInitialTagState(ctx, fcc, parent, uniqueId, path);
184 }
185 else
186 {
187 path = _template.getValue(ctx);
188 }
189 try
190 {
191 boolean oldMarkInitialState = false;
192 Boolean isBuildingInitialState = null;
193 if (markInitialState)
194 {
195
196 oldMarkInitialState = fcc.isMarkInitialState();
197 fcc.setMarkInitialState(true);
198 isBuildingInitialState = (Boolean) ctx.getFacesContext().getAttributes().put(
199 StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
200 }
201 try
202 {
203 ctx.includeFacelet(parent, path);
204 }
205 finally
206 {
207 if (markInitialState)
208 {
209
210 if (isBuildingInitialState == null)
211 {
212 ctx.getFacesContext().getAttributes().remove(
213 StateManager.IS_BUILDING_INITIAL_STATE);
214 }
215 else
216 {
217 ctx.getFacesContext().getAttributes().put(
218 StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState);
219 }
220 fcc.setMarkInitialState(oldMarkInitialState);
221 }
222 }
223 }
224 finally
225 {
226
227 actx.popClient(this);
228 }
229 if (!_template.isLiteral())
230 {
231 fcc.endComponentUniqueIdSection();
232 }
233 if (!_template.isLiteral() && fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() &&
234 !fcc.isRefreshingTransientBuild())
235 {
236
237 ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent);
238 }
239 }
240
241 public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException,
242 FaceletException, ELException
243 {
244 if (name != null)
245 {
246 DefineHandler handler = _handlers.get(name);
247 if (handler != null)
248 {
249 handler.applyDefinition(ctx, parent);
250 return true;
251 }
252 else
253 {
254 return false;
255 }
256 }
257 else
258 {
259 this.nextHandler.apply(ctx, parent);
260 return true;
261 }
262 }
263 }