jdk/src/share/classes/javax/swing/text/html/HTMLDocument.java
author darcy
Wed, 22 Jan 2014 23:20:58 -0800
changeset 22567 5816a47fa4dd
parent 22260 c9185e010e03
child 22574 7f8ce0c8c20a
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
/*
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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 java.awt.font.TextAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.undo.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.swing.SwingUtilities2;
5763
9a942d6fb0aa 6636983: Japanese text does not display correctly in a JEditorPane
rupashka
parents: 5506
diff changeset
    37
import static sun.swing.SwingUtilities2.IMPLIED_CR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A document that models HTML.  The purpose of this model is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * support both browsing and editing.  As a result, the structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * described by an HTML document is not exactly replicated by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The element structure that is modeled by default, is built by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * class <code>HTMLDocument.HTMLReader</code>, which implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <code>HTMLEditorKit.ParserCallback</code> protocol that the parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * expects.  To change the structure one can subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <code>HTMLReader</code>, and reimplement the method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * #getReader(int)} to return the new reader implementation.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * documentation for <code>HTMLReader</code> should be consulted for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * the details of the default structure created.  The intent is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the document be non-lossy (although reproducing the HTML format may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * result in a different format).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>The document models only HTML, and makes no attempt to store
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * view attributes in it.  The elements are identified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <code>StyleContext.NameAttribute</code> attribute, which should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * always have a value of type <code>HTML.Tag</code> that identifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the kind of element.  Some of the elements (such as comments) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * synthesized.  The <code>HTMLFactory</code> uses this attribute to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * determine what kind of view to build.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>This document supports incremental loading.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <code>TokenThreshold</code> property controls how much of the parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * is buffered before trying to update the element structure of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * document.  This property is set by the <code>EditorKit</code> so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * that subclasses can disable it.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>The <code>Base</code> property determines the URL against which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * relative URLs are resolved.  By default, this will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <code>Document.StreamDescriptionProperty</code> if the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * property is a URL.  If a &lt;BASE&gt; tag is encountered, the base
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * will become the URL specified by that tag.  Because the base URL is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * a property, it can of course be set directly.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>The default content storage mechanism for this document is a gap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * buffer (<code>GapContent</code>).  Alternatives can be supplied by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * using the constructor that takes a <code>Content</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * implementation.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <h2>Modifying HTMLDocument</h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>In addition to the methods provided by Document and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * StyledDocument for mutating an HTMLDocument, HTMLDocument provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * a number of convenience methods.  The following methods can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * to insert HTML content into an existing document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *   <li>{@link #setInnerHTML(Element, String)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   <li>{@link #setOuterHTML(Element, String)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   <li>{@link #insertBeforeStart(Element, String)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   <li>{@link #insertAfterStart(Element, String)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   <li>{@link #insertBeforeEnd(Element, String)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   <li>{@link #insertAfterEnd(Element, String)}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <p>The following examples illustrate using these methods.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * example assumes the HTML document is initialized in the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * way:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * JEditorPane p = new JEditorPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * p.setContentType("text/html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * p.setText("..."); // Document text is provided below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * HTMLDocument d = (HTMLDocument) p.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <p>With the following HTML content:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   110
 * &lt;html&gt;
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   111
 *   &lt;head&gt;
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   112
 *     &lt;title&gt;An example HTMLDocument&lt;/title&gt;
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   113
 *     &lt;style type="text/css"&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *       div { background-color: silver; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *       ul { color: red; }
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   116
 *     &lt;/style&gt;
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   117
 *   &lt;/head&gt;
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   118
 *   &lt;body&gt;
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   119
 *     &lt;div id="BOX"&gt;
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   120
 *       &lt;p&gt;Paragraph 1&lt;/p&gt;
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   121
 *       &lt;p&gt;Paragraph 2&lt;/p&gt;
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   122
 *     &lt;/div&gt;
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   123
 *   &lt;/body&gt;
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   124
 * &lt;/html&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <p>All the methods for modifying an HTML document require an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * Element}.  Elements can be obtained from an HTML document by using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * the method {@link #getElement(Element e, Object attribute, Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * value)}.  It returns the first descendant element that contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * specified attribute with the given value, in depth-first order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * For example, <code>d.getElement(d.getDefaultRootElement(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * StyleConstants.NameAttribute, HTML.Tag.P)</code> returns the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * paragraph element.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <p>A convenient shortcut for locating elements is the method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * #getElement(String)}; returns an element whose <code>ID</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * attribute matches the specified value.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <code>d.getElement("BOX")</code> returns the <code>DIV</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * element.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <p>The {@link #getIterator(HTML.Tag t)} method can also be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * finding all occurrences of the specified HTML tag in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <h3>Inserting elements</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <p>Elements can be inserted before or after the existing children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * of any non-leaf element by using the methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * <code>insertAfterStart</code> and <code>insertBeforeEnd</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * For example, if <code>e</code> is the <code>DIV</code> element,
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   152
 * <code>d.insertAfterStart(e, "&lt;ul&gt;&lt;li&gt;List
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   153
 * Item&lt;/li&gt;&lt;/ul&gt;")</code> inserts the list before the first
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   154
 * paragraph, and <code>d.insertBeforeEnd(e, "&lt;ul&gt;&lt;li&gt;List
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   155
 * Item&lt;/li&gt;&lt;/ul&gt;")</code> inserts the list after the last
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * paragraph.  The <code>DIV</code> block becomes the parent of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * newly inserted elements.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * <p>Sibling elements can be inserted before or after any element by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * using the methods <code>insertBeforeStart</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * <code>insertAfterEnd</code>.  For example, if <code>e</code> is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * <code>DIV</code> element, <code>d.insertBeforeStart(e,
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   163
 * "&lt;ul&gt;&lt;li&gt;List Item&lt;/li&gt;&lt;/ul&gt;")</code> inserts the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * before the <code>DIV</code> element, and <code>d.insertAfterEnd(e,
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   165
 * "&lt;ul&gt;&lt;li&gt;List Item&lt;/li&gt;&lt;/ul&gt;")</code> inserts the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * after the <code>DIV</code> element.  The newly inserted elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * become siblings of the <code>DIV</code> element.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * <h3>Replacing elements</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <p>Elements and all their descendants can be replaced by using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * methods <code>setInnerHTML</code> and <code>setOuterHTML</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * For example, if <code>e</code> is the <code>DIV</code> element,
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   174
 * <code>d.setInnerHTML(e, "&lt;ul&gt;&lt;li&gt;List
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   175
 * Item&lt;/li&gt;&lt;/ul&gt;")</code> replaces all children paragraphs with
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   176
 * the list, and <code>d.setOuterHTML(e, "&lt;ul&gt;&lt;li&gt;List
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   177
 * Item&lt;/li&gt;&lt;/ul&gt;")</code> replaces the <code>DIV</code> element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * itself.  In latter case the parent of the list is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * <code>BODY</code> element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * <h3>Summary</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * <p>The following table shows the example document and the results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * of various methods described above.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
   186
 * <table border=1 cellspacing=0 summary="HTML Content of example above">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *   <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *     <th>Example</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *     <th><code>insertAfterStart</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *     <th><code>insertBeforeEnd</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *     <th><code>insertBeforeStart</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *     <th><code>insertAfterEnd</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *     <th><code>setInnerHTML</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *     <th><code>setOuterHTML</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *   </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *   <tr valign="top">
20451
4cedf4e1560a 8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents: 20158
diff changeset
   197
 *     <td style="white-space:nowrap">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *       <div style="background-color: silver;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *         <p>Paragraph 1</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *         <p>Paragraph 2</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *       </div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * <!--insertAfterStart-->
20451
4cedf4e1560a 8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents: 20158
diff changeset
   204
 *     <td style="white-space:nowrap">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *       <div style="background-color: silver;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 *         <ul style="color: red;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *           <li>List Item</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *         </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *         <p>Paragraph 1</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *         <p>Paragraph 2</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 *       </div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * <!--insertBeforeEnd-->
20451
4cedf4e1560a 8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents: 20158
diff changeset
   214
 *     <td style="white-space:nowrap">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 *       <div style="background-color: silver;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 *         <p>Paragraph 1</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *         <p>Paragraph 2</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 *         <ul style="color: red;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *           <li>List Item</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *         </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 *       </div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * <!--insertBeforeStart-->
20451
4cedf4e1560a 8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents: 20158
diff changeset
   224
 *     <td style="white-space:nowrap">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 *       <ul style="color: red;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 *         <li>List Item</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *       </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 *       <div style="background-color: silver;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 *         <p>Paragraph 1</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 *         <p>Paragraph 2</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *       </div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * <!--insertAfterEnd-->
20451
4cedf4e1560a 8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents: 20158
diff changeset
   234
 *     <td style="white-space:nowrap">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *       <div style="background-color: silver;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *         <p>Paragraph 1</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *         <p>Paragraph 2</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *       </div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *       <ul style="color: red;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *         <li>List Item</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *       </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * <!--setInnerHTML-->
20451
4cedf4e1560a 8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents: 20158
diff changeset
   244
 *     <td style="white-space:nowrap">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *       <div style="background-color: silver;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *         <ul style="color: red;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 *           <li>List Item</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *         </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *       </div>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * <!--setOuterHTML-->
20451
4cedf4e1560a 8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents: 20158
diff changeset
   252
 *     <td style="white-space:nowrap">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 *       <ul style="color: red;">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 *         <li>List Item</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 *       </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 *     </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 *   </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * <p><strong>Warning:</strong> Serialized objects of this class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * not be compatible with future Swing releases. The current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * serialization support is appropriate for short term storage or RMI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * between applications running the same version of Swing.  As of 1.4,
21244
7c2ac5ca05a2 8026021: more fix of javadoc errors and warnings reported by doclint, see the description
cl
parents: 20455
diff changeset
   264
 * support for long term storage of all JavaBeans&trade;
7c2ac5ca05a2 8026021: more fix of javadoc errors and warnings reported by doclint, see the description
cl
parents: 20455
diff changeset
   265
 * has been added to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * <code>java.beans</code> package.  Please see {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 * java.beans.XMLEncoder}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 * @author  Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * @author  Sunita Mani
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
public class HTMLDocument extends DefaultStyledDocument {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Constructs an HTML document using the default buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * and a default <code>StyleSheet</code>.  This is a convenience
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * method for the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <code>HTMLDocument(Content, StyleSheet)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public HTMLDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        this(new GapContent(BUFFER_SIZE_DEFAULT), new StyleSheet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Constructs an HTML document with the default content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * storage implementation and the specified style/attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * storage mechanism.  This is a convenience method for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <code>HTMLDocument(Content, StyleSheet)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param styles  the styles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public HTMLDocument(StyleSheet styles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        this(new GapContent(BUFFER_SIZE_DEFAULT), styles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Constructs an HTML document with the given content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * storage implementation and the given style/attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * storage mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param c  the container for the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param styles the styles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public HTMLDocument(Content c, StyleSheet styles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        super(c, styles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Fetches the reader for the parser to use when loading the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * with HTML.  This is implemented to return an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <code>HTMLDocument.HTMLReader</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Subclasses can reimplement this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * method to change how the document gets structured if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * (For example, to handle custom tags, or structurally represent character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * style elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param pos the starting position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @return the reader used by the parser to load the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public HTMLEditorKit.ParserCallback getReader(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        Object desc = getProperty(Document.StreamDescriptionProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (desc instanceof URL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            setBase((URL)desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        HTMLReader reader = new HTMLReader(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return reader;
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
     * Returns the reader for the parser to use to load the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * with HTML.  This is implemented to return an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <code>HTMLDocument.HTMLReader</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Subclasses can reimplement this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * method to change how the document gets structured if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * (For example, to handle custom tags, or structurally represent character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * style elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p>This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <code>getReader(int, int, int, HTML.Tag, TRUE)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param popDepth   the number of <code>ElementSpec.EndTagTypes</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *          to generate before inserting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param pushDepth  the number of <code>ElementSpec.StartTagTypes</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *          with a direction of <code>ElementSpec.JoinNextDirection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *          that should be generated before inserting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *          but after the end tags have been generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param insertTag  the first tag to start inserting into document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return the reader used by the parser to load the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public HTMLEditorKit.ParserCallback getReader(int pos, int popDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                                  int pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                                  HTML.Tag insertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return getReader(pos, popDepth, pushDepth, insertTag, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Fetches the reader for the parser to use to load the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * with HTML.  This is implemented to return an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * HTMLDocument.HTMLReader.  Subclasses can reimplement this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * method to change how the document get structured if desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * (e.g. to handle custom tags, structurally represent character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * style elements, etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param popDepth   the number of <code>ElementSpec.EndTagTypes</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *          to generate before inserting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param pushDepth  the number of <code>ElementSpec.StartTagTypes</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *          with a direction of <code>ElementSpec.JoinNextDirection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *          that should be generated before inserting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *          but after the end tags have been generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param insertTag  the first tag to start inserting into document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param insertInsertTag  false if all the Elements after insertTag should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *        be inserted; otherwise insertTag will be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return the reader used by the parser to load the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    HTMLEditorKit.ParserCallback getReader(int pos, int popDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                           int pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                           HTML.Tag insertTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                           boolean insertInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        Object desc = getProperty(Document.StreamDescriptionProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (desc instanceof URL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            setBase((URL)desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        HTMLReader reader = new HTMLReader(pos, popDepth, pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                           insertTag, insertInsertTag, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                           true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Returns the location to resolve relative URLs against.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * default this will be the document's URL if the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * was loaded from a URL.  If a base tag is found and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * can be parsed, it will be used as the base location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @return the base location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public URL getBase() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return base;
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
     * Sets the location to resolve relative URLs against.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * default this will be the document's URL if the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * was loaded from a URL.  If a base tag is found and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * can be parsed, it will be used as the base location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <p>This also sets the base of the <code>StyleSheet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * to be <code>u</code> as well as the base of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param u  the desired base URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public void setBase(URL u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        base = u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        getStyleSheet().setBase(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Inserts new elements in bulk.  This is how elements get created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * in the document.  The parsing determines what structure is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * and creates the specification as a set of tokens that describe the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * edit while leaving the document free of a write-lock.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * can then be called in bursts by the reader to acquire a write-lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * for a shorter duration (i.e. while the document is actually being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * altered).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param offset the starting offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @param data the element data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @exception BadLocationException  if the given position does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *   represent a valid location in the associated document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    protected void insert(int offset, ElementSpec[] data) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        super.insert(offset, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Updates document structure as a result of text insertion.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * will happen within a write lock.  This implementation simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * parses the inserted content for line breaks and builds up a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * of instructions for the element buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param chng a description of the document change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param attr the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if(attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            attr = contentAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // If this is the composed text element, merge the content attribute to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        else if (attr.isDefined(StyleConstants.ComposedTextAttribute)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            ((MutableAttributeSet)attr).addAttributes(contentAttributeSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (attr.isDefined(IMPLIED_CR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            ((MutableAttributeSet)attr).removeAttribute(IMPLIED_CR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        super.insertUpdate(chng, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Replaces the contents of the document with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * element specifications.  This is called before insert if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * the loading is done in bursts.  This is the only method called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * if loading the document entirely in one burst.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param data  the new contents of the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    protected void create(ElementSpec[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        super.create(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Sets attributes for a paragraph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * are not. Please see
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20451
diff changeset
   477
     * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
10316
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 10100
diff changeset
   478
     * in Swing</A> for more information.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @param offset the offset into the paragraph (must be at least 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param length the number of characters affected (must be at least 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @param s the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @param replace whether to replace existing attributes, or merge them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public void setParagraphAttributes(int offset, int length, AttributeSet s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                       boolean replace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            // Make sure we send out a change for the length of the paragraph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            int end = Math.min(offset + length, getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            Element e = getParagraphElement(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            offset = e.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            e = getParagraphElement(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            length = Math.max(0, e.getEndOffset() - offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            DefaultDocumentEvent changes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                new DefaultDocumentEvent(offset, length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                                         DocumentEvent.EventType.CHANGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            AttributeSet sCopy = s.copyAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            int lastEnd = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            for (int pos = offset; pos <= end; pos = lastEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                Element paragraph = getParagraphElement(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                if (lastEnd == paragraph.getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    lastEnd++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    lastEnd = paragraph.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                MutableAttributeSet attr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    (MutableAttributeSet) paragraph.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                changes.addEdit(new AttributeUndoableEdit(paragraph, sCopy, replace));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                if (replace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    attr.removeAttributes(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                attr.addAttributes(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            changes.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            fireChangedUpdate(changes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
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
     * Fetches the <code>StyleSheet</code> with the document-specific display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * rules (CSS) that were specified in the HTML document itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @return the <code>StyleSheet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public StyleSheet getStyleSheet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        return (StyleSheet) getAttributeContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Fetches an iterator for the specified HTML tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * This can be used for things like iterating over the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * set of anchors contained, or iterating over the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @param t the requested <code>HTML.Tag</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @return the <code>Iterator</code> for the given HTML tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @see javax.swing.text.html.HTML.Tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    public Iterator getIterator(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (t.isBlock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            // TBD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        return new LeafIterator(t, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Creates a document leaf element that directly represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * text (doesn't have any children).  This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * to return an element of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * <code>HTMLDocument.RunElement</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @param parent the parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param a the attributes for the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param p0 the beginning of the range (must be at least 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @param p1 the end of the range (must be at least p0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @return the new element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    protected Element createLeafElement(Element parent, AttributeSet a, int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return new RunElement(parent, a, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Creates a document branch element, that can contain other elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * This is implemented to return an element of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * <code>HTMLDocument.BlockElement</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @param parent the parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @param a the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @return the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    protected Element createBranchElement(Element parent, AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        return new BlockElement(parent, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Creates the root element to be used to represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * default document structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @return the element base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    protected AbstractElement createDefaultRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        // grabs a write-lock for this initialization and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        // abandon it during initialization so in normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        // operation we can detect an illegitimate attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        // to mutate attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        MutableAttributeSet a = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.HTML);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        BlockElement html = new BlockElement(null, a.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        a.removeAttributes(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.BODY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        BlockElement body = new BlockElement(html, a.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        a.removeAttributes(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.P);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        getStyleSheet().addCSSAttributeFromHTML(a, CSS.Attribute.MARGIN_TOP, "0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        BlockElement paragraph = new BlockElement(body, a.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        a.removeAttributes(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        RunElement brk = new RunElement(paragraph, a, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        Element[] buff = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        buff[0] = brk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        paragraph.replace(0, 0, buff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        buff[0] = paragraph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        body.replace(0, 0, buff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        buff[0] = body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        html.replace(0, 0, buff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Sets the number of tokens to buffer before trying to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * the documents element structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @param n  the number of tokens to buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    public void setTokenThreshold(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        putProperty(TokenThreshold, new Integer(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Gets the number of tokens to buffer before trying to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * the documents element structure.  The default value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <code>Integer.MAX_VALUE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @return the number of tokens to buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    public int getTokenThreshold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        Integer i = (Integer) getProperty(TokenThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if (i != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            return i.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * Determines how unknown tags are handled by the parser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * If set to true, unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * tags are put in the model, otherwise they are dropped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @param preservesTags  true if unknown tags should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *          saved in the model, otherwise tags are dropped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @see javax.swing.text.html.HTML.Tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    public void setPreservesUnknownTags(boolean preservesTags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        preservesUnknownTags = preservesTags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Returns the behavior the parser observes when encountering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * unknown tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @see javax.swing.text.html.HTML.Tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @return true if unknown tags are to be preserved when parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    public boolean getPreservesUnknownTags() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return preservesUnknownTags;
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
     * Processes <code>HyperlinkEvents</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * are generated by documents in an HTML frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * The <code>HyperlinkEvent</code> type, as the parameter suggests,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * is <code>HTMLFrameHyperlinkEvent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * In addition to the typical information contained in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * <code>HyperlinkEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * this event contains the element that corresponds to the frame in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * which the click happened (the source element) and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * target name.  The target name has 4 possible values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * <li>  _self
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * <li>  _parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <li>  _top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * <li>  a named frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * If target is _self, the action is to change the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * <code>HTML.Attribute.SRC</code> attribute and fires a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * <code>ChangedUpdate</code> event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * If the target is _parent, then it deletes the parent element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * which is a &lt;FRAMESET&gt; element, and inserts a new &lt;FRAME&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * element, and sets its <code>HTML.Attribute.SRC</code> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * to have a value equal to the destination URL and fire a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * <code>RemovedUpdate</code> and <code>InsertUpdate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * If the target is _top, this method does nothing. In the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * of the view for a frame, namely the <code>FrameView</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * the processing of _top is handled.  Given that _top implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * replacing the entire document, it made sense to handle this outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * of the document that it will replace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * If the target is a named frame, then the element hierarchy is searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * for an element with a name equal to the target, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * <code>HTML.Attribute.SRC</code> attribute is updated and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * <code>ChangedUpdate</code> event is fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        String frameName = e.getTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        Element element = e.getSourceElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        String urlStr = e.getURL().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (frameName.equals("_self")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
              The source and destination elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
              are the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            updateFrame(element, urlStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        } else if (frameName.equals("_parent")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
              The destination is the parent of the frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            updateFrameSet(element.getParentElement(), urlStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
              locate a named frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            Element targetElement = findFrame(frameName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            if (targetElement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                updateFrame(targetElement, urlStr);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Searches the element hierarchy for an FRAME element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * that has its name attribute equal to the <code>frameName</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @param frameName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @return the element whose NAME attribute has a value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     *          <code>frameName</code>; returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *          if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    private Element findFrame(String frameName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        ElementIterator it = new ElementIterator(this);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   745
        Element next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        while ((next = it.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            AttributeSet attr = next.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            if (matchNameAttribute(attr, HTML.Tag.FRAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                String frameTarget = (String)attr.getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                if (frameTarget != null && frameTarget.equals(frameName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * Returns true if <code>StyleConstants.NameAttribute</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * equal to the tag that is passed in as a parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @param attr the attributes to be matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param tag the value to be matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @return true if there is a match, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @see javax.swing.text.html.HTML.Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    static boolean matchNameAttribute(AttributeSet attr, HTML.Tag tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        Object o = attr.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (o instanceof HTML.Tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            HTML.Tag name = (HTML.Tag) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            if (name == tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * Replaces a frameset branch Element with a frame leaf element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @param element the frameset element to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @param url     the value for the SRC attribute for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *                new frame that will replace the frameset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    private void updateFrameSet(Element element, String url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            int startOffset = element.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            int endOffset = Math.min(getLength(), element.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            String html = "<frame";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                html += " src=\"" + url + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            html += ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            installParserIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            setOuterHTML(element, html);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        } catch (BadLocationException e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            // Should handle this better
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            // Should handle this better
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * Updates the Frame elements <code>HTML.Attribute.SRC attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * and fires a <code>ChangedUpdate</code> event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @param element a FRAME element whose SRC attribute will be updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @param url     a string specifying the new value for the SRC attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    private void updateFrame(Element element, String url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            DefaultDocumentEvent changes = new DefaultDocumentEvent(element.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                                                                    1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                                                                    DocumentEvent.EventType.CHANGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            AttributeSet sCopy = element.getAttributes().copyAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            MutableAttributeSet attr = (MutableAttributeSet) element.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            changes.addEdit(new AttributeUndoableEdit(element, sCopy, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            attr.removeAttribute(HTML.Attribute.SRC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            attr.addAttribute(HTML.Attribute.SRC, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            changes.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            fireChangedUpdate(changes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * Returns true if the document will be viewed in a frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * @return true if document will be viewed in a frame, otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    boolean isFrameDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        return frameDocument;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * Sets a boolean state about whether the document will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * viewed in a frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @param frameDoc  true if the document will be viewed in a frame,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *          otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    void setFrameDocumentState(boolean frameDoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        this.frameDocument = frameDoc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * Adds the specified map, this will remove a Map that has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * previously registered with the same name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @param map  the <code>Map</code> to be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    void addMap(Map map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        String     name = map.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            Object     maps = getProperty(MAP_PROPERTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            if (maps == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                maps = new Hashtable(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                putProperty(MAP_PROPERTY, maps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            if (maps instanceof Hashtable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                ((Hashtable)maps).put("#" + name, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Removes a previously registered map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @param map the <code>Map</code> to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    void removeMap(Map map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        String     name = map.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            Object     maps = getProperty(MAP_PROPERTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            if (maps instanceof Hashtable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                ((Hashtable)maps).remove("#" + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * Returns the Map associated with the given name.
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   891
     * @param name the name of the desired <code>Map</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @return the <code>Map</code> or <code>null</code> if it can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *          be found, or if <code>name</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    Map getMap(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            Object     maps = getProperty(MAP_PROPERTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            if (maps != null && (maps instanceof Hashtable)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                return (Map)((Hashtable)maps).get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Returns an <code>Enumeration</code> of the possible Maps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * @return the enumerated list of maps, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *          if the maps are not an instance of <code>Hashtable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    Enumeration getMaps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        Object     maps = getProperty(MAP_PROPERTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (maps instanceof Hashtable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            return ((Hashtable)maps).elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * Sets the content type language used for style sheets that do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * explicitly specify the type. The default is text/css.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @param contentType  the content type language for the style sheets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    /* public */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    void setDefaultStyleSheetType(String contentType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        putProperty(StyleType, contentType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * Returns the content type language used for style sheets. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * is text/css.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @return the content type language used for the style sheets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /* public */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    String getDefaultStyleSheetType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        String retValue = (String)getProperty(StyleType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        if (retValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            return "text/css";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * Sets the parser that is used by the methods that insert html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * into the existing document, such as <code>setInnerHTML</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * and <code>setOuterHTML</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * <code>HTMLEditorKit.createDefaultDocument</code> will set the parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * for you. If you create an <code>HTMLDocument</code> by hand,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * be sure and set the parser accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @param parser the parser to be used for text insertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    public void setParser(HTMLEditorKit.Parser parser) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        this.parser = parser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        putProperty("__PARSER__", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * Returns the parser that is used when inserting HTML into the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @return the parser used for text insertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    public HTMLEditorKit.Parser getParser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        Object p = getProperty("__PARSER__");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        if (p instanceof HTMLEditorKit.Parser) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            return (HTMLEditorKit.Parser)p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        return parser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * Replaces the children of the given element with the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * specified as an HTML string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * <p>This will be seen as at least two events, n inserts followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * a remove.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * <p>Consider the following structure (the <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * parameter is <b>in bold</b>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   988
     *     &lt;body&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *       |
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   990
     *     <b>&lt;div&gt;</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *      /  \
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   992
     *    &lt;p&gt;   &lt;p&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
   995
     * <p>Invoking <code>setInnerHTML(elem, "&lt;ul&gt;&lt;li&gt;")</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * results in the following structure (new elements are <font
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
   997
     * style="color: red;">in red</font>).</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1000
     *     &lt;body&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     *       |
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1002
     *     <b>&lt;div&gt;</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     *         \
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1004
     *         <font style="color: red;">&lt;ul&gt;</font>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *           \
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1006
     *           <font style="color: red;">&lt;li&gt;</font>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * <p>Parameter <code>elem</code> must not be a leaf element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * otherwise an <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * If either <code>elem</code> or <code>htmlText</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * is <code>null</code>, no changes are made to the document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  1014
     * <p>For this to work correctly, the document must have an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * <code>HTMLEditorKit.Parser</code> set. This will be the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * if the document was created from an HTMLEditorKit via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * <code>createDefaultDocument</code> method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @param elem the branch element whose children will be replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * @param htmlText the string to be parsed and assigned to <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * @throws IllegalArgumentException if <code>elem</code> is a leaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @throws IllegalStateException if an <code>HTMLEditorKit.Parser</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *         has not been defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    public void setInnerHTML(Element elem, String htmlText) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                             BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        verifyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        if (elem != null && elem.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                ("Can not set inner HTML of a leaf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        if (elem != null && htmlText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            int oldCount = elem.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            int insertPosition = elem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            insertHTML(elem, elem.getStartOffset(), htmlText, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            if (elem.getElementCount() > oldCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                // Elements were inserted, do the cleanup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                removeElements(elem, elem.getElementCount() - oldCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                               oldCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * Replaces the given element in the parent with the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * specified as an HTML string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * <p>This will be seen as at least two events, n inserts followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * a remove.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * <p>When replacing a leaf this will attempt to make sure there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * a newline present if one is needed. This may result in an additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * element being inserted. Consider, if you were to replace a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * element that contained a newline with &lt;img&gt; this would create
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  1056
     * two elements, one for the image, and one for the newline.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * <p>If you try to replace the element at length you will most
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * likely end up with two elements, eg
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * <code>setOuterHTML(getCharacterElement (getLength()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * "blah")</code> will result in two leaf elements at the end, one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * representing 'blah', and the other representing the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * element.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * <p>Consider the following structure (the <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * parameter is <b>in bold</b>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1069
     *     &lt;body&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     *       |
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1071
     *     <b>&lt;div&gt;</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     *      /  \
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1073
     *    &lt;p&gt;   &lt;p&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1076
     * <p>Invoking <code>setOuterHTML(elem, "&lt;ul&gt;&lt;li&gt;")</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * results in the following structure (new elements are <font
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1078
     * style="color: red;">in red</font>).</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1081
     *    &lt;body&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *      |
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1083
     *     <font style="color: red;">&lt;ul&gt;</font>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *       \
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1085
     *       <font style="color: red;">&lt;li&gt;</font>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * <p>If either <code>elem</code> or <code>htmlText</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * parameter is <code>null</code>, no changes are made to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  1092
     * <p>For this to work correctly, the document must have an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * HTMLEditorKit.Parser set. This will be the case if the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * was created from an HTMLEditorKit via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * <code>createDefaultDocument</code> method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * @param elem the element to replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * @param htmlText the string to be parsed and inserted in place of <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * @throws IllegalStateException if an HTMLEditorKit.Parser has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *         been set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    public void setOuterHTML(Element elem, String htmlText) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                            BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        verifyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        if (elem != null && elem.getParentElement() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            htmlText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            int start = elem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            int end = elem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            int startLength = getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            // We don't want a newline if elem is a leaf, and doesn't contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            // a newline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            boolean wantsNewline = !elem.isLeaf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            if (!wantsNewline && (end > startLength ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                                 getText(end - 1, 1).charAt(0) == NEWLINE[0])){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                wantsNewline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            Element parent = elem.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            int oldCount = parent.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            insertHTML(parent, start, htmlText, wantsNewline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            // Remove old.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            int newLength = getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            if (oldCount != parent.getElementCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                int removeIndex = parent.getElementIndex(start + newLength -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                                                         startLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                removeElements(parent, removeIndex, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * Inserts the HTML specified as a string at the start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * <p>Consider the following structure (the <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * parameter is <b>in bold</b>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1139
     *     &lt;body&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *       |
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1141
     *     <b>&lt;div&gt;</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *      /  \
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1143
     *    &lt;p&gt;   &lt;p&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * <p>Invoking <code>insertAfterStart(elem,
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1147
     * "&lt;ul&gt;&lt;li&gt;")</code> results in the following structure
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1148
     * (new elements are <font style="color: red;">in red</font>).</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1151
     *        &lt;body&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     *          |
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1153
     *        <b>&lt;div&gt;</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     *       /  |  \
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1155
     *    <font style="color: red;">&lt;ul&gt;</font> &lt;p&gt; &lt;p&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *     /
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1157
     *  <font style="color: red;">&lt;li&gt;</font>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * <p>Unlike the <code>insertBeforeStart</code> method, new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     *  elements become <em>children</em> of the specified element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *  not siblings.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * <p>Parameter <code>elem</code> must not be a leaf element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * otherwise an <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * If either <code>elem</code> or <code>htmlText</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * is <code>null</code>, no changes are made to the document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  1169
     * <p>For this to work correctly, the document must have an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * <code>HTMLEditorKit.Parser</code> set. This will be the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * if the document was created from an HTMLEditorKit via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * <code>createDefaultDocument</code> method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * @param elem the branch element to be the root for the new text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * @param htmlText the string to be parsed and assigned to <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * @throws IllegalArgumentException if <code>elem</code> is a leaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * @throws IllegalStateException if an HTMLEditorKit.Parser has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     *         been set on the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    public void insertAfterStart(Element elem, String htmlText) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                                 BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        verifyParser();
10867
bf858c670ee2 7099251: javax.swing.text.html.HTMLDocument.insertAfterStart(null, something) throws NPE
rupashka
parents: 10316
diff changeset
  1184
bf858c670ee2 7099251: javax.swing.text.html.HTMLDocument.insertAfterStart(null, something) throws NPE
rupashka
parents: 10316
diff changeset
  1185
        if (elem == null || htmlText == null) {
bf858c670ee2 7099251: javax.swing.text.html.HTMLDocument.insertAfterStart(null, something) throws NPE
rupashka
parents: 10316
diff changeset
  1186
            return;
bf858c670ee2 7099251: javax.swing.text.html.HTMLDocument.insertAfterStart(null, something) throws NPE
rupashka
parents: 10316
diff changeset
  1187
        }
bf858c670ee2 7099251: javax.swing.text.html.HTMLDocument.insertAfterStart(null, something) throws NPE
rupashka
parents: 10316
diff changeset
  1188
bf858c670ee2 7099251: javax.swing.text.html.HTMLDocument.insertAfterStart(null, something) throws NPE
rupashka
parents: 10316
diff changeset
  1189
        if (elem.isLeaf()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                ("Can not insert HTML after start of a leaf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        insertHTML(elem, elem.getStartOffset(), htmlText, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * Inserts the HTML specified as a string at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * <p> If <code>elem</code>'s children are leaves, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * character at a <code>elem.getEndOffset() - 1</code> is a newline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * this will insert before the newline so that there isn't text after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * the newline.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * <p>Consider the following structure (the <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * parameter is <b>in bold</b>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1209
     *     &lt;body&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     *       |
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1211
     *     <b>&lt;div&gt;</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     *      /  \
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1213
     *    &lt;p&gt;   &lt;p&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1216
     * <p>Invoking <code>insertBeforeEnd(elem, "&lt;ul&gt;&lt;li&gt;")</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * results in the following structure (new elements are <font
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1218
     * style="color: red;">in red</font>).</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1221
     *        &lt;body&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     *          |
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1223
     *        <b>&lt;div&gt;</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     *       /  |  \
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1225
     *     &lt;p&gt; &lt;p&gt; <font style="color: red;">&lt;ul&gt;</font>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     *               \
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1227
     *               <font style="color: red;">&lt;li&gt;</font>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * <p>Unlike the <code>insertAfterEnd</code> method, new elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * become <em>children</em> of the specified element, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * siblings.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * <p>Parameter <code>elem</code> must not be a leaf element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * otherwise an <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * If either <code>elem</code> or <code>htmlText</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * is <code>null</code>, no changes are made to the document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  1239
     * <p>For this to work correctly, the document must have an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * <code>HTMLEditorKit.Parser</code> set. This will be the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * if the document was created from an HTMLEditorKit via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * <code>createDefaultDocument</code> method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * @param elem the element to be the root for the new text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * @param htmlText the string to be parsed and assigned to <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * @throws IllegalArgumentException if <code>elem</code> is a leaf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * @throws IllegalStateException if an HTMLEditorKit.Parser has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     *         been set on the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    public void insertBeforeEnd(Element elem, String htmlText) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                                BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        verifyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        if (elem != null && elem.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                ("Can not set inner HTML before end of leaf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        if (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            int offset = elem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            if (elem.getElement(elem.getElementIndex(offset - 1)).isLeaf() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                getText(offset - 1, 1).charAt(0) == NEWLINE[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                offset--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            insertHTML(elem, offset, htmlText, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * Inserts the HTML specified as a string before the start of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * the given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * <p>Consider the following structure (the <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * parameter is <b>in bold</b>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1276
     *     &lt;body&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     *       |
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1278
     *     <b>&lt;div&gt;</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     *      /  \
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1280
     *    &lt;p&gt;   &lt;p&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * <p>Invoking <code>insertBeforeStart(elem,
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1284
     * "&lt;ul&gt;&lt;li&gt;")</code> results in the following structure
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1285
     * (new elements are <font style="color: red;">in red</font>).</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1288
     *        &lt;body&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     *         /  \
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1290
     *      <font style="color: red;">&lt;ul&gt;</font> <b>&lt;div&gt;</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *       /    /  \
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1292
     *     <font style="color: red;">&lt;li&gt;</font> &lt;p&gt;  &lt;p&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * <p>Unlike the <code>insertAfterStart</code> method, new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * elements become <em>siblings</em> of the specified element, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * children.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * <p>If either <code>elem</code> or <code>htmlText</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * parameter is <code>null</code>, no changes are made to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  1303
     * <p>For this to work correctly, the document must have an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * <code>HTMLEditorKit.Parser</code> set. This will be the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * if the document was created from an HTMLEditorKit via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * <code>createDefaultDocument</code> method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * @param elem the element the content is inserted before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * @param htmlText the string to be parsed and inserted before <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * @throws IllegalStateException if an HTMLEditorKit.Parser has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     *         been set on the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    public void insertBeforeStart(Element elem, String htmlText) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                                  BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        verifyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        if (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            Element parent = elem.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                insertHTML(parent, elem.getStartOffset(), htmlText, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * Inserts the HTML specified as a string after the the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * <p>Consider the following structure (the <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * parameter is <b>in bold</b>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1334
     *     &lt;body&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     *       |
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1336
     *     <b>&lt;div&gt;</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     *      /  \
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1338
     *    &lt;p&gt;   &lt;p&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1341
     * <p>Invoking <code>insertAfterEnd(elem, "&lt;ul&gt;&lt;li&gt;")</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * results in the following structure (new elements are <font
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1343
     * style="color: red;">in red</font>).</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * <pre>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  1346
     *        &lt;body&gt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     *         /  \
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1348
     *      <b>&lt;div&gt;</b> <font style="color: red;">&lt;ul&gt;</font>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     *       / \    \
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21982
diff changeset
  1350
     *     &lt;p&gt; &lt;p&gt;  <font style="color: red;">&lt;li&gt;</font>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * <p>Unlike the <code>insertBeforeEnd</code> method, new elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * become <em>siblings</em> of the specified element, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * children.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * <p>If either <code>elem</code> or <code>htmlText</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * parameter is <code>null</code>, no changes are made to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * document.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  1361
     * <p>For this to work correctly, the document must have an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * <code>HTMLEditorKit.Parser</code> set. This will be the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * if the document was created from an HTMLEditorKit via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * <code>createDefaultDocument</code> method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * @param elem the element the content is inserted after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * @param htmlText the string to be parsed and inserted after <code>elem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * @throws IllegalStateException if an HTMLEditorKit.Parser has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     *         been set on the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    public void insertAfterEnd(Element elem, String htmlText) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                               BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        verifyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        if (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            Element parent = elem.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                int offset = elem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                if (offset > getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                    offset--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                else if (elem.isLeaf() && getText(offset - 1, 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                    charAt(0) == NEWLINE[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    offset--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                insertHTML(parent, offset, htmlText, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * Returns the element that has the given id <code>Attribute</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * If the element can't be found, <code>null</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * Note that this method works on an <code>Attribute</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * <i>not</i> a character tag.  In the following HTML snippet:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * <code>&lt;a id="HelloThere"&gt;</code> the attribute is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * 'id' and the character tag is 'a'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * <code>getElement(RootElement, HTML.Attribute.id, id)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * This is not thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * @param id  the string representing the desired <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * @return the element with the specified <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     *          or <code>null</code> if it can't be found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     *          or <code>null</code> if <code>id</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * @see javax.swing.text.html.HTML.Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    public Element getElement(String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        if (id == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        return getElement(getDefaultRootElement(), HTML.Attribute.ID, id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                          true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * Returns the child element of <code>e</code> that contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * attribute, <code>attribute</code> with value <code>value</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * <code>null</code> if one isn't found. This is not thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * @param e the root element where the search begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * @param attribute the desired <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * @param value the values for the specified <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * @return the element with the specified <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     *          and the specified <code>value</code>, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     *          if it can't be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * @see javax.swing.text.html.HTML.Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    public Element getElement(Element e, Object attribute, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        return getElement(e, attribute, value, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * Returns the child element of <code>e</code> that contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * attribute, <code>attribute</code> with value <code>value</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * <code>null</code> if one isn't found. This is not thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * If <code>searchLeafAttributes</code> is true, and <code>e</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * a leaf, any attributes that are instances of <code>HTML.Tag</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * with a value that is an <code>AttributeSet</code> will also be checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * @param e the root element where the search begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * @param attribute the desired <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * @param value the values for the specified <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * @return the element with the specified <code>Attribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     *          and the specified <code>value</code>, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     *          if it can't be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * @see javax.swing.text.html.HTML.Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    private Element getElement(Element e, Object attribute, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                               boolean searchLeafAttributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        AttributeSet attr = e.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        if (attr != null && attr.isDefined(attribute)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            if (value.equals(attr.getAttribute(attribute))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        if (!e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            for (int counter = 0, maxCounter = e.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                 counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                Element retValue = getElement(e.getElement(counter), attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                                              value, searchLeafAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                if (retValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                    return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        else if (searchLeafAttributes && attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            // For some leaf elements we store the actual attributes inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            // the AttributeSet of the Element (such as anchors).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            Enumeration names = attr.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                    Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                    if ((name instanceof HTML.Tag) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                        (attr.getAttribute(name) instanceof AttributeSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                        AttributeSet check = (AttributeSet)attr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                                             getAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                        if (check.isDefined(attribute) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                            value.equals(check.getAttribute(attribute))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * Verifies the document has an <code>HTMLEditorKit.Parser</code> set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * If <code>getParser</code> returns <code>null</code>, this will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * IllegalStateException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * @throws IllegalStateException if the document does not have a Parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    private void verifyParser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        if (getParser() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            throw new IllegalStateException("No HTMLEditorKit.Parser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * Installs a default Parser if one has not been installed yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    private void installParserIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        if (getParser() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            setParser(new HTMLEditorKit().getParser());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * Inserts a string of HTML into the document at the given position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * <code>parent</code> is used to identify the location to insert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * <code>html</code>. If <code>parent</code> is a leaf this can have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * unexpected results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    private void insertHTML(Element parent, int offset, String html,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                            boolean wantsTrailingNewline)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                 throws BadLocationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        if (parent != null && html != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            HTMLEditorKit.Parser parser = getParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            if (parser != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                int lastOffset = Math.max(0, offset - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                Element charElement = getCharacterElement(lastOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                Element commonParent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                int pop = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                int push = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                if (parent.getStartOffset() > lastOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                    while (commonParent != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                           commonParent.getStartOffset() > lastOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                        commonParent = commonParent.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                        push++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                    if (commonParent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                        throw new BadLocationException("No common parent",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                                                       offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                while (charElement != null && charElement != commonParent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                    pop++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                    charElement = charElement.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                if (charElement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                    // Found it, do the insert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                    HTMLReader reader = new HTMLReader(offset, pop - 1, push,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                                                       null, false, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                                                       wantsTrailingNewline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                    parser.parse(new StringReader(html), reader, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                    reader.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * Removes child Elements of the passed in Element <code>e</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * will do the necessary cleanup to ensure the element representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * end character is correctly created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * <p>This is not a general purpose method, it assumes that <code>e</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * will still have at least one child after the remove, and it assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * the character at <code>e.getStartOffset() - 1</code> is a newline and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * is of length 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
    private void removeElements(Element e, int index, int count) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            int start = e.getElement(index).getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            int end = e.getElement(index + count - 1).getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            if (end > getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                removeElementsAtEnd(e, index, count, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                removeElements(e, index, count, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * Called to remove child elements of <code>e</code> when one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * elements to remove is representing the end character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * <p>Since the Content will not allow a removal to the end character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * this will do a remove from <code>start - 1</code> to <code>end</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * The end Element(s) will be removed, and the element representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * <code>start - 1</code> to <code>start</code> will be recreated. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * Element has to be recreated as after the content removal its offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     * become <code>start - 1</code> to <code>start - 1</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    private void removeElementsAtEnd(Element e, int index, int count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                         int start, int end) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        // index must be > 0 otherwise no insert would have happened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        boolean isLeaf = (e.getElement(index - 1).isLeaf());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        DefaultDocumentEvent dde = new DefaultDocumentEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                       start - 1, end - start + 1, DocumentEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                       EventType.REMOVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        if (isLeaf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            Element endE = getCharacterElement(getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            // e.getElement(index - 1) should represent the newline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            index--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            if (endE.getParentElement() != e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                // The hiearchies don't match, we'll have to manually
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                // recreate the leaf at e.getElement(index - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                replace(dde, e, index, ++count, start, end, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                // The hierarchies for the end Element and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                // e.getElement(index - 1), match, we can safely remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                // the Elements and the end content will be aligned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                // appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                replace(dde, e, index, count, start, end, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            // Not a leaf, descend until we find the leaf representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            // start - 1 and remove it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            Element newLineE = e.getElement(index - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            while (!newLineE.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                newLineE = newLineE.getElement(newLineE.getElementCount() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            newLineE = newLineE.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            replace(dde, e, index, count, start, end, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            replace(dde, newLineE, newLineE.getElementCount() - 1, 1, start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                    end, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        postRemoveUpdate(dde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        dde.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        fireRemoveUpdate(dde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        fireUndoableEditUpdate(new UndoableEditEvent(this, dde));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * This is used by <code>removeElementsAtEnd</code>, it removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * <code>count</code> elements starting at <code>start</code> from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     * <code>e</code>.  If <code>remove</code> is true text of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * <code>start - 1</code> to <code>end - 1</code> is removed.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * <code>create</code> is true a new leaf is created of length 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    private void replace(DefaultDocumentEvent dde, Element e, int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                         int count, int start, int end, boolean remove,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                         boolean create) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        Element[] added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        AttributeSet attrs = e.getElement(index).getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        Element[] removed = new Element[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        for (int counter = 0; counter < count; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            removed[counter] = e.getElement(counter + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        if (remove) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            UndoableEdit u = getContent().remove(start - 1, end - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            if (u != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                dde.addEdit(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        if (create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            added = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            added[0] = createLeafElement(e, attrs, start - 1, start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            added = new Element[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        dde.addEdit(new ElementEdit(e, index, removed, added));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        ((AbstractDocument.BranchElement)e).replace(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                                             index, removed.length, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * Called to remove child Elements when the end is not touched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    private void removeElements(Element e, int index, int count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                             int start, int end) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        Element[] removed = new Element[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        Element[] added = new Element[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        for (int counter = 0; counter < count; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            removed[counter] = e.getElement(counter + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        DefaultDocumentEvent dde = new DefaultDocumentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                (start, end - start, DocumentEvent.EventType.REMOVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        ((AbstractDocument.BranchElement)e).replace(index, removed.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                                                    added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        dde.addEdit(new ElementEdit(e, index, removed, added));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        UndoableEdit u = getContent().remove(start, end - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        if (u != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            dde.addEdit(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        postRemoveUpdate(dde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        dde.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        fireRemoveUpdate(dde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        if (u != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            fireUndoableEditUpdate(new UndoableEditEvent(this, dde));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
    // These two are provided for inner class access. The are named different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    // than the super class as the super class implementations are final.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
    void obtainLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    void releaseLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        writeUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
    // Provided for inner class access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
    protected void fireChangedUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        super.fireChangedUpdate(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
    protected void fireUndoableEditUpdate(UndoableEditEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        super.fireUndoableEditUpdate(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
    boolean hasBaseTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        return hasBaseTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    String getBaseTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        return baseTarget;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * state defines whether the document is a frame document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
    private boolean frameDocument = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    private boolean preservesUnknownTags = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * Used to store button groups for radio buttons in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * a form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1764
    private HashMap<String, ButtonGroup> radioButtonGroupsMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * Document property for the number of tokens to buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     * before building an element subtree to represent them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
    static final String TokenThreshold = "token threshold";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
    private static final int MaxThreshold = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    private static final int StepThreshold = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     * Document property key value. The value for the key will be a Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * of Strings that are comments not found in the body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
    public static final String AdditionalComments = "AdditionalComments";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     * Document property key value. The value for the key will be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * String indicating the default type of stylesheet links.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    /* public */ static final String StyleType = "StyleType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * The location to resolve relative URLs against.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * default this will be the document's URL if the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * was loaded from a URL.  If a base tag is found and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * can be parsed, it will be used as the base location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
    URL base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * does the document have base tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    boolean hasBaseTag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * BASE tag's TARGET attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
    private String baseTarget = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * The parser that is used when inserting html into the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    private HTMLEditorKit.Parser parser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * Used for inserts when a null AttributeSet is supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    private static AttributeSet contentAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * Property Maps are registered under, will be a Hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    static String MAP_PROPERTY = "__MAP__";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    private static char[] NEWLINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     * I18N property key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     *
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1828
     * @see AbstractDocument#I18NProperty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
    private static final String I18NProperty = "i18n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        contentAttributeSet = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        ((MutableAttributeSet)contentAttributeSet).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                        addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                                     HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        NEWLINE = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        NEWLINE[0] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * An iterator to iterate over a particular type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * tag.  The iterator is not thread safe.  If reliable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * access to the document is not already ensured by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * the context under which the iterator is being used,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * its use should be performed under the protection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     * Document.render.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
    public static abstract class Iterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
         * Return the attributes for this tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
         * @return the <code>AttributeSet</code> for this tag, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
         *      <code>null</code> if none can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        public abstract AttributeSet getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
         * Returns the start of the range for which the current occurrence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
         * the tag is defined and has the same attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
         * @return the start of the range, or -1 if it can't be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        public abstract int getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
         * Returns the end of the range for which the current occurrence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
         * the tag is defined and has the same attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
         * @return the end of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        public abstract int getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
         * Move the iterator forward to the next occurrence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
         * of the tag it represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        public abstract void next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
         * Indicates if the iterator is currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
         * representing an occurrence of a tag.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
         * false there are no more tags for this iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
         * @return true if the iterator is currently representing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
         *              occurrence of a tag, otherwise returns false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        public abstract boolean isValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
         * Type of tag this iterator represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        public abstract HTML.Tag getTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * An iterator to iterate over a particular type of tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
    static class LeafIterator extends Iterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        LeafIterator(HTML.Tag t, Document doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            tag = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            pos = new ElementIterator(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            endOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
         * Returns the attributes for this tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
         * @return the <code>AttributeSet</code> for this tag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
         *              or <code>null</code> if none can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        public AttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            Element elem = pos.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            if (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                AttributeSet a = (AttributeSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                    elem.getAttributes().getAttribute(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                if (a == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1919
                    a = elem.getAttributes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
         * Returns the start of the range for which the current occurrence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
         * the tag is defined and has the same attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
         * @return the start of the range, or -1 if it can't be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        public int getStartOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            Element elem = pos.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            if (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                return elem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
         * Returns the end of the range for which the current occurrence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
         * the tag is defined and has the same attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
         * @return the end of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        public int getEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            return endOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
         * Moves the iterator forward to the next occurrence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
         * of the tag it represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        public void next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            for (nextLeaf(pos); isValid(); nextLeaf(pos)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                Element elem = pos.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                if (elem.getStartOffset() >= endOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                    AttributeSet a = pos.current().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                    if (a.isDefined(tag) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                        a.getAttribute(StyleConstants.NameAttribute) == tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                        // we found the next one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                        setEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
         * Returns the type of tag this iterator represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
         * @return the <code>HTML.Tag</code> that this iterator represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
         * @see javax.swing.text.html.HTML.Tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        public HTML.Tag getTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            return tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
         * Returns true if the current position is not <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
         * @return true if current position is not <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
         *              otherwise returns false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        public boolean isValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            return (pos.current() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
         * Moves the given iterator to the next leaf element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
         * @param iter  the iterator to be scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        void nextLeaf(ElementIterator iter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            for (iter.next(); iter.current() != null; iter.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                Element e = iter.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                if (e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
         * Marches a cloned iterator forward to locate the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
         * of the run.  This sets the value of <code>endOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        void setEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
            AttributeSet a0 = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            endOffset = pos.current().getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            ElementIterator fwd = (ElementIterator) pos.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
            for (nextLeaf(fwd); fwd.current() != null; nextLeaf(fwd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                Element e = fwd.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                AttributeSet a1 = (AttributeSet) e.getAttributes().getAttribute(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                if ((a1 == null) || (! a1.equals(a0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                endOffset = e.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        private int endOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
        private HTML.Tag tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        private ElementIterator pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * An HTML reader to load an HTML document with an HTML
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     * element structure.  This is a set of callbacks from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     * the parser, implemented to create a set of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * tagged with attributes.  The parse builds up tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * (ElementSpec) that describe the element subtree desired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * and burst it into the document under the protection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * a write lock using the insert method on the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * outer class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * The reader can be configured by registering actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * (of type <code>HTMLDocument.HTMLReader.TagAction</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * that describe how to handle the action.  The idea behind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * the actions provided is that the most natural text editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     * operations can be provided if the element structure boils
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     * down to paragraphs with runs of some kind of style
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * in them.  Some things are more naturally specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     * structurally, so arbitrary structure should be allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * above the paragraphs, but will need to be edited with structural
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * actions.  The implication of this is that some of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     * HTML elements specified in the stream being parsed will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * be collapsed into attributes, and in some cases paragraphs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     * will be synthesized.  When HTML elements have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     * converted to attributes, the attribute key will be of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     * type HTML.Tag, and the value will be of type AttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     * so that no information is lost.  This enables many of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     * existing actions to work so that the user can type input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * hit the return key, backspace, delete, etc and have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * reasonable result.  Selections can be created, and attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * applied or removed, etc.  With this in mind, the work done
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * by the reader can be categorized into the following kinds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * of tasks:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * <dt>Block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * <dd>Build the structure like it's specified in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * This produces elements that contain other elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * <dt>Paragraph
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * <dd>Like block except that it's expected that the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     * will be used with a paragraph view so a paragraph element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * won't need to be synthesized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     * <dt>Character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * <dd>Contribute the element as an attribute that will start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * and stop at arbitrary text locations.  This will ultimately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * be mixed into a run of text, with all of the currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * flattened HTML character elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * <dt>Special
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     * <dd>Produce an embedded graphical element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * <dt>Form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     * <dd>Produce an element that is like the embedded graphical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * element, except that it also has a component model associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     * with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     * <dt>Hidden
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     * <dd>Create an element that is hidden from view when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     * document is being viewed read-only, and visible when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * document is being edited.  This is useful to keep the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     * model from losing information, and used to store things
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * like comments and unrecognized tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     * Currently, &lt;APPLET&gt;, &lt;PARAM&gt;, &lt;MAP&gt;, &lt;AREA&gt;, &lt;LINK&gt;,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     * &lt;SCRIPT&gt; and &lt;STYLE&gt; are unsupported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * The assignment of the actions described is shown in the
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
  2092
     * following table for the tags defined in <code>HTML.Tag</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * <table border=1 summary="HTML tags and assigned actions">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * <tr><th>Tag</th><th>Action</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * <tr><td><code>HTML.Tag.A</code>         <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     * <tr><td><code>HTML.Tag.ADDRESS</code>   <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     * <tr><td><code>HTML.Tag.APPLET</code>    <td>HiddenAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * <tr><td><code>HTML.Tag.AREA</code>      <td>AreaAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * <tr><td><code>HTML.Tag.B</code>         <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     * <tr><td><code>HTML.Tag.BASE</code>      <td>BaseAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     * <tr><td><code>HTML.Tag.BASEFONT</code>  <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * <tr><td><code>HTML.Tag.BIG</code>       <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     * <tr><td><code>HTML.Tag.BLOCKQUOTE</code><td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     * <tr><td><code>HTML.Tag.BODY</code>      <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     * <tr><td><code>HTML.Tag.BR</code>        <td>SpecialAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     * <tr><td><code>HTML.Tag.CAPTION</code>   <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * <tr><td><code>HTML.Tag.CENTER</code>    <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     * <tr><td><code>HTML.Tag.CITE</code>      <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     * <tr><td><code>HTML.Tag.CODE</code>      <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     * <tr><td><code>HTML.Tag.DD</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * <tr><td><code>HTML.Tag.DFN</code>       <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * <tr><td><code>HTML.Tag.DIR</code>       <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * <tr><td><code>HTML.Tag.DIV</code>       <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * <tr><td><code>HTML.Tag.DL</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * <tr><td><code>HTML.Tag.DT</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     * <tr><td><code>HTML.Tag.EM</code>        <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     * <tr><td><code>HTML.Tag.FONT</code>      <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * <tr><td><code>HTML.Tag.FORM</code>      <td>As of 1.4 a BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * <tr><td><code>HTML.Tag.FRAME</code>     <td>SpecialAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     * <tr><td><code>HTML.Tag.FRAMESET</code>  <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * <tr><td><code>HTML.Tag.H1</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * <tr><td><code>HTML.Tag.H2</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     * <tr><td><code>HTML.Tag.H3</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * <tr><td><code>HTML.Tag.H4</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * <tr><td><code>HTML.Tag.H5</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     * <tr><td><code>HTML.Tag.H6</code>        <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * <tr><td><code>HTML.Tag.HEAD</code>      <td>HeadAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * <tr><td><code>HTML.Tag.HR</code>        <td>SpecialAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * <tr><td><code>HTML.Tag.HTML</code>      <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * <tr><td><code>HTML.Tag.I</code>         <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     * <tr><td><code>HTML.Tag.IMG</code>       <td>SpecialAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * <tr><td><code>HTML.Tag.INPUT</code>     <td>FormAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     * <tr><td><code>HTML.Tag.ISINDEX</code>   <td>IsndexAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     * <tr><td><code>HTML.Tag.KBD</code>       <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     * <tr><td><code>HTML.Tag.LI</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     * <tr><td><code>HTML.Tag.LINK</code>      <td>LinkAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     * <tr><td><code>HTML.Tag.MAP</code>       <td>MapAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     * <tr><td><code>HTML.Tag.MENU</code>      <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     * <tr><td><code>HTML.Tag.META</code>      <td>MetaAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     * <tr><td><code>HTML.Tag.NOFRAMES</code>  <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * <tr><td><code>HTML.Tag.OBJECT</code>    <td>SpecialAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * <tr><td><code>HTML.Tag.OL</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * <tr><td><code>HTML.Tag.OPTION</code>    <td>FormAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     * <tr><td><code>HTML.Tag.P</code>         <td>ParagraphAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     * <tr><td><code>HTML.Tag.PARAM</code>     <td>HiddenAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     * <tr><td><code>HTML.Tag.PRE</code>       <td>PreAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     * <tr><td><code>HTML.Tag.SAMP</code>      <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * <tr><td><code>HTML.Tag.SCRIPT</code>    <td>HiddenAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * <tr><td><code>HTML.Tag.SELECT</code>    <td>FormAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * <tr><td><code>HTML.Tag.SMALL</code>     <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     * <tr><td><code>HTML.Tag.STRIKE</code>    <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     * <tr><td><code>HTML.Tag.S</code>         <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     * <tr><td><code>HTML.Tag.STRONG</code>    <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
     * <tr><td><code>HTML.Tag.STYLE</code>     <td>StyleAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     * <tr><td><code>HTML.Tag.SUB</code>       <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * <tr><td><code>HTML.Tag.SUP</code>       <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     * <tr><td><code>HTML.Tag.TABLE</code>     <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     * <tr><td><code>HTML.Tag.TD</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     * <tr><td><code>HTML.Tag.TEXTAREA</code>  <td>FormAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     * <tr><td><code>HTML.Tag.TH</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * <tr><td><code>HTML.Tag.TITLE</code>     <td>TitleAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     * <tr><td><code>HTML.Tag.TR</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     * <tr><td><code>HTML.Tag.TT</code>        <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     * <tr><td><code>HTML.Tag.U</code>         <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     * <tr><td><code>HTML.Tag.UL</code>        <td>BlockAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     * <tr><td><code>HTML.Tag.VAR</code>       <td>CharacterAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     * <p>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10867
diff changeset
  2169
     * Once &lt;/html&gt; is encountered, the Actions are no longer notified.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
    public class HTMLReader extends HTMLEditorKit.ParserCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        public HTMLReader(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
            this(offset, 0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        public HTMLReader(int offset, int popDepth, int pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                          HTML.Tag insertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
            this(offset, popDepth, pushDepth, insertTag, true, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
         * Generates a RuntimeException (will eventually generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
         * a BadLocationException when API changes are alloced) if inserting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
         * into non empty document, <code>insertTag</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
         * non-<code>null</code>, and <code>offset</code> is not in the body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
        // PENDING(sky): Add throws BadLocationException and remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        // RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
        HTMLReader(int offset, int popDepth, int pushDepth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                   HTML.Tag insertTag, boolean insertInsertTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                   boolean insertAfterImplied, boolean wantsTrailingNewline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
            emptyDocument = (getLength() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            isStyleCSS = "text/css".equals(getDefaultStyleSheetType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            threshold = HTMLDocument.this.getTokenThreshold();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2197
            tagMap = new Hashtable<HTML.Tag, TagAction>(57);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            TagAction na = new TagAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            TagAction ba = new BlockAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
            TagAction pa = new ParagraphAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            TagAction ca = new CharacterAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            TagAction sa = new SpecialAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            TagAction fa = new FormAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
            TagAction ha = new HiddenAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
            TagAction conv = new ConvertAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
            // register handlers for the well known tags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
            tagMap.put(HTML.Tag.A, new AnchorAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
            tagMap.put(HTML.Tag.ADDRESS, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
            tagMap.put(HTML.Tag.APPLET, ha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
            tagMap.put(HTML.Tag.AREA, new AreaAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
            tagMap.put(HTML.Tag.B, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
            tagMap.put(HTML.Tag.BASE, new BaseAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
            tagMap.put(HTML.Tag.BASEFONT, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
            tagMap.put(HTML.Tag.BIG, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
            tagMap.put(HTML.Tag.BLOCKQUOTE, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            tagMap.put(HTML.Tag.BODY, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            tagMap.put(HTML.Tag.BR, sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            tagMap.put(HTML.Tag.CAPTION, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            tagMap.put(HTML.Tag.CENTER, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
            tagMap.put(HTML.Tag.CITE, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            tagMap.put(HTML.Tag.CODE, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
            tagMap.put(HTML.Tag.DD, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            tagMap.put(HTML.Tag.DFN, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
            tagMap.put(HTML.Tag.DIR, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            tagMap.put(HTML.Tag.DIV, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
            tagMap.put(HTML.Tag.DL, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
            tagMap.put(HTML.Tag.DT, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            tagMap.put(HTML.Tag.EM, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
            tagMap.put(HTML.Tag.FONT, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            tagMap.put(HTML.Tag.FORM, new FormTagAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            tagMap.put(HTML.Tag.FRAME, sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
            tagMap.put(HTML.Tag.FRAMESET, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
            tagMap.put(HTML.Tag.H1, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
            tagMap.put(HTML.Tag.H2, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            tagMap.put(HTML.Tag.H3, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
            tagMap.put(HTML.Tag.H4, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
            tagMap.put(HTML.Tag.H5, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            tagMap.put(HTML.Tag.H6, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            tagMap.put(HTML.Tag.HEAD, new HeadAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            tagMap.put(HTML.Tag.HR, sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            tagMap.put(HTML.Tag.HTML, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            tagMap.put(HTML.Tag.I, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            tagMap.put(HTML.Tag.IMG, sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            tagMap.put(HTML.Tag.INPUT, fa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            tagMap.put(HTML.Tag.ISINDEX, new IsindexAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            tagMap.put(HTML.Tag.KBD, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            tagMap.put(HTML.Tag.LI, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
            tagMap.put(HTML.Tag.LINK, new LinkAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
            tagMap.put(HTML.Tag.MAP, new MapAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
            tagMap.put(HTML.Tag.MENU, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            tagMap.put(HTML.Tag.META, new MetaAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            tagMap.put(HTML.Tag.NOBR, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            tagMap.put(HTML.Tag.NOFRAMES, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            tagMap.put(HTML.Tag.OBJECT, sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            tagMap.put(HTML.Tag.OL, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            tagMap.put(HTML.Tag.OPTION, fa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            tagMap.put(HTML.Tag.P, pa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
            tagMap.put(HTML.Tag.PARAM, new ObjectAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            tagMap.put(HTML.Tag.PRE, new PreAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
            tagMap.put(HTML.Tag.SAMP, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
            tagMap.put(HTML.Tag.SCRIPT, ha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
            tagMap.put(HTML.Tag.SELECT, fa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
            tagMap.put(HTML.Tag.SMALL, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
            tagMap.put(HTML.Tag.SPAN, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            tagMap.put(HTML.Tag.STRIKE, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            tagMap.put(HTML.Tag.S, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            tagMap.put(HTML.Tag.STRONG, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            tagMap.put(HTML.Tag.STYLE, new StyleAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            tagMap.put(HTML.Tag.SUB, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            tagMap.put(HTML.Tag.SUP, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            tagMap.put(HTML.Tag.TABLE, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
            tagMap.put(HTML.Tag.TD, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            tagMap.put(HTML.Tag.TEXTAREA, fa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
            tagMap.put(HTML.Tag.TH, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
            tagMap.put(HTML.Tag.TITLE, new TitleAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
            tagMap.put(HTML.Tag.TR, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
            tagMap.put(HTML.Tag.TT, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
            tagMap.put(HTML.Tag.U, conv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
            tagMap.put(HTML.Tag.UL, ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
            tagMap.put(HTML.Tag.VAR, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
            if (insertTag != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                this.insertTag = insertTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                this.popDepth = popDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                this.pushDepth = pushDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                this.insertInsertTag = insertInsertTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                foundInsertTag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                foundInsertTag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
            if (insertAfterImplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                this.popDepth = popDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                this.pushDepth = pushDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                this.insertAfterImplied = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                foundInsertTag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                midInsert = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                this.insertInsertTag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                this.wantsTrailingNewline = wantsTrailingNewline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                midInsert = (!emptyDocument && insertTag == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                if (midInsert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                    generateEndsSpecsForMidInsert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
             * This block initializes the <code>inParagraph</code> flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
             * It is left in <code>false</code> value automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
             * if the target document is empty or future inserts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
             * were positioned into the 'body' tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            if (!emptyDocument && !midInsert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                int targetOffset = Math.max(this.offset - 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                Element elem =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                        HTMLDocument.this.getCharacterElement(targetOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                /* Going up by the left document structure path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                for (int i = 0; i <= this.popDepth; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                    elem = elem.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                /* Going down by the right document structure path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                for (int i = 0; i < this.pushDepth; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                    int index = elem.getElementIndex(this.offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                    elem = elem.getElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                AttributeSet attrs = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                if (attrs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
                    HTML.Tag tagToInsertInto =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                            (HTML.Tag) attrs.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                    if (tagToInsertInto != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                        this.inParagraph = tagToInsertInto.isParagraph();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
         * Generates an initial batch of end <code>ElementSpecs</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
         * in parseBuffer to position future inserts into the body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
        private void generateEndsSpecsForMidInsert() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            int           count = heightToElementWithName(HTML.Tag.BODY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                                                   Math.max(0, offset - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            boolean       joinNext = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            if (count == -1 && offset > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                count = heightToElementWithName(HTML.Tag.BODY, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                if (count != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                    // Previous isn't in body, but current is. Have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                    // do some end specs, followed by join next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                    count = depthTo(offset - 1) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                    joinNext = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
            if (count == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                throw new RuntimeException("Must insert new content into body element-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
            if (count != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                // Insert a newline, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                    if (!joinNext && offset > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                        !getText(offset - 1, 1).equals("\n")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                        SimpleAttributeSet newAttrs = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                        newAttrs.addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                                              HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                        ElementSpec spec = new ElementSpec(newAttrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                                    ElementSpec.ContentType, NEWLINE, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                        parseBuffer.addElement(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                    // Should never throw, but will catch anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                } catch (BadLocationException ble) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                while (count-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                    parseBuffer.addElement(new ElementSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
                                           (null, ElementSpec.EndTagType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                if (joinNext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                    ElementSpec spec = new ElementSpec(null, ElementSpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
                                                       StartTagType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                    spec.setDirection(ElementSpec.JoinNextDirection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
                    parseBuffer.addElement(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
            // We should probably throw an exception if (count == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
            // Or look for the body and reset the offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
         * @return number of parents to reach the child at offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        private int depthTo(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
            Element       e = getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            int           count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
            while (!e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                e = e.getElement(e.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
         * @return number of parents of the leaf at <code>offset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
         *         until a parent with name, <code>name</code> has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
         *         found. -1 indicates no matching parent with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
         *         <code>name</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
        private int heightToElementWithName(Object name, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            Element       e = getCharacterElement(offset).getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
            int           count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            while (e != null && e.getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
                   (StyleConstants.NameAttribute) != name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                e = e.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
            return (e == null) ? -1 : count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
         * This will make sure there aren't two BODYs (the second is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
         * typically created when you do a remove all, and then an insert).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
        private void adjustEndElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
            int length = getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
            if (length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            obtainLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                Element[] pPath = getPathTo(length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                int pLength = pPath.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                if (pLength > 1 && pPath[1].getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
                         (StyleConstants.NameAttribute) == HTML.Tag.BODY &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
                         pPath[1].getEndOffset() == length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
                    String lastText = getText(length - 1, 1);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2439
                    DefaultDocumentEvent event;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
                    Element[] added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
                    Element[] removed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
                    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
                    // Remove the fake second body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
                    added = new Element[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
                    removed = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                    index = pPath[0].getElementIndex(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
                    removed[0] = pPath[0].getElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                    ((BranchElement)pPath[0]).replace(index, 1, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
                    ElementEdit firstEdit = new ElementEdit(pPath[0], index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
                                                            removed, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
                    // Insert a new element to represent the end that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
                    // second body was representing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
                    SimpleAttributeSet sas = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
                    sas.addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
                                         HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
                    sas.addAttribute(IMPLIED_CR, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
                    added = new Element[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
                    added[0] = createLeafElement(pPath[pLength - 1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
                                                     sas, length, length + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
                    index = pPath[pLength - 1].getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
                    ((BranchElement)pPath[pLength - 1]).replace(index, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
                                                                added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
                    event = new DefaultDocumentEvent(length, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
                                            DocumentEvent.EventType.CHANGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
                    event.addEdit(new ElementEdit(pPath[pLength - 1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
                                         index, new Element[0], added));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                    event.addEdit(firstEdit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
                    event.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
                    fireChangedUpdate(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                    fireUndoableEditUpdate(new UndoableEditEvent(this, event));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
                    if (lastText.equals("\n")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                        // We now have two \n's, one part of the Document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                        // We need to remove one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                        event = new DefaultDocumentEvent(length - 1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                                           DocumentEvent.EventType.REMOVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                        removeUpdate(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
                        UndoableEdit u = getContent().remove(length - 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                        if (u != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
                            event.addEdit(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                        postRemoveUpdate(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                        // Mark the edit as done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                        event.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
                        fireRemoveUpdate(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                        fireUndoableEditUpdate(new UndoableEditEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
                                               this, event));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
            catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
            finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                releaseLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        private Element[] getPathTo(int offset) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2500
            Stack<Element> elements = new Stack<Element>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
            Element e = getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
            int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
            while (!e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                elements.push(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                e = e.getElement(e.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
            Element[] retValue = new Element[elements.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
            elements.copyInto(retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
        // -- HTMLEditorKit.ParserCallback methods --------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
         * The last method called on the reader.  It allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
         * any pending changes to be flushed into the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
         * Since this is currently loading synchronously, the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
         * set of changes are pushed in at this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
        public void flush() throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
            if (emptyDocument && !insertAfterImplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
                if (HTMLDocument.this.getLength() > 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
                                      parseBuffer.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                    flushBuffer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                    adjustEndElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
                // We won't insert when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
                flushBuffer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
         * Called by the parser to indicate a block of text was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
         * encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
        public void handleText(char[] data, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
            if (receivedEndHTML || (midInsert && !inBody)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
            // see if complex glyph layout support is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
            if(HTMLDocument.this.getProperty(I18NProperty).equals( Boolean.FALSE ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
                // if a default direction of right-to-left has been specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
                // we want complex layout even if the text is all left to right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
                Object d = getProperty(TextAttribute.RUN_DIRECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
                if ((d != null) && (d.equals(TextAttribute.RUN_DIRECTION_RTL))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
                    HTMLDocument.this.putProperty( I18NProperty, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
                    if (SwingUtilities2.isComplexLayout(data, 0, data.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
                        HTMLDocument.this.putProperty( I18NProperty, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
            if (inTextArea) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                textAreaContent(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
            } else if (inPre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
                preContent(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
            } else if (inTitle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
                putProperty(Document.TitleProperty, new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
            } else if (option != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                option.setLabel(new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
            } else if (inStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                if (styles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                    styles.addElement(new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
            } else if (inBlock > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                if (!foundInsertTag && insertAfterImplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                    // Assume content should be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                    foundInsertTag(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                    foundInsertTag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                    inParagraph = impliedP = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                if (data.length >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                    addContent(data, 0, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
         * Callback from the parser.  Route to the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
         * handler for the tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
        public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
            if (receivedEndHTML) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
            if (midInsert && !inBody) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                if (t == HTML.Tag.BODY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                    inBody = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                    // Increment inBlock since we know we are in the body,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                    // this is needed incase an implied-p is needed. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                    // inBlock isn't incremented, and an implied-p is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                    // encountered, addContent won't be called!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                    inBlock++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            if (!inBody && t == HTML.Tag.BODY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                inBody = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
            if (isStyleCSS && a.isDefined(HTML.Attribute.STYLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                // Map the style attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                String decl = (String)a.getAttribute(HTML.Attribute.STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                a.removeAttribute(HTML.Attribute.STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
                styleAttributes = getStyleSheet().getDeclaration(decl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
                a.addAttributes(styleAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                styleAttributes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
            }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2614
            TagAction action = tagMap.get(t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
                action.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        public void handleComment(char[] data, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
            if (receivedEndHTML) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
                addExternalComment(new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
            if (inStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                if (styles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
                    styles.addElement(new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
            else if (getPreservesUnknownTags()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
                if (inBlock == 0 && (foundInsertTag ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
                                     insertTag != HTML.Tag.COMMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
                    // Comment outside of body, will not be able to show it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
                    // but can add it as a property on the Document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
                    addExternalComment(new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
                SimpleAttributeSet sas = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                sas.addAttribute(HTML.Attribute.COMMENT, new String(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
                addSpecialElement(HTML.Tag.COMMENT, sas);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2644
            TagAction action = tagMap.get(HTML.Tag.COMMENT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                action.start(HTML.Tag.COMMENT, new SimpleAttributeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                action.end(HTML.Tag.COMMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
         * Adds the comment <code>comment</code> to the set of comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
         * maintained outside of the scope of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
        private void addExternalComment(String comment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
            Object comments = getProperty(AdditionalComments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
            if (comments != null && !(comments instanceof Vector)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
                // No place to put comment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
            if (comments == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                comments = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
                putProperty(AdditionalComments, comments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
            ((Vector)comments).addElement(comment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
         * Callback from the parser.  Route to the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
         * handler for the tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
        public void handleEndTag(HTML.Tag t, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
            if (receivedEndHTML || (midInsert && !inBody)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
            if (t == HTML.Tag.HTML) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
                receivedEndHTML = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
            if (t == HTML.Tag.BODY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
                inBody = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
                if (midInsert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
                    inBlock--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
            }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2685
            TagAction action = tagMap.get(t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
                action.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
         * Callback from the parser.  Route to the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
         * handler for the tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
        public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
            if (receivedEndHTML || (midInsert && !inBody)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
            if (isStyleCSS && a.isDefined(HTML.Attribute.STYLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                // Map the style attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
                String decl = (String)a.getAttribute(HTML.Attribute.STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
                a.removeAttribute(HTML.Attribute.STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
                styleAttributes = getStyleSheet().getDeclaration(decl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
                a.addAttributes(styleAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
                styleAttributes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2711
            TagAction action = tagMap.get(t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
                action.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
                action.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
            else if (getPreservesUnknownTags()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
                // unknown tag, only add if should preserve it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
                addSpecialElement(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
         * This is invoked after the stream has been parsed, but before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
         * <code>flush</code>. <code>eol</code> will be one of \n, \r
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
         * or \r\n, which ever is encountered the most in parsing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
         * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
        public void handleEndOfLineString(String eol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
            if (emptyDocument && eol != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
                putProperty(DefaultEditorKit.EndOfLineStringProperty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
                            eol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
        // ---- tag handling support ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
         * Registers a handler for the given tag.  By default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
         * all of the well-known tags will have been registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
         * This can be used to change the handling of a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
         * tag or to add support for custom tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
        protected void registerTag(HTML.Tag t, TagAction a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
            tagMap.put(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
         * An action to be performed in response
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
         * to parsing a tag.  This allows customization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
         * of how each tag is handled and avoids a large
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
         * switch statement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
        public class TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
             * Called when a start tag is seen for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
             * type of tag this action was registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
             * to.  The tag argument indicates the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
             * tag for those actions that are shared across
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
             * many tags.  By default this does nothing and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
             * completely ignores the tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
             * Called when an end tag is seen for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
             * type of tag this action was registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
             * to.  The tag argument indicates the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
             * tag for those actions that are shared across
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
             * many tags.  By default this does nothing and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
             * completely ignores the tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
        public class BlockAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
                blockOpen(t, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
                blockClose(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
         * Action used for the actual element form tag. This is named such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
         * as there was already a public class named FormAction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
        private class FormTagAction extends BlockAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                super.start(t, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
                // initialize a ButtonGroupsMap when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
                // FORM tag is encountered.  This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
                // be used for any radio buttons that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
                // might be defined in the FORM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
                // for new group new ButtonGroup will be created (fix for 4529702)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                // group name is a key in radioButtonGroupsMap
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2806
                radioButtonGroupsMap = new HashMap<String, ButtonGroup>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
                super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
                // reset the button group to null since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                // the form has ended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                radioButtonGroupsMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
        public class ParagraphAction extends BlockAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                super.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                inParagraph = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
                super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
                inParagraph = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
        public class SpecialAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
                addSpecialElement(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
        public class IsindexAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
                blockOpen(HTML.Tag.IMPLIED, new SimpleAttributeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
                addSpecialElement(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
                blockClose(HTML.Tag.IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
        public class HiddenAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
                addSpecialElement(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
                if (!isEmpty(t)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
                    MutableAttributeSet a = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
                    a.addAttribute(HTML.Attribute.ENDTAG, "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
                    addSpecialElement(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
            boolean isEmpty(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
                if (t == HTML.Tag.APPLET ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
                    t == HTML.Tag.SCRIPT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
         * Subclass of HiddenAction to set the content type for style sheets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
         * and to set the name of the default style sheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
        class MetaAction extends HiddenAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
                Object equiv = a.getAttribute(HTML.Attribute.HTTPEQUIV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
                if (equiv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
                    equiv = ((String)equiv).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
                    if (equiv.equals("content-style-type")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
                        String value = (String)a.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
                                       (HTML.Attribute.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
                        setDefaultStyleSheetType(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
                        isStyleCSS = "text/css".equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
                                      (getDefaultStyleSheetType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                    else if (equiv.equals("default-style")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                        defaultStyle = (String)a.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
                                       (HTML.Attribute.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
                super.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
            boolean isEmpty(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
         * End if overridden to create the necessary stylesheets that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
         * are referenced via the link tag. It is done in this manner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
         * as the meta tag can be used to specify an alternate style sheet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
         * and is not guaranteed to come before the link tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
        class HeadAction extends BlockAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
                inHead = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                // This check of the insertTag is put in to avoid considering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                // the implied-p that is generated for the head. This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                // inserts for HR to work correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                if ((insertTag == null && !insertAfterImplied) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
                    (insertTag == HTML.Tag.HEAD) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                    (insertAfterImplied &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
                     (foundInsertTag || !a.isDefined(IMPLIED)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
                    super.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                inHead = inStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
                // See if there is a StyleSheet to link to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
                if (styles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                    boolean isDefaultCSS = isStyleCSS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
                    for (int counter = 0, maxCounter = styles.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
                         counter < maxCounter;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
                        Object value = styles.elementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
                        if (value == HTML.Tag.LINK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
                            handleLink((AttributeSet)styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
                                       elementAt(++counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                            counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                            // Rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
                            // First element gives type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
                            String type = (String)styles.elementAt(++counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
                            boolean isCSS = (type == null) ? isDefaultCSS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
                                            type.equals("text/css");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
                            while (++counter < maxCounter &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
                                   (styles.elementAt(counter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
                                    instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
                                if (isCSS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
                                    addCSSRules((String)styles.elementAt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
                                                (counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
                if ((insertTag == null && !insertAfterImplied) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
                    insertTag == HTML.Tag.HEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
                    (insertAfterImplied && foundInsertTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
                    super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
            boolean isEmpty(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
            private void handleLink(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
                // Link.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
                String type = (String)attr.getAttribute(HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
                if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
                    type = getDefaultStyleSheetType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                // Only choose if type==text/css
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
                // Select link if rel==stylesheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                // Otherwise if rel==alternate stylesheet and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                //   title matches default style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                if (type.equals("text/css")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
                    String rel = (String)attr.getAttribute(HTML.Attribute.REL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
                    String title = (String)attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
                                               (HTML.Attribute.TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
                    String media = (String)attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
                                                   (HTML.Attribute.MEDIA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
                    if (media == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
                        media = "all";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
                        media = media.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
                    if (rel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
                        rel = rel.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
                        if ((media.indexOf("all") != -1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
                             media.indexOf("screen") != -1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
                            (rel.equals("stylesheet") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
                             (rel.equals("alternate stylesheet") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
                              title.equals(defaultStyle)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
                            linkCSSStyleSheet((String)attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
                                              (HTML.Attribute.HREF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
         * A subclass to add the AttributeSet to styles if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
         * attributes contains an attribute for 'rel' with value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
         * 'stylesheet' or 'alternate stylesheet'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
        class LinkAction extends HiddenAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
                String rel = (String)a.getAttribute(HTML.Attribute.REL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
                if (rel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
                    rel = rel.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
                    if (rel.equals("stylesheet") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
                        rel.equals("alternate stylesheet")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
                        if (styles == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3019
                            styles = new Vector<Object>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
                        styles.addElement(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
                        styles.addElement(a.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
                super.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
        class MapAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
                lastMap = new Map((String)a.getAttribute(HTML.Attribute.NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
                addMap(lastMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
        class AreaAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
                if (lastMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
                    lastMap.addArea(a.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        class StyleAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
                if (inHead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
                    if (styles == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3059
                        styles = new Vector<Object>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
                    styles.addElement(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
                    styles.addElement(a.getAttribute(HTML.Attribute.TYPE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
                    inStyle = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
                inStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
            boolean isEmpty(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
        public class PreAction extends BlockAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
                inPre = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
                blockOpen(t, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
                attr.addAttribute(CSS.Attribute.WHITE_SPACE, "pre");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
                blockOpen(HTML.Tag.IMPLIED, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
                blockClose(HTML.Tag.IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
                // set inPre to false after closing, so that if a newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
                // is added it won't generate a blockOpen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
                inPre = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
                blockClose(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
        public class CharacterAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
                pushCharacterStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
                if (!foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
                    // Note that the third argument should really be based off
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
                    // inParagraph and impliedP. If we're wrong (that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
                    // insertTagDepthDelta shouldn't be changed), we'll end up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
                    // removing an extra EndSpec, which won't matter anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
                    boolean insert = canInsertTag(t, attr, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
                    if (foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
                        if (!inParagraph) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
                            inParagraph = impliedP = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
                    if (!insert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
                if (attr.isDefined(IMPLIED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
                    attr.removeAttribute(IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
                charAttr.addAttribute(t, attr.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
                if (styleAttributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
                    charAttr.addAttributes(styleAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
                popCharacterStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
         * Provides conversion of HTML tag/attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
         * mappings that have a corresponding StyleConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
         * and CSS mapping.  The conversion is to CSS attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
        class ConvertAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
                pushCharacterStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
                if (!foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
                    // Note that the third argument should really be based off
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
                    // inParagraph and impliedP. If we're wrong (that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
                    // insertTagDepthDelta shouldn't be changed), we'll end up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
                    // removing an extra EndSpec, which won't matter anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
                    boolean insert = canInsertTag(t, attr, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
                    if (foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
                        if (!inParagraph) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
                            inParagraph = impliedP = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
                    if (!insert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
                if (attr.isDefined(IMPLIED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
                    attr.removeAttribute(IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
                if (styleAttributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
                    charAttr.addAttributes(styleAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
                // We also need to add attr, otherwise we lose custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
                // attributes, including class/id for style lookups, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
                // further confuse style lookup (doesn't have tag).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
                charAttr.addAttribute(t, attr.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
                StyleSheet sheet = getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
                if (t == HTML.Tag.B) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
                    sheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_WEIGHT, "bold");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
                } else if (t == HTML.Tag.I) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
                    sheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_STYLE, "italic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
                } else if (t == HTML.Tag.U) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
                    Object v = charAttr.getAttribute(CSS.Attribute.TEXT_DECORATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
                    String value = "underline";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
                    value = (v != null) ? value + "," + v.toString() : value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
                    sheet.addCSSAttribute(charAttr, CSS.Attribute.TEXT_DECORATION, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
                } else if (t == HTML.Tag.STRIKE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
                    Object v = charAttr.getAttribute(CSS.Attribute.TEXT_DECORATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
                    String value = "line-through";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
                    value = (v != null) ? value + "," + v.toString() : value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
                    sheet.addCSSAttribute(charAttr, CSS.Attribute.TEXT_DECORATION, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
                } else if (t == HTML.Tag.SUP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
                    Object v = charAttr.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
                    String value = "sup";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
                    value = (v != null) ? value + "," + v.toString() : value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
                    sheet.addCSSAttribute(charAttr, CSS.Attribute.VERTICAL_ALIGN, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
                } else if (t == HTML.Tag.SUB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
                    Object v = charAttr.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
                    String value = "sub";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
                    value = (v != null) ? value + "," + v.toString() : value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
                    sheet.addCSSAttribute(charAttr, CSS.Attribute.VERTICAL_ALIGN, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
                } else if (t == HTML.Tag.FONT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
                    String color = (String) attr.getAttribute(HTML.Attribute.COLOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
                    if (color != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
                        sheet.addCSSAttribute(charAttr, CSS.Attribute.COLOR, color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
                    String face = (String) attr.getAttribute(HTML.Attribute.FACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
                    if (face != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
                        sheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_FAMILY, face);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
                    String size = (String) attr.getAttribute(HTML.Attribute.SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
                    if (size != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
                        sheet.addCSSAttributeFromHTML(charAttr, CSS.Attribute.FONT_SIZE, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
                popCharacterStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
        class AnchorAction extends CharacterAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
                // set flag to catch empty anchors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
                emptyAnchor = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
                super.start(t, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
                if (emptyAnchor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
                    // if the anchor was empty it was probably a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
                    // named anchor point and we don't want to throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
                    // it away.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
                    char[] one = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
                    one[0] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
                    addContent(one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
                super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
        class TitleAction extends HiddenAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
                inTitle = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
                super.start(t, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
                inTitle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
                super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
            boolean isEmpty(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
        class BaseAction extends TagAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
                String href = (String) attr.getAttribute(HTML.Attribute.HREF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
                if (href != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
                        URL newBase = new URL(base, href);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
                        setBase(newBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
                        hasBaseTag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
                    } catch (MalformedURLException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
                baseTarget = (String) attr.getAttribute(HTML.Attribute.TARGET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
        class ObjectAction extends SpecialAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
            public void start(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
                if (t == HTML.Tag.PARAM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
                    addParameter(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
                    super.start(t, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
                if (t != HTML.Tag.PARAM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
                    super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
            void addParameter(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
                String name = (String) a.getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
                String value = (String) a.getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
                if ((name != null) && (value != null)) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3284
                    ElementSpec objSpec = parseBuffer.lastElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
                    MutableAttributeSet objAttr = (MutableAttributeSet) objSpec.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
                    objAttr.addAttribute(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
         * Action to support forms by building all of the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
         * used to represent form controls.  This will process
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
         * the &lt;INPUT&gt;, &lt;TEXTAREA&gt;, &lt;SELECT&gt;,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
         * and &lt;OPTION&gt; tags.  The element created by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
         * this action is expected to have the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
         * <code>StyleConstants.ModelAttribute</code> set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
         * the model that holds the state for the form control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
         * This enables multiple views, and allows document to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
         * be iterated over picking up the data of the form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
         * The following are the model assignments for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
         * various type of form elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
         * <table summary="model assignments for the various types of form elements">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
         *   <th>Element Type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
         *   <th>Model Type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
         *   <td>input, type button
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
         *   <td>{@link DefaultButtonModel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
         *   <td>input, type checkbox
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
         *   <td>{@link javax.swing.JToggleButton.ToggleButtonModel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
         *   <td>input, type image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
         *   <td>{@link DefaultButtonModel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
         *   <td>input, type password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
         *   <td>{@link PlainDocument}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
         *   <td>input, type radio
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
         *   <td>{@link javax.swing.JToggleButton.ToggleButtonModel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
         *   <td>input, type reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
         *   <td>{@link DefaultButtonModel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
         *   <td>input, type submit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
         *   <td>{@link DefaultButtonModel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
         *   <td>input, type text or type is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
         *   <td>{@link PlainDocument}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
         *   <td>select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
         *   <td>{@link DefaultComboBoxModel} or an {@link DefaultListModel}, with an item type of Option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
         * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
         *   <td>textarea
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
         *   <td>{@link PlainDocument}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
         * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
        public class FormAction extends SpecialAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
            public void start(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
                if (t == HTML.Tag.INPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
                    String type = (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
                        attr.getAttribute(HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
                    /*
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  3347
                     * if type is not defined the default is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
                     * assumed to be text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
                    if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
                        type = "text";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
                        attr.addAttribute(HTML.Attribute.TYPE, "text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
                    setModel(type, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
                } else if (t == HTML.Tag.TEXTAREA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
                    inTextArea = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
                    textAreaDocument = new TextAreaDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
                    attr.addAttribute(StyleConstants.ModelAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
                                      textAreaDocument);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
                } else if (t == HTML.Tag.SELECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
                    int size = HTML.getIntegerAttributeValue(attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
                                                             HTML.Attribute.SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
                                                             1);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3364
                    boolean multiple = attr.getAttribute(HTML.Attribute.MULTIPLE) != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
                    if ((size > 1) || multiple) {
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
  3366
                        OptionListModel<Option> m = new OptionListModel<Option>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
                        if (multiple) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
                            m.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
                        selectModel = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
                    } else {
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
  3372
                        selectModel = new OptionComboBoxModel<Option>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
                    attr.addAttribute(StyleConstants.ModelAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
                                      selectModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
                // build the element, unless this is an option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
                if (t == HTML.Tag.OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
                    option = new Option(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
                    if (selectModel instanceof OptionListModel) {
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
  3384
                        OptionListModel<Option> m = (OptionListModel<Option>) selectModel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
                        m.addElement(option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
                        if (option.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
                            m.addSelectionInterval(optionCount, optionCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
                            m.setInitialSelection(optionCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
                    } else if (selectModel instanceof OptionComboBoxModel) {
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
  3391
                        OptionComboBoxModel<Option> m = (OptionComboBoxModel<Option>) selectModel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
                        m.addElement(option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
                        if (option.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
                            m.setSelectedItem(option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
                            m.setInitialSelection(option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
                    optionCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
                    super.start(t, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
            public void end(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
                if (t == HTML.Tag.OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
                    option = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
                    if (t == HTML.Tag.SELECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
                        selectModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
                        optionCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
                    } else if (t == HTML.Tag.TEXTAREA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
                        inTextArea = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
                        /* Now that the textarea has ended,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
                         * store the entire initial text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
                         * of the text area.  This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
                         * enable us to restore the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
                         * state if a reset is requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
                        textAreaDocument.storeInitialText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
                    super.end(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
            void setModel(String type, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
                if (type.equals("submit") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
                    type.equals("reset") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
                    type.equals("image")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
                    // button model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
                    attr.addAttribute(StyleConstants.ModelAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
                                      new DefaultButtonModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
                } else if (type.equals("text") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
                           type.equals("password")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
                    // plain text model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
                    int maxLength = HTML.getIntegerAttributeValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
                                       attr, HTML.Attribute.MAXLENGTH, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
                    Document doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
                    if (maxLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
                        doc = new FixedLengthDocument(maxLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
                        doc = new PlainDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
                    String value = (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
                        attr.getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
                        doc.insertString(0, value, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
                    } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
                    attr.addAttribute(StyleConstants.ModelAttribute, doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
                } else if (type.equals("file")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
                    // plain text model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                    attr.addAttribute(StyleConstants.ModelAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
                                      new PlainDocument());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
                } else if (type.equals("checkbox") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
                           type.equals("radio")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
                    JToggleButton.ToggleButtonModel model = new JToggleButton.ToggleButtonModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
                    if (type.equals("radio")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
                        String name = (String) attr.getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
                        if ( radioButtonGroupsMap == null ) { //fix for 4772743
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3464
                           radioButtonGroupsMap = new HashMap<String, ButtonGroup>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
                        }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3466
                        ButtonGroup radioButtonGroup = radioButtonGroupsMap.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
                        if (radioButtonGroup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
                            radioButtonGroup = new ButtonGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
                            radioButtonGroupsMap.put(name,radioButtonGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
                        model.setGroup(radioButtonGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
                    boolean checked = (attr.getAttribute(HTML.Attribute.CHECKED) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
                    model.setSelected(checked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
                    attr.addAttribute(StyleConstants.ModelAttribute, model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
             * If a &lt;SELECT&gt; tag is being processed, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
             * model will be a reference to the model being filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
             * with the &lt;OPTION&gt; elements (which produce
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
             * objects of type <code>Option</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
            Object selectModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
            int optionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
        // --- utility methods used by the reader ------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
         * Pushes the current character style on a stack in preparation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
         * for forming a new nested character style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
        protected void pushCharacterStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
            charAttrStack.push(charAttr.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
         * Pops a previously pushed character style off the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
         * to return to a previous style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
        protected void popCharacterStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
            if (!charAttrStack.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
                charAttr = (MutableAttributeSet) charAttrStack.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
                charAttrStack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
         * Adds the given content to the textarea document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
         * This method gets called when we are in a textarea
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
         * context.  Therefore all text that is seen belongs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
         * to the text area and is hence added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
         * TextAreaDocument associated with the text area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
        protected void textAreaContent(char[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
                textAreaDocument.insertString(textAreaDocument.getLength(), new String(data), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
                // Should do something reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
         * Adds the given content that was encountered in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
         * PRE element.  This synthesizes lines to hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
         * runs of text, and makes calls to addContent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
         * actually add the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
        protected void preContent(char[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
            int last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
            for (int i = 0; i < data.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
                if (data[i] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
                    addContent(data, last, i - last + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
                    blockClose(HTML.Tag.IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
                    MutableAttributeSet a = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
                    a.addAttribute(CSS.Attribute.WHITE_SPACE, "pre");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
                    blockOpen(HTML.Tag.IMPLIED, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
                    last = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
            if (last < data.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
                addContent(data, last, data.length - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
         * Adds an instruction to the parse buffer to create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
         * block element with the given attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
        protected void blockOpen(HTML.Tag t, MutableAttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
            if (impliedP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
                blockClose(HTML.Tag.IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
            inBlock++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
            if (!canInsertTag(t, attr, true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
            if (attr.isDefined(IMPLIED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
                attr.removeAttribute(IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
            lastWasNewline = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
            attr.addAttribute(StyleConstants.NameAttribute, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
            ElementSpec es = new ElementSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
                attr.copyAttributes(), ElementSpec.StartTagType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
            parseBuffer.addElement(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
         * Adds an instruction to the parse buffer to close out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
         * a block element of the given type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
        protected void blockClose(HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
            inBlock--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
            if (!foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
            // Add a new line, if the last character wasn't one. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
            // needed for proper positioning of the cursor. addContent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
            // with true will force an implied paragraph to be generated if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
            // there isn't one. This may result in a rather bogus structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
            // (perhaps a table with a child pargraph), but the paragraph
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
            // is needed for proper positioning and display.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
            if(!lastWasNewline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
                pushCharacterStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
                charAttr.addAttribute(IMPLIED_CR, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
                addContent(NEWLINE, 0, 1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
                popCharacterStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
                lastWasNewline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
            if (impliedP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
                impliedP = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
                inParagraph = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
                if (t != HTML.Tag.IMPLIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
                    blockClose(HTML.Tag.IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
            // an open/close with no content will be removed, so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
            // add a space of content to keep the element being formed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
            ElementSpec prev = (parseBuffer.size() > 0) ?
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3608
                parseBuffer.lastElement() : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
            if (prev != null && prev.getType() == ElementSpec.StartTagType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
                char[] one = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
                one[0] = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
                addContent(one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
            ElementSpec es = new ElementSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
                null, ElementSpec.EndTagType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
            parseBuffer.addElement(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
         * Adds some text with the current character attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
         * @param data the content to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
         * @param offs the initial offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
         * @param length the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
        protected void addContent(char[] data, int offs, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
            addContent(data, offs, length, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
         * Adds some text with the current character attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
         * @param data the content to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
         * @param offs the initial offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
         * @param length the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
         * @param generateImpliedPIfNecessary whether to generate implied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
         * paragraphs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
        protected void addContent(char[] data, int offs, int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
                                  boolean generateImpliedPIfNecessary) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
            if (!foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
            if (generateImpliedPIfNecessary && (! inParagraph) && (! inPre)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
                blockOpen(HTML.Tag.IMPLIED, new SimpleAttributeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
                inParagraph = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
                impliedP = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
            emptyAnchor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
            charAttr.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
            AttributeSet a = charAttr.copyAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
            ElementSpec es = new ElementSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
                a, ElementSpec.ContentType, data, offs, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
            parseBuffer.addElement(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
            if (parseBuffer.size() > threshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
                if ( threshold <= MaxThreshold ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
                    threshold *= StepThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
                    flushBuffer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
                } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
            if(length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
                lastWasNewline = (data[offs + length - 1] == '\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
         * Adds content that is basically specified entirely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
         * in the attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
        protected void addSpecialElement(HTML.Tag t, MutableAttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
            if ((t != HTML.Tag.FRAME) && (! inParagraph) && (! inPre)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
                nextTagAfterPImplied = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
                blockOpen(HTML.Tag.IMPLIED, new SimpleAttributeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
                nextTagAfterPImplied = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
                inParagraph = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
                impliedP = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
            if (!canInsertTag(t, a, t.isBlock())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
            if (a.isDefined(IMPLIED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
                a.removeAttribute(IMPLIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
            emptyAnchor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
            a.addAttributes(charAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
            a.addAttribute(StyleConstants.NameAttribute, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
            char[] one = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
            one[0] = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
            ElementSpec es = new ElementSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
                a.copyAttributes(), ElementSpec.ContentType, one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
            parseBuffer.addElement(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
            // Set this to avoid generating a newline for frames, frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
            // shouldn't have any content, and shouldn't need a newline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
            if (t == HTML.Tag.FRAME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
                lastWasNewline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
         * Flushes the current parse buffer into the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
         * @param endOfStream true if there is no more content to parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
        void flushBuffer(boolean endOfStream) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
            int oldLength = HTMLDocument.this.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
            int size = parseBuffer.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
            if (endOfStream && (insertTag != null || insertAfterImplied) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
                size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
                adjustEndSpecsForPartialInsert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
                size = parseBuffer.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
            ElementSpec[] spec = new ElementSpec[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
            parseBuffer.copyInto(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
            if (oldLength == 0 && (insertTag == null && !insertAfterImplied)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
                create(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
                insert(offset, spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
            parseBuffer.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
            offset += HTMLDocument.this.getLength() - oldLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
            flushCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
         * This will be invoked for the last flush, if <code>insertTag</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
         * is non null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
        private void adjustEndSpecsForPartialInsert() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
            int size = parseBuffer.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
            if (insertTagDepthDelta < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
                // When inserting via an insertTag, the depths (of the tree
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  3737
                // being read in, and existing hierarchy) may not match up.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
                // This attemps to clean it up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
                int removeCounter = insertTagDepthDelta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
                while (removeCounter < 0 && size >= 0 &&
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3741
                        parseBuffer.elementAt(size - 1).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
                       getType() == ElementSpec.EndTagType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
                    parseBuffer.removeElementAt(--size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
                    removeCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
            if (flushCount == 0 && (!insertAfterImplied ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
                                    !wantsTrailingNewline)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
                // If this starts with content (or popDepth > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
                // pushDepth > 0) and ends with EndTagTypes, make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
                // the last content isn't a \n, otherwise will end up with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
                // an extra \n in the middle of content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
                int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
                if (pushDepth > 0) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3755
                    if (parseBuffer.elementAt(0).getType() ==
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
                        ElementSpec.ContentType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
                        index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
                index += (popDepth + pushDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
                int cCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
                int cStart = index;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3763
                while (index < size && parseBuffer.elementAt
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3764
                        (index).getType() == ElementSpec.ContentType) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
                    index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
                    cCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
                if (cCount > 1) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3769
                    while (index < size && parseBuffer.elementAt
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3770
                            (index).getType() == ElementSpec.EndTagType) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
                        index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
                    if (index == size) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3774
                        char[] lastText = parseBuffer.elementAt
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3775
                                (cStart + cCount - 1).getArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
                        if (lastText.length == 1 && lastText[0] == NEWLINE[0]){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
                            index = cStart + cCount - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
                            while (size > index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
                                parseBuffer.removeElementAt(--size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
            if (wantsTrailingNewline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
                // Make sure there is in fact a newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
                for (int counter = parseBuffer.size() - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
                                   counter--) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3789
                    ElementSpec spec = parseBuffer.elementAt(counter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
                    if (spec.getType() == ElementSpec.ContentType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
                        if (spec.getArray()[spec.getLength() - 1] != '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
                            SimpleAttributeSet attrs =new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
                            attrs.addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
                                               HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
                            parseBuffer.insertElementAt(new ElementSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
                                    attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
                                    ElementSpec.ContentType, NEWLINE, 0, 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
                                    counter + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
         * Adds the CSS rules in <code>rules</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
        void addCSSRules(String rules) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
            StyleSheet ss = getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
            ss.addRule(rules);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
         * Adds the CSS stylesheet at <code>href</code> to the known list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
         * of stylesheets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
        void linkCSSStyleSheet(String href) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3820
            URL url;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
                url = new URL(base, href);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
            } catch (MalformedURLException mfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
                    url = new URL(href);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
                } catch (MalformedURLException mfe2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
                    url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
            if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
                getStyleSheet().importStyleSheet(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
         * Returns true if can insert starting at <code>t</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
         * will return false if the insert tag is set, and hasn't been found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
         * yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
        private boolean canInsertTag(HTML.Tag t, AttributeSet attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
                                     boolean isBlockTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
            if (!foundInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
                boolean needPImplied = ((t == HTML.Tag.IMPLIED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
                                                          && (!inParagraph)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
                                                          && (!inPre));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
                if (needPImplied && (nextTagAfterPImplied != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
                     * If insertTag == null then just proceed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
                     * foundInsertTag() call below and return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
                    if (insertTag != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
                        boolean nextTagIsInsertTag =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
                                isInsertTag(nextTagAfterPImplied);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
                        if ( (! nextTagIsInsertTag) || (! insertInsertTag) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
                     *  Proceed to foundInsertTag() call...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
                 } else if ((insertTag != null && !isInsertTag(t))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
                               || (insertAfterImplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
                                    && (attr == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
                                        || attr.isDefined(IMPLIED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
                                        || t == HTML.Tag.IMPLIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
                                       )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
                                   )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
                           ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
                // Allow the insert if t matches the insert tag, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
                // insertAfterImplied is true and the element is implied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
                foundInsertTag(isBlockTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
                if (!insertInsertTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
        private boolean isInsertTag(HTML.Tag tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
            return (insertTag == tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
        private void foundInsertTag(boolean isBlockTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
            foundInsertTag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
            if (!insertAfterImplied && (popDepth > 0 || pushDepth > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
                    if (offset == 0 || !getText(offset - 1, 1).equals("\n")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
                        // Need to insert a newline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
                        AttributeSet newAttrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
                        boolean joinP = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
                        if (offset != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
                            // Determine if we can use JoinPrevious, we can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
                            // if the Element has some attributes that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
                            // not meant to be duplicated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
                            Element charElement = getCharacterElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
                                                    (offset - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
                            AttributeSet attrs = charElement.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
                            if (attrs.isDefined(StyleConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
                                                ComposedTextAttribute)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
                                joinP = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
                                Object name = attrs.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
                                              (StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
                                if (name instanceof HTML.Tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
                                    HTML.Tag tag = (HTML.Tag)name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
                                    if (tag == HTML.Tag.IMG ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
                                        tag == HTML.Tag.HR ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
                                        tag == HTML.Tag.COMMENT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
                                        (tag instanceof HTML.UnknownTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
                                        joinP = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
                        if (!joinP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
                            // If not joining with the previous element, be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
                            // sure and set the name (otherwise it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
                            // inherited).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
                            newAttrs = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
                            ((SimpleAttributeSet)newAttrs).addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
                                              (StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
                                               HTML.Tag.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
                        ElementSpec es = new ElementSpec(newAttrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
                                     ElementSpec.ContentType, NEWLINE, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
                                     NEWLINE.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
                        if (joinP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
                            es.setDirection(ElementSpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
                                            JoinPreviousDirection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
                        parseBuffer.addElement(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
                } catch (BadLocationException ble) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
            // pops
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
            for (int counter = 0; counter < popDepth; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
                parseBuffer.addElement(new ElementSpec(null, ElementSpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
                                                       EndTagType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
            // pushes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
            for (int counter = 0; counter < pushDepth; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
                ElementSpec es = new ElementSpec(null, ElementSpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
                                                 StartTagType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
                es.setDirection(ElementSpec.JoinNextDirection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
                parseBuffer.addElement(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
            insertTagDepthDelta = depthTo(Math.max(0, offset - 1)) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
                                  popDepth + pushDepth - inBlock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
            if (isBlockTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
                // A start spec will be added (for this tag), so we account
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
                // for it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
                insertTagDepthDelta++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
                // An implied paragraph close (end spec) is going to be added,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
                // so we account for it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
                insertTagDepthDelta--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
                inParagraph = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
                lastWasNewline = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
        /**
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
  3971
         * This is set to true when and end is invoked for {@literal <html>}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
        private boolean receivedEndHTML;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
        /** Number of times <code>flushBuffer</code> has been invoked. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
        private int flushCount;
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21244
diff changeset
  3976
        /** If true, behavior is similar to insertTag, but instead of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
         * waiting for insertTag will wait for first Element without
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
         * an 'implied' attribute and begin inserting then. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
        private boolean insertAfterImplied;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3980
        /** This is only used if insertAfterImplied is true. If false, only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3981
         * inserting content, and there is a trailing newline it is removed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3982
        private boolean wantsTrailingNewline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3983
        int threshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
        int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
        boolean inParagraph = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
        boolean impliedP = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
        boolean inPre = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
        boolean inTextArea = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
        TextAreaDocument textAreaDocument = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
        boolean inTitle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
        boolean lastWasNewline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
        boolean emptyAnchor;
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21278
diff changeset
  3993
        /** True if (!emptyDocument &amp;&amp; insertTag == null), this is used so
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
         * much it is cached. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
        boolean midInsert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
        /** True when the body has been encountered. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
        boolean inBody;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
        /** If non null, gives parent Tag that insert is to happen at. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
        HTML.Tag insertTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
        /** If true, the insertTag is inserted, otherwise elements after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
         * the insertTag is found are inserted. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4002
        boolean insertInsertTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4003
        /** Set to true when insertTag has been found. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4004
        boolean foundInsertTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4005
        /** When foundInsertTag is set to true, this will be updated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
         * reflect the delta between the two structures. That is, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
         * will be the depth the inserts are happening at minus the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4008
         * depth of the tags being passed in. A value of 0 (the common
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4009
         * case) indicates the structures match, a value greater than 0 indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4010
         * the insert is happening at a deeper depth than the stream is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4011
         * parsing, and a value less than 0 indicates the insert is happening earlier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4012
         * in the tree that the parser thinks and that we will need to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
         * EndTagType specs in the flushBuffer method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
        int insertTagDepthDelta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
        /** How many parents to ascend before insert new elements. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
        int popDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
        /** How many parents to descend (relative to popDepth) before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
         * inserting. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
        int pushDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
        /** Last Map that was encountered. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
        Map lastMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
        /** Set to true when a style element is encountered. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
        boolean inStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
        /** Name of style to use. Obtained from Meta tag. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
        String defaultStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
        /** Vector describing styles that should be include. Will consist
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
         * of a bunch of HTML.Tags, which will either be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
         * <p>LINK: in which case it is followed by an AttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
         * <p>STYLE: in which case the following element is a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
         * indicating the type (may be null), and the elements following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
         * it until the next HTML.Tag are the rules as Strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
         */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  4034
        Vector<Object> styles;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
        /** True if inside the head tag. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
        boolean inHead = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
        /** Set to true if the style language is text/css. Since this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4038
         * used alot, it is cached. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4039
        boolean isStyleCSS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4040
        /** True if inserting into an empty document. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4041
        boolean emptyDocument;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
        /** Attributes from a style Attribute. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
        AttributeSet styleAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4045
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4046
         * Current option, if in an option element (needed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4047
         * load the label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4048
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
        Option option;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  4051
        protected Vector<ElementSpec> parseBuffer = new Vector<ElementSpec>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
        protected MutableAttributeSet charAttr = new TaggedAttributeSet();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  4053
        Stack<AttributeSet> charAttrStack = new Stack<AttributeSet>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  4054
        Hashtable<HTML.Tag, TagAction> tagMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
        int inBlock = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4058
         * This attribute is sometimes used to refer to next tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
         * to be handled after p-implied when the latter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
         * the current tag which is being handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
        private HTML.Tag nextTagAfterPImplied = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
     * Used by StyleSheet to determine when to avoid removing HTML.Tags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
     * matching StyleConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
    static class TaggedAttributeSet extends SimpleAttributeSet {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
        TaggedAttributeSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
     * An element that represents a chunk of text that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
     * a set of HTML character level attributes assigned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4081
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4082
    public class RunElement extends LeafElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4084
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
         * Constructs an element that represents content within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
         * document (has no children).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
         * @param parent  the parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
         * @param a       the element attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
         * @param offs0   the start offset (must be at least 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
         * @param offs1   the end offset (must be at least offs0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4093
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4094
        public RunElement(Element parent, AttributeSet a, int offs0, int offs1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4095
            super(parent, a, offs0, offs1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
         * Gets the name of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
         * @return the name, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
            Object o = getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
            if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
                return o.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
            return super.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4111
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4112
         * Gets the resolving parent.  HTML attributes are not inherited
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4113
         * at the model level so we override this to return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
         * @return null, there are none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4116
         * @see AttributeSet#getResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4117
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4118
        public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4119
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
     * An element that represents a structural <em>block</em> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
     * HTML.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
    public class BlockElement extends BranchElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4130
         * Constructs a composite element that initially contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
         * no children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
         * @param parent  the parent element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
         * @param a       the attributes for the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
        public BlockElement(Element parent, AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
            super(parent, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
         * Gets the name of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
         * @return the name, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4146
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4147
            Object o = getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4148
            if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4149
                return o.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
            return super.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4154
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4155
         * Gets the resolving parent.  HTML attributes are not inherited
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4156
         * at the model level so we override this to return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
         * @return null, there are none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
         * @see AttributeSet#getResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
        public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
     * Document that allows you to set the maximum length of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
    private static class FixedLengthDocument extends PlainDocument {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
        private int maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
        public FixedLengthDocument(int maxLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
            this.maxLength = maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
        public void insertString(int offset, String str, AttributeSet a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
            throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
            if (str != null && str.length() + getLength() <= maxLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
                super.insertString(offset, str, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4185
}