1. /*
  2. * @(#)TagStack.java 1.7 00/02/02
  3. *
  4. * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.text.html.parser;
  11. import java.util.BitSet;
  12. import java.util.Vector;
  13. import java.io.*;
  14. /**
  15. * A stack of tags. Used while parsing an HTML document.
  16. * It, together with the ContentModelStates, defines the
  17. * complete state of the parser while reading a document.
  18. * When a start tag is encountered an element is pushed onto
  19. * the stack, when an end tag is enountered an element is popped
  20. * of the stack.
  21. *
  22. * @see Parser
  23. * @see DTD
  24. * @see ContentModelState
  25. * @version 1.7, 02/02/00
  26. * @author Arthur van Hoff
  27. */
  28. final
  29. class TagStack implements DTDConstants {
  30. TagElement tag;
  31. Element elem;
  32. ContentModelState state;
  33. TagStack next;
  34. BitSet inclusions;
  35. BitSet exclusions;
  36. boolean net;
  37. boolean pre;
  38. /**
  39. * Construct a stack element.
  40. */
  41. TagStack(TagElement tag, TagStack next) {
  42. this.tag = tag;
  43. this.elem = tag.getElement();
  44. this.next = next;
  45. Element elem = tag.getElement();
  46. if (elem.getContent() != null) {
  47. this.state = new ContentModelState(elem.getContent());
  48. }
  49. if (next != null) {
  50. inclusions = next.inclusions;
  51. exclusions = next.exclusions;
  52. pre = next.pre;
  53. }
  54. if (tag.isPreformatted()) {
  55. pre = true;
  56. }
  57. if (elem.inclusions != null) {
  58. if (inclusions != null) {
  59. inclusions = (BitSet)inclusions.clone();
  60. inclusions.or(elem.inclusions);
  61. } else {
  62. inclusions = elem.inclusions;
  63. }
  64. }
  65. if (elem.exclusions != null) {
  66. if (exclusions != null) {
  67. exclusions = (BitSet)exclusions.clone();
  68. exclusions.or(elem.exclusions);
  69. } else {
  70. exclusions = elem.exclusions;
  71. }
  72. }
  73. }
  74. /**
  75. * Return the element that must come next in the
  76. * input stream.
  77. */
  78. public Element first() {
  79. return (state != null) ? state.first() : null;
  80. }
  81. /**
  82. * Return the ContentModel that must be satisfied by
  83. * what comes next in the input stream.
  84. */
  85. public ContentModel contentModel() {
  86. if (state == null) {
  87. return null;
  88. } else {
  89. return state.getModel();
  90. }
  91. }
  92. /**
  93. * Return true if the element that is contained at
  94. * the index specified by the parameter is part of
  95. * the exclusions specified in the DTD for the element
  96. * currently on the TagStack.
  97. */
  98. boolean excluded(int elemIndex) {
  99. return (exclusions != null) && exclusions.get(elem.getIndex());
  100. }
  101. /**
  102. * Update the Vector elemVec with all the elements that
  103. * are part of the inclusions listed in DTD for the element
  104. * currently on the TagStack.
  105. */
  106. boolean included(Vector elemVec, DTD dtd) {
  107. for (int i = 0 ; i < inclusions.size(); i++) {
  108. if (inclusions.get(i)) {
  109. elemVec.addElement(dtd.getElement(i));
  110. System.out.println("Element add thru' inclusions: " + dtd.getElement(i).getName());
  111. }
  112. }
  113. return (!elemVec.isEmpty());
  114. }
  115. /**
  116. * Advance the state by reducing the given element.
  117. * Returns false if the element is not legal and the
  118. * state is not advanced.
  119. */
  120. boolean advance(Element elem) {
  121. if ((exclusions != null) && exclusions.get(elem.getIndex())) {
  122. return false;
  123. }
  124. if (state != null) {
  125. ContentModelState newState = state.advance(elem);
  126. if (newState != null) {
  127. state = newState;
  128. return true;
  129. }
  130. } else if (this.elem.getType() == ANY) {
  131. return true;
  132. }
  133. return (inclusions != null) && inclusions.get(elem.getIndex());
  134. }
  135. /**
  136. * Return true if the current state can be terminated.
  137. */
  138. boolean terminate() {
  139. return (state == null) || state.terminate();
  140. }
  141. /**
  142. * Convert to a string.
  143. */
  144. public String toString() {
  145. return (next == null) ?
  146. "<" + tag.getElement().getName() + ">" :
  147. next + " <" + tag.getElement().getName() + ">";
  148. }
  149. }
  150. class NPrintWriter extends PrintWriter {
  151. private int numLines = 5;
  152. private int numPrinted = 0;
  153. public NPrintWriter (int numberOfLines) {
  154. super(System.out);
  155. numLines = numberOfLines;
  156. }
  157. public void println(char[] array) {
  158. if (numPrinted >= numLines) {
  159. return;
  160. }
  161. char[] partialArray = null;
  162. for (int i = 0; i < array.length; i++) {
  163. if (array[i] == '\n') {
  164. numPrinted++;
  165. }
  166. if (numPrinted == numLines) {
  167. System.arraycopy(array, 0, partialArray, 0, i);
  168. }
  169. }
  170. if (partialArray != null) {
  171. super.print(partialArray);
  172. }
  173. if (numPrinted == numLines) {
  174. return;
  175. }
  176. super.println(array);
  177. numPrinted++;
  178. }
  179. }