jaxp/src/share/classes/com/sun/org/apache/xml/internal/serialize/IndentPrinter.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 6 7f561c08de6b
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2002,2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xml.internal.serialize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import java.io.Writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.io.StringWriter;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 * Extends {@link Printer} and adds support for indentation and line
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * wrapping.
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
public class IndentPrinter
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
    extends Printer
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
     * Holds the currently accumulating text line. This buffer will constantly
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
     * be reused by deleting its contents instead of reallocating it.
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
    private StringBuffer    _line;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
     * Holds the currently accumulating text that follows {@link #_line}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
     * When the end of the part is identified by a call to {@link #printSpace}
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
     * or {@link #breakLine}, this part is added to the accumulated line.
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
    private StringBuffer    _text;
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
     * Counts how many white spaces come between the accumulated line and the
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
     * current accumulated text. Multiple spaces at the end of the a line
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
     * will not be printed.
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    private int             _spaces;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
     * Holds the indentation for the current line that is now accumulating in
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
     * memory and will be sent for printing shortly.
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    private int             _thisIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
     * Holds the indentation for the next line to be printed. After this line is
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
     * printed, {@link #_nextIndent} is assigned to {@link #_thisIndent}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    private int             _nextIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    public IndentPrinter( Writer writer, OutputFormat format)
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
        super( writer, format );
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
        // Initialize everything for a first/second run.
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
        _line = new StringBuffer( 80 );
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
        _text = new StringBuffer( 20 );
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
        _spaces = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
        _thisIndent = _nextIndent = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
     * Called by any of the DTD handlers to enter DTD mode.
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
     * Once entered, all output will be accumulated in a string
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
     * that can be printed as part of the document's DTD.
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
     * This method may be called any number of time but will only
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
     * have affect the first time it's called. To exist DTD state
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
     * and get the accumulated DTD, call {@link #leaveDTD}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    public void enterDTD()
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
        // Can only enter DTD state once. Once we're out of DTD
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
        // state, can no longer re-enter it.
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        if ( _dtdWriter == null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
            _line.append( _text );
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
            _text = new StringBuffer( 20 );
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
            flushLine( false );
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
            _dtdWriter = new StringWriter();
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
            _docWriter = _writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
            _writer = _dtdWriter;
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
     * Called by the root element to leave DTD mode and if any
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
     * DTD parts were printer, will return a string with their
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
     * textual content.
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    public String leaveDTD()
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        // Only works if we're going out of DTD mode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        if ( _writer == _dtdWriter ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
            _line.append( _text );
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
            _text = new StringBuffer( 20 );
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
            flushLine( false );
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
            _writer = _docWriter;
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
            return _dtdWriter.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
        } else
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
     * Called to print additional text. Each time this method is called
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
     * it accumulates more text. When a space is printed ({@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
     * #printSpace}) all the accumulated text becomes one part and is
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
     * added to the accumulate line. When a line is long enough, it can
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
     * be broken at its text boundary.
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
     * @param text The text to print
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    public void printText( String text )
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
        _text.append( text );
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
    public void printText( StringBuffer text )
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
        _text.append( text.toString() );
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    public void printText( char ch )
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        _text.append( ch );
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
    public void printText( char[] chars, int start, int length )
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
        _text.append( chars, start, length );
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
     * Called to print a single space between text parts that may be
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
     * broken into separate lines. Must not be called to print a space
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
     * when preserving spaces. The text accumulated so far with {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
     * #printText} will be added to the accumulated line, and a space
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
     * separator will be counted. If the line accumulated so far is
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
     * long enough, it will be printed.
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
    public void printSpace()
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
        // The line consists of the text accumulated in _line,
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
        // followed by one or more spaces as counted by _spaces,
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
        // followed by more space accumulated in _text:
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
        // -  Text is printed and accumulated into _text.
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
        // -  A space is printed, so _text is added to _line and
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
        //    a space is counted.
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
        // -  More text is printed and accumulated into _text.
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
        // -  A space is printed, the previous spaces are added
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        //    to _line, the _text is added to _line, and a new
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        //    space is counted.
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
        // If text was accumulated with printText(), then the space
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
        // means we have to move that text into the line and
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
        // start accumulating new text with printText().
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
        if ( _text.length() > 0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
            // If the text breaks a line bounary, wrap to the next line.
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
            // The printed line size consists of the indentation we're going
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
            // to use next, the accumulated line so far, some spaces and the
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
            // accumulated text so far.
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
            if ( _format.getLineWidth() > 0 &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
                 _thisIndent + _line.length() + _spaces + _text.length() > _format.getLineWidth() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
                flushLine( false );
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
                    // Print line and new line, then zero the line contents.
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
                    _writer.write( _format.getLineSeparator() );
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
                } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
                    // We don't throw an exception, but hold it
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
                    // until the end of the document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
                    if ( _exception == null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
                        _exception = except;
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
            // Add as many spaces as we accumulaed before.
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
            // At the end of this loop, _spaces is zero.
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
            while ( _spaces > 0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
                _line.append( ' ' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
                --_spaces;
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
            _line.append( _text );
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
            _text = new StringBuffer( 20 );
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
        // Starting a new word: accumulate the text between the line
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
        // and this new word; not a new word: just add another space.
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
        ++_spaces;
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
     * Called to print a line consisting of the text accumulated so
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
     * far. This is equivalent to calling {@link #printSpace} but
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
     * forcing the line to print and starting a new line ({@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
     * #printSpace} will only start a new line if the current line
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
     * is long enough).
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
    public void breakLine()
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
        breakLine( false );
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
    public void breakLine( boolean preserveSpace )
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
        // Equivalent to calling printSpace and forcing a flushLine.
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
        if ( _text.length() > 0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
            while ( _spaces > 0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
                _line.append( ' ' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
                --_spaces;
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
            _line.append( _text );
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
            _text = new StringBuffer( 20 );
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
        flushLine( preserveSpace );
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
            // Print line and new line, then zero the line contents.
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
            _writer.write( _format.getLineSeparator() );
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
            // We don't throw an exception, but hold it
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
            // until the end of the document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
            if ( _exception == null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
                _exception = except;
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
     * Flushes the line accumulated so far to the writer and get ready
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
     * to accumulate the next line. This method is called by {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
     * #printText} and {@link #printSpace} when the accumulated line plus
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
     * accumulated text are two long to fit on a given line. At the end of
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
     * this method _line is empty and _spaces is zero.
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
    public void flushLine( boolean preserveSpace )
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
        int     indent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
        if ( _line.length() > 0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
                if ( _format.getIndenting() && ! preserveSpace ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
                    // Make sure the indentation does not blow us away.
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
                    indent = _thisIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
                    if ( ( 2 * indent ) > _format.getLineWidth() && _format.getLineWidth() > 0 )
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
                        indent = _format.getLineWidth() / 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
                    // Print the indentation as spaces and set the current
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
                    // indentation to the next expected indentation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
                    while ( indent > 0 ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
                        _writer.write( ' ' );
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
                        --indent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
                _thisIndent = _nextIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
                // There is no need to print the spaces at the end of the line,
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
                // they are simply stripped and replaced with a single line
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
                // separator.
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
                _spaces = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
                _writer.write( _line.toString() );
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
                _line = new StringBuffer( 40 );
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
            } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
                // We don't throw an exception, but hold it
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
                // until the end of the document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
                if ( _exception == null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
                    _exception = except;
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
     * Flush the output stream. Must be called when done printing
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
     * the document, otherwise some text might be buffered.
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
    public void flush()
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
        if ( _line.length() > 0 || _text.length() > 0 )
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
            breakLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
            _writer.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
        } catch ( IOException except ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
            // We don't throw an exception, but hold it
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
            // until the end of the document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
            if ( _exception == null )
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
                _exception = except;
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
     * Increment the indentation for the next line.
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
    public void indent()
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
        _nextIndent += _format.getIndent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
     * Decrement the indentation for the next line.
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
    public void unindent()
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
        _nextIndent -= _format.getIndent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
        if ( _nextIndent < 0 )
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
            _nextIndent = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
        // If there is no current line and we're de-identing then
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
        // this indentation level is actually the next level.
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
        if ( ( _line.length() + _spaces + _text.length() ) == 0 )
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
            _thisIndent = _nextIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
    public int getNextIndent()
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
        return _nextIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
    public void setNextIndent( int indent )
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
        _nextIndent = indent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
    public void setThisIndent( int indent )
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
        _thisIndent = indent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
}