KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > serialize > Printer


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
58
59 // Sep 14, 2000:
60
// Fixed serializer to report IO exception directly, instead at
61
// the end of document processing.
62
// Reported by Patrick Higgins <phiggins@transzap.com>
63

64
65 package com.sun.org.apache.xml.internal.serialize;
66
67
68 import java.io.Writer JavaDoc;
69 import java.io.StringWriter JavaDoc;
70 import java.io.IOException JavaDoc;
71
72
73 /**
74  * The printer is responsible for sending text to the output stream
75  * or writer. This class performs direct writing for efficiency.
76  * {@link IndentPrinter} supports indentation and line wrapping by
77  * extending this class.
78  *
79  * @version $Revision: 1.2 $ $Date: 2003/11/18 00:23:04 $
80  * @author <a HREF="mailto:arkin@intalio.com">Assaf Arkin</a>
81  */

82 public class Printer
83 {
84
85
86     /**
87      * The output format associated with this serializer. This will never
88      * be a null reference. If no format was passed to the constructor,
89      * the default one for this document type will be used. The format
90      * object is never changed by the serializer.
91      */

92     protected final OutputFormat _format;
93
94
95     /**
96      * The writer to which the document is written.
97      */

98     protected Writer JavaDoc _writer;
99
100
101     /**
102      * The DTD writer. When we switch to DTD mode, all output is
103      * accumulated in this DTD writer. When we switch out of it,
104      * the output is obtained as a string. Must not be reset to
105      * null until we're done with the document.
106      */

107     protected StringWriter JavaDoc _dtdWriter;
108
109
110     /**
111      * Holds a reference to the document writer while we are
112      * in DTD mode.
113      */

114     protected Writer JavaDoc _docWriter;
115
116
117     /**
118      * Holds the exception thrown by the serializer. Exceptions do not cause
119      * the serializer to quit, but are held and one is thrown at the end.
120      */

121     protected IOException JavaDoc _exception;
122
123
124     /**
125      * The size of the output buffer.
126      */

127     private static final int BufferSize = 4096;
128
129
130     /**
131      * Output buffer.
132      */

133     private final char[] _buffer = new char[ BufferSize ];
134
135
136     /**
137      * Position within the output buffer.
138      */

139     private int _pos = 0;
140
141
142     public Printer( Writer JavaDoc writer, OutputFormat format)
143     {
144         _writer = writer;
145         _format = format;
146         _exception = null;
147         _dtdWriter = null;
148         _docWriter = null;
149         _pos = 0;
150     }
151
152
153     public IOException JavaDoc getException()
154     {
155         return _exception;
156     }
157
158
159     /**
160      * Called by any of the DTD handlers to enter DTD mode.
161      * Once entered, all output will be accumulated in a string
162      * that can be printed as part of the document's DTD.
163      * This method may be called any number of time but will only
164      * have affect the first time it's called. To exist DTD state
165      * and get the accumulated DTD, call {@link #leaveDTD}.
166      */

167     public void enterDTD()
168         throws IOException JavaDoc
169     {
170         // Can only enter DTD state once. Once we're out of DTD
171
// state, can no longer re-enter it.
172
if ( _dtdWriter == null ) {
173         flushLine( false );
174
175             _dtdWriter = new StringWriter JavaDoc();
176             _docWriter = _writer;
177             _writer = _dtdWriter;
178         }
179     }
180
181
182     /**
183      * Called by the root element to leave DTD mode and if any
184      * DTD parts were printer, will return a string with their
185      * textual content.
186      */

187     public String JavaDoc leaveDTD()
188         throws IOException JavaDoc
189     {
190         // Only works if we're going out of DTD mode.
191
if ( _writer == _dtdWriter ) {
192         flushLine( false );
193
194             _writer = _docWriter;
195             return _dtdWriter.toString();
196         } else
197             return null;
198     }
199
200
201     public void printText( String JavaDoc text )
202         throws IOException JavaDoc
203     {
204         try {
205             int length = text.length();
206             for ( int i = 0 ; i < length ; ++i ) {
207                 if ( _pos == BufferSize ) {
208                     _writer.write( _buffer );
209                     _pos = 0;
210                 }
211                 _buffer[ _pos ] = text.charAt( i );
212                 ++_pos;
213             }
214         } catch ( IOException JavaDoc except ) {
215             // We don't throw an exception, but hold it
216
// until the end of the document.
217
if ( _exception == null )
218                 _exception = except;
219             throw except;
220         }
221     }
222
223
224     public void printText( StringBuffer JavaDoc text )
225         throws IOException JavaDoc
226     {
227         try {
228             int length = text.length();
229             for ( int i = 0 ; i < length ; ++i ) {
230                 if ( _pos == BufferSize ) {
231                     _writer.write( _buffer );
232                     _pos = 0;
233                 }
234                 _buffer[ _pos ] = text.charAt( i );
235                 ++_pos;
236             }
237         } catch ( IOException JavaDoc except ) {
238             // We don't throw an exception, but hold it
239
// until the end of the document.
240
if ( _exception == null )
241                 _exception = except;
242             throw except;
243         }
244     }
245
246
247     public void printText( char[] chars, int start, int length )
248         throws IOException JavaDoc
249     {
250         try {
251             while ( length-- > 0 ) {
252                 if ( _pos == BufferSize ) {
253                     _writer.write( _buffer );
254                     _pos = 0;
255                 }
256                 _buffer[ _pos ] = chars[ start ];
257                 ++start;
258                 ++_pos;
259             }
260         } catch ( IOException JavaDoc except ) {
261             // We don't throw an exception, but hold it
262
// until the end of the document.
263
if ( _exception == null )
264                 _exception = except;
265             throw except;
266         }
267     }
268
269
270     public void printText( char ch )
271         throws IOException JavaDoc
272     {
273         try {
274             if ( _pos == BufferSize ) {
275                 _writer.write( _buffer );
276                 _pos = 0;
277             }
278             _buffer[ _pos ] = ch;
279             ++_pos;
280         } catch ( IOException JavaDoc except ) {
281             // We don't throw an exception, but hold it
282
// until the end of the document.
283
if ( _exception == null )
284                 _exception = except;
285             throw except;
286         }
287     }
288
289
290     public void printSpace()
291         throws IOException JavaDoc
292     {
293         try {
294             if ( _pos == BufferSize ) {
295                 _writer.write( _buffer );
296                 _pos = 0;
297             }
298             _buffer[ _pos ] = ' ';
299             ++_pos;
300         } catch ( IOException JavaDoc except ) {
301             // We don't throw an exception, but hold it
302
// until the end of the document.
303
if ( _exception == null )
304                 _exception = except;
305             throw except;
306         }
307     }
308
309
310     public void breakLine()
311         throws IOException JavaDoc
312     {
313         try {
314             if ( _pos == BufferSize ) {
315                 _writer.write( _buffer );
316                 _pos = 0;
317             }
318             String JavaDoc line = _format.getLineSeparator();
319             char [] chL = line.toCharArray();
320             for(int i =0; i<chL.length;i++){
321                 _buffer[ _pos++] = chL[i];
322             }
323             //_buffer[ _pos ] = '\n';
324
//++_pos;
325
} catch ( IOException JavaDoc except ) {
326             // We don't throw an exception, but hold it
327
// until the end of the document.
328
if ( _exception == null )
329                 _exception = except;
330             throw except;
331         }
332     }
333
334
335     public void breakLine( boolean preserveSpace )
336         throws IOException JavaDoc
337     {
338         breakLine();
339     }
340
341
342     public void flushLine( boolean preserveSpace )
343         throws IOException JavaDoc
344     {
345         // Write anything left in the buffer into the writer.
346
try {
347             _writer.write( _buffer, 0, _pos );
348         } catch ( IOException JavaDoc except ) {
349             // We don't throw an exception, but hold it
350
// until the end of the document.
351
if ( _exception == null )
352                 _exception = except;
353         }
354         _pos = 0;
355     }
356
357
358     /**
359      * Flush the output stream. Must be called when done printing
360      * the document, otherwise some text might be buffered.
361      */

362     public void flush()
363         throws IOException JavaDoc
364     {
365         try {
366             _writer.write( _buffer, 0, _pos );
367             _writer.flush();
368         } catch ( IOException JavaDoc except ) {
369             // We don't throw an exception, but hold it
370
// until the end of the document.
371
if ( _exception == null )
372                 _exception = except;
373             throw except;
374         }
375         _pos = 0;
376     }
377
378
379     public void indent()
380     {
381         // NOOP
382
}
383
384
385     public void unindent()
386     {
387         // NOOP
388
}
389
390
391     public int getNextIndent()
392     {
393         return 0;
394     }
395
396
397     public void setNextIndent( int indent )
398     {
399     }
400
401
402     public void setThisIndent( int indent )
403     {
404     }
405
406
407 }
408
Popular Tags