KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > serialize > Printer


1 /*
2  * Copyright 1999-2002,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
18 // Sep 14, 2000:
19
// Fixed serializer to report IO exception directly, instead at
20
// the end of document processing.
21
// Reported by Patrick Higgins <phiggins@transzap.com>
22

23
24 package org.apache.xml.serialize;
25
26
27 import java.io.Writer JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.io.IOException JavaDoc;
30
31
32 /**
33  * The printer is responsible for sending text to the output stream
34  * or writer. This class performs direct writing for efficiency.
35  * {@link IndentPrinter} supports indentation and line wrapping by
36  * extending this class.
37  *
38  * @version $Revision: 1.8 $ $Date: 2004/02/24 23:34:03 $
39  * @author <a HREF="mailto:arkin@intalio.com">Assaf Arkin</a>
40  */

41 public class Printer
42 {
43
44
45     /**
46      * The output format associated with this serializer. This will never
47      * be a null reference. If no format was passed to the constructor,
48      * the default one for this document type will be used. The format
49      * object is never changed by the serializer.
50      */

51     protected final OutputFormat _format;
52
53
54     /**
55      * The writer to which the document is written.
56      */

57     protected Writer JavaDoc _writer;
58
59
60     /**
61      * The DTD writer. When we switch to DTD mode, all output is
62      * accumulated in this DTD writer. When we switch out of it,
63      * the output is obtained as a string. Must not be reset to
64      * null until we're done with the document.
65      */

66     protected StringWriter JavaDoc _dtdWriter;
67
68
69     /**
70      * Holds a reference to the document writer while we are
71      * in DTD mode.
72      */

73     protected Writer JavaDoc _docWriter;
74
75
76     /**
77      * Holds the exception thrown by the serializer. Exceptions do not cause
78      * the serializer to quit, but are held and one is thrown at the end.
79      */

80     protected IOException JavaDoc _exception;
81
82
83     /**
84      * The size of the output buffer.
85      */

86     private static final int BufferSize = 4096;
87
88
89     /**
90      * Output buffer.
91      */

92     private final char[] _buffer = new char[ BufferSize ];
93
94
95     /**
96      * Position within the output buffer.
97      */

98     private int _pos = 0;
99
100
101     public Printer( Writer JavaDoc writer, OutputFormat format)
102     {
103         _writer = writer;
104         _format = format;
105         _exception = null;
106         _dtdWriter = null;
107         _docWriter = null;
108         _pos = 0;
109     }
110
111
112     public IOException JavaDoc getException()
113     {
114         return _exception;
115     }
116
117
118     /**
119      * Called by any of the DTD handlers to enter DTD mode.
120      * Once entered, all output will be accumulated in a string
121      * that can be printed as part of the document's DTD.
122      * This method may be called any number of time but will only
123      * have affect the first time it's called. To exist DTD state
124      * and get the accumulated DTD, call {@link #leaveDTD}.
125      */

126     public void enterDTD()
127         throws IOException JavaDoc
128     {
129         // Can only enter DTD state once. Once we're out of DTD
130
// state, can no longer re-enter it.
131
if ( _dtdWriter == null ) {
132         flushLine( false );
133
134             _dtdWriter = new StringWriter JavaDoc();
135             _docWriter = _writer;
136             _writer = _dtdWriter;
137         }
138     }
139
140
141     /**
142      * Called by the root element to leave DTD mode and if any
143      * DTD parts were printer, will return a string with their
144      * textual content.
145      */

146     public String JavaDoc leaveDTD()
147         throws IOException JavaDoc
148     {
149         // Only works if we're going out of DTD mode.
150
if ( _writer == _dtdWriter ) {
151         flushLine( false );
152
153             _writer = _docWriter;
154             return _dtdWriter.toString();
155         } else
156             return null;
157     }
158
159
160     public void printText( String JavaDoc text )
161         throws IOException JavaDoc
162     {
163         try {
164             int length = text.length();
165             for ( int i = 0 ; i < length ; ++i ) {
166                 if ( _pos == BufferSize ) {
167                     _writer.write( _buffer );
168                     _pos = 0;
169                 }
170                 _buffer[ _pos ] = text.charAt( i );
171                 ++_pos;
172             }
173         } catch ( IOException JavaDoc except ) {
174             // We don't throw an exception, but hold it
175
// until the end of the document.
176
if ( _exception == null )
177                 _exception = except;
178             throw except;
179         }
180     }
181
182
183     public void printText( StringBuffer JavaDoc text )
184         throws IOException JavaDoc
185     {
186         try {
187             int length = text.length();
188             for ( int i = 0 ; i < length ; ++i ) {
189                 if ( _pos == BufferSize ) {
190                     _writer.write( _buffer );
191                     _pos = 0;
192                 }
193                 _buffer[ _pos ] = text.charAt( i );
194                 ++_pos;
195             }
196         } catch ( IOException JavaDoc except ) {
197             // We don't throw an exception, but hold it
198
// until the end of the document.
199
if ( _exception == null )
200                 _exception = except;
201             throw except;
202         }
203     }
204
205
206     public void printText( char[] chars, int start, int length )
207         throws IOException JavaDoc
208     {
209         try {
210             while ( length-- > 0 ) {
211                 if ( _pos == BufferSize ) {
212                     _writer.write( _buffer );
213                     _pos = 0;
214                 }
215                 _buffer[ _pos ] = chars[ start ];
216                 ++start;
217                 ++_pos;
218             }
219         } catch ( IOException JavaDoc except ) {
220             // We don't throw an exception, but hold it
221
// until the end of the document.
222
if ( _exception == null )
223                 _exception = except;
224             throw except;
225         }
226     }
227
228
229     public void printText( char ch )
230         throws IOException JavaDoc
231     {
232         try {
233             if ( _pos == BufferSize ) {
234                 _writer.write( _buffer );
235                 _pos = 0;
236             }
237             _buffer[ _pos ] = ch;
238             ++_pos;
239         } catch ( IOException JavaDoc except ) {
240             // We don't throw an exception, but hold it
241
// until the end of the document.
242
if ( _exception == null )
243                 _exception = except;
244             throw except;
245         }
246     }
247
248
249     public void printSpace()
250         throws IOException JavaDoc
251     {
252         try {
253             if ( _pos == BufferSize ) {
254                 _writer.write( _buffer );
255                 _pos = 0;
256             }
257             _buffer[ _pos ] = ' ';
258             ++_pos;
259         } catch ( IOException JavaDoc except ) {
260             // We don't throw an exception, but hold it
261
// until the end of the document.
262
if ( _exception == null )
263                 _exception = except;
264             throw except;
265         }
266     }
267
268
269     public void breakLine()
270         throws IOException JavaDoc
271     {
272         try {
273             if ( _pos == BufferSize ) {
274                 _writer.write( _buffer );
275                 _pos = 0;
276             }
277             _buffer[ _pos ] = '\n';
278             ++_pos;
279         } catch ( IOException JavaDoc 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
288
289     public void breakLine( boolean preserveSpace )
290         throws IOException JavaDoc
291     {
292         breakLine();
293     }
294
295
296     public void flushLine( boolean preserveSpace )
297         throws IOException JavaDoc
298     {
299         // Write anything left in the buffer into the writer.
300
try {
301             _writer.write( _buffer, 0, _pos );
302         } catch ( IOException JavaDoc except ) {
303             // We don't throw an exception, but hold it
304
// until the end of the document.
305
if ( _exception == null )
306                 _exception = except;
307         }
308         _pos = 0;
309     }
310
311
312     /**
313      * Flush the output stream. Must be called when done printing
314      * the document, otherwise some text might be buffered.
315      */

316     public void flush()
317         throws IOException JavaDoc
318     {
319         try {
320             _writer.write( _buffer, 0, _pos );
321             _writer.flush();
322         } catch ( IOException JavaDoc except ) {
323             // We don't throw an exception, but hold it
324
// until the end of the document.
325
if ( _exception == null )
326                 _exception = except;
327             throw except;
328         }
329         _pos = 0;
330     }
331
332
333     public void indent()
334     {
335         // NOOP
336
}
337
338
339     public void unindent()
340     {
341         // NOOP
342
}
343
344
345     public int getNextIndent()
346     {
347         return 0;
348     }
349
350
351     public void setNextIndent( int indent )
352     {
353     }
354
355
356     public void setThisIndent( int indent )
357     {
358     }
359
360
361 }
362
Popular Tags