1 /*
2 * Copyright 1998 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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 * Update the Vector elemVec with all the elements that
129 * are part of the inclusions listed in DTD for the element
130 * currently on the TagStack.
131 */
132 boolean included(Vector elemVec, DTD dtd) {
133
134 for (int i = 0 ; i < inclusions.size(); i++) {
135 if (inclusions.get(i)) {
136 elemVec.addElement(dtd.getElement(i));
137 System.out.println("Element add thru' inclusions: " + dtd.getElement(i).getName());
138 }
139 }
140 return (!elemVec.isEmpty());
141 }
142
143
144 /**
145 * Advance the state by reducing the given element.
146 * Returns false if the element is not legal and the
147 * state is not advanced.
148 */
149 boolean advance(Element elem) {
150 if ((exclusions != null) && exclusions.get(elem.getIndex())) {
151 return false;
152 }
153 if (state != null) {
154 ContentModelState newState = state.advance(elem);
155 if (newState != null) {
156 state = newState;
157 return true;
158 }
159 } else if (this.elem.getType() == ANY) {
160 return true;
161 }
162 return (inclusions != null) && inclusions.get(elem.getIndex());
163 }
164
165 /**
166 * Return true if the current state can be terminated.
167 */
168 boolean terminate() {
169 return (state == null) || state.terminate();
170 }
171
172 /**
173 * Convert to a string.
174 */
175 public String toString() {
176 return (next == null) ?
177 "<" + tag.getElement().getName() + ">" :
178 next + " <" + tag.getElement().getName() + ">";
179 }
180 }
181
182 class NPrintWriter extends PrintWriter {
183
184 private int numLines = 5;
185 private int numPrinted = 0;
186
187 public NPrintWriter (int numberOfLines) {
188 super(System.out);
189 numLines = numberOfLines;
190 }
191
192 public void println(char[] array) {
193 if (numPrinted >= numLines) {
194 return;
195 }
196
197 char[] partialArray = null;
198
199 for (int i = 0; i < array.length; i++) {
200 if (array[i] == '\n') {
201 numPrinted++;
202 }
203
204 if (numPrinted == numLines) {
205 System.arraycopy(array, 0, partialArray, 0, i);
206 }
207 }
208
209 if (partialArray != null) {
210 super.print(partialArray);
211 }
212
213 if (numPrinted == numLines) {
214 return;
215 }
216
217 super.println(array);
218 numPrinted++;
219 }
220 }