1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.taglib.core; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.util.Locale; |
23 | |
import java.util.logging.Level; |
24 | |
import java.util.logging.Logger; |
25 | |
|
26 | |
import javax.el.ELContext; |
27 | |
import javax.el.MethodExpression; |
28 | |
import javax.el.ValueExpression; |
29 | |
import javax.faces.FacesException; |
30 | |
import javax.faces.component.UIComponent; |
31 | |
import javax.faces.component.UIViewRoot; |
32 | |
import javax.faces.context.FacesContext; |
33 | |
import javax.faces.render.RenderKitFactory; |
34 | |
import javax.faces.webapp.UIComponentELTag; |
35 | |
import javax.servlet.jsp.JspException; |
36 | |
import javax.servlet.jsp.jstl.core.Config; |
37 | |
|
38 | |
import org.apache.myfaces.application.jsp.ServletViewResponseWrapper; |
39 | |
import org.apache.myfaces.shared.util.LocaleUtils; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public class ViewTag extends UIComponentELTag |
47 | |
{ |
48 | |
|
49 | 0 | private static final Logger log = Logger.getLogger(ViewTag.class.getName()); |
50 | |
|
51 | |
@Override |
52 | |
public String getComponentType() |
53 | |
{ |
54 | 0 | return UIViewRoot.COMPONENT_TYPE; |
55 | |
} |
56 | |
|
57 | |
@Override |
58 | |
public String getRendererType() |
59 | |
{ |
60 | 0 | return null; |
61 | |
} |
62 | |
|
63 | |
private ValueExpression _locale; |
64 | |
private ValueExpression _renderKitId; |
65 | |
|
66 | |
private MethodExpression _beforePhase; |
67 | |
private MethodExpression _afterPhase; |
68 | |
|
69 | |
public void setLocale(ValueExpression locale) |
70 | |
{ |
71 | 0 | _locale = locale; |
72 | 0 | } |
73 | |
|
74 | |
public void setRenderKitId(ValueExpression renderKitId) |
75 | |
{ |
76 | 0 | _renderKitId = renderKitId; |
77 | 0 | } |
78 | |
|
79 | |
public void setBeforePhase(MethodExpression beforePhase) |
80 | |
{ |
81 | 0 | _beforePhase = beforePhase; |
82 | 0 | } |
83 | |
|
84 | |
public void setAfterPhase(MethodExpression afterPhase) |
85 | |
{ |
86 | 0 | _afterPhase = afterPhase; |
87 | 0 | } |
88 | |
|
89 | |
@Override |
90 | |
public int doStartTag() throws JspException |
91 | |
{ |
92 | 0 | if (log.isLoggable(Level.FINEST)) |
93 | |
{ |
94 | 0 | log.finest("entering ViewTag.doStartTag"); |
95 | |
} |
96 | |
|
97 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
98 | 0 | Object response = facesContext.getExternalContext().getResponse(); |
99 | 0 | if (response instanceof ServletViewResponseWrapper) |
100 | |
{ |
101 | |
try |
102 | |
{ |
103 | 0 | pageContext.getOut().flush(); |
104 | 0 | ((ServletViewResponseWrapper)response).flushToWrappedResponse(); |
105 | |
} |
106 | 0 | catch (IOException e) |
107 | |
{ |
108 | 0 | throw new JspException("Can't write content above <f:view> tag" + " " + e.getMessage()); |
109 | 0 | } |
110 | |
} |
111 | |
|
112 | 0 | int retVal = super.doStartTag(); |
113 | |
|
114 | 0 | Config.set(pageContext.getRequest(), Config.FMT_LOCALE, facesContext.getViewRoot().getLocale()); |
115 | |
|
116 | 0 | if (log.isLoggable(Level.FINEST)) |
117 | |
{ |
118 | 0 | log.finest("leaving ViewTag.doStartTag"); |
119 | |
} |
120 | 0 | return retVal; |
121 | |
} |
122 | |
|
123 | |
@Override |
124 | |
public int doEndTag() throws JspException |
125 | |
{ |
126 | 0 | if (log.isLoggable(Level.FINEST)) |
127 | |
{ |
128 | 0 | log.finest("entering ViewTag.doEndTag"); |
129 | |
} |
130 | 0 | int retVal = super.doEndTag(); |
131 | |
|
132 | 0 | if (log.isLoggable(Level.FINEST)) |
133 | |
{ |
134 | 0 | log.finest("leaving ViewTag.doEndTag"); |
135 | |
} |
136 | 0 | return retVal; |
137 | |
} |
138 | |
|
139 | |
@Override |
140 | |
public int doAfterBody() throws JspException |
141 | |
{ |
142 | 0 | if (log.isLoggable(Level.FINEST)) |
143 | |
{ |
144 | 0 | log.finest("entering ViewTag.doAfterBody"); |
145 | |
} |
146 | |
|
147 | 0 | UIComponent verbatimComp = createVerbatimComponentFromBodyContent(); |
148 | |
|
149 | 0 | if (verbatimComp != null) |
150 | |
{ |
151 | 0 | FacesContext.getCurrentInstance().getViewRoot().getChildren().add(verbatimComp); |
152 | |
} |
153 | |
|
154 | 0 | if (log.isLoggable(Level.FINEST)) |
155 | |
{ |
156 | 0 | log.finest("leaving ViewTag.doAfterBody"); |
157 | |
} |
158 | 0 | return EVAL_PAGE; |
159 | |
} |
160 | |
|
161 | |
@Override |
162 | |
protected void setProperties(UIComponent component) |
163 | |
{ |
164 | 0 | super.setProperties(component); |
165 | |
|
166 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
167 | 0 | ELContext elContext = facesContext.getELContext(); |
168 | |
|
169 | 0 | UIViewRoot viewRoot = (UIViewRoot)component; |
170 | |
|
171 | |
|
172 | 0 | if (_locale != null) |
173 | |
{ |
174 | |
Locale locale; |
175 | 0 | if (_locale.isLiteralText()) |
176 | |
{ |
177 | 0 | locale = LocaleUtils.toLocale(_locale.getValue(elContext).toString()); |
178 | |
} |
179 | |
else |
180 | |
{ |
181 | 0 | component.setValueExpression("locale", _locale); |
182 | |
|
183 | 0 | Object localeValue = _locale.getValue(elContext); |
184 | |
|
185 | 0 | if (localeValue instanceof Locale) |
186 | |
{ |
187 | 0 | locale = (Locale)localeValue; |
188 | |
} |
189 | 0 | else if (localeValue instanceof String) |
190 | |
{ |
191 | 0 | locale = LocaleUtils.toLocale((String)localeValue); |
192 | |
} |
193 | |
else |
194 | |
{ |
195 | 0 | if (localeValue != null) |
196 | |
{ |
197 | 0 | throw new IllegalArgumentException("Locale or String class expected. Expression: " + _locale |
198 | |
+ ". Return class: " + localeValue.getClass().getName()); |
199 | |
} |
200 | |
|
201 | 0 | throw new IllegalArgumentException("Locale or String class expected. Expression: " + _locale |
202 | |
+ ". Return value null"); |
203 | |
} |
204 | |
} |
205 | 0 | viewRoot.setLocale(locale); |
206 | 0 | Config.set(pageContext.getRequest(), Config.FMT_LOCALE, locale); |
207 | |
} |
208 | |
|
209 | |
|
210 | 0 | if (_renderKitId != null) |
211 | |
{ |
212 | 0 | if (_renderKitId.isLiteralText()) |
213 | |
{ |
214 | 0 | viewRoot.setRenderKitId(_renderKitId.getValue(elContext).toString()); |
215 | |
} |
216 | |
else |
217 | |
{ |
218 | 0 | viewRoot.setValueExpression("renderKitId", _renderKitId); |
219 | 0 | viewRoot.setRenderKitId(null); |
220 | |
} |
221 | |
} |
222 | 0 | else if (viewRoot.getRenderKitId() == null) |
223 | |
{ |
224 | 0 | String defaultRenderKitId = facesContext.getApplication().getDefaultRenderKitId(); |
225 | |
|
226 | 0 | if (defaultRenderKitId != null) |
227 | |
{ |
228 | 0 | viewRoot.setRenderKitId(defaultRenderKitId); |
229 | |
} |
230 | |
else |
231 | |
{ |
232 | 0 | viewRoot.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT); |
233 | |
} |
234 | |
} |
235 | |
|
236 | |
|
237 | 0 | if (_beforePhase != null) |
238 | |
{ |
239 | 0 | if (_beforePhase.isLiteralText()) |
240 | |
{ |
241 | 0 | throw new FacesException("Invalid method expression for attribute 'beforePhase' in the view tag: " |
242 | |
+ _beforePhase.getExpressionString()); |
243 | |
} |
244 | |
|
245 | 0 | viewRoot.setBeforePhaseListener(_beforePhase); |
246 | |
|
247 | |
} |
248 | |
|
249 | |
|
250 | 0 | if (_afterPhase != null) |
251 | |
{ |
252 | 0 | if (_afterPhase.isLiteralText()) |
253 | |
{ |
254 | 0 | throw new FacesException("Invalid method expression for attribute 'beforePhase' in the view tag: " |
255 | |
+ _afterPhase.getExpressionString()); |
256 | |
} |
257 | |
|
258 | 0 | viewRoot.setAfterPhaseListener(_afterPhase); |
259 | |
|
260 | |
} |
261 | 0 | } |
262 | |
} |