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