jdk/src/share/classes/javax/swing/text/html/parser/TagStack.java
author darcy
Wed, 22 Jan 2014 23:20:58 -0800
changeset 22567 5816a47fa4dd
parent 5506 202f599c92aa
permissions -rw-r--r--
8032047: Fix static lint warnings in client libraries 8032048: Add static lint warning to build of jdk repository Reviewed-by: pchelko, serb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
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 java.util.BitSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A stack of tags. Used while parsing an HTML document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * It, together with the ContentModelStates, defines the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * complete state of the parser while reading a document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * When a start tag is encountered an element is pushed onto
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * the stack, when an end tag is enountered an element is popped
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * of the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see Parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see DTD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see ContentModelState
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
class TagStack implements DTDConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    TagElement tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    Element elem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    ContentModelState state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    TagStack next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    BitSet inclusions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    BitSet exclusions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    boolean net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    boolean pre;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Construct a stack element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    TagStack(TagElement tag, TagStack next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.tag = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.elem = tag.getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        Element elem = tag.getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (elem.getContent() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            this.state = new ContentModelState(elem.getContent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (next != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            inclusions = next.inclusions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            exclusions = next.exclusions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            pre = next.pre;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (tag.isPreformatted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            pre = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (elem.inclusions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (inclusions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                inclusions = (BitSet)inclusions.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                inclusions.or(elem.inclusions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                inclusions = elem.inclusions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (elem.exclusions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (exclusions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                exclusions = (BitSet)exclusions.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                exclusions.or(elem.exclusions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                exclusions = elem.exclusions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Return the element that must come next in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public Element first() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return (state != null) ? state.first() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Return the ContentModel that must be satisfied by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * what comes next in the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public ContentModel contentModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (state == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return state.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Return true if the element that is contained at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * the index specified by the parameter is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * the exclusions specified in the DTD for the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * currently on the TagStack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    boolean excluded(int elemIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return (exclusions != null) && exclusions.get(elem.getIndex());
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Advance the state by reducing the given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Returns false if the element is not legal and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * state is not advanced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    boolean advance(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if ((exclusions != null) && exclusions.get(elem.getIndex())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (state != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            ContentModelState newState = state.advance(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (newState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } else if (this.elem.getType() == ANY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return (inclusions != null) && inclusions.get(elem.getIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Return true if the current state can be terminated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    boolean terminate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return (state == null) || state.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Convert to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return (next == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            "<" + tag.getElement().getName() + ">" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            next + " <" + tag.getElement().getName() + ">";
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
class NPrintWriter extends PrintWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private int numLines = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private int numPrinted = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public NPrintWriter (int numberOfLines) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        super(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        numLines = numberOfLines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public void println(char[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (numPrinted >= numLines) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        char[] partialArray = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        for (int i = 0; i < array.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (array[i] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                numPrinted++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (numPrinted == numLines) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                System.arraycopy(array, 0, partialArray, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (partialArray != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            super.print(partialArray);
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 (numPrinted == numLines) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        super.println(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        numPrinted++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}