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