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.renderkit.html.util;
20
21 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlResponseWriterImpl;
22
23 import javax.faces.context.ResponseWriter;
24 import java.io.*;
25
26 /**A buffer for content which should not directly be rendered to the page.
27 *
28 * @author Sylvain Vieujot (latest modification by $Author: grantsmith $)
29 * @version $Revision: 169649 $ $Date: 2005-05-11 17:47:12 +0200 (Wed, 11 May 2005) $
30 */
31 public class HtmlBufferResponseWriterWrapper extends HtmlResponseWriterImpl {
32
33 /**
34 * Buffer writer to write content to and buffer it.
35 *
36 * Moved from OutputStream to Writer to account for issue
37 * TOMAHAWK-648.
38 */
39 private StringWriter bufferWriter;
40 /**
41 * Writer to wrap buffer-writer.
42 */
43 private PrintWriter wrapperWriter;
44 /**
45 * Original response writer.
46 */
47 private ResponseWriter initialWriter;
48
49
50 /**Get the writer that should have originally been written to.
51 *
52 * @return The original writer.
53 */
54 public ResponseWriter getInitialWriter()
55 {
56 return initialWriter;
57 }
58
59 /**Create an instance of the HtmlBufferResponseWriterWrapper
60 *
61 * @param initialWriter The writer the content should have originally gone to, this will only be used to copy settings.
62 * @return A properly initialized writer which stores the output in a buffer; writer is wrapped.
63 */
64 static public HtmlBufferResponseWriterWrapper getInstance(ResponseWriter initialWriter)
65 {
66 StringWriter bufferWriter = new StringWriter();
67 PrintWriter wrapperWriter = new PrintWriter(bufferWriter, true);
68
69 return new HtmlBufferResponseWriterWrapper(initialWriter, bufferWriter, wrapperWriter);
70
71 }
72
73 /**Constructor for the HtmlBufferResponseWriterWrapper.
74 *
75 * @param initialWriter The writer the content should have originally gone to, this will only be used to copy settings.
76 * @param bufferWriter A buffer to store content to.
77 * @param wrapperWriter A wrapper around the buffer.
78 */
79 private HtmlBufferResponseWriterWrapper(ResponseWriter initialWriter, StringWriter bufferWriter, PrintWriter wrapperWriter)
80 {
81 super(wrapperWriter, (initialWriter == null) ? null : initialWriter.getContentType(),
82 (initialWriter == null) ? null : initialWriter.getCharacterEncoding());
83
84 this.bufferWriter = bufferWriter;
85 this.wrapperWriter = wrapperWriter;
86 this.initialWriter = initialWriter;
87 }
88
89
90 /**Get the content of the buffer.
91 *
92 * @return The content of the buffered and wrapped writer.
93 */
94 public String toString()
95 {
96 wrapperWriter.flush();
97 wrapperWriter.close();
98 return bufferWriter.toString();
99 }
100 }