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 "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 * $Id: SerializerTraceWriter.java 468654 2006-10-28 07:09:23Z minchau $
21 package org.apache.xml.serializer;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.io.Writer;
28 * This class wraps the real writer, it only purpose is to send
29 * CHARACTERTOSTREAM events to the trace listener.
30 * Each method immediately sends the call to the wrapped writer unchanged, but
31 * in addition it collects characters to be issued to a trace listener.
33 * In this way the trace
34 * listener knows what characters have been written to the output Writer.
36 * There may still be differences in what the trace events say is going to the
37 * output writer and what is really going there. These differences will be due
38 * to the fact that this class is UTF-8 encoding before emiting the trace event
39 * and the underlying writer may not be UTF-8 encoding. There may also be
40 * encoding differences. So the main pupose of this class is to provide a
41 * resonable facsimile of the true output.
45 final class SerializerTraceWriter extends Writer implements WriterChain
48 /** The real writer to immediately write to.
49 * This reference may be null, in which case nothing is written out, but
50 * only the trace events are fired for output.
52 private final java.io.Writer m_writer;
54 /** The tracer to send events to */
55 private final SerializerTrace m_tracer;
57 /** The size of the internal buffer, just to keep too many
58 * events from being sent to the tracer
60 private int buf_length;
63 * Internal buffer to collect the characters to go to the trace listener.
69 * How many bytes have been collected and still need to go to trace
75 * Creates or replaces the internal buffer, and makes sure it has a few
76 * extra bytes slight overflow of the last UTF8 encoded character.
79 private void setBufferSize(int size)
81 buf = new byte[size + 3];
88 * If the writer passed in is null, then this SerializerTraceWriter will
89 * only signal trace events of what would have been written to that writer.
90 * If the writer passed in is not null then the trace events will mirror
91 * what is going to that writer. In this way tools, such as a debugger, can
92 * gather information on what is being written out.
94 * @param out the Writer to write to (possibly null)
95 * @param tracer the tracer to inform that characters are being written
97 public SerializerTraceWriter(Writer out, SerializerTrace tracer)
105 * Flush out the collected characters by sending them to the trace
106 * listener. These characters are never written to the real writer
107 * (m_writer) because that has already happened with every method
108 * call. This method simple informs the listener of what has already
110 * @throws IOException
112 private void flushBuffer() throws IOException
115 // Just for tracing purposes
118 char[] chars = new char[count];
119 for(int i=0; i<count; i++)
120 chars[i] = (char) buf[i];
122 if (m_tracer != null)
123 m_tracer.fireGenerateEvent(
124 SerializerTrace.EVENTTYPE_OUTPUT_CHARACTERS,
134 * Flush the internal buffer and flush the Writer
135 * @see java.io.Writer#flush()
137 public void flush() throws java.io.IOException
139 // send to the real writer
140 if (m_writer != null)
143 // from here on just for tracing purposes
148 * Flush the internal buffer and close the Writer
149 * @see java.io.Writer#close()
151 public void close() throws java.io.IOException
153 // send to the real writer
154 if (m_writer != null)
157 // from here on just for tracing purposes
163 * Write a single character. The character to be written is contained in
164 * the 16 low-order bits of the given integer value; the 16 high-order bits
167 * <p> Subclasses that intend to support efficient single-character output
168 * should override this method.
170 * @param c int specifying a character to be written.
171 * @exception IOException If an I/O error occurs
173 public void write(final int c) throws IOException
175 // send to the real writer
176 if (m_writer != null)
179 // ---------- from here on just collect for tracing purposes
181 /* If we are close to the end of the buffer then flush it.
182 * Remember the buffer can hold a few more characters than buf_length
184 if (count >= buf_length)
189 buf[count++] = (byte) (c);
193 buf[count++] = (byte) (0xc0 + (c >> 6));
194 buf[count++] = (byte) (0x80 + (c & 0x3f));
198 buf[count++] = (byte) (0xe0 + (c >> 12));
199 buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f));
200 buf[count++] = (byte) (0x80 + (c & 0x3f));
205 * Write a portion of an array of characters.
207 * @param chars Array of characters
208 * @param start Offset from which to start writing characters
209 * @param length Number of characters to write
211 * @exception IOException If an I/O error occurs
213 * @throws java.io.IOException
215 public void write(final char chars[], final int start, final int length)
216 throws java.io.IOException
218 // send to the real writer
219 if (m_writer != null)
220 m_writer.write(chars, start, length);
222 // from here on just collect for tracing purposes
223 int lengthx3 = (length << 1) + length;
225 if (lengthx3 >= buf_length)
228 /* If the request length exceeds the size of the output buffer,
229 * flush the output buffer and make the buffer bigger to handle.
233 setBufferSize(2 * lengthx3);
237 if (lengthx3 > buf_length - count)
242 final int n = length + start;
243 for (int i = start; i < n; i++)
245 final char c = chars[i];
248 buf[count++] = (byte) (c);
251 buf[count++] = (byte) (0xc0 + (c >> 6));
252 buf[count++] = (byte) (0x80 + (c & 0x3f));
256 buf[count++] = (byte) (0xe0 + (c >> 12));
257 buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f));
258 buf[count++] = (byte) (0x80 + (c & 0x3f));
267 * @param s String to be written
269 * @exception IOException If an I/O error occurs
271 public void write(final String s) throws IOException
273 // send to the real writer
274 if (m_writer != null)
277 // from here on just collect for tracing purposes
278 final int length = s.length();
280 // We multiply the length by three since this is the maximum length
281 // of the characters that we can put into the buffer. It is possible
282 // for each Unicode character to expand to three bytes.
284 int lengthx3 = (length << 1) + length;
286 if (lengthx3 >= buf_length)
289 /* If the request length exceeds the size of the output buffer,
290 * flush the output buffer and make the buffer bigger to handle.
294 setBufferSize(2 * lengthx3);
297 if (lengthx3 > buf_length - count)
302 for (int i = 0; i < length; i++)
304 final char c = s.charAt(i);
307 buf[count++] = (byte) (c);
310 buf[count++] = (byte) (0xc0 + (c >> 6));
311 buf[count++] = (byte) (0x80 + (c & 0x3f));
315 buf[count++] = (byte) (0xe0 + (c >> 12));
316 buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f));
317 buf[count++] = (byte) (0x80 + (c & 0x3f));
323 * Get the writer that this one directly wraps.
325 public Writer getWriter()
331 * Get the OutputStream that is the at the end of the
334 public OutputStream getOutputStream()
336 OutputStream retval = null;
337 if (m_writer instanceof WriterChain)
338 retval = ((WriterChain) m_writer).getOutputStream();