1 16 package org.apache.myfaces.taglib.core; 17 18 import org.apache.myfaces.application.MyfacesStateManager; 19 import org.apache.myfaces.application.jsp.JspViewHandlerImpl; 20 import org.apache.myfaces.renderkit.html.HtmlLinkRendererBase; 21 import org.apache.myfaces.util.LocaleUtils; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import javax.faces.application.StateManager; 27 import javax.faces.component.UIComponent; 28 import javax.faces.component.UIViewRoot; 29 import javax.faces.context.FacesContext; 30 import javax.faces.context.ResponseWriter; 31 import javax.faces.el.ValueBinding; 32 import javax.faces.webapp.UIComponentBodyTag; 33 import javax.faces.webapp.UIComponentTag; 34 import javax.servlet.ServletRequest ; 35 import javax.servlet.jsp.JspException ; 36 import javax.servlet.jsp.jstl.core.Config; 37 import javax.servlet.jsp.tagext.BodyContent ; 38 import javax.servlet.jsp.tagext.BodyTag ; 39 import java.io.IOException ; 40 import java.util.Locale ; 41 42 65 public class ViewTag 66 extends UIComponentBodyTag 67 { 68 private static final Log log = LogFactory.getLog(ViewTag.class); 69 70 public String getComponentType() 71 { 72 return UIViewRoot.COMPONENT_TYPE; 73 } 74 75 public String getRendererType() 76 { 77 return null; 78 } 79 80 private String _locale; 81 82 public void setLocale(String locale) 83 { 84 _locale = locale; 85 } 86 87 public int doStartTag() throws JspException 88 { 89 if (log.isTraceEnabled()) log.trace("entering ViewTag.doStartTag"); 90 super.doStartTag(); 91 FacesContext facesContext = FacesContext.getCurrentInstance(); 92 ResponseWriter responseWriter = facesContext.getResponseWriter(); 93 try 94 { 95 responseWriter.startDocument(); 96 } 97 catch (IOException e) 98 { 99 log.error("Error writing startDocument", e); 100 throw new JspException (e); 101 } 102 103 StateManager stateManager = facesContext.getApplication().getStateManager(); 104 if (stateManager.isSavingStateInClient(facesContext)) 105 { 106 if (log.isTraceEnabled()) log.trace("leaving ViewTag.doStartTag"); 107 return BodyTag.EVAL_BODY_BUFFERED; 108 } 109 else 110 { 111 if (log.isTraceEnabled()) log.trace("leaving ViewTag.doStartTag"); 112 return BodyTag.EVAL_BODY_INCLUDE; 113 } 114 } 115 116 protected boolean isSuppressed() 117 { 118 return true; 119 } 120 121 public int doEndTag() throws JspException 122 { 123 if (log.isTraceEnabled()) log.trace("entering ViewTag.doEndTag"); 124 FacesContext facesContext = FacesContext.getCurrentInstance(); 125 ResponseWriter responseWriter = facesContext.getResponseWriter(); 126 127 try 128 { 129 responseWriter.endDocument(); 130 } 131 catch (IOException e) 132 { 133 log.error("Error writing endDocument", e); 134 throw new JspException (e); 135 } 136 137 StateManager stateManager = facesContext.getApplication().getStateManager(); 138 if (!stateManager.isSavingStateInClient(facesContext)) 139 { 140 stateManager.saveSerializedView(facesContext); 142 } 143 144 if (log.isTraceEnabled()) log.trace("leaving ViewTag.doEndTag"); 145 return super.doEndTag(); 146 } 147 148 public int doAfterBody() throws JspException 149 { 150 if (log.isTraceEnabled()) log.trace("entering ViewTag.doAfterBody"); 151 try 152 { 153 BodyContent bodyContent = getBodyContent(); 154 if (bodyContent != null) 155 { 156 FacesContext facesContext = FacesContext.getCurrentInstance(); 157 StateManager stateManager = facesContext.getApplication().getStateManager(); 158 StateManager.SerializedView serializedView 159 = stateManager.saveSerializedView(facesContext); 160 if (serializedView != null) 161 { 162 ResponseWriter bufferWriter = facesContext.getResponseWriter(); 164 bufferWriter.flush(); 165 ResponseWriter realWriter = bufferWriter.cloneWithWriter(getPreviousOut()); 167 facesContext.setResponseWriter(realWriter); 168 169 String bodyStr = bodyContent.getString(); 170 int form_marker = bodyStr.indexOf(JspViewHandlerImpl.FORM_STATE_MARKER); 171 int url_marker = bodyStr.indexOf(HtmlLinkRendererBase.URL_STATE_MARKER); 172 int lastMarkerEnd = 0; 173 while (form_marker != -1 || url_marker != -1) 174 { 175 if (url_marker == -1 || (form_marker != -1 && form_marker < url_marker)) 176 { 177 realWriter.write(bodyStr, lastMarkerEnd, form_marker - lastMarkerEnd); 179 stateManager.writeState(facesContext, serializedView); 180 lastMarkerEnd = form_marker + JspViewHandlerImpl.FORM_STATE_MARKER_LEN; 181 form_marker = bodyStr.indexOf(JspViewHandlerImpl.FORM_STATE_MARKER, lastMarkerEnd); 182 } 183 else 184 { 185 realWriter.write(bodyStr, lastMarkerEnd, url_marker - lastMarkerEnd); 187 if (stateManager instanceof MyfacesStateManager) 188 { 189 ((MyfacesStateManager)stateManager).writeStateAsUrlParams(facesContext, 190 serializedView); 191 } 192 else 193 { 194 log.error("Current StateManager is no MyfacesStateManager and does not support saving state in url parameters."); 195 } 196 lastMarkerEnd = url_marker + HtmlLinkRendererBase.URL_STATE_MARKER_LEN; 197 url_marker = bodyStr.indexOf(HtmlLinkRendererBase.URL_STATE_MARKER, lastMarkerEnd); 198 } 199 } 200 realWriter.write(bodyStr, lastMarkerEnd, bodyStr.length() - lastMarkerEnd); 201 } 202 else 203 { 204 bodyContent.writeOut(getPreviousOut()); 205 } 206 } 207 } 208 catch (IOException e) 209 { 210 log.error("Error writing body content", e); 211 throw new JspException (e); 212 } 213 if (log.isTraceEnabled()) log.trace("leaving ViewTag.doAfterBody"); 214 return super.doAfterBody(); 215 } 216 217 protected void setProperties(UIComponent component) 218 { 219 super.setProperties(component); 220 221 if (_locale != null) 222 { 223 Locale locale; 224 if (UIComponentTag.isValueReference(_locale)) 225 { 226 FacesContext context = FacesContext.getCurrentInstance(); 227 ValueBinding vb = context.getApplication().createValueBinding(_locale); 228 Object localeValue = vb.getValue(context); 229 if (localeValue instanceof Locale ) 230 { 231 locale = (Locale ) localeValue; 232 } 233 else if (localeValue instanceof String ) 234 { 235 locale = LocaleUtils.toLocale((String ) localeValue); 236 } 237 else 238 { 239 throw new IllegalArgumentException ( 240 "Locale or String class expected. Expression: " + _locale 241 + ". Return class: " + localeValue.getClass().getName()); 242 } 243 } 244 else 245 { 246 locale = LocaleUtils.toLocale(_locale); 247 } 248 ((UIViewRoot)component).setLocale(locale); 249 Config.set((ServletRequest )getFacesContext().getExternalContext().getRequest(), 250 Config.FMT_LOCALE, 251 locale); 252 } 253 } 254 } 255 | Popular Tags |