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.jstl.core;
20
21 import java.io.IOException;
22
23 import javax.el.ELException;
24 import javax.faces.FacesException;
25 import javax.faces.application.StateManager;
26 import javax.faces.component.UIComponent;
27 import javax.faces.event.PhaseId;
28 import javax.faces.view.facelets.FaceletContext;
29 import javax.faces.view.facelets.TagAttribute;
30 import javax.faces.view.facelets.TagConfig;
31 import javax.faces.view.facelets.TagHandler;
32
33 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
34 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
35 import org.apache.myfaces.view.facelets.AbstractFaceletContext;
36 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
37 import org.apache.myfaces.view.facelets.tag.ComponentContainerHandler;
38 import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
39
40
41
42
43
44
45
46
47
48 @JSFFaceletTag(name="c:if")
49 @JSFFaceletAttribute(
50 name="scope",
51 className="java.lang.String",
52 longDescription="Scope for var.")
53 public final class IfHandler extends TagHandler implements ComponentContainerHandler
54 {
55
56
57
58
59
60 @JSFFaceletAttribute(className="boolean", required=true)
61 private final TagAttribute test;
62
63
64
65
66
67
68 @JSFFaceletAttribute(className="java.lang.String")
69 private final TagAttribute var;
70
71
72
73
74 public IfHandler(TagConfig config)
75 {
76 super(config);
77 this.test = this.getRequiredAttribute("test");
78 this.var = this.getAttribute("var");
79 }
80
81 public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, ELException
82 {
83 FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
84 AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
85 String uniqueId = actx.generateUniqueFaceletTagId(
86 fcc.startComponentUniqueIdSection(), tagId);
87 Boolean restoredValue = (Boolean) ComponentSupport.restoreInitialTagState(ctx, fcc, parent, uniqueId);
88 boolean b = false;
89 boolean markInitialState = false;
90 try
91 {
92 if (restoredValue != null)
93 {
94 if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId()))
95 {
96 b = this.test.getBoolean(ctx);
97 if (!restoredValue.equals(b))
98 {
99 markInitialState = true;
100 }
101 }
102 else
103 {
104 b = restoredValue;
105 }
106 }
107 else
108 {
109
110 b = this.test.getBoolean(ctx);
111 }
112
113 if (this.var != null)
114 {
115 ctx.setAttribute(var.getValue(ctx), Boolean.valueOf(b));
116 }
117 if (b)
118 {
119 boolean oldMarkInitialState = false;
120 Boolean isBuildingInitialState = null;
121 try
122 {
123 if (markInitialState)
124 {
125
126 oldMarkInitialState = fcc.isMarkInitialState();
127 fcc.setMarkInitialState(true);
128 isBuildingInitialState = (Boolean) ctx.getFacesContext().getAttributes().put(
129 StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
130 }
131 this.nextHandler.apply(ctx, parent);
132 }
133 finally
134 {
135 if (markInitialState)
136 {
137
138 if (isBuildingInitialState == null)
139 {
140 ctx.getFacesContext().getAttributes().remove(
141 StateManager.IS_BUILDING_INITIAL_STATE);
142 }
143 else
144 {
145 ctx.getFacesContext().getAttributes().put(
146 StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState);
147 }
148 fcc.setMarkInitialState(oldMarkInitialState);
149 }
150 }
151 }
152 }
153 finally
154 {
155 fcc.endComponentUniqueIdSection();
156 }
157
158 ComponentSupport.saveInitialTagState(ctx, fcc, parent, uniqueId, b);
159 if (fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() && !fcc.isRefreshingTransientBuild())
160 {
161
162 ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent);
163 }
164 if (fcc.isDynamicComponentSection())
165 {
166 ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent);
167 }
168 }
169 }