FileDocCategorySizeDatePackage
Printer.javaAPI DocJava SE 5 API11493Fri Aug 26 14:56:02 BST 2005com.sun.org.apache.xml.internal.serialize

Printer

public class Printer extends Object
The printer is responsible for sending text to the output stream or writer. This class performs direct writing for efficiency. {@link IndentPrinter} supports indentation and line wrapping by extending this class.
version
$Revision: 1.2 $ $Date: 2003/11/18 00:23:04 $
author
Assaf Arkin

Fields Summary
protected final OutputFormat
_format
The output format associated with this serializer. This will never be a null reference. If no format was passed to the constructor, the default one for this document type will be used. The format object is never changed by the serializer.
protected Writer
_writer
The writer to which the document is written.
protected StringWriter
_dtdWriter
The DTD writer. When we switch to DTD mode, all output is accumulated in this DTD writer. When we switch out of it, the output is obtained as a string. Must not be reset to null until we're done with the document.
protected Writer
_docWriter
Holds a reference to the document writer while we are in DTD mode.
protected IOException
_exception
Holds the exception thrown by the serializer. Exceptions do not cause the serializer to quit, but are held and one is thrown at the end.
private static final int
BufferSize
The size of the output buffer.
private final char[]
_buffer
Output buffer.
private int
_pos
Position within the output buffer.
Constructors Summary
public Printer(Writer writer, OutputFormat format)



         
    
        _writer = writer;
        _format = format;
        _exception = null;
        _dtdWriter = null;
        _docWriter = null;
        _pos = 0;
    
Methods Summary
public voidbreakLine()

        try {
            if ( _pos == BufferSize ) {
                _writer.write( _buffer );
                _pos = 0;
            }
            String line = _format.getLineSeparator();
            char [] chL = line.toCharArray();
            for(int i =0; i<chL.length;i++){
                _buffer[ _pos++] =  chL[i];
            }
            //_buffer[ _pos ] = '\n';
            //++_pos;
        } catch ( IOException except ) {
            // We don't throw an exception, but hold it
            // until the end of the document.
            if ( _exception == null )
                _exception = except;
            throw except;
        }
    
public voidbreakLine(boolean preserveSpace)

        breakLine();
    
public voidenterDTD()
Called by any of the DTD handlers to enter DTD mode. Once entered, all output will be accumulated in a string that can be printed as part of the document's DTD. This method may be called any number of time but will only have affect the first time it's called. To exist DTD state and get the accumulated DTD, call {@link #leaveDTD}.

        // Can only enter DTD state once. Once we're out of DTD
        // state, can no longer re-enter it.
        if ( _dtdWriter == null ) {
	    flushLine( false );

			_dtdWriter = new StringWriter();
            _docWriter = _writer;
            _writer = _dtdWriter;
        }
    
public voidflush()
Flush the output stream. Must be called when done printing the document, otherwise some text might be buffered.

        try {
            _writer.write( _buffer, 0, _pos );
            _writer.flush();
        } catch ( IOException except ) {
            // We don't throw an exception, but hold it
            // until the end of the document.
            if ( _exception == null )
                _exception = except;
            throw except;
        }
        _pos = 0;
    
public voidflushLine(boolean preserveSpace)

        // Write anything left in the buffer into the writer.
        try {
            _writer.write( _buffer, 0, _pos );
        } catch ( IOException except ) {
            // We don't throw an exception, but hold it
            // until the end of the document.
            if ( _exception == null )
                _exception = except;
        }
        _pos = 0;
    
public java.io.IOExceptiongetException()

        return _exception;
    
public intgetNextIndent()

        return 0;
    
public voidindent()

        // NOOP
    
public java.lang.StringleaveDTD()
Called by the root element to leave DTD mode and if any DTD parts were printer, will return a string with their textual content.

        // Only works if we're going out of DTD mode.
        if ( _writer == _dtdWriter ) {
	    flushLine( false );

			_writer = _docWriter;
            return _dtdWriter.toString();
        } else
            return null;
    
public voidprintSpace()

        try {
            if ( _pos == BufferSize ) {
                _writer.write( _buffer );
                _pos = 0;
            }
            _buffer[ _pos ] = ' ";
            ++_pos;
        } catch ( IOException except ) {
            // We don't throw an exception, but hold it
            // until the end of the document.
            if ( _exception == null )
                _exception = except;
            throw except;
        }
    
public voidprintText(java.lang.String text)

        try {
            int length = text.length();
            for ( int i = 0 ; i < length ; ++i ) {
                if ( _pos == BufferSize ) {
                    _writer.write( _buffer );
                    _pos = 0;
                }
                _buffer[ _pos ] = text.charAt( i );
                ++_pos;
            }
        } catch ( IOException except ) {
            // We don't throw an exception, but hold it
            // until the end of the document.
            if ( _exception == null )
                _exception = except;
            throw except;
        }
    
public voidprintText(java.lang.StringBuffer text)

        try {
            int length = text.length();
            for ( int i = 0 ; i < length ; ++i ) {
                if ( _pos == BufferSize ) {
                    _writer.write( _buffer );
                    _pos = 0;
                }
                _buffer[ _pos ] = text.charAt( i );
                ++_pos;
            }
        } catch ( IOException except ) {
            // We don't throw an exception, but hold it
            // until the end of the document.
            if ( _exception == null )
                _exception = except;
            throw except;
        }
    
public voidprintText(char[] chars, int start, int length)

        try {
            while ( length-- > 0 ) {
                if ( _pos == BufferSize ) {
                    _writer.write( _buffer );
                    _pos = 0;
                }
                _buffer[ _pos ] = chars[ start ];
                ++start;
                ++_pos;
            }
        } catch ( IOException except ) {
            // We don't throw an exception, but hold it
            // until the end of the document.
            if ( _exception == null )
                _exception = except;
            throw except;
        }
    
public voidprintText(char ch)

        try {
            if ( _pos == BufferSize ) {
                _writer.write( _buffer );
                _pos = 0;
            }
            _buffer[ _pos ] = ch;
            ++_pos;
        } catch ( IOException except ) {
            // We don't throw an exception, but hold it
            // until the end of the document.
            if ( _exception == null )
                _exception = except;
            throw except;
        }
    
public voidsetNextIndent(int indent)

    
public voidsetThisIndent(int indent)

    
public voidunindent()

        // NOOP