View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.buffer;
20  
21  import java.io.IOException;
22  import java.util.Iterator;
23  
24  import javax.faces.component.UIComponent;
25  import javax.faces.context.FacesContext;
26  import javax.faces.render.Renderer;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.myfaces.renderkit.html.util.DummyFormUtils;
31  import org.apache.myfaces.renderkit.html.util.HtmlBufferResponseWriterWrapper;
32  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
33  
34  /**
35   * 
36   * @JSFRenderer
37   *   renderKitId = "HTML_BASIC"
38   *   family = "javax.faces.Data"
39   *   type = "org.apache.myfaces.Buffer" 
40   * 
41   * @author Sylvain Vieujot (latest modification by $Author: lu4242 $)
42   * @version $Revision: 659874 $ $Date: 2008-05-24 15:59:15 -0500 (Sat, 24 May 2008) $
43   */
44  public class BufferRenderer extends Renderer {
45      private static final Log log = LogFactory.getLog(BufferRenderer.class);
46  
47      public static final String RENDERER_TYPE = "org.apache.myfaces.Buffer";
48  
49      private HtmlBufferResponseWriterWrapper getResponseWriter(FacesContext context) {
50          return HtmlBufferResponseWriterWrapper.getInstance(context.getResponseWriter());
51      }
52  
53      public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) {
54          RendererUtils.checkParamValidity(facesContext, uiComponent, Buffer.class);
55          facesContext.setResponseWriter( getResponseWriter(facesContext) );
56      }
57  
58      public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException{
59          RendererUtils.checkParamValidity(facesContext, component, Buffer.class);
60          RendererUtils.renderChildren(facesContext, component);
61      }
62  
63      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) {
64          Buffer buffer = (Buffer)uiComponent;
65          HtmlBufferResponseWriterWrapper writer = (HtmlBufferResponseWriterWrapper) facesContext.getResponseWriter();
66          buffer.fill(writer.toString(), facesContext);
67  
68          facesContext.setResponseWriter( writer.getInitialWriter() );
69  
70          if( DummyFormUtils.getDummyFormParameters(facesContext) != null ){
71              try{ // Attempt to add the dummy form params (will not work with Sun RI)
72                  if( DummyFormUtils.isWriteDummyForm(facesContext) )
73                      DummyFormUtils.setWriteDummyForm(facesContext, true );
74                  for(Iterator i = DummyFormUtils.getDummyFormParameters(facesContext).iterator() ; i.hasNext() ;)
75                      DummyFormUtils.addDummyFormParameter(facesContext, i.next().toString() );
76              } catch (Exception e) {
77                  log.warn("Dummy form parameters are not supported by this JSF implementation.");
78              }
79          }
80      }
81  
82  }