1. /*
  2. * Copyright 2003-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. /*
  17. * $Id: SerializerTraceWriter.java,v 1.2 2004/02/17 04:18:18 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.serializer;
  20. import java.io.IOException;
  21. import java.io.Writer;
  22. /**
  23. * This class wraps the real writer, it only purpose is to send
  24. * CHARACTERTOSTREAM events to the trace listener.
  25. * Each method immediately sends the call to the wrapped writer unchanged, but
  26. * in addition it collects characters to be issued to a trace listener.
  27. *
  28. * In this way the trace
  29. * listener knows what characters have been written to the output Writer.
  30. *
  31. * There may still be differences in what the trace events say is going to the
  32. * output writer and what is really going there. These differences will be due
  33. * to the fact that this class is UTF-8 encoding before emiting the trace event
  34. * and the underlying writer may not be UTF-8 encoding. There may also be
  35. * encoding differences. So the main pupose of this class is to provide a
  36. * resonable facsimile of the true output.
  37. *
  38. */
  39. public class SerializerTraceWriter extends Writer
  40. {
  41. /** The real writer to immediately write to.
  42. * This reference may be null, in which case nothing is written out, but
  43. * only the trace events are fired for output.
  44. */
  45. private final java.io.Writer m_writer;
  46. /** The tracer to send events to */
  47. private final SerializerTrace m_tracer;
  48. /** The size of the internal buffer, just to keep too many
  49. * events from being sent to the tracer
  50. */
  51. private int buf_length;
  52. /**
  53. * Internal buffer to collect the characters to go to the trace listener.
  54. *
  55. */
  56. private byte buf[];
  57. /**
  58. * How many bytes have been collected and still need to go to trace
  59. * listener.
  60. */
  61. private int count;
  62. /**
  63. * Creates or replaces the internal buffer, and makes sure it has a few
  64. * extra bytes slight overflow of the last UTF8 encoded character.
  65. * @param size
  66. */
  67. private void setBufferSize(int size)
  68. {
  69. buf = new byte[size + 3];
  70. buf_length = size;
  71. count = 0;
  72. }
  73. /**
  74. * Constructor.
  75. * If the writer passed in is null, then this SerializerTraceWriter will
  76. * only signal trace events of what would have been written to that writer.
  77. * If the writer passed in is not null then the trace events will mirror
  78. * what is going to that writer. In this way tools, such as a debugger, can
  79. * gather information on what is being written out.
  80. *
  81. * @param out the Writer to write to (possibly null)
  82. * @param tracer the tracer to inform that characters are being written
  83. */
  84. public SerializerTraceWriter(Writer out, SerializerTrace tracer)
  85. {
  86. m_writer = out;
  87. m_tracer = tracer;
  88. setBufferSize(1024);
  89. }
  90. /**
  91. * Flush out the collected characters by sending them to the trace
  92. * listener. These characters are never written to the real writer
  93. * (m_writer) because that has already happened with every method
  94. * call. This method simple informs the listener of what has already
  95. * happened.
  96. * @throws IOException
  97. */
  98. private void flushBuffer() throws IOException
  99. {
  100. // Just for tracing purposes
  101. if (count > 0)
  102. {
  103. char[] chars = new char[count];
  104. for(int i=0; i<count; i++)
  105. chars[i] = (char) buf[i];
  106. if (m_tracer != null)
  107. m_tracer.fireGenerateEvent(
  108. SerializerTrace.EVENTTYPE_OUTPUT_CHARACTERS,
  109. chars,
  110. 0,
  111. chars.length);
  112. count = 0;
  113. }
  114. }
  115. /**
  116. * Flush the internal buffer and flush the Writer
  117. * @see java.io.Writer#flush()
  118. */
  119. public void flush() throws java.io.IOException
  120. {
  121. // send to the real writer
  122. if (m_writer != null)
  123. m_writer.flush();
  124. // from here on just for tracing purposes
  125. flushBuffer();
  126. }
  127. /**
  128. * Flush the internal buffer and close the Writer
  129. * @see java.io.Writer#close()
  130. */
  131. public void close() throws java.io.IOException
  132. {
  133. // send to the real writer
  134. if (m_writer != null)
  135. m_writer.close();
  136. // from here on just for tracing purposes
  137. flushBuffer();
  138. }
  139. /**
  140. * Write a single character. The character to be written is contained in
  141. * the 16 low-order bits of the given integer value; the 16 high-order bits
  142. * are ignored.
  143. *
  144. * <p> Subclasses that intend to support efficient single-character output
  145. * should override this method.
  146. *
  147. * @param c int specifying a character to be written.
  148. * @exception IOException If an I/O error occurs
  149. */
  150. public void write(final int c) throws IOException
  151. {
  152. // send to the real writer
  153. if (m_writer != null)
  154. m_writer.write(c);
  155. // ---------- from here on just collect for tracing purposes
  156. /* If we are close to the end of the buffer then flush it.
  157. * Remember the buffer can hold a few more characters than buf_length
  158. */
  159. if (count >= buf_length)
  160. flushBuffer();
  161. if (c < 0x80)
  162. {
  163. buf[count++] = (byte) (c);
  164. }
  165. else if (c < 0x800)
  166. {
  167. buf[count++] = (byte) (0xc0 + (c >> 6));
  168. buf[count++] = (byte) (0x80 + (c & 0x3f));
  169. }
  170. else
  171. {
  172. buf[count++] = (byte) (0xe0 + (c >> 12));
  173. buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f));
  174. buf[count++] = (byte) (0x80 + (c & 0x3f));
  175. }
  176. }
  177. /**
  178. * Write a portion of an array of characters.
  179. *
  180. * @param chars Array of characters
  181. * @param start Offset from which to start writing characters
  182. * @param length Number of characters to write
  183. *
  184. * @exception IOException If an I/O error occurs
  185. *
  186. * @throws java.io.IOException
  187. */
  188. public void write(final char chars[], final int start, final int length)
  189. throws java.io.IOException
  190. {
  191. // send to the real writer
  192. if (m_writer != null)
  193. m_writer.write(chars, start, length);
  194. // from here on just collect for tracing purposes
  195. int lengthx3 = (length << 1) + length;
  196. if (lengthx3 >= buf_length)
  197. {
  198. /* If the request length exceeds the size of the output buffer,
  199. * flush the output buffer and make the buffer bigger to handle.
  200. */
  201. flushBuffer();
  202. setBufferSize(2 * lengthx3);
  203. }
  204. if (lengthx3 > buf_length - count)
  205. {
  206. flushBuffer();
  207. }
  208. final int n = length + start;
  209. for (int i = start; i < n; i++)
  210. {
  211. final char c = chars[i];
  212. if (c < 0x80)
  213. buf[count++] = (byte) (c);
  214. else if (c < 0x800)
  215. {
  216. buf[count++] = (byte) (0xc0 + (c >> 6));
  217. buf[count++] = (byte) (0x80 + (c & 0x3f));
  218. }
  219. else
  220. {
  221. buf[count++] = (byte) (0xe0 + (c >> 12));
  222. buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f));
  223. buf[count++] = (byte) (0x80 + (c & 0x3f));
  224. }
  225. }
  226. }
  227. /**
  228. * Write a string.
  229. *
  230. * @param s String to be written
  231. *
  232. * @exception IOException If an I/O error occurs
  233. */
  234. public void write(final String s) throws IOException
  235. {
  236. // send to the real writer
  237. if (m_writer != null)
  238. m_writer.write(s);
  239. // from here on just collect for tracing purposes
  240. final int length = s.length();
  241. // We multiply the length by three since this is the maximum length
  242. // of the characters that we can put into the buffer. It is possible
  243. // for each Unicode character to expand to three bytes.
  244. int lengthx3 = (length << 1) + length;
  245. if (lengthx3 >= buf_length)
  246. {
  247. /* If the request length exceeds the size of the output buffer,
  248. * flush the output buffer and make the buffer bigger to handle.
  249. */
  250. flushBuffer();
  251. setBufferSize(2 * lengthx3);
  252. }
  253. if (lengthx3 > buf_length - count)
  254. {
  255. flushBuffer();
  256. }
  257. for (int i = 0; i < length; i++)
  258. {
  259. final char c = s.charAt(i);
  260. if (c < 0x80)
  261. buf[count++] = (byte) (c);
  262. else if (c < 0x800)
  263. {
  264. buf[count++] = (byte) (0xc0 + (c >> 6));
  265. buf[count++] = (byte) (0x80 + (c & 0x3f));
  266. }
  267. else
  268. {
  269. buf[count++] = (byte) (0xe0 + (c >> 12));
  270. buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f));
  271. buf[count++] = (byte) (0x80 + (c & 0x3f));
  272. }
  273. }
  274. }
  275. }