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