jdk/src/share/classes/javax/swing/text/html/HTMLWriter.java
author darcy
Wed, 22 Jan 2014 23:20:58 -0800
changeset 22567 5816a47fa4dd
parent 21278 ef8a3a2a72f2
child 23010 6dadb192ad81
permissions -rw-r--r--
8032047: Fix static lint warnings in client libraries 8032048: Add static lint warning to build of jdk repository Reviewed-by: pchelko, serb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 1998, 2008, 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: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.Writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This is a writer for HTMLDocuments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author  Sunita Mani
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class HTMLWriter extends AbstractWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Stores all elements for which end tags have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * be emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    49
    private Stack<Element> blockElementStack = new Stack<Element>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private boolean inContent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private boolean inPre = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /** When inPre is true, this will indicate the end offset of the pre
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * element. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int preEndOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private boolean inTextArea = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private boolean newlineOutputed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private boolean completeDoc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Stores all embedded tags. Embedded tags are tags that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * stored as attributes in other tags. Generally they're
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * character level attributes.  Examples include
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * &lt;b&gt;, &lt;i&gt;, &lt;font&gt;, and &lt;a&gt;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    65
    private Vector<HTML.Tag> tags = new Vector<HTML.Tag>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Values for the tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    70
    private Vector<Object> tagValues = new Vector<Object>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Used when writing out content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private Segment segment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * This is used in closeOutUnwantedEmbeddedTags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    80
    private Vector<HTML.Tag> tagsToRemove = new Vector<HTML.Tag>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Set to true after the head has been output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private boolean wroteHead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Set to true when entities (such as &lt;) should be replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private boolean replaceEntities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Temporary buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private char[] tempChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Creates a new HTMLWriter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param w   a Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param doc  an HTMLDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public HTMLWriter(Writer w, HTMLDocument doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this(w, doc, 0, doc.getLength());
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 HTMLWriter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param w  a Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param doc an HTMLDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param pos the document location from which to fetch the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param len the amount to write out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public HTMLWriter(Writer w, HTMLDocument doc, int pos, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        super(w, doc, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        completeDoc = (pos == 0 && len == doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        setLineLength(80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Iterates over the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Element tree and controls the writing out of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * all the tags and its attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @exception BadLocationException if pos represents an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *            location within the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public void write() throws IOException, BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        ElementIterator it = getElementIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Element current = null;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   136
        Element next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        wroteHead = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        setCurrentLineLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        replaceEntities = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        setCanWrapLines(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (segment == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            segment = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        inPre = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        boolean forcedBody = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        while ((next = it.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (!inRange(next)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                if (completeDoc && next.getAttributes().getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        StyleConstants.NameAttribute) == HTML.Tag.BODY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    forcedBody = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                  if next is child of current increment indent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                if (indentNeedsIncrementing(current, next)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    incrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                } else if (current.getParentElement() != next.getParentElement()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                       next and current are not siblings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                       so emit end tags for items on the stack until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                       item on top of the stack, is the parent of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                       next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   172
                    Element top = blockElementStack.peek();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    while (top != next.getParentElement()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                           pop() will return top.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        blockElementStack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        if (!synthesizedElement(top)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                            AttributeSet attrs = top.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                            if (!matchNameAttribute(attrs, HTML.Tag.PRE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                !isFormElementWithContent(attrs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                decrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                            endTag(top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   186
                        top = blockElementStack.peek();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                } else if (current.getParentElement() == next.getParentElement()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                       if next and current are siblings the indent level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                       is correct.  But, we need to make sure that if current is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                       on the stack, we pop it off, and put out its end tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   194
                    Element top = blockElementStack.peek();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    if (top == current) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        blockElementStack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        endTag(top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (!next.isLeaf() || isFormElementWithContent(next.getAttributes())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                blockElementStack.push(next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                startTag(next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                emptyTag(next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            current = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        /* Emit all remaining end tags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        /* A null parameter ensures that all embedded tags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
           currently in the tags vector have their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
           corresponding end tags written out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        closeOutUnwantedEmbeddedTags(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (forcedBody) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            blockElementStack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            endTag(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        while (!blockElementStack.empty()) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   222
            current = blockElementStack.pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (!synthesizedElement(current)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                AttributeSet attrs = current.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                if (!matchNameAttribute(attrs, HTML.Tag.PRE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                              !isFormElementWithContent(attrs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    decrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                endTag(current);
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
        if (completeDoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            writeAdditionalComments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        segment.array = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Writes out the attribute set.  Ignores all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * attributes with a key of type HTML.Tag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * attributes with a key of type StyleConstants,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * and attributes with a key of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * HTML.Attribute.ENDTAG.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param attr   an AttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    protected void writeAttributes(AttributeSet attr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // translate css attributes to html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        convAttr.removeAttributes(convAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        convertToHTML32(attr, convAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        Enumeration names = convAttr.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (name instanceof HTML.Tag ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                name instanceof StyleConstants ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                name == HTML.Attribute.ENDTAG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            write(" " + name + "=\"" + convAttr.getAttribute(name) + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Writes out all empty elements (all tags that have no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * corresponding end tag).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param elem   an Element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @exception BadLocationException if pos represents an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *            location within the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    protected void emptyTag(Element elem) throws BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (!inContent && !inPre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        AttributeSet attr = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        closeOutUnwantedEmbeddedTags(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        writeEmbeddedTags(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (matchNameAttribute(attr, HTML.Tag.CONTENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            inContent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            text(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } else if (matchNameAttribute(attr, HTML.Tag.COMMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            comment(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            boolean isBlock = isBlockTag(elem.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (inContent && isBlock ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            Object nameTag = (attr != null) ? attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                              (StyleConstants.NameAttribute) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            Object endTag = (attr != null) ? attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                              (HTML.Attribute.ENDTAG) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            boolean outputEndTag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            // If an instance of an UNKNOWN Tag, or an instance of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            // tag that is only visible during editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (nameTag != null && endTag != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                (endTag instanceof String) &&
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   311
                endTag.equals("true")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                outputEndTag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (completeDoc && matchNameAttribute(attr, HTML.Tag.HEAD)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                if (outputEndTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    // Write out any styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    writeStyles(((HTMLDocument)getDocument()).getStyleSheet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                wroteHead = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            write('<');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (outputEndTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                write('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            write(elem.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            writeAttributes(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            write('>');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (matchNameAttribute(attr, HTML.Tag.TITLE) && !outputEndTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                Document doc = elem.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                String title = (String)doc.getProperty(Document.TitleProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                write(title);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } else if (!inContent || isBlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (isBlock && inContent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
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
     * Determines if the HTML.Tag associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * element is a block tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param attr  an AttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return  true if tag is block tag, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    protected boolean isBlockTag(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        Object o = attr.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (o instanceof HTML.Tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            HTML.Tag name = (HTML.Tag) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            return name.isBlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Writes out a start tag for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Ignores all synthesized elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param elem   an Element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    protected void startTag(Element elem) throws IOException, BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (synthesizedElement(elem)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // Determine the name, as an HTML.Tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        AttributeSet attr = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        Object nameAttribute = attr.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        HTML.Tag name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (nameAttribute instanceof HTML.Tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            name = (HTML.Tag)nameAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (name == HTML.Tag.PRE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            inPre = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            preEndOffset = elem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        // write out end tags for item on stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        closeOutUnwantedEmbeddedTags(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (inContent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            inContent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            newlineOutputed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (completeDoc && name == HTML.Tag.BODY && !wroteHead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            // If the head has not been output, output it and the styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            wroteHead = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            write("<head>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            incrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            writeStyles(((HTMLDocument)getDocument()).getStyleSheet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            decrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            write("</head>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        write('<');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        write(elem.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        writeAttributes(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        write('>');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (name != HTML.Tag.PRE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (name == HTML.Tag.TEXTAREA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            textAreaContent(elem.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        } else if (name == HTML.Tag.SELECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            selectContent(elem.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } else if (completeDoc && name == HTML.Tag.BODY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            // Write out the maps, which is not stored as Elements in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // the Document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            writeMaps(((HTMLDocument)getDocument()).getMaps());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        else if (name == HTML.Tag.HEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            HTMLDocument document = (HTMLDocument)getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            wroteHead = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            incrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            writeStyles(document.getStyleSheet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (document.hasBaseTag()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                write("<base href=\"" + document.getBase() + "\">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            decrIndent();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Writes out text that is contained in a TEXTAREA form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param attr  an AttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @exception BadLocationException if pos represents an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *            location within the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    protected void textAreaContent(AttributeSet attr) throws BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        Document doc = (Document)attr.getAttribute(StyleConstants.ModelAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (doc != null && doc.getLength() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (segment == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                segment = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            doc.getText(0, doc.getLength(), segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if (segment.count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                inTextArea = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                incrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                setCanWrapLines(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                replaceEntities = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                write(segment.array, segment.offset, segment.count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                replaceEntities = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                setCanWrapLines(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                inTextArea = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                decrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
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 text.  If a range is specified when the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * is invoked, then only the appropriate range of text is written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @param elem   an Element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @exception BadLocationException if pos represents an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *            location within the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    protected void text(Element elem) throws BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        int start = Math.max(getStartOffset(), elem.getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        int end = Math.min(getEndOffset(), elem.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (start < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            if (segment == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                segment = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            getDocument().getText(start, end - start, segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            newlineOutputed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (segment.count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                if (segment.array[segment.offset + segment.count - 1] == '\n'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    newlineOutputed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                if (inPre && end == preEndOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    if (segment.count > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        segment.count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                replaceEntities = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                setCanWrapLines(!inPre);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                write(segment.array, segment.offset, segment.count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                setCanWrapLines(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                replaceEntities = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * Writes out the content of the SELECT form element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @param attr the AttributeSet associated with the form element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    protected void selectContent(AttributeSet attr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        Object model = attr.getAttribute(StyleConstants.ModelAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        incrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (model instanceof OptionListModel) {
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 5506
diff changeset
   530
            OptionListModel<Option> listModel = (OptionListModel<Option>) model;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            int size = listModel.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            for (int i = 0; i < size; i++) {
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 5506
diff changeset
   533
                Option option = listModel.getElementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                writeOption(option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        } else if (model instanceof OptionComboBoxModel) {
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 5506
diff changeset
   537
            OptionComboBoxModel<Option> comboBoxModel = (OptionComboBoxModel<Option>) model;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            int size = comboBoxModel.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            for (int i = 0; i < size; i++) {
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 5506
diff changeset
   540
                Option option = comboBoxModel.getElementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                writeOption(option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        decrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Writes out the content of the Option form element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param option  an Option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    protected void writeOption(Option option) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        write('<');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        write("option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        // PENDING: should this be changed to check for null first?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        Object value = option.getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                              (HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            write(" value="+ value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (option.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            write(" selected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        write('>');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if (option.getLabel() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            write(option.getLabel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Writes out an end tag for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @param elem    an Element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    protected void endTag(Element elem) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        if (synthesizedElement(elem)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        // write out end tags for item on stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        closeOutUnwantedEmbeddedTags(elem.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (inContent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            if (!newlineOutputed && !inPre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            newlineOutputed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            inContent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        if (!inPre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        if (matchNameAttribute(elem.getAttributes(), HTML.Tag.PRE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            inPre = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        write('<');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        write('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        write(elem.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        write('>');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Writes out comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @param elem    an Element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @exception BadLocationException if pos represents an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *            location within the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    protected void comment(Element elem) throws BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        AttributeSet as = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (matchNameAttribute(as, HTML.Tag.COMMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            Object comment = as.getAttribute(HTML.Attribute.COMMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            if (comment instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                writeComment((String)comment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                writeComment(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * Writes out comment string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @param string   the comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @exception BadLocationException if pos represents an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *            location within the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    void writeComment(String string) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        write("<!--");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if (string != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            write(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        write("-->");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * Writes out any additional comments (comments outside of the body)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * stored under the property HTMLDocument.AdditionalComments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    void writeAdditionalComments() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        Object comments = getDocument().getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                        (HTMLDocument.AdditionalComments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (comments instanceof Vector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            Vector v = (Vector)comments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            for (int counter = 0, maxCounter = v.size(); counter < maxCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                 counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                writeComment(v.elementAt(counter).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * Returns true if the element is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * synthesized element.  Currently we are only testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * for the p-implied tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    protected boolean synthesizedElement(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if (matchNameAttribute(elem.getAttributes(), HTML.Tag.IMPLIED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * Returns true if the StyleConstants.NameAttribute is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * equal to the tag that is passed in as a parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    protected boolean matchNameAttribute(AttributeSet attr, HTML.Tag tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        Object o = attr.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (o instanceof HTML.Tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            HTML.Tag name = (HTML.Tag) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            if (name == tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * Searches for embedded tags in the AttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * and writes them out.  It also stores these tags in a vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * so that when appropriate the corresponding end tags can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * written out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    protected void writeEmbeddedTags(AttributeSet attr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        // translate css attributes to html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        attr = convertToHTML(attr, oConvAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        Enumeration names = attr.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            if (name instanceof HTML.Tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                HTML.Tag tag = (HTML.Tag)name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                if (tag == HTML.Tag.FORM || tags.contains(tag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                write('<');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                write(tag.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                Object o = attr.getAttribute(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                if (o != null && o instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    writeAttributes((AttributeSet)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                write('>');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                tags.addElement(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                tagValues.addElement(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * Searches the attribute set for a tag, both of which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * are passed in as a parameter.  Returns true if no match is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    private boolean noMatchForTagInAttributes(AttributeSet attr, HTML.Tag t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                              Object tagValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        if (attr != null && attr.isDefined(t)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            Object newValue = attr.getAttribute(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            if ((tagValue == null) ? (newValue == null) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                (newValue != null && tagValue.equals(newValue))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Searches the attribute set and for each tag
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10100
diff changeset
   753
     * that is stored in the tag vector.  If the tag is not found,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * then the tag is removed from the vector and a corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * end tag is written out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    protected void closeOutUnwantedEmbeddedTags(AttributeSet attr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        tagsToRemove.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        // translate css attributes to html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        attr = convertToHTML(attr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        HTML.Tag t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        Object tValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        int firstIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        int size = tags.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        // First, find all the tags that need to be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        for (int i = size - 1; i >= 0; i--) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   772
            t = tags.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            tValue = tagValues.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            if ((attr == null) || noMatchForTagInAttributes(attr, t, tValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                firstIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                tagsToRemove.addElement(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if (firstIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            // Then close them out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            boolean removeAll = ((size - firstIndex) == tagsToRemove.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            for (int i = size - 1; i >= firstIndex; i--) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   783
                t = tags.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                if (removeAll || tagsToRemove.contains(t)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    tags.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    tagValues.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                write('<');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                write('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                write(t.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                write('>');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            // Have to output any tags after firstIndex that still remaing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            // as we closed them out, but they should remain open.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            size = tags.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            for (int i = firstIndex; i < size; i++) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   797
                t = tags.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                write('<');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                write(t.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                Object o = tagValues.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                if (o != null && o instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    writeAttributes((AttributeSet)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                write('>');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * Determines if the element associated with the attributeset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * is a TEXTAREA or SELECT.  If true, returns true else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    private boolean isFormElementWithContent(AttributeSet attr) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   816
        return matchNameAttribute(attr, HTML.Tag.TEXTAREA) ||
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   817
                matchNameAttribute(attr, HTML.Tag.SELECT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * Determines whether a the indentation needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * incremented.  Basically, if next is a child of current, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * next is NOT a synthesized element, the indent level will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * incremented.  If there is a parent-child relationship and "next"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * is a synthesized element, then its children must be indented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * This state is maintained by the indentNext boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @return boolean that's true if indent level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     *         needs incrementing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    private boolean indentNext = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    private boolean indentNeedsIncrementing(Element current, Element next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        if ((next.getParentElement() == current) && !inPre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            if (indentNext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                indentNext = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            } else if (synthesizedElement(next)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                indentNext = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            } else if (!synthesizedElement(current)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * Outputs the maps as elements. Maps are not stored as elements in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * the document, and as such this is used to output them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    void writeMaps(Enumeration maps) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        if (maps != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            while(maps.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                Map map = (Map)maps.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                String name = map.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                incrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                write("<map");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    write(" name=\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                    write(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                    write("\">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    write('>');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                incrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                // Output the areas
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                AttributeSet[] areas = map.getAreas();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                if (areas != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                    for (int counter = 0, maxCounter = areas.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                         counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                        indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                        write("<area");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                        writeAttributes(areas[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                        write("></area>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                        writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                decrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                write("</map>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                decrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * Outputs the styles as a single element. Styles are not stored as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * elements, but part of the document. For the time being styles are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * written out as a comment, inside a style tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    void writeStyles(StyleSheet sheet) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        if (sheet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            Enumeration styles = sheet.getStyleNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            if (styles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                boolean outputStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                while (styles.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                    String name = (String)styles.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    // Don't write out the default style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                    if (!StyleContext.DEFAULT_STYLE.equals(name) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                        writeStyle(name, sheet.getStyle(name), outputStyle)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                        outputStyle = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                if (outputStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    writeStyleEndTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * Outputs the named style. <code>outputStyle</code> indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * whether or not a style has been output yet. This will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * true if a style is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    boolean writeStyle(String name, Style style, boolean outputStyle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                 throws IOException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        boolean didOutputStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        Enumeration attributes = style.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        if (attributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            while (attributes.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                Object attribute = attributes.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                if (attribute instanceof CSS.Attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    String value = style.getAttribute(attribute).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                        if (!outputStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                            writeStyleStartTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                            outputStyle = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                        if (!didOutputStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                            didOutputStyle = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                            indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                            write(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                            write(" {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                            write(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                        write(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                        write(attribute.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                        write(": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                        write(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        if (didOutputStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            write(" }");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        return didOutputStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    void writeStyleStartTag() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        write("<style type=\"text/css\">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        incrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        write("<!--");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        incrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    void writeStyleEndTag() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        decrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        write("-->");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        decrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        write("</style>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        indentSmart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    // --- conversion support ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * Convert the give set of attributes to be html for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * the purpose of writing them out.  Any keys that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * have been converted will not appear in the resultant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * set.  Any keys not converted will appear in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * resultant set the same as the received set.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * This will put the converted values into <code>to</code>, unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * it is null in which case a temporary AttributeSet will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    AttributeSet convertToHTML(AttributeSet from, MutableAttributeSet to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        if (to == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            to = convAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        to.removeAttributes(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        if (writeCSS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            convertToHTML40(from, to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            convertToHTML32(from, to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        return to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * If true, the writer will emit CSS attributes in preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * to HTML tags/attributes (i.e. It will emit an HTML 4.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * style).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    private boolean writeCSS = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * Buffer for the purpose of attribute conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    private MutableAttributeSet convAttr = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * Buffer for the purpose of attribute conversion. This can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * used if convAttr is being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    private MutableAttributeSet oConvAttr = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * Create an older style of HTML attributes.  This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * convert character level attributes that have a StyleConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * mapping over to an HTML tag/attribute.  Other CSS attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * will be placed in an HTML style attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    private static void convertToHTML32(AttributeSet from, MutableAttributeSet to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        if (from == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        Enumeration keys = from.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        String value = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            Object key = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            if (key instanceof CSS.Attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                if ((key == CSS.Attribute.FONT_FAMILY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                    (key == CSS.Attribute.FONT_SIZE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    (key == CSS.Attribute.COLOR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    createFontAttribute((CSS.Attribute)key, from, to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                } else if (key == CSS.Attribute.FONT_WEIGHT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    // add a bold tag is weight is bold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    CSS.FontWeight weightValue = (CSS.FontWeight)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                        from.getAttribute(CSS.Attribute.FONT_WEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                    if ((weightValue != null) && (weightValue.getValue() > 400)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                        addAttribute(to, HTML.Tag.B, SimpleAttributeSet.EMPTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                } else if (key == CSS.Attribute.FONT_STYLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                    String s = from.getAttribute(key).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                    if (s.indexOf("italic") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                        addAttribute(to, HTML.Tag.I, SimpleAttributeSet.EMPTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                } else if (key == CSS.Attribute.TEXT_DECORATION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    String decor = from.getAttribute(key).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    if (decor.indexOf("underline") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                        addAttribute(to, HTML.Tag.U, SimpleAttributeSet.EMPTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                    if (decor.indexOf("line-through") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                        addAttribute(to, HTML.Tag.STRIKE, SimpleAttributeSet.EMPTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                } else if (key == CSS.Attribute.VERTICAL_ALIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                    String vAlign = from.getAttribute(key).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                    if (vAlign.indexOf("sup") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                        addAttribute(to, HTML.Tag.SUP, SimpleAttributeSet.EMPTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                    if (vAlign.indexOf("sub") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                        addAttribute(to, HTML.Tag.SUB, SimpleAttributeSet.EMPTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                } else if (key == CSS.Attribute.TEXT_ALIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    addAttribute(to, HTML.Attribute.ALIGN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                                    from.getAttribute(key).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    // default is to store in a HTML style attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    if (value.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                        value = value + "; ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                    value = value + key + ": " + from.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                Object attr = from.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                if (attr instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    attr = ((AttributeSet)attr).copyAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                addAttribute(to, key, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        if (value.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            to.addAttribute(HTML.Attribute.STYLE, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * Add an attribute only if it doesn't exist so that we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * loose information replacing it with SimpleAttributeSet.EMPTY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    private static void addAttribute(MutableAttributeSet to, Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        Object attr = to.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        if (attr == null || attr == SimpleAttributeSet.EMPTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            to.addAttribute(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            if (attr instanceof MutableAttributeSet &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                value instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                ((MutableAttributeSet)attr).addAttributes((AttributeSet)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * Create/update an HTML &lt;font&gt; tag attribute.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * value of the attribute should be a MutableAttributeSet so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * that the attributes can be updated as they are discovered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    private static void createFontAttribute(CSS.Attribute a, AttributeSet from,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                                    MutableAttributeSet to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        MutableAttributeSet fontAttr = (MutableAttributeSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            to.getAttribute(HTML.Tag.FONT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        if (fontAttr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            fontAttr = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            to.addAttribute(HTML.Tag.FONT, fontAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        // edit the parameters to the font tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        String htmlValue = from.getAttribute(a).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        if (a == CSS.Attribute.FONT_FAMILY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            fontAttr.addAttribute(HTML.Attribute.FACE, htmlValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        } else if (a == CSS.Attribute.FONT_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            fontAttr.addAttribute(HTML.Attribute.SIZE, htmlValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        } else if (a == CSS.Attribute.COLOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            fontAttr.addAttribute(HTML.Attribute.COLOR, htmlValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * Copies the given AttributeSet to a new set, converting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * any CSS attributes found to arguments of an HTML style
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    private static void convertToHTML40(AttributeSet from, MutableAttributeSet to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        Enumeration keys = from.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        String value = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            Object key = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            if (key instanceof CSS.Attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                value = value + " " + key + "=" + from.getAttribute(key) + ";";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                to.addAttribute(key, from.getAttribute(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        if (value.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            to.addAttribute(HTML.Attribute.STYLE, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    // Overrides the writing methods to only break a string when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    // canBreakString is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    // In a future release it is likely AbstractWriter will get this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    // functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * Writes the line separator. This is overriden to make sure we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * replace the newline content in case it is outside normal ascii.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    protected void writeLineSeparator() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        boolean oldReplace = replaceEntities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        replaceEntities = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        super.writeLineSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        replaceEntities = oldReplace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        indented = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * This method is overriden to map any character entities, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * &lt; to &amp;lt;. <code>super.output</code> will be invoked to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * write the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    protected void output(char[] chars, int start, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                   throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        if (!replaceEntities) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            super.output(chars, start, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        int last = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        length += start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        for (int counter = start; counter < length; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            // This will change, we need better support character level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            // entities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            switch(chars[counter]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                // Character level entities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            case '<':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                if (counter > last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                    super.output(chars, last, counter - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                last = counter + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                output("&lt;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            case '>':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                if (counter > last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                    super.output(chars, last, counter - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                last = counter + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                output("&gt;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            case '&':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                if (counter > last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                    super.output(chars, last, counter - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                last = counter + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                output("&amp;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            case '"':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                if (counter > last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                    super.output(chars, last, counter - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                last = counter + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                output("&quot;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                // Special characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                if (chars[counter] < ' ' || chars[counter] > 127) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                    if (counter > last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                        super.output(chars, last, counter - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                    last = counter + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                    // If the character is outside of ascii, write the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                    // numeric value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                    output("&#");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                    output(String.valueOf((int)chars[counter]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                    output(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        if (last < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            super.output(chars, last, length - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * This directly invokes super's <code>output</code> after converting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * <code>string</code> to a char[].
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    private void output(String string) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        int length = string.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        if (tempChars == null || tempChars.length < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            tempChars = new char[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        string.getChars(0, length, tempChars, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        super.output(tempChars, 0, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    private boolean indented = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * Writes indent only once per line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    private void indentSmart() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        if (!indented) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            indent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            indented = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
}