001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.taglib.core;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.myfaces.application.jsp.ViewResponseWrapper;
021: import org.apache.myfaces.shared_impl.util.LocaleUtils;
022:
023: import javax.el.ELContext;
024: import javax.el.MethodExpression;
025: import javax.el.ValueExpression;
026: import javax.faces.FacesException;
027: import javax.faces.component.UIComponent;
028: import javax.faces.component.UIViewRoot;
029: import javax.faces.context.FacesContext;
030: import javax.faces.render.RenderKitFactory;
031: import javax.faces.webapp.UIComponentELTag;
032: import javax.servlet.jsp.JspException;
033: import javax.servlet.jsp.jstl.core.Config;
034: import java.io.IOException;
035: import java.util.Locale;
036:
037: /**
038: * @author Manfred Geiler (latest modification by $Author: baranda $)
039: * @author Bruno Aranda (JSR-252)
040: * @version $Revision: 542009 $ $Date: 2007-05-27 19:46:22 +0200 (So, 27 Mai 2007) $
041: */
042: public class ViewTag extends UIComponentELTag {
043: private static final Log log = LogFactory.getLog(ViewTag.class);
044:
045: public String getComponentType() {
046: return UIViewRoot.COMPONENT_TYPE;
047: }
048:
049: public String getRendererType() {
050: return null;
051: }
052:
053: private ValueExpression _locale;
054: private ValueExpression _renderKitId;
055:
056: private MethodExpression _beforePhase;
057: private MethodExpression _afterPhase;
058:
059: public void setLocale(ValueExpression locale) {
060: _locale = locale;
061: }
062:
063: public void setRenderKitId(ValueExpression renderKitId) {
064: _renderKitId = renderKitId;
065: }
066:
067: public void setBeforePhase(MethodExpression beforePhase) {
068: _beforePhase = beforePhase;
069: }
070:
071: public void setAfterPhase(MethodExpression afterPhase) {
072: _afterPhase = afterPhase;
073: }
074:
075: public int doStartTag() throws JspException {
076: if (log.isTraceEnabled())
077: log.trace("entering ViewTag.doStartTag");
078:
079: FacesContext facesContext = FacesContext.getCurrentInstance();
080: Object response = facesContext.getExternalContext()
081: .getResponse();
082: if (response instanceof ViewResponseWrapper) {
083: try {
084: pageContext.getOut().flush();
085: ((ViewResponseWrapper) response)
086: .flushToWrappedResponse();
087: } catch (IOException e) {
088: throw new JspException(
089: "Can't write content above <f:view> tag" + " "
090: + e.getMessage());
091: }
092: }
093:
094: int retVal = super .doStartTag();
095:
096: Config.set(pageContext.getRequest(), Config.FMT_LOCALE,
097: facesContext.getViewRoot().getLocale());
098:
099: if (log.isTraceEnabled())
100: log.trace("leaving ViewTag.doStartTag");
101: return retVal;
102: }
103:
104: public int doEndTag() throws JspException {
105: if (log.isTraceEnabled())
106: log.trace("entering ViewTag.doEndTag");
107: int retVal = super .doEndTag();
108:
109: if (log.isTraceEnabled())
110: log.trace("leaving ViewTag.doEndTag");
111: return retVal;
112: }
113:
114: public int doAfterBody() throws JspException {
115: if (log.isTraceEnabled())
116: log.trace("entering ViewTag.doAfterBody");
117:
118: UIComponent verbatimComp = createVerbatimComponentFromBodyContent();
119:
120: if (verbatimComp != null) {
121: FacesContext.getCurrentInstance().getViewRoot()
122: .getChildren().add(verbatimComp);
123: }
124:
125: if (log.isTraceEnabled())
126: log.trace("leaving ViewTag.doAfterBody");
127: return EVAL_PAGE;
128: }
129:
130: protected void setProperties(UIComponent component) {
131: super .setProperties(component);
132:
133: FacesContext facesContext = FacesContext.getCurrentInstance();
134: ELContext elContext = facesContext.getELContext();
135:
136: UIViewRoot viewRoot = (UIViewRoot) component;
137:
138: // locale
139: if (_locale != null) {
140: Locale locale;
141: if (_locale.isLiteralText()) {
142: locale = LocaleUtils.toLocale(_locale.getValue(
143: elContext).toString());
144: } else {
145: component.setValueExpression("locale", _locale);
146:
147: Object localeValue = _locale.getValue(elContext);
148:
149: if (localeValue instanceof Locale) {
150: locale = (Locale) localeValue;
151: } else if (localeValue instanceof String) {
152: locale = LocaleUtils.toLocale((String) localeValue);
153: } else {
154: if (localeValue != null) {
155: throw new IllegalArgumentException(
156: "Locale or String class expected. Expression: "
157: + _locale
158: + ". Return class: "
159: + localeValue.getClass()
160: .getName());
161: }
162:
163: throw new IllegalArgumentException(
164: "Locale or String class expected. Expression: "
165: + _locale + ". Return value null");
166: }
167: }
168: viewRoot.setLocale(locale);
169: Config.set(pageContext.getRequest(), Config.FMT_LOCALE,
170: locale);
171: }
172:
173: // renderkitId
174: if (_renderKitId != null) {
175: if (_renderKitId.isLiteralText()) {
176: viewRoot.setRenderKitId(_renderKitId
177: .getValue(elContext).toString());
178: } else {
179: viewRoot
180: .setValueExpression("renderKitId", _renderKitId);
181: viewRoot.setRenderKitId(null);
182: }
183: } else if (viewRoot.getRenderKitId() == null) {
184: String defaultRenderKitId = facesContext.getApplication()
185: .getDefaultRenderKitId();
186:
187: if (defaultRenderKitId != null) {
188: viewRoot.setRenderKitId(defaultRenderKitId);
189: } else {
190: viewRoot
191: .setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
192: }
193: }
194:
195: // beforePhase
196: if (_beforePhase != null) {
197: if (_beforePhase.isLiteralText()) {
198: throw new FacesException(
199: "Invalid method expression for attribute 'beforePhase' in the view tag: "
200: + _beforePhase.getExpressionString());
201: }
202:
203: viewRoot.setBeforePhaseListener(_beforePhase);
204:
205: }
206:
207: // afterPhase
208: if (_afterPhase != null) {
209: if (_afterPhase.isLiteralText()) {
210: throw new FacesException(
211: "Invalid method expression for attribute 'beforePhase' in the view tag: "
212: + _afterPhase.getExpressionString());
213: }
214:
215: viewRoot.setAfterPhaseListener(_afterPhase);
216:
217: }
218: }
219: }
|