jdk/src/share/classes/javax/swing/text/AbstractWriter.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 23328 4c53a6ebc779
child 25193 187a455af8f8
permissions -rw-r--r--
8041917: unexcepted behavior of LineBorder while using Boolean variable true Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.Writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * AbstractWriter is an abstract class that actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * does the work of writing out the element tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * including the attributes.  In terms of how much is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * written out per line, the writer defaults to 100.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * But this value can be set by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Sunita Mani
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public abstract class AbstractWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private ElementIterator it;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private Writer out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private int indentLevel = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private int indentSpace = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private Document doc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private int maxLineLength = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private int currLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private int startOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int endOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // If (indentLevel * indentSpace) becomes >= maxLineLength, this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // get incremened instead of indentLevel to avoid indenting going greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // than line length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int offsetIndent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * String used for end of line. If the Document has the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * EndOfLineStringProperty, it will be used for newlines. Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * the System property line.separator will be used. The line separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * can also be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private String lineSeparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * True indicates that when writing, the line can be split, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * indicates that even if the line is > than max line length it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * not be split.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private boolean canWrapLines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * True while the current line is empty. This will remain true after
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * indenting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private boolean isLineEmpty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Used when indenting. Will contain the spaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private char[] indentChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Used when writing out a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private char[] tempChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * This is used in <code>writeLineSeparator</code> instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * tempChars. If tempChars were used it would mean write couldn't invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * <code>writeLineSeparator</code> as it might have been passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * tempChars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private char[] newlineChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Used for writing text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private Segment segment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * How the text packages models newlines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @see #getLineSeparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected static final char NEWLINE = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Creates a new AbstractWriter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Initializes the ElementIterator with the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * root of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param w a Writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param doc a Document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    protected AbstractWriter(Writer w, Document doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this(w, doc, 0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Creates a new AbstractWriter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Initializes the ElementIterator with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * element passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param w a Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param doc an Element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param pos The location in the document to fetch the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *   content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param len The amount to write out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    protected AbstractWriter(Writer w, Document doc, int pos, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.doc = doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        it = new ElementIterator(doc.getDefaultRootElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        out = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        startOffset = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        endOffset = pos + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        Object docNewline = doc.getProperty(DefaultEditorKit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                       EndOfLineStringProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (docNewline instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            setLineSeparator((String)docNewline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        else {
23307
5e534f20d09a 8035310: The line.separator property can be retrieved via public API
malenkov
parents: 21278
diff changeset
   144
            String newline = System.lineSeparator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (newline == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                // Should not get here, but if we do it means we could not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                // find a newline string, use \n in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                newline = "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            setLineSeparator(newline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        canWrapLines = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Creates a new AbstractWriter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Initializes the ElementIterator with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * element passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param w a Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param root an Element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    protected AbstractWriter(Writer w, Element root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this(w, root, 0, root.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Creates a new AbstractWriter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Initializes the ElementIterator with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * element passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param w a Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param root an Element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param pos The location in the document to fetch the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *   content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param len The amount to write out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected AbstractWriter(Writer w, Element root, int pos, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        this.doc = root.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        it = new ElementIterator(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        out = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        startOffset = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        endOffset = pos + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        canWrapLines = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Returns the first offset to be output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public int getStartOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return startOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Returns the last offset to be output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public int getEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return endOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Fetches the ElementIterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return the ElementIterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    protected ElementIterator getElementIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return it;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Returns the Writer that is used to output the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    protected Writer getWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Fetches the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return the Document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    protected Document getDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * This method determines whether the current element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * is in the range specified.  When no range is specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * the range is initialized to be the entire document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * inRange() returns true if the range specified intersects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * with the element's range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param  next an Element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @return boolean that indicates whether the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *         is in the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    protected boolean inRange(Element next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int startOffset = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        int endOffset = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if ((next.getStartOffset() >= startOffset &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
             next.getStartOffset()  < endOffset) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            (startOffset >= next.getStartOffset() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
             startOffset < next.getEndOffset())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * This abstract method needs to be implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * by subclasses.  Its responsibility is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * iterate over the elements and use the write()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * methods to generate output in the desired format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    abstract protected void write() throws IOException, BadLocationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Returns the text associated with the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * The assumption here is that the element is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * leaf element.  Throws a BadLocationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * when encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param     elem an <code>Element</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @exception BadLocationException if pos represents an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *            location within the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return    the text as a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    protected String getText(Element elem) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return doc.getText(elem.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                           elem.getEndOffset() - elem.getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Writes out text.  If a range is specified when the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * is invoked, then only the appropriate range of text is written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param     elem an Element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @exception BadLocationException if pos represents an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *            location within the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    protected void text(Element elem) throws BadLocationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                             IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int start = Math.max(getStartOffset(), elem.getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int end = Math.min(getEndOffset(), elem.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (start < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (segment == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                segment = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            getDocument().getText(start, end - start, segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (segment.count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                write(segment.array, segment.offset, segment.count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Enables subclasses to set the number of characters they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * want written per line.   The default is 100.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param l the maximum line length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    protected void setLineLength(int l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        maxLineLength = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns the maximum line length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    protected int getLineLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return maxLineLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Sets the current line length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    protected void setCurrentLineLength(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        currLength = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        isLineEmpty = (currLength == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Returns the current line length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    protected int getCurrentLineLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return currLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Returns true if the current line should be considered empty. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * is true when <code>getCurrentLineLength</code> == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <code>indent</code> has been invoked on an empty line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    protected boolean isLineEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return isLineEmpty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Sets whether or not lines can be wrapped. This can be toggled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * during the writing of lines. For example, outputting HTML might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * set this to false when outputting a quoted string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    protected void setCanWrapLines(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        canWrapLines = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Returns whether or not the lines can be wrapped. If this is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * no lineSeparator's will be output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    protected boolean getCanWrapLines() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return canWrapLines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Enables subclasses to specify how many spaces an indent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * maps to. When indentation takes place, the indent level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * is multiplied by this mapping.  The default is 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param space an int representing the space to indent mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    protected void setIndentSpace(int space) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        indentSpace = space;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Returns the amount of space to indent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    protected int getIndentSpace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return indentSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20428
diff changeset
   396
     * Sets the String used to represent newlines. This is initialized
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * in the constructor from either the Document, or the System property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * line.separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public void setLineSeparator(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        lineSeparator = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Returns the string used to represent newlines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public String getLineSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return lineSeparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Increments the indent level. If indenting would cause
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 5506
diff changeset
   417
     * <code>getIndentSpace()</code> *<code>getIndentLevel()</code> to be &gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * than <code>getLineLength()</code> this will not cause an indent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    protected void incrIndent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        // Only increment to a certain point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (offsetIndent > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            offsetIndent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            if (++indentLevel * getIndentSpace() >= getLineLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                offsetIndent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                --indentLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Decrements the indent level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    protected void decrIndent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (offsetIndent > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            --offsetIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            indentLevel--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Returns the current indentation level. That is, the number of times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * <code>incrIndent</code> has been invoked minus the number of times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * <code>decrIndent</code> has been invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    protected int getIndentLevel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        return indentLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Does indentation. The number of spaces written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * out is indent level times the space to map mapping. If the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * line is empty, this will not make it so that the current line is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * still considered empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    protected void indent() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        int max = getIndentLevel() * getIndentSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (indentChars == null || max > indentChars.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            indentChars = new char[max];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            for (int counter = 0; counter < max; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                indentChars[counter] = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        int length = getCurrentLineLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        boolean wasEmpty = isLineEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        output(indentChars, 0, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (wasEmpty && length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            isLineEmpty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Writes out a character. This is implemented to invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * the <code>write</code> method that takes a char[].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @param     ch a char.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    protected void write(char ch) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (tempChars == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            tempChars = new char[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        tempChars[0] = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        write(tempChars, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Writes out a string. This is implemented to invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * <code>write</code> method that takes a char[].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @param     content a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    protected void write(String content) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (content == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        int size = content.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (tempChars == null || tempChars.length < size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            tempChars = new char[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        content.getChars(0, size, tempChars, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        write(tempChars, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Writes the line separator. This invokes <code>output</code> directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * as well as setting the <code>lineLength</code> to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    protected void writeLineSeparator() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        String newline = getLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int length = newline.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (newlineChars == null || newlineChars.length < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            newlineChars = new char[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        newline.getChars(0, length, newlineChars, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        output(newlineChars, 0, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        setCurrentLineLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * All write methods call into this one. If <code>getCanWrapLines()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * returns false, this will call <code>output</code> with each sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * of <code>chars</code> that doesn't contain a NEWLINE, followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * by a call to <code>writeLineSeparator</code>. On the other hand,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * if <code>getCanWrapLines()</code> returns true, this will split the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * string, as necessary, so <code>getLineLength</code> is honored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * The only exception is if the current string contains no whitespace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * and won't fit in which case the line length will exceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <code>getLineLength</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    protected void write(char[] chars, int startIndex, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                   throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (!getCanWrapLines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            // We can not break string, just track if a newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            // is in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            int lastIndex = startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            int endIndex = startIndex + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            int newlineIndex = indexOf(chars, NEWLINE, startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            while (newlineIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                if (newlineIndex > lastIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    output(chars, lastIndex, newlineIndex - lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                lastIndex = newlineIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                newlineIndex = indexOf(chars, '\n', lastIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            if (lastIndex < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                output(chars, lastIndex, endIndex - lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            // We can break chars if the length exceeds maxLength.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            int lastIndex = startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            int endIndex = startIndex + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            int lineLength = getCurrentLineLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            int maxLength = getLineLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            while (lastIndex < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                int newlineIndex = indexOf(chars, NEWLINE, lastIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                           endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                boolean needsNewline = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                boolean forceNewLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                lineLength = getCurrentLineLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                if (newlineIndex != -1 && (lineLength +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                              (newlineIndex - lastIndex)) < maxLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    if (newlineIndex > lastIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                        output(chars, lastIndex, newlineIndex - lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    lastIndex = newlineIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    forceNewLine = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                else if (newlineIndex == -1 && (lineLength +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                                (endIndex - lastIndex)) < maxLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    if (endIndex > lastIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                        output(chars, lastIndex, endIndex - lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    lastIndex = endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    // Need to break chars, find a place to split chars at,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    // from lastIndex to endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    // or maxLength - lineLength whichever is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    int breakPoint = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    int maxBreak = Math.min(endIndex - lastIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                            maxLength - lineLength - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    int counter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    while (counter < maxBreak) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                        if (Character.isWhitespace(chars[counter +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                                                        lastIndex])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                            breakPoint = counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                        counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    if (breakPoint != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        // Found a place to break at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                        breakPoint += lastIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                        output(chars, lastIndex, breakPoint - lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        lastIndex = breakPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                        needsNewline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                        // No where good to break.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                        // find the next whitespace, or write out the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                        // whole string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                            // maxBreak will be negative if current line too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                            // long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                            counter = Math.max(0, maxBreak);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                            maxBreak = endIndex - lastIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                            while (counter < maxBreak) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                if (Character.isWhitespace(chars[counter +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                                                                lastIndex])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                                    breakPoint = counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                                counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                            if (breakPoint == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                                output(chars, lastIndex, endIndex - lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                                breakPoint = endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                                breakPoint += lastIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                                if (chars[breakPoint] == NEWLINE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                                    output(chars, lastIndex, breakPoint++ -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                                           lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                                forceNewLine = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                                    output(chars, lastIndex, ++breakPoint -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                              lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                needsNewline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                            lastIndex = breakPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                if (forceNewLine || needsNewline || lastIndex < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    if (lastIndex < endIndex || !forceNewLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                        indent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 5506
diff changeset
   663
     * Writes out the set of attributes as " &lt;name&gt;=&lt;value&gt;"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * pairs. It throws an IOException when encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @param     attr an AttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    protected void writeAttributes(AttributeSet attr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        Enumeration names = attr.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            write(" " + name + "=" + attr.getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * The last stop in writing out content. All the write methods eventually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * make it to this method, which invokes <code>write</code> on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * <p>This method also updates the line length based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * <code>length</code>. If this is invoked to output a newline, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * current line length will need to be reset as will no longer be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * valid. If it is up to the caller to do this. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * <code>writeLineSeparator</code> to write out a newline, which will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * property update the current line length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    protected void output(char[] content, int start, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                   throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        getWriter().write(content, start, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        setCurrentLineLength(getCurrentLineLength() + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20428
diff changeset
   698
     * Support method to locate an occurrence of a particular character.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    private int indexOf(char[] chars, char sChar, int startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                        int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        while(startIndex < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            if (chars[startIndex] == sChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                return startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            startIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
}