KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > html > parser > TagStack


1 /*
2  * @(#)TagStack.java 1.10 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.text.html.parser;
9
10 import java.util.BitSet JavaDoc;
11 import java.util.Vector JavaDoc;
12 import java.io.*;
13
14
15 /**
16  * A stack of tags. Used while parsing an HTML document.
17  * It, together with the ContentModelStates, defines the
18  * complete state of the parser while reading a document.
19  * When a start tag is encountered an element is pushed onto
20  * the stack, when an end tag is enountered an element is popped
21  * of the stack.
22  *
23  * @see Parser
24  * @see DTD
25  * @see ContentModelState
26  * @version 1.10, 12/19/03
27  * @author Arthur van Hoff
28  */

29 final
30 class TagStack implements DTDConstants JavaDoc {
31     TagElement JavaDoc tag;
32     Element JavaDoc elem;
33     ContentModelState JavaDoc state;
34     TagStack JavaDoc next;
35     BitSet JavaDoc inclusions;
36     BitSet JavaDoc exclusions;
37     boolean net;
38     boolean pre;
39
40     /**
41      * Construct a stack element.
42      */

43     TagStack(TagElement JavaDoc tag, TagStack JavaDoc next) {
44     this.tag = tag;
45     this.elem = tag.getElement();
46     this.next = next;
47
48     Element JavaDoc elem = tag.getElement();
49     if (elem.getContent() != null) {
50         this.state = new ContentModelState JavaDoc(elem.getContent());
51     }
52
53     if (next != null) {
54         inclusions = next.inclusions;
55         exclusions = next.exclusions;
56         pre = next.pre;
57     }
58     if (tag.isPreformatted()) {
59         pre = true;
60     }
61
62     if (elem.inclusions != null) {
63         if (inclusions != null) {
64         inclusions = (BitSet JavaDoc)inclusions.clone();
65         inclusions.or(elem.inclusions);
66         } else {
67         inclusions = elem.inclusions;
68         }
69     }
70     if (elem.exclusions != null) {
71         if (exclusions != null) {
72         exclusions = (BitSet JavaDoc)exclusions.clone();
73         exclusions.or(elem.exclusions);
74         } else {
75         exclusions = elem.exclusions;
76         }
77     }
78     }
79
80     /**
81      * Return the element that must come next in the
82      * input stream.
83      */

84     public Element JavaDoc first() {
85     return (state != null) ? state.first() : null;
86     }
87
88     /**
89      * Return the ContentModel that must be satisfied by
90      * what comes next in the input stream.
91      */

92     public ContentModel JavaDoc contentModel() {
93     if (state == null) {
94         return null;
95     } else {
96         return state.getModel();
97     }
98     }
99
100     /**
101      * Return true if the element that is contained at
102      * the index specified by the parameter is part of
103      * the exclusions specified in the DTD for the element
104      * currently on the TagStack.
105      */

106     boolean excluded(int elemIndex) {
107     return (exclusions != null) && exclusions.get(elem.getIndex());
108     }
109
110     /**
111      * Update the Vector elemVec with all the elements that
112      * are part of the inclusions listed in DTD for the element
113      * currently on the TagStack.
114      */

115     boolean included(Vector JavaDoc elemVec, DTD JavaDoc dtd) {
116
117     for (int i = 0 ; i < inclusions.size(); i++) {
118         if (inclusions.get(i)) {
119         elemVec.addElement(dtd.getElement(i));
120         System.out.println("Element add thru' inclusions: " + dtd.getElement(i).getName());
121         }
122     }
123     return (!elemVec.isEmpty());
124     }
125
126
127     /**
128      * Advance the state by reducing the given element.
129      * Returns false if the element is not legal and the
130      * state is not advanced.
131      */

132     boolean advance(Element JavaDoc elem) {
133     if ((exclusions != null) && exclusions.get(elem.getIndex())) {
134         return false;
135     }
136     if (state != null) {
137         ContentModelState JavaDoc newState = state.advance(elem);
138         if (newState != null) {
139         state = newState;
140         return true;
141         }
142     } else if (this.elem.getType() == ANY) {
143         return true;
144     }
145     return (inclusions != null) && inclusions.get(elem.getIndex());
146     }
147
148     /**
149      * Return true if the current state can be terminated.
150      */

151     boolean terminate() {
152     return (state == null) || state.terminate();
153     }
154
155     /**
156      * Convert to a string.
157      */

158     public String JavaDoc toString() {
159     return (next == null) ?
160         "<" + tag.getElement().getName() + ">" :
161         next + " <" + tag.getElement().getName() + ">";
162     }
163 }
164
165 class NPrintWriter extends PrintWriter {
166
167     private int numLines = 5;
168     private int numPrinted = 0;
169
170     public NPrintWriter (int numberOfLines) {
171     super(System.out);
172     numLines = numberOfLines;
173     }
174
175     public void println(char[] array) {
176     if (numPrinted >= numLines) {
177         return;
178     }
179
180     char[] partialArray = null;
181
182     for (int i = 0; i < array.length; i++) {
183         if (array[i] == '\n') {
184         numPrinted++;
185         }
186
187         if (numPrinted == numLines) {
188         System.arraycopy(array, 0, partialArray, 0, i);
189         }
190     }
191
192     if (partialArray != null) {
193         super.print(partialArray);
194     }
195
196     if (numPrinted == numLines) {
197         return;
198     }
199
200     super.println(array);
201     numPrinted++;
202     }
203 }
204
205
206
Popular Tags