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.jsf.core;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.el.ELException;
26 import javax.el.MethodExpression;
27 import javax.faces.FacesException;
28 import javax.faces.component.UIComponent;
29 import javax.faces.component.UIViewRoot;
30 import javax.faces.event.PhaseEvent;
31 import javax.faces.view.facelets.FaceletContext;
32 import javax.faces.view.facelets.FaceletException;
33 import javax.faces.view.facelets.TagAttribute;
34 import javax.faces.view.facelets.TagConfig;
35 import javax.faces.view.facelets.TagHandler;
36
37 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
38 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
39 import org.apache.myfaces.shared.util.StringUtils;
40 import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
41
42
43
44
45
46
47
48
49 @JSFFaceletTag(
50 name = "f:view",
51 bodyContent = "empty",
52 componentClass="javax.faces.component.UIViewRoot")
53 public final class ViewHandler extends TagHandler
54 {
55
56 private final static Class<?>[] LISTENER_SIG = new Class<?>[] { PhaseEvent.class };
57
58 @JSFFaceletAttribute
59 private final TagAttribute locale;
60
61 @JSFFaceletAttribute
62 private final TagAttribute renderKitId;
63
64 @JSFFaceletAttribute
65 private final TagAttribute contentType;
66
67 @JSFFaceletAttribute
68 private final TagAttribute encoding;
69
70 @JSFFaceletAttribute
71 private final TagAttribute beforePhase;
72
73 @JSFFaceletAttribute
74 private final TagAttribute afterPhase;
75
76 @JSFFaceletAttribute(name="transient")
77 private final TagAttribute transientAttribute;
78
79 private final TagAttribute contracts;
80
81 private final TagAttribute oamEnableViewPool;
82
83
84
85
86 public ViewHandler(TagConfig config)
87 {
88 super(config);
89 this.locale = this.getAttribute("locale");
90 this.renderKitId = this.getAttribute("renderKitId");
91 this.contentType = this.getAttribute("contentType");
92 this.encoding = this.getAttribute("encoding");
93 this.beforePhase = this.getAttribute("beforePhase");
94 this.afterPhase = this.getAttribute("afterPhase");
95 this.transientAttribute = this.getAttribute("transient");
96 this.contracts = this.getAttribute("contracts");
97 this.oamEnableViewPool = this.getAttribute("oamEnableViewPool");
98 }
99
100
101
102
103
104
105 public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
106 ELException
107 {
108 UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent);
109 if (root != null)
110 {
111 if (this.locale != null)
112 {
113 root.setLocale(ComponentSupport.getLocale(ctx, this.locale));
114 }
115 if (this.renderKitId != null)
116 {
117 String v = this.renderKitId.getValue(ctx);
118 root.setRenderKitId(v);
119 }
120 String encodingValue = null;
121 if (this.contentType != null)
122 {
123
124
125
126 String v = this.contentType.getValue(ctx);
127 if (v != null)
128 {
129 int j = v.indexOf(';');
130 if (j >= 0)
131 {
132 int i = v.indexOf("charset",j);
133 if (i >= 0)
134 {
135 i = v.indexOf('=',i)+1;
136 if (v.length() > i)
137 {
138 encodingValue = v.substring(i);
139 }
140
141
142
143 v = v.substring(0 , j);
144 }
145 }
146 }
147 ctx.getFacesContext().getAttributes().put("facelets.ContentType", v);
148 }
149 if (this.encoding != null)
150 {
151 String v = this.encoding.getValue(ctx);
152 ctx.getFacesContext().getAttributes().put("facelets.Encoding", v);
153 }
154 else if (encodingValue != null)
155 {
156 ctx.getFacesContext().getAttributes().put("facelets.Encoding", encodingValue);
157 }
158 if (this.beforePhase != null)
159 {
160 MethodExpression m = this.beforePhase.getMethodExpression(ctx, null, LISTENER_SIG);
161 root.setBeforePhaseListener(m);
162 }
163 if (this.afterPhase != null)
164 {
165 MethodExpression m = this.afterPhase.getMethodExpression(ctx, null, LISTENER_SIG);
166 root.setAfterPhaseListener(m);
167 }
168 if (this.transientAttribute != null)
169 {
170 root.setTransient(this.transientAttribute.getBoolean(ctx));
171 }
172 if (this.contracts != null)
173 {
174 String contractsValue = this.contracts.getValue(ctx);
175 if (contractsValue != null)
176 {
177 String[] values = StringUtils.trim(
178 StringUtils.splitShortString(contractsValue, ','));
179 if (values != null)
180 {
181 List<String> list = new ArrayList<String>();
182 for (String v : values)
183 {
184 list.add(v);
185 }
186 ctx.getFacesContext().setResourceLibraryContracts(list);
187 }
188 }
189 }
190 if (this.oamEnableViewPool != null)
191 {
192 root.getAttributes().put("oamEnableViewPool", this.oamEnableViewPool.getBoolean(ctx));
193 }
194 }
195 this.nextHandler.apply(ctx, parent);
196 }
197
198 }