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.compiler; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.io.Writer; |
23 | |
|
24 | |
import javax.el.ELException; |
25 | |
import javax.faces.FacesException; |
26 | |
import javax.faces.component.UIComponent; |
27 | |
import javax.faces.component.UniqueIdVendor; |
28 | |
import javax.faces.view.facelets.FaceletContext; |
29 | |
import javax.faces.view.facelets.FaceletException; |
30 | |
|
31 | |
import org.apache.myfaces.view.facelets.FaceletCompositionContext; |
32 | |
import org.apache.myfaces.view.facelets.el.ELText; |
33 | |
import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport; |
34 | |
import org.apache.myfaces.view.facelets.util.FastWriter; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
final class UIInstructionHandler extends AbstractUIHandler |
41 | |
{ |
42 | |
|
43 | |
private final String alias; |
44 | |
|
45 | |
private final String id; |
46 | |
|
47 | |
private final ELText txt; |
48 | |
|
49 | |
private final Instruction[] instructions; |
50 | |
|
51 | |
private final int length; |
52 | |
|
53 | |
private final boolean literal; |
54 | |
|
55 | |
public UIInstructionHandler(String alias, String id, Instruction[] instructions, ELText txt) |
56 | 0 | { |
57 | 0 | this.alias = alias; |
58 | 0 | this.id = id; |
59 | 0 | this.instructions = instructions; |
60 | 0 | this.txt = txt; |
61 | 0 | this.length = txt.toString().length(); |
62 | |
|
63 | 0 | boolean literal = true; |
64 | 0 | int size = instructions.length; |
65 | |
|
66 | 0 | for (int i = 0; i < size; i++) |
67 | |
{ |
68 | 0 | Instruction ins = this.instructions[i]; |
69 | 0 | if (!ins.isLiteral()) |
70 | |
{ |
71 | 0 | literal = false; |
72 | 0 | break; |
73 | |
} |
74 | |
} |
75 | |
|
76 | 0 | this.literal = literal; |
77 | 0 | } |
78 | |
|
79 | |
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, |
80 | |
ELException |
81 | |
{ |
82 | 0 | if (parent != null) |
83 | |
{ |
84 | 0 | String facetName = this.getFacetName(ctx, parent); |
85 | |
|
86 | |
|
87 | 0 | String id = ctx.generateUniqueId(this.id); |
88 | |
|
89 | |
|
90 | 0 | UIComponent c = null; |
91 | 0 | FaceletCompositionContext mctx= FaceletCompositionContext.getCurrentInstance(ctx); |
92 | |
|
93 | 0 | if (mctx.isRefreshingSection()) |
94 | |
{ |
95 | 0 | if (facetName != null) |
96 | |
{ |
97 | 0 | c = ComponentSupport.findChildInFacetByTagId(parent, id, facetName); |
98 | |
} |
99 | |
else |
100 | |
{ |
101 | 0 | c = ComponentSupport.findChildInChildrenByTagId(parent, id); |
102 | |
} |
103 | |
} |
104 | 0 | boolean componentFound = false; |
105 | 0 | if (c != null) |
106 | |
{ |
107 | 0 | componentFound = true; |
108 | |
|
109 | 0 | mctx.incrementUniqueComponentId(); |
110 | |
|
111 | 0 | mctx.markForDeletion(c); |
112 | |
} |
113 | |
else |
114 | |
{ |
115 | |
Instruction[] applied; |
116 | 0 | String componentId = mctx.generateUniqueComponentId(); |
117 | 0 | if (this.literal) |
118 | |
{ |
119 | 0 | applied = this.instructions; |
120 | |
} |
121 | |
else |
122 | |
{ |
123 | 0 | int size = this.instructions.length; |
124 | 0 | applied = new Instruction[size]; |
125 | |
|
126 | |
|
127 | |
Instruction ins; |
128 | 0 | for (int i = 0; i < size; i++) |
129 | |
{ |
130 | 0 | ins = this.instructions[i]; |
131 | 0 | applied[i] = ins.apply(ctx.getExpressionFactory(), ctx); |
132 | |
} |
133 | |
} |
134 | |
|
135 | 0 | c = new UIInstructions(txt, applied); |
136 | |
|
137 | |
|
138 | |
|
139 | 0 | UniqueIdVendor uniqueIdVendor |
140 | |
= mctx.getUniqueIdVendorFromStack(); |
141 | 0 | if (uniqueIdVendor == null) |
142 | |
{ |
143 | 0 | uniqueIdVendor = ComponentSupport.getViewRoot(ctx, parent); |
144 | |
} |
145 | 0 | if (uniqueIdVendor != null) |
146 | |
{ |
147 | |
|
148 | |
|
149 | |
|
150 | 0 | String uid = uniqueIdVendor.createUniqueId(ctx.getFacesContext(), componentId); |
151 | 0 | c.setId(uid); |
152 | |
} |
153 | |
|
154 | 0 | ((UIInstructions)c).setMarkCreated(id); |
155 | |
} |
156 | |
|
157 | 0 | boolean oldProcessingEvents = ctx.getFacesContext().isProcessingEvents(); |
158 | |
|
159 | 0 | if (componentFound) |
160 | |
{ |
161 | 0 | mctx.finalizeForDeletion(c); |
162 | 0 | if (mctx.isRefreshingSection()) |
163 | |
{ |
164 | 0 | ctx.getFacesContext().setProcessingEvents(false); |
165 | |
} |
166 | 0 | if (facetName == null) |
167 | |
{ |
168 | 0 | parent.getChildren().remove(c); |
169 | |
} |
170 | |
else |
171 | |
{ |
172 | 0 | ComponentSupport.removeFacet(ctx, parent, c, facetName); |
173 | |
} |
174 | 0 | if (mctx.isRefreshingSection()) |
175 | |
{ |
176 | 0 | ctx.getFacesContext().setProcessingEvents(oldProcessingEvents); |
177 | |
} |
178 | |
} |
179 | 0 | if (componentFound && mctx.isRefreshingSection()) |
180 | |
{ |
181 | 0 | ctx.getFacesContext().setProcessingEvents(false); |
182 | |
} |
183 | 0 | if (facetName == null) |
184 | |
{ |
185 | 0 | parent.getChildren().add(c); |
186 | |
} |
187 | |
else |
188 | |
{ |
189 | 0 | ComponentSupport.addFacet(ctx, parent, c, facetName); |
190 | |
} |
191 | 0 | if (componentFound && mctx.isRefreshingSection()) |
192 | |
{ |
193 | 0 | ctx.getFacesContext().setProcessingEvents(oldProcessingEvents); |
194 | |
} |
195 | |
} |
196 | 0 | } |
197 | |
|
198 | |
public String toString() |
199 | |
{ |
200 | 0 | return this.txt.toString(); |
201 | |
} |
202 | |
|
203 | |
public String getText() |
204 | |
{ |
205 | 0 | return this.txt.toString(); |
206 | |
} |
207 | |
|
208 | |
public String getText(FaceletContext ctx) |
209 | |
{ |
210 | 0 | Writer writer = new FastWriter(this.length); |
211 | |
try |
212 | |
{ |
213 | 0 | this.txt.apply(ctx.getExpressionFactory(), ctx).write(writer, ctx); |
214 | |
} |
215 | 0 | catch (IOException e) |
216 | |
{ |
217 | 0 | throw new ELException(this.alias + ": " + e.getMessage(), e.getCause()); |
218 | 0 | } |
219 | 0 | return writer.toString(); |
220 | |
} |
221 | |
|
222 | |
} |