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