1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. // Sep 14, 2000:
  58. // Fixed serializer to report IO exception directly, instead at
  59. // the end of document processing.
  60. // Reported by Patrick Higgins <phiggins@transzap.com>
  61. package com.sun.org.apache.xml.internal.serialize;
  62. import java.io.Writer;
  63. import java.io.StringWriter;
  64. import java.io.IOException;
  65. /**
  66. * The printer is responsible for sending text to the output stream
  67. * or writer. This class performs direct writing for efficiency.
  68. * {@link IndentPrinter} supports indentation and line wrapping by
  69. * extending this class.
  70. *
  71. * @version $Revision: 1.2 $ $Date: 2003/11/18 00:23:04 $
  72. * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
  73. */
  74. public class Printer
  75. {
  76. /**
  77. * The output format associated with this serializer. This will never
  78. * be a null reference. If no format was passed to the constructor,
  79. * the default one for this document type will be used. The format
  80. * object is never changed by the serializer.
  81. */
  82. protected final OutputFormat _format;
  83. /**
  84. * The writer to which the document is written.
  85. */
  86. protected Writer _writer;
  87. /**
  88. * The DTD writer. When we switch to DTD mode, all output is
  89. * accumulated in this DTD writer. When we switch out of it,
  90. * the output is obtained as a string. Must not be reset to
  91. * null until we're done with the document.
  92. */
  93. protected StringWriter _dtdWriter;
  94. /**
  95. * Holds a reference to the document writer while we are
  96. * in DTD mode.
  97. */
  98. protected Writer _docWriter;
  99. /**
  100. * Holds the exception thrown by the serializer. Exceptions do not cause
  101. * the serializer to quit, but are held and one is thrown at the end.
  102. */
  103. protected IOException _exception;
  104. /**
  105. * The size of the output buffer.
  106. */
  107. private static final int BufferSize = 4096;
  108. /**
  109. * Output buffer.
  110. */
  111. private final char[] _buffer = new char[ BufferSize ];
  112. /**
  113. * Position within the output buffer.
  114. */
  115. private int _pos = 0;
  116. public Printer( Writer writer, OutputFormat format)
  117. {
  118. _writer = writer;
  119. _format = format;
  120. _exception = null;
  121. _dtdWriter = null;
  122. _docWriter = null;
  123. _pos = 0;
  124. }
  125. public IOException getException()
  126. {
  127. return _exception;
  128. }
  129. /**
  130. * Called by any of the DTD handlers to enter DTD mode.
  131. * Once entered, all output will be accumulated in a string
  132. * that can be printed as part of the document's DTD.
  133. * This method may be called any number of time but will only
  134. * have affect the first time it's called. To exist DTD state
  135. * and get the accumulated DTD, call {@link #leaveDTD}.
  136. */
  137. public void enterDTD()
  138. throws IOException
  139. {
  140. // Can only enter DTD state once. Once we're out of DTD
  141. // state, can no longer re-enter it.
  142. if ( _dtdWriter == null ) {
  143. flushLine( false );
  144. _dtdWriter = new StringWriter();
  145. _docWriter = _writer;
  146. _writer = _dtdWriter;
  147. }
  148. }
  149. /**
  150. * Called by the root element to leave DTD mode and if any
  151. * DTD parts were printer, will return a string with their
  152. * textual content.
  153. */
  154. public String leaveDTD()
  155. throws IOException
  156. {
  157. // Only works if we're going out of DTD mode.
  158. if ( _writer == _dtdWriter ) {
  159. flushLine( false );
  160. _writer = _docWriter;
  161. return _dtdWriter.toString();
  162. } else
  163. return null;
  164. }
  165. public void printText( String text )
  166. throws IOException
  167. {
  168. try {
  169. int length = text.length();
  170. for ( int i = 0 ; i < length ; ++i ) {
  171. if ( _pos == BufferSize ) {
  172. _writer.write( _buffer );
  173. _pos = 0;
  174. }
  175. _buffer[ _pos ] = text.charAt( i );
  176. ++_pos;
  177. }
  178. } catch ( IOException except ) {
  179. // We don't throw an exception, but hold it
  180. // until the end of the document.
  181. if ( _exception == null )
  182. _exception = except;
  183. throw except;
  184. }
  185. }
  186. public void printText( StringBuffer text )
  187. throws IOException
  188. {
  189. try {
  190. int length = text.length();
  191. for ( int i = 0 ; i < length ; ++i ) {
  192. if ( _pos == BufferSize ) {
  193. _writer.write( _buffer );
  194. _pos = 0;
  195. }
  196. _buffer[ _pos ] = text.charAt( i );
  197. ++_pos;
  198. }
  199. } catch ( IOException except ) {
  200. // We don't throw an exception, but hold it
  201. // until the end of the document.
  202. if ( _exception == null )
  203. _exception = except;
  204. throw except;
  205. }
  206. }
  207. public void printText( char[] chars, int start, int length )
  208. throws IOException
  209. {
  210. try {
  211. while ( length-- > 0 ) {
  212. if ( _pos == BufferSize ) {
  213. _writer.write( _buffer );
  214. _pos = 0;
  215. }
  216. _buffer[ _pos ] = chars[ start ];
  217. ++start;
  218. ++_pos;
  219. }
  220. } catch ( IOException except ) {
  221. // We don't throw an exception, but hold it
  222. // until the end of the document.
  223. if ( _exception == null )
  224. _exception = except;
  225. throw except;
  226. }
  227. }
  228. public void printText( char ch )
  229. throws IOException
  230. {
  231. try {
  232. if ( _pos == BufferSize ) {
  233. _writer.write( _buffer );
  234. _pos = 0;
  235. }
  236. _buffer[ _pos ] = ch;
  237. ++_pos;
  238. } catch ( IOException except ) {
  239. // We don't throw an exception, but hold it
  240. // until the end of the document.
  241. if ( _exception == null )
  242. _exception = except;
  243. throw except;
  244. }
  245. }
  246. public void printSpace()
  247. throws IOException
  248. {
  249. try {
  250. if ( _pos == BufferSize ) {
  251. _writer.write( _buffer );
  252. _pos = 0;
  253. }
  254. _buffer[ _pos ] = ' ';
  255. ++_pos;
  256. } catch ( IOException except ) {
  257. // We don't throw an exception, but hold it
  258. // until the end of the document.
  259. if ( _exception == null )
  260. _exception = except;
  261. throw except;
  262. }
  263. }
  264. public void breakLine()
  265. throws IOException
  266. {
  267. try {
  268. if ( _pos == BufferSize ) {
  269. _writer.write( _buffer );
  270. _pos = 0;
  271. }
  272. String line = _format.getLineSeparator();
  273. char [] chL = line.toCharArray();
  274. for(int i =0; i<chL.length;i++){
  275. _buffer[ _pos++] = chL[i];
  276. }
  277. //_buffer[ _pos ] = '\n';
  278. //++_pos;
  279. } catch ( IOException except ) {
  280. // We don't throw an exception, but hold it
  281. // until the end of the document.
  282. if ( _exception == null )
  283. _exception = except;
  284. throw except;
  285. }
  286. }
  287. public void breakLine( boolean preserveSpace )
  288. throws IOException
  289. {
  290. breakLine();
  291. }
  292. public void flushLine( boolean preserveSpace )
  293. throws IOException
  294. {
  295. // Write anything left in the buffer into the writer.
  296. try {
  297. _writer.write( _buffer, 0, _pos );
  298. } catch ( IOException except ) {
  299. // We don't throw an exception, but hold it
  300. // until the end of the document.
  301. if ( _exception == null )
  302. _exception = except;
  303. }
  304. _pos = 0;
  305. }
  306. /**
  307. * Flush the output stream. Must be called when done printing
  308. * the document, otherwise some text might be buffered.
  309. */
  310. public void flush()
  311. throws IOException
  312. {
  313. try {
  314. _writer.write( _buffer, 0, _pos );
  315. _writer.flush();
  316. } catch ( IOException except ) {
  317. // We don't throw an exception, but hold it
  318. // until the end of the document.
  319. if ( _exception == null )
  320. _exception = except;
  321. throw except;
  322. }
  323. _pos = 0;
  324. }
  325. public void indent()
  326. {
  327. // NOOP
  328. }
  329. public void unindent()
  330. {
  331. // NOOP
  332. }
  333. public int getNextIndent()
  334. {
  335. return 0;
  336. }
  337. public void setNextIndent( int indent )
  338. {
  339. }
  340. public void setThisIndent( int indent )
  341. {
  342. }
  343. }