jdk/src/share/classes/javax/swing/text/html/parser/DocumentParser.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 20169 d7fa6d7586c9
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.text.html.parser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.text.SimpleAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.text.html.HTMLEditorKit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.text.html.HTML;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.text.ChangedCharSetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A Parser for HTML Documents (actually, you can specify a DTD, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * you should really only use this class with the html dtd in swing).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Reads an InputStream of HTML and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * invokes the appropriate methods in the ParserCallback class. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * is the default parser used by HTMLEditorKit to parse HTML url's.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>This will message the callback for all valid tags, as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * tags that are implied but not explicitly specified. For example, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * html string (&lt;p&gt;blah) only has a p tag defined. The callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * will see the following methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <ol><li><i>handleStartTag(html, ...)</i></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *     <li><i>handleStartTag(head, ...)</i></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *     <li><i>handleEndTag(head)</i></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *     <li><i>handleStartTag(body, ...)</i></li>
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
    51
 *     <li><i>handleStartTag(p, ...)</i></li>
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
    52
 *     <li><i>handleText(...)</i></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *     <li><i>handleEndTag(p)</i></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     <li><i>handleEndTag(body)</i></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     <li><i>handleEndTag(html)</i></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * The items in <i>italic</i> are implied, that is, although they were not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * explicitly specified, to be correct html they should have been present
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * (head isn't necessary, but it is still generated). For tags that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * are implied, the AttributeSet argument will have a value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <code>Boolean.TRUE</code> for the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <code>HTMLEditorKit.ParserCallback.IMPLIED</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>HTML.Attributes defines a type safe enumeration of html attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * If an attribute key of a tag is defined in HTML.Attribute, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * HTML.Attribute will be used as the key, otherwise a String will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * For example &lt;p foo=bar class=neat&gt; has two attributes. foo is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * not defined in HTML.Attribute, where as class is, therefore the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * AttributeSet will have two values in it, HTML.Attribute.CLASS with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * a String value of 'neat' and the String key 'foo' with a String value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * 'bar'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>The position argument will indicate the start of the tag, comment
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
    72
 * or text. Similar to arrays, the first character in the stream has a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * position of 0. For tags that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * implied the position will indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * the location of the next encountered tag. In the first example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * the implied start body and html tags will have the same position as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * p tag, and the implied end p, html and body tags will all have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p>As html skips whitespace the position for text will be the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * of the first valid character, eg in the string '\n\n\nblah'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * the text 'blah' will have a position of 3, the newlines are skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * For attributes that do not have a value, eg in the html
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * string <code>&lt;foo blah&gt;</code> the attribute <code>blah</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * does not have a value, there are two possible values that will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * placed in the AttributeSet's value:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <li>If the DTD does not contain an definition for the element, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *     definition does not have an explicit value then the value in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *     AttributeSet will be <code>HTML.NULL_ATTRIBUTE_VALUE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <li>If the DTD contains an explicit value, as in:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *     <code>&lt;!ATTLIST OPTION selected (selected) #IMPLIED&gt;</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *     this value from the dtd (in this case selected) will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Once the stream has been parsed, the callback is notified of the most
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * likely end of line string. The end of line string will be one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * \n, \r or \r\n, which ever is encountered the most in parsing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @author      Sunita Mani
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
public class DocumentParser extends javax.swing.text.html.parser.Parser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private int inbody;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private int intitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private int inhead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private int instyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private int inscript;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private boolean seentitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private HTMLEditorKit.ParserCallback callback = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private boolean ignoreCharSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static final boolean debugFlag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public DocumentParser(DTD dtd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        super(dtd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public void parse(Reader in,  HTMLEditorKit.ParserCallback callback, boolean ignoreCharSet) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.ignoreCharSet = ignoreCharSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.callback = callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        parse(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // end of line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        callback.handleEndOfLineString(getEndOfLineString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Handle Start Tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected void handleStartTag(TagElement tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Element elem = tag.getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (elem == dtd.body) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            inbody++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } else if (elem == dtd.html) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else if (elem == dtd.head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            inhead++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else if (elem == dtd.title) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            intitle++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } else if (elem == dtd.style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            instyle++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } else if (elem == dtd.script) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            inscript++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (debugFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (tag.fictional()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                debug("Start Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                debug("Start Tag: " + tag.getHTMLTag() + " attributes: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                      getAttributes() + " pos: " + getCurrentPos());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (tag.fictional()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            SimpleAttributeSet attrs = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                               Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            callback.handleStartTag(tag.getHTMLTag(), attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                    getBlockStartPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            callback.handleStartTag(tag.getHTMLTag(), getAttributes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                    getBlockStartPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            flushAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected void handleComment(char text[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (debugFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            debug("comment: ->" + new String(text) + "<-"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                  + " pos: " + getCurrentPos());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        callback.handleComment(text, getBlockStartPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Handle Empty Tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        Element elem = tag.getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (elem == dtd.meta && !ignoreCharSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            SimpleAttributeSet atts = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (atts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                String content = (String)atts.getAttribute(HTML.Attribute.CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                if (content != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    if ("content-type".equalsIgnoreCase((String)atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                        if (!content.equalsIgnoreCase("text/html") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                !content.equalsIgnoreCase("text/plain")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                            throw new ChangedCharSetException(content, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    } else if ("charset" .equalsIgnoreCase((String)atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        throw new ChangedCharSetException(content, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (inbody != 0 || elem == dtd.meta || elem == dtd.base || elem == dtd.isindex || elem == dtd.style || elem == dtd.link) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (debugFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                if (tag.fictional()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    debug("Empty Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    debug("Empty Tag: " + tag.getHTMLTag() + " attributes: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                          + getAttributes() + " pos: " + getCurrentPos());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (tag.fictional()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                SimpleAttributeSet attrs = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                   Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                callback.handleSimpleTag(tag.getHTMLTag(), attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                         getBlockStartPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                callback.handleSimpleTag(tag.getHTMLTag(), getAttributes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                         getBlockStartPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                flushAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Handle End Tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    protected void handleEndTag(TagElement tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        Element elem = tag.getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (elem == dtd.body) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            inbody--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        } else if (elem == dtd.title) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            intitle--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            seentitle = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        } else if (elem == dtd.head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            inhead--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } else if (elem == dtd.style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            instyle--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } else if (elem == dtd.script) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            inscript--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (debugFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            debug("End Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        callback.handleEndTag(tag.getHTMLTag(), getBlockStartPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Handle Text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    protected void handleText(char data[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (inscript != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                callback.handleComment(data, getBlockStartPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (inbody != 0 || ((instyle != 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                ((intitle != 0) && !seentitle))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                if (debugFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    debug("text:  ->" + new String(data) + "<-" + " pos: " + getCurrentPos());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                callback.handleText(data, getBlockStartPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Error handling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    protected void handleError(int ln, String errorMsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (debugFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            debug("Error: ->" + errorMsg + "<-" + " pos: " + getCurrentPos());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        /* PENDING: need to improve the error string. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        callback.handleError(errorMsg, getCurrentPos());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * debug messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private void debug(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
}