jdk/src/share/classes/javax/swing/text/html/MinimalHTMLWriter.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7014 eb4fcf73ee99
child 25148 1026dc322690
child 25193 187a455af8f8
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7014
diff changeset
     2
 * Copyright (c) 1998, 2010, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.Writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * MinimalHTMLWriter is a fallback writer used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * HTMLEditorKit to write out HTML for a document that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * is a not produced by the EditorKit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The format for the document is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * &lt;html&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *   &lt;head&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *     &lt;style&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *        &lt;!-- list of named styles
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *         p.normal {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *            font-family: SansSerif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *            margin-height: 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *            font-size: 14
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *        --&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *      &lt;/style&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   &lt;/head&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   &lt;body&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *    &lt;p style=normal&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *        <b>Bold, italic, and underline attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *        of the run are emitted as HTML tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *        The remaining attributes are emitted as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *        part of the style attribute of a &lt;span&gt; tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *        The syntax is similar to inline styles.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *    &lt;/p&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   &lt;/body&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * &lt;/html&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @author Sunita Mani
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public class MinimalHTMLWriter extends AbstractWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * These static finals are used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * tweak and query the fontMask about which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * of these tags need to be generated or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * terminated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final int BOLD = 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final int ITALIC = 0x02;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final int UNDERLINE = 0x04;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // Used to map StyleConstants to CSS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final CSS css = new CSS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private int fontMask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    int startOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    int endOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Stores the attributes of the previous run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Used to compare with the current run's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * attributeset.  If identical, then a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * &lt;span&gt; tag is not emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private AttributeSet fontAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Maps from style name as held by the Document, to the archived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * style name (style name written out). These may differ.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   100
    private Hashtable<String, String> styleNameMapping;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Creates a new MinimalHTMLWriter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param w  Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param doc StyledDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public MinimalHTMLWriter(Writer w, StyledDocument doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        super(w, doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Creates a new MinimalHTMLWriter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param w  Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param doc StyledDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param pos The location in the document to fetch the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *   content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param len The amount to write out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public MinimalHTMLWriter(Writer w, StyledDocument doc, int pos, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        super(w, doc, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Generates HTML output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * from a StyledDocument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @exception BadLocationException if pos represents an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *            location within the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void write() throws IOException, BadLocationException {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   137
        styleNameMapping = new Hashtable<String, String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        writeStartTag("<html>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        writeHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        writeBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        writeEndTag("</html>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Writes out all the attributes for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * following types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *  StyleConstants.ParagraphConstants,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *  StyleConstants.CharacterConstants,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *  StyleConstants.FontConstants,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *  StyleConstants.ColorConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * The attribute name and value are separated by a colon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Each pair is separated by a semicolon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    protected void writeAttributes(AttributeSet attr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        Enumeration attributeNames = attr.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        while (attributeNames.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            Object name = attributeNames.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if ((name instanceof StyleConstants.ParagraphConstants) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                (name instanceof StyleConstants.CharacterConstants) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                (name instanceof StyleConstants.FontConstants) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                (name instanceof StyleConstants.ColorConstants)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                indent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                write(name.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                write(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                write(css.styleConstantsValueToCSSValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                      ((StyleConstants)name, attr.getAttribute(name)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                      toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                write(';');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                write(NEWLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Writes out text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    protected void text(Element elem) throws IOException, BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        String contentStr = getText(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if ((contentStr.length() > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            (contentStr.charAt(contentStr.length()-1) == NEWLINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            contentStr = contentStr.substring(0, contentStr.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (contentStr.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            write(contentStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Writes out a start tag appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * indented.  Also increments the indent level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    protected void writeStartTag(String tag) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        indent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        write(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        write(NEWLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        incrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Writes out an end tag appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * indented.  Also decrements the indent level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    protected void writeEndTag(String endTag) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        decrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        indent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        write(endTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        write(NEWLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Writes out the &lt;head&gt; and &lt;style&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * tags, and then invokes writeStyles() to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * out all the named styles as the content of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * &lt;style&gt; tag.  The content is surrounded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * valid HTML comment markers to ensure that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * document is viewable in applications/browsers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * that do not support the tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    protected void writeHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        writeStartTag("<head>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        writeStartTag("<style>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        writeStartTag("<!--");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        writeStyles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        writeEndTag("-->");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        writeEndTag("</style>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        writeEndTag("</head>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Writes out all the named styles as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * content of the &lt;style&gt; tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
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
    protected void writeStyles() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         *  Access to DefaultStyledDocument done to workaround
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         *  a missing API in styled document to access the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         *  stylenames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        DefaultStyledDocument styledDoc =  ((DefaultStyledDocument)getDocument());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        Enumeration styleNames = styledDoc.getStyleNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        while (styleNames.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            Style s = styledDoc.getStyle((String)styleNames.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            /** PENDING: Once the name attribute is removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                from the list we check check for 0. **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (s.getAttributeCount() == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                s.isDefined(StyleConstants.NameAttribute)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            indent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            write("p." + addStyleName(s.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            write(" {\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            incrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            writeAttributes(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            decrIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            indent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            write("}\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Iterates over the elements in the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * and processes elements based on whether they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * branch elements or leaf elements.  This method specially handles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * leaf elements that are text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    protected void writeBody() throws IOException, BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        ElementIterator it = getElementIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
          This will be a section element for a styled document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
          We represent this element in HTML as the body tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
          Therefore we ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        it.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   299
        Element next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        writeStartTag("<body>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        boolean inContent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        while((next = it.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (!inRange(next)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (next instanceof AbstractDocument.BranchElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                if (inContent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    writeEndParagraph();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    inContent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    fontMask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                writeStartParagraph(next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            } else if (isText(next)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                writeContent(next, !inContent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                inContent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                writeLeaf(next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                inContent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (inContent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            writeEndParagraph();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        writeEndTag("</body>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Emits an end tag for a &lt;p&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * tag.  Before writing out the tag, this method ensures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * that all other tags that have been opened are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * appropriately closed off.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    protected void writeEndParagraph() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        writeEndMask(fontMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (inFontTag()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            endSpanTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            write(NEWLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        writeEndTag("</p>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Emits the start tag for a paragraph. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * the paragraph has a named style associated with it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * then this method also generates a class attribute for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * &lt;p&gt; tag and sets its value to be the name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    protected void writeStartParagraph(Element elem) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        AttributeSet attr = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        Object resolveAttr = attr.getAttribute(StyleConstants.ResolveAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (resolveAttr instanceof StyleContext.NamedStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            writeStartTag("<p class=" + mapStyleName(((StyleContext.NamedStyle)resolveAttr).getName()) + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            writeStartTag("<p>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Responsible for writing out other non-text leaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    protected void writeLeaf(Element elem) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        indent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (elem.getName() == StyleConstants.IconElementName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            writeImage(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } else if (elem.getName() == StyleConstants.ComponentElementName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            writeComponent(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Responsible for handling Icon Elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * deliberately unimplemented.  How to implement this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * an issue of policy.  For example, if you're generating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * an &lt;img&gt; tag, how should you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * represent the src attribute (the location of the image)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * In certain cases it could be a URL, in others it could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * be read from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param elem element of type StyleConstants.IconElementName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    protected void writeImage(Element elem) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Responsible for handling Component Elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * deliberately unimplemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * How this method is implemented is a matter of policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    protected void writeComponent(Element elem) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Returns true if the element is a text element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    protected boolean isText(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        return (elem.getName() == AbstractDocument.ContentElementName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Writes out the attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * in an HTML-compliant manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @exception BadLocationException if pos represents an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *            location within the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    protected void writeContent(Element elem,  boolean needsIndenting)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        throws IOException, BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        AttributeSet attr = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        writeNonHTMLAttributes(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (needsIndenting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            indent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        writeHTMLTags(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        text(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Generates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * bold &lt;b&gt;, italic &lt;i&gt;, and &lt;u&gt; tags for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * text based on its attribute settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    protected void writeHTMLTags(AttributeSet attr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        int oldMask = fontMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        setFontMask(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        int endMask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        int startMask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if ((oldMask & BOLD) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            if ((fontMask & BOLD) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                endMask |= BOLD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        } else if ((fontMask & BOLD) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            startMask |= BOLD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if ((oldMask & ITALIC) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if ((fontMask & ITALIC) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                endMask |= ITALIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        } else if ((fontMask & ITALIC) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            startMask |= ITALIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if ((oldMask & UNDERLINE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            if ((fontMask & UNDERLINE) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                endMask |= UNDERLINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        } else if ((fontMask & UNDERLINE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            startMask |= UNDERLINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        writeEndMask(endMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        writeStartMask(startMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Tweaks the appropriate bits of fontMask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * to reflect whether the text is to be displayed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * bold, italic, and/or with an underline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    private void setFontMask(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (StyleConstants.isBold(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            fontMask |= BOLD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (StyleConstants.isItalic(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            fontMask |= ITALIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (StyleConstants.isUnderline(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            fontMask |= UNDERLINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Writes out start tags &lt;u&gt;, &lt;i&gt;, and &lt;b&gt; based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * the mask settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    private void writeStartMask(int mask) throws IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (mask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            if ((mask & UNDERLINE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                write("<u>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            if ((mask & ITALIC) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                write("<i>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            if ((mask & BOLD) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                write("<b>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * Writes out end tags for &lt;u&gt;, &lt;i&gt;, and &lt;b&gt; based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * the mask settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private void writeEndMask(int mask) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (mask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            if ((mask & BOLD) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                write("</b>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            if ((mask & ITALIC) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                write("</i>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if ((mask & UNDERLINE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                write("</u>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
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
     * Writes out the remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * character-level attributes (attributes other than bold,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * italic, and underline) in an HTML-compliant way.  Given that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * attributes such as font family and font size have no direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * mapping to HTML tags, a &lt;span&gt; tag is generated and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * style attribute is set to contain the list of remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * attributes just like inline styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    protected void writeNonHTMLAttributes(AttributeSet attr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        String style = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        String separator = "; ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (inFontTag() && fontAttributes.isEqual(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        boolean first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        Color color = (Color)attr.getAttribute(StyleConstants.Foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if (color != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            style += "color: " + css.styleConstantsValueToCSSValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                                    ((StyleConstants)StyleConstants.Foreground,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                     color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        Integer size = (Integer)attr.getAttribute(StyleConstants.FontSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (size != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if (!first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                style += separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            style += "font-size: " + size.intValue() + "pt";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        String family = (String)attr.getAttribute(StyleConstants.FontFamily);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (family != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            if (!first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                style += separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            style += "font-family: " + family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (style.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            if (fontMask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                writeEndMask(fontMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                fontMask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            startSpanTag(style);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            fontAttributes = attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        else if (fontAttributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            writeEndMask(fontMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            fontMask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            endSpanTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
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
     * Returns true if we are currently in a &lt;font&gt; tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    protected boolean inFontTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        return (fontAttributes != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * This is no longer used, instead &lt;span&gt; will be written out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * Writes out an end tag for the &lt;font&gt; tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    protected void endFontTag() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        write(NEWLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        writeEndTag("</font>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        fontAttributes = 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
     * This is no longer used, instead &lt;span&gt; will be written out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * Writes out a start tag for the &lt;font&gt; tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * Because font tags cannot be nested,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * this method closes out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * any enclosing font tag before writing out a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * new start tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    protected void startFontTag(String style) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        boolean callIndent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (inFontTag()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            endFontTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            callIndent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        writeStartTag("<font style=\"" + style + "\">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if (callIndent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            indent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Writes out a start tag for the &lt;font&gt; tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Because font tags cannot be nested,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * this method closes out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * any enclosing font tag before writing out a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * new start tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    private void startSpanTag(String style) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        boolean callIndent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        if (inFontTag()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            endSpanTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            callIndent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        writeStartTag("<span style=\"" + style + "\">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (callIndent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            indent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * Writes out an end tag for the &lt;span&gt; tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    private void endSpanTag() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        write(NEWLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        writeEndTag("</span>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        fontAttributes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Adds the style named <code>style</code> to the style mapping. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * returns the name that should be used when outputting. CSS does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * allow the full Unicode set to be used as a style name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    private String addStyleName(String style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        if (styleNameMapping == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   694
        StringBuilder sb = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        for (int counter = style.length() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            if (!isValidCharacter(style.charAt(counter))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                if (sb == null) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   698
                    sb = new StringBuilder(style);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                sb.setCharAt(counter, 'a');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        String mappedName = (sb != null) ? sb.toString() : style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        while (styleNameMapping.get(mappedName) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            mappedName = mappedName + 'x';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        styleNameMapping.put(style, mappedName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        return mappedName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * Returns the mapped style name corresponding to <code>style</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    private String mapStyleName(String style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        if (styleNameMapping == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   718
        String retValue = styleNameMapping.get(style);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        return (retValue == null) ? style : retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    private boolean isValidCharacter(char character) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return ((character >= 'a' && character <= 'z') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                (character >= 'A' && character <= 'Z'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
}