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