6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 2003-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
/*
|
|
21 |
* $Id: SerializerTraceWriter.java,v 1.2.4.1 2005/09/15 08:15:25 suresh_emailid Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xml.internal.serializer;
|
|
24 |
|
|
25 |
import java.io.IOException;
|
|
26 |
import java.io.OutputStream;
|
|
27 |
import java.io.Writer;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* This class wraps the real writer, it only purpose is to send
|
|
31 |
* CHARACTERTOSTREAM events to the trace listener.
|
|
32 |
* Each method immediately sends the call to the wrapped writer unchanged, but
|
|
33 |
* in addition it collects characters to be issued to a trace listener.
|
|
34 |
*
|
|
35 |
* In this way the trace
|
|
36 |
* listener knows what characters have been written to the output Writer.
|
|
37 |
*
|
|
38 |
* There may still be differences in what the trace events say is going to the
|
|
39 |
* output writer and what is really going there. These differences will be due
|
|
40 |
* to the fact that this class is UTF-8 encoding before emiting the trace event
|
|
41 |
* and the underlying writer may not be UTF-8 encoding. There may also be
|
|
42 |
* encoding differences. So the main pupose of this class is to provide a
|
|
43 |
* resonable facsimile of the true output.
|
|
44 |
*
|
|
45 |
* @xsl.usage internal
|
|
46 |
*/
|
|
47 |
final class SerializerTraceWriter extends Writer implements WriterChain
|
|
48 |
{
|
|
49 |
|
|
50 |
/** The real writer to immediately write to.
|
|
51 |
* This reference may be null, in which case nothing is written out, but
|
|
52 |
* only the trace events are fired for output.
|
|
53 |
*/
|
|
54 |
private final java.io.Writer m_writer;
|
|
55 |
|
|
56 |
/** The tracer to send events to */
|
|
57 |
private final SerializerTrace m_tracer;
|
|
58 |
|
|
59 |
/** The size of the internal buffer, just to keep too many
|
|
60 |
* events from being sent to the tracer
|
|
61 |
*/
|
|
62 |
private int buf_length;
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Internal buffer to collect the characters to go to the trace listener.
|
|
66 |
*
|
|
67 |
*/
|
|
68 |
private byte buf[];
|
|
69 |
|
|
70 |
/**
|
|
71 |
* How many bytes have been collected and still need to go to trace
|
|
72 |
* listener.
|
|
73 |
*/
|
|
74 |
private int count;
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Creates or replaces the internal buffer, and makes sure it has a few
|
|
78 |
* extra bytes slight overflow of the last UTF8 encoded character.
|
|
79 |
* @param size
|
|
80 |
*/
|
|
81 |
private void setBufferSize(int size)
|
|
82 |
{
|
|
83 |
buf = new byte[size + 3];
|
|
84 |
buf_length = size;
|
|
85 |
count = 0;
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Constructor.
|
|
90 |
* If the writer passed in is null, then this SerializerTraceWriter will
|
|
91 |
* only signal trace events of what would have been written to that writer.
|
|
92 |
* If the writer passed in is not null then the trace events will mirror
|
|
93 |
* what is going to that writer. In this way tools, such as a debugger, can
|
|
94 |
* gather information on what is being written out.
|
|
95 |
*
|
|
96 |
* @param out the Writer to write to (possibly null)
|
|
97 |
* @param tracer the tracer to inform that characters are being written
|
|
98 |
*/
|
|
99 |
public SerializerTraceWriter(Writer out, SerializerTrace tracer)
|
|
100 |
{
|
|
101 |
m_writer = out;
|
|
102 |
m_tracer = tracer;
|
|
103 |
setBufferSize(1024);
|
|
104 |
}
|
|
105 |
|
|
106 |
/**
|
|
107 |
* Flush out the collected characters by sending them to the trace
|
|
108 |
* listener. These characters are never written to the real writer
|
|
109 |
* (m_writer) because that has already happened with every method
|
|
110 |
* call. This method simple informs the listener of what has already
|
|
111 |
* happened.
|
|
112 |
* @throws IOException
|
|
113 |
*/
|
|
114 |
private void flushBuffer() throws IOException
|
|
115 |
{
|
|
116 |
|
|
117 |
// Just for tracing purposes
|
|
118 |
if (count > 0)
|
|
119 |
{
|
|
120 |
char[] chars = new char[count];
|
|
121 |
for(int i=0; i<count; i++)
|
|
122 |
chars[i] = (char) buf[i];
|
|
123 |
|
|
124 |
if (m_tracer != null)
|
|
125 |
m_tracer.fireGenerateEvent(
|
|
126 |
SerializerTrace.EVENTTYPE_OUTPUT_CHARACTERS,
|
|
127 |
chars,
|
|
128 |
0,
|
|
129 |
chars.length);
|
|
130 |
|
|
131 |
count = 0;
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* Flush the internal buffer and flush the Writer
|
|
137 |
* @see java.io.Writer#flush()
|
|
138 |
*/
|
|
139 |
public void flush() throws java.io.IOException
|
|
140 |
{
|
|
141 |
// send to the real writer
|
|
142 |
if (m_writer != null)
|
|
143 |
m_writer.flush();
|
|
144 |
|
|
145 |
// from here on just for tracing purposes
|
|
146 |
flushBuffer();
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Flush the internal buffer and close the Writer
|
|
151 |
* @see java.io.Writer#close()
|
|
152 |
*/
|
|
153 |
public void close() throws java.io.IOException
|
|
154 |
{
|
|
155 |
// send to the real writer
|
|
156 |
if (m_writer != null)
|
|
157 |
m_writer.close();
|
|
158 |
|
|
159 |
// from here on just for tracing purposes
|
|
160 |
flushBuffer();
|
|
161 |
}
|
|
162 |
|
|
163 |
|
|
164 |
/**
|
|
165 |
* Write a single character. The character to be written is contained in
|
|
166 |
* the 16 low-order bits of the given integer value; the 16 high-order bits
|
|
167 |
* are ignored.
|
|
168 |
*
|
|
169 |
* <p> Subclasses that intend to support efficient single-character output
|
|
170 |
* should override this method.
|
|
171 |
*
|
|
172 |
* @param c int specifying a character to be written.
|
|
173 |
* @exception IOException If an I/O error occurs
|
|
174 |
*/
|
|
175 |
public void write(final int c) throws IOException
|
|
176 |
{
|
|
177 |
// send to the real writer
|
|
178 |
if (m_writer != null)
|
|
179 |
m_writer.write(c);
|
|
180 |
|
|
181 |
// ---------- from here on just collect for tracing purposes
|
|
182 |
|
|
183 |
/* If we are close to the end of the buffer then flush it.
|
|
184 |
* Remember the buffer can hold a few more characters than buf_length
|
|
185 |
*/
|
|
186 |
if (count >= buf_length)
|
|
187 |
flushBuffer();
|
|
188 |
|
|
189 |
if (c < 0x80)
|
|
190 |
{
|
|
191 |
buf[count++] = (byte) (c);
|
|
192 |
}
|
|
193 |
else if (c < 0x800)
|
|
194 |
{
|
|
195 |
buf[count++] = (byte) (0xc0 + (c >> 6));
|
|
196 |
buf[count++] = (byte) (0x80 + (c & 0x3f));
|
|
197 |
}
|
|
198 |
else
|
|
199 |
{
|
|
200 |
buf[count++] = (byte) (0xe0 + (c >> 12));
|
|
201 |
buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f));
|
|
202 |
buf[count++] = (byte) (0x80 + (c & 0x3f));
|
|
203 |
}
|
|
204 |
}
|
|
205 |
|
|
206 |
/**
|
|
207 |
* Write a portion of an array of characters.
|
|
208 |
*
|
|
209 |
* @param chars Array of characters
|
|
210 |
* @param start Offset from which to start writing characters
|
|
211 |
* @param length Number of characters to write
|
|
212 |
*
|
|
213 |
* @exception IOException If an I/O error occurs
|
|
214 |
*
|
|
215 |
* @throws java.io.IOException
|
|
216 |
*/
|
|
217 |
public void write(final char chars[], final int start, final int length)
|
|
218 |
throws java.io.IOException
|
|
219 |
{
|
|
220 |
// send to the real writer
|
|
221 |
if (m_writer != null)
|
|
222 |
m_writer.write(chars, start, length);
|
|
223 |
|
|
224 |
// from here on just collect for tracing purposes
|
|
225 |
int lengthx3 = (length << 1) + length;
|
|
226 |
|
|
227 |
if (lengthx3 >= buf_length)
|
|
228 |
{
|
|
229 |
|
|
230 |
/* If the request length exceeds the size of the output buffer,
|
|
231 |
* flush the output buffer and make the buffer bigger to handle.
|
|
232 |
*/
|
|
233 |
|
|
234 |
flushBuffer();
|
|
235 |
setBufferSize(2 * lengthx3);
|
|
236 |
|
|
237 |
}
|
|
238 |
|
|
239 |
if (lengthx3 > buf_length - count)
|
|
240 |
{
|
|
241 |
flushBuffer();
|
|
242 |
}
|
|
243 |
|
|
244 |
final int n = length + start;
|
|
245 |
for (int i = start; i < n; i++)
|
|
246 |
{
|
|
247 |
final char c = chars[i];
|
|
248 |
|
|
249 |
if (c < 0x80)
|
|
250 |
buf[count++] = (byte) (c);
|
|
251 |
else if (c < 0x800)
|
|
252 |
{
|
|
253 |
buf[count++] = (byte) (0xc0 + (c >> 6));
|
|
254 |
buf[count++] = (byte) (0x80 + (c & 0x3f));
|
|
255 |
}
|
|
256 |
else
|
|
257 |
{
|
|
258 |
buf[count++] = (byte) (0xe0 + (c >> 12));
|
|
259 |
buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f));
|
|
260 |
buf[count++] = (byte) (0x80 + (c & 0x3f));
|
|
261 |
}
|
|
262 |
}
|
|
263 |
|
|
264 |
}
|
|
265 |
|
|
266 |
/**
|
|
267 |
* Write a string.
|
|
268 |
*
|
|
269 |
* @param s String to be written
|
|
270 |
*
|
|
271 |
* @exception IOException If an I/O error occurs
|
|
272 |
*/
|
|
273 |
public void write(final String s) throws IOException
|
|
274 |
{
|
|
275 |
// send to the real writer
|
|
276 |
if (m_writer != null)
|
|
277 |
m_writer.write(s);
|
|
278 |
|
|
279 |
// from here on just collect for tracing purposes
|
|
280 |
final int length = s.length();
|
|
281 |
|
|
282 |
// We multiply the length by three since this is the maximum length
|
|
283 |
// of the characters that we can put into the buffer. It is possible
|
|
284 |
// for each Unicode character to expand to three bytes.
|
|
285 |
|
|
286 |
int lengthx3 = (length << 1) + length;
|
|
287 |
|
|
288 |
if (lengthx3 >= buf_length)
|
|
289 |
{
|
|
290 |
|
|
291 |
/* If the request length exceeds the size of the output buffer,
|
|
292 |
* flush the output buffer and make the buffer bigger to handle.
|
|
293 |
*/
|
|
294 |
|
|
295 |
flushBuffer();
|
|
296 |
setBufferSize(2 * lengthx3);
|
|
297 |
}
|
|
298 |
|
|
299 |
if (lengthx3 > buf_length - count)
|
|
300 |
{
|
|
301 |
flushBuffer();
|
|
302 |
}
|
|
303 |
|
|
304 |
for (int i = 0; i < length; i++)
|
|
305 |
{
|
|
306 |
final char c = s.charAt(i);
|
|
307 |
|
|
308 |
if (c < 0x80)
|
|
309 |
buf[count++] = (byte) (c);
|
|
310 |
else if (c < 0x800)
|
|
311 |
{
|
|
312 |
buf[count++] = (byte) (0xc0 + (c >> 6));
|
|
313 |
buf[count++] = (byte) (0x80 + (c & 0x3f));
|
|
314 |
}
|
|
315 |
else
|
|
316 |
{
|
|
317 |
buf[count++] = (byte) (0xe0 + (c >> 12));
|
|
318 |
buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f));
|
|
319 |
buf[count++] = (byte) (0x80 + (c & 0x3f));
|
|
320 |
}
|
|
321 |
}
|
|
322 |
}
|
|
323 |
|
|
324 |
/**
|
|
325 |
* Get the writer that this one directly wraps.
|
|
326 |
*/
|
|
327 |
public Writer getWriter()
|
|
328 |
{
|
|
329 |
return m_writer;
|
|
330 |
}
|
|
331 |
|
|
332 |
/**
|
|
333 |
* Get the OutputStream that is the at the end of the
|
|
334 |
* chain of writers.
|
|
335 |
*/
|
|
336 |
public OutputStream getOutputStream()
|
|
337 |
{
|
|
338 |
OutputStream retval = null;
|
|
339 |
if (m_writer instanceof WriterChain)
|
|
340 |
retval = ((WriterChain) m_writer).getOutputStream();
|
|
341 |
return retval;
|
|
342 |
}
|
|
343 |
}
|