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.net.URL; |
23 | |
import java.util.Collection; |
24 | |
|
25 | |
import javax.el.ELException; |
26 | |
import javax.el.ValueExpression; |
27 | |
import javax.el.VariableMapper; |
28 | |
import javax.faces.FacesException; |
29 | |
import javax.faces.application.ProjectStage; |
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.shared.util.ClassUtils; |
42 | |
import org.apache.myfaces.view.facelets.AbstractFaceletContext; |
43 | |
import org.apache.myfaces.view.facelets.FaceletCompositionContext; |
44 | |
import org.apache.myfaces.view.facelets.el.VariableMapperWrapper; |
45 | |
import org.apache.myfaces.view.facelets.impl.TemplateContextImpl; |
46 | |
import org.apache.myfaces.view.facelets.tag.ComponentContainerHandler; |
47 | |
import org.apache.myfaces.view.facelets.tag.TagHandlerUtils; |
48 | |
import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
@JSFFaceletTag(name="ui:include", bodyContent="JSP") |
63 | |
public final class IncludeHandler extends TagHandler implements ComponentContainerHandler |
64 | |
{ |
65 | |
|
66 | |
private static final String ERROR_PAGE_INCLUDE_PATH = "javax.faces.error.xhtml"; |
67 | |
private static final String ERROR_FACELET = "META-INF/rsc/myfaces-dev-error-include.xhtml"; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
@JSFFaceletAttribute( |
74 | |
className="javax.el.ValueExpression", |
75 | |
deferredValueType="java.lang.String", |
76 | |
required=true) |
77 | |
private final TagAttribute src; |
78 | |
|
79 | |
private final ParamHandler[] _params; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public IncludeHandler(TagConfig config) |
85 | |
{ |
86 | 0 | super(config); |
87 | 0 | this.src = this.getRequiredAttribute("src"); |
88 | |
|
89 | 0 | Collection<ParamHandler> params = TagHandlerUtils.findNextByType(nextHandler, ParamHandler.class); |
90 | 0 | if (!params.isEmpty()) |
91 | |
{ |
92 | 0 | int i = 0; |
93 | 0 | _params = new ParamHandler[params.size()]; |
94 | 0 | for (ParamHandler handler : params) |
95 | |
{ |
96 | 0 | _params[i++] = handler; |
97 | 0 | } |
98 | 0 | } |
99 | |
else |
100 | |
{ |
101 | 0 | _params = null; |
102 | |
} |
103 | 0 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, |
111 | |
ELException |
112 | |
{ |
113 | 0 | AbstractFaceletContext actx = (AbstractFaceletContext) ctx; |
114 | 0 | FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx); |
115 | |
String path; |
116 | 0 | boolean markInitialState = false; |
117 | 0 | String uniqueId = null; |
118 | 0 | if (!src.isLiteral()) |
119 | |
{ |
120 | 0 | uniqueId = actx.generateUniqueFaceletTagId( |
121 | |
fcc.startComponentUniqueIdSection(), tagId); |
122 | |
} |
123 | 0 | else if (_params != null) |
124 | |
{ |
125 | 0 | uniqueId = actx.generateUniqueFaceletTagId( |
126 | |
fcc.generateUniqueComponentId(), tagId); |
127 | |
} |
128 | 0 | if (!src.isLiteral()) |
129 | |
{ |
130 | |
|
131 | |
|
132 | 0 | String restoredPath = (String) ComponentSupport.restoreInitialTagState(ctx, fcc, parent, uniqueId); |
133 | 0 | if (restoredPath != null) |
134 | |
{ |
135 | |
|
136 | |
|
137 | 0 | if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId())) |
138 | |
{ |
139 | 0 | path = this.src.getValue(ctx); |
140 | 0 | if (path == null || path.length() == 0) |
141 | |
{ |
142 | 0 | return; |
143 | |
} |
144 | 0 | if (!path.equals(restoredPath)) |
145 | |
{ |
146 | 0 | markInitialState = true; |
147 | |
} |
148 | |
} |
149 | |
else |
150 | |
{ |
151 | 0 | path = restoredPath; |
152 | |
} |
153 | |
} |
154 | |
else |
155 | |
{ |
156 | |
|
157 | 0 | path = this.src.getValue(ctx); |
158 | |
} |
159 | 0 | ComponentSupport.saveInitialTagState(ctx, fcc, parent, uniqueId, path); |
160 | 0 | } |
161 | |
else |
162 | |
{ |
163 | 0 | path = this.src.getValue(ctx); |
164 | |
} |
165 | |
try |
166 | |
{ |
167 | 0 | if (path == null || path.length() == 0) |
168 | |
{ |
169 | |
return; |
170 | |
} |
171 | 0 | VariableMapper orig = ctx.getVariableMapper(); |
172 | 0 | ctx.setVariableMapper(new VariableMapperWrapper(orig)); |
173 | |
try |
174 | |
{ |
175 | |
|
176 | |
|
177 | |
|
178 | 0 | URL url = null; |
179 | 0 | boolean oldMarkInitialState = false; |
180 | 0 | Boolean isBuildingInitialState = null; |
181 | |
|
182 | |
|
183 | 0 | if (ctx.getFacesContext().isProjectStage(ProjectStage.Development) |
184 | |
&& ERROR_PAGE_INCLUDE_PATH.equals(path)) |
185 | |
{ |
186 | 0 | url =ClassUtils.getResource(ERROR_FACELET); |
187 | |
} |
188 | 0 | if (markInitialState) |
189 | |
{ |
190 | |
|
191 | 0 | oldMarkInitialState = fcc.isMarkInitialState(); |
192 | 0 | fcc.setMarkInitialState(true); |
193 | 0 | isBuildingInitialState = (Boolean) ctx.getFacesContext().getAttributes().put( |
194 | |
StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE); |
195 | |
} |
196 | |
try |
197 | |
{ |
198 | 0 | if (_params != null) |
199 | |
{ |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | 0 | String[] names = new String[_params.length]; |
207 | 0 | ValueExpression[] values = new ValueExpression[_params.length]; |
208 | |
|
209 | 0 | for (int i = 0; i < _params.length; i++) |
210 | |
{ |
211 | 0 | names[i] = _params[i].getName(ctx); |
212 | 0 | values[i] = _params[i].getValue(ctx); |
213 | |
} |
214 | |
|
215 | 0 | actx.pushTemplateContext(new TemplateContextImpl()); |
216 | |
|
217 | 0 | for (int i = 0; i < _params.length; i++) |
218 | |
{ |
219 | 0 | _params[i].apply(ctx, parent, names[i], values[i], uniqueId); |
220 | |
} |
221 | 0 | } |
222 | |
else |
223 | |
{ |
224 | 0 | actx.pushTemplateContext(new TemplateContextImpl()); |
225 | |
} |
226 | 0 | if (url == null) |
227 | |
{ |
228 | 0 | ctx.includeFacelet(parent, path); |
229 | |
} |
230 | |
else |
231 | |
{ |
232 | 0 | ctx.includeFacelet(parent, url); |
233 | |
} |
234 | |
} |
235 | |
finally |
236 | |
{ |
237 | 0 | if (markInitialState) |
238 | |
{ |
239 | |
|
240 | 0 | if (isBuildingInitialState == null) |
241 | |
{ |
242 | 0 | ctx.getFacesContext().getAttributes().remove( |
243 | |
StateManager.IS_BUILDING_INITIAL_STATE); |
244 | |
} |
245 | |
else |
246 | |
{ |
247 | 0 | ctx.getFacesContext().getAttributes().put( |
248 | |
StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState); |
249 | |
} |
250 | 0 | fcc.setMarkInitialState(oldMarkInitialState); |
251 | |
} |
252 | 0 | actx.popTemplateContext(); |
253 | 0 | } |
254 | |
} |
255 | |
finally |
256 | |
{ |
257 | 0 | ctx.setVariableMapper(orig); |
258 | 0 | } |
259 | |
} |
260 | |
finally |
261 | |
{ |
262 | 0 | if (!src.isLiteral()) |
263 | |
{ |
264 | 0 | fcc.endComponentUniqueIdSection(); |
265 | |
|
266 | 0 | if (fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() && |
267 | |
!fcc.isRefreshingTransientBuild()) |
268 | |
{ |
269 | |
|
270 | 0 | ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent); |
271 | |
} |
272 | 0 | if (fcc.isDynamicComponentSection()) |
273 | |
{ |
274 | 0 | ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent); |
275 | |
} |
276 | |
} |
277 | |
} |
278 | 0 | } |
279 | |
} |