KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > buffer > BufferRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.buffer;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.io.UnsupportedEncodingException JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import javax.faces.component.UIComponent;
26 import javax.faces.context.FacesContext;
27 import javax.faces.context.ResponseWriter;
28 import javax.faces.render.Renderer;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.myfaces.renderkit.RendererUtils;
33 import org.apache.myfaces.renderkit.html.HtmlResponseWriterImpl;
34
35 /**
36  * @author Sylvain Vieujot (latest modification by $Author: svieujot $)
37  * @version $Revision: 1.3 $ $Date: 2005/02/01 16:54:07 $
38  * $Log: BufferRenderer.java,v $
39  * Revision 1.3 2005/02/01 16:54:07 svieujot
40  * Clean up.
41  *
42  * Revision 1.2 2005/02/01 16:51:48 svieujot
43  * Move the HtmlResponseWriterImpl to the shared sources directory.
44  *
45  * Revision 1.1 2005/01/04 15:41:06 svieujot
46  * new x:buffer component.
47  *
48  */

49 public class BufferRenderer extends Renderer {
50     private static final Log log = LogFactory.getLog(BufferRenderer.class);
51     
52     public static final String JavaDoc RENDERER_TYPE = "org.apache.myfaces.Buffer";
53
54     private ResponseWriter initialWriter;
55     private HtmlBufferResponseWriterWrapper bufferWriter;
56     
57     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) {
58         RendererUtils.checkParamValidity(facesContext, uiComponent, Buffer.class);
59
60         initialWriter = facesContext.getResponseWriter();
61         bufferWriter = HtmlBufferResponseWriterWrapper.getInstance( initialWriter );
62         facesContext.setResponseWriter( bufferWriter );
63     }
64     
65     public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException JavaDoc{
66         RendererUtils.checkParamValidity(facesContext, component, Buffer.class);
67         RendererUtils.renderChildren(facesContext, component);
68     }
69
70     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) {
71         Buffer buffer = (Buffer)uiComponent;
72         buffer.fill(bufferWriter, facesContext);
73         
74         if( bufferWriter.getDummyFormParams() != null ){
75             // Attempt to add the dummy form params (will not work with Sun RI) using reflexion.
76
try {
77                 Method JavaDoc add = initialWriter.getClass().getDeclaredMethod("addDummyFormParameter", new Class JavaDoc[] {String JavaDoc.class});
78                 for(Iterator JavaDoc i = bufferWriter.getDummyFormParams().iterator() ; i.hasNext() ;){
79                     add.invoke(initialWriter, new Object JavaDoc[] {i.next()});
80                 }
81             } catch (Exception JavaDoc e) {
82                 log.warn("Dummy form parameters are not supported by this JSF implementation.");
83             }
84         }
85         
86         facesContext.setResponseWriter( initialWriter );
87     }
88     
89     private static class HtmlBufferResponseWriterWrapper extends HtmlResponseWriterImpl {
90         
91         private ByteArrayOutputStream JavaDoc stream;
92         private PrintWriter JavaDoc writer;
93         
94         static public HtmlBufferResponseWriterWrapper getInstance(ResponseWriter initialWriter){
95             ByteArrayOutputStream JavaDoc instanceStream = new ByteArrayOutputStream JavaDoc();
96             PrintWriter JavaDoc instanceWriter = new PrintWriter JavaDoc(instanceStream, true);
97             
98             return new HtmlBufferResponseWriterWrapper(initialWriter, instanceStream, instanceWriter);
99         }
100         
101         private HtmlBufferResponseWriterWrapper(ResponseWriter initialWriter, ByteArrayOutputStream JavaDoc stream, PrintWriter JavaDoc writer){
102             super(writer, initialWriter.getContentType(), initialWriter.getCharacterEncoding());
103             this.stream = stream;
104             this.writer = writer;
105         }
106         
107         public String JavaDoc toString(){
108             try{
109                 stream.flush();
110                 writer.close();
111                 
112                 return stream.toString( getCharacterEncoding() );
113             }catch(UnsupportedEncodingException JavaDoc e){
114                 // an attempt to set an invalid character encoding would have caused this exception before
115
throw new RuntimeException JavaDoc("ResponseWriter accepted invalid character encoding " + getCharacterEncoding());
116             } catch (IOException JavaDoc e) {
117                 throw new RuntimeException JavaDoc( e );
118             }
119         }
120     }
121 }
Popular Tags