You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mm...@apache.org on 2006/09/19 08:20:49 UTC

svn commit: r447758 - /myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/HtmlBufferResponseWriterWrapper.java

Author: mmarinschek
Date: Mon Sep 18 23:20:49 2006
New Revision: 447758

URL: http://svn.apache.org/viewvc?view=rev&rev=447758
Log:
proposed fix for [TOMAHAWK-648]   Buffer component doesn't support any apecial characters and [TOMAHAWK-45]

Modified:
    myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/HtmlBufferResponseWriterWrapper.java

Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/HtmlBufferResponseWriterWrapper.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/HtmlBufferResponseWriterWrapper.java?view=diff&rev=447758&r1=447757&r2=447758
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/HtmlBufferResponseWriterWrapper.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/HtmlBufferResponseWriterWrapper.java Mon Sep 18 23:20:49 2006
@@ -16,49 +16,81 @@
 package org.apache.myfaces.renderkit.html.util;
 
 import javax.faces.context.ResponseWriter;
-import java.io.ByteArrayOutputStream;
-import java.io.PrintWriter;
-import java.io.UnsupportedEncodingException;
-import java.io.IOException;
+import java.io.*;
 
-/**
+/**A buffer for content which should not directly be rendered to the page.
+ *
  * @author Sylvain Vieujot (latest modification by $Author: grantsmith $)
  * @version $Revision: 169649 $ $Date: 2005-05-11 17:47:12 +0200 (Wed, 11 May 2005) $
  */
-public class HtmlBufferResponseWriterWrapper extends org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlResponseWriterImpl {
+public class HtmlBufferResponseWriterWrapper extends org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlResponseWriterImpl
+{
 
-    private ByteArrayOutputStream stream;
-    private PrintWriter writer;
+    /**
+     * Buffer writer to write content to and buffer it.
+     *
+     * Moved from OutputStream to Writer to account for issue
+     * TOMAHAWK-648.
+     */
+    private StringWriter bufferWriter;
+    /**
+     * Writer to wrap buffer-writer.
+     */
+    private PrintWriter wrapperWriter;
+    /**
+     * Original response writer.
+     */
     private ResponseWriter initialWriter;
 
-    public ResponseWriter getInitialWriter() {
+
+    /**Get the writer that should have originally been written to.
+     *
+     * @return The original writer.
+     */
+    public ResponseWriter getInitialWriter()
+    {
         return initialWriter;
     }
 
-    static public HtmlBufferResponseWriterWrapper getInstance(ResponseWriter initialWriter){
-        ByteArrayOutputStream instanceStream = new ByteArrayOutputStream();
-        PrintWriter instanceWriter = new PrintWriter(instanceStream, true);
+    /**Create an instance of the HtmlBufferResponseWriterWrapper
+     *
+     * @param initialWriter The writer the content should have originally gone to, this will only be used to copy settings.
+     * @return A properly initialized writer which stores the output in a buffer; writer is wrapped.
+     */
+    static public HtmlBufferResponseWriterWrapper getInstance(ResponseWriter initialWriter)
+    {
+        StringWriter bufferWriter = new StringWriter();
+        PrintWriter wrapperWriter = new PrintWriter(bufferWriter, true);
+
+        return new HtmlBufferResponseWriterWrapper(initialWriter, bufferWriter, wrapperWriter);
 
-        return new HtmlBufferResponseWriterWrapper(initialWriter, instanceStream, instanceWriter);
     }
 
-    private HtmlBufferResponseWriterWrapper(ResponseWriter initialWriter, ByteArrayOutputStream stream, PrintWriter writer){
-        super(writer, initialWriter==null?null:initialWriter.getContentType(), initialWriter==null?null:initialWriter.getCharacterEncoding());
-        this.stream = stream;
-        this.writer = writer;
+    /**Constructor for the HtmlBufferResponseWriterWrapper.
+     *
+     * @param initialWriter The writer the content should have originally gone to, this will only be used to copy settings.
+     * @param bufferWriter A buffer to store content to.
+     * @param wrapperWriter A wrapper around the buffer.
+     */
+    private HtmlBufferResponseWriterWrapper(ResponseWriter initialWriter, StringWriter bufferWriter, PrintWriter wrapperWriter)
+    {
+        super(wrapperWriter, (initialWriter == null) ? null : initialWriter.getContentType(),
+                (initialWriter == null) ? null : initialWriter.getCharacterEncoding());
+
+        this.bufferWriter = bufferWriter;
+        this.wrapperWriter = wrapperWriter;
         this.initialWriter = initialWriter;
     }
 
-    public String toString(){
-        try{
-            stream.flush();
-            writer.close();           
-            return stream.toString( getCharacterEncoding() );
-        }catch(UnsupportedEncodingException e){
-            // an attempt to set an invalid character encoding would have caused this exception before
-            throw new RuntimeException("ResponseWriter accepted invalid character encoding " + getCharacterEncoding());
-        } catch (IOException e) {
-            throw new RuntimeException( e );
-        }
+
+    /**Get the content of the buffer.
+     *
+     * @return The content of the buffered and wrapped writer.
+     */
+    public String toString()
+    {
+        wrapperWriter.flush();
+        wrapperWriter.close();
+        return bufferWriter.toString();
     }
 }