1. /*
  2. * @(#)ContentModelState.java 1.11 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. package javax.swing.text.html.parser;
  8. /**
  9. * A content model state. This is basically a list of pointers to
  10. * the BNF expression representing the model (the ContentModel).
  11. * Each element in a DTD has a content model which describes the
  12. * elements that may occur inside, and the order in which they can
  13. * occur.
  14. * <p>
  15. * Each time a token is reduced a new state is created.
  16. * <p>
  17. * See Annex H on page 556 of the SGML handbook for more information.
  18. *
  19. * @see Parser
  20. * @see DTD
  21. * @see Element
  22. * @see ContentModel
  23. * @author Arthur van Hoff
  24. * @version 1.11 12/19/03
  25. */
  26. class ContentModelState {
  27. ContentModel model;
  28. long value;
  29. ContentModelState next;
  30. /**
  31. * Create a content model state for a content model.
  32. */
  33. public ContentModelState(ContentModel model) {
  34. this(model, null, 0);
  35. }
  36. /**
  37. * Create a content model state for a content model given the
  38. * remaining state that needs to be reduce.
  39. */
  40. ContentModelState(Object content, ContentModelState next) {
  41. this(content, next, 0);
  42. }
  43. /**
  44. * Create a content model state for a content model given the
  45. * remaining state that needs to be reduce.
  46. */
  47. ContentModelState(Object content, ContentModelState next, long value) {
  48. this.model = (ContentModel)content;
  49. this.next = next;
  50. this.value = value;
  51. }
  52. /**
  53. * Return the content model that is relevant to the current state.
  54. */
  55. public ContentModel getModel() {
  56. ContentModel m = model;
  57. for (int i = 0; i < value; i++) {
  58. if (m.next != null) {
  59. m = m.next;
  60. } else {
  61. return null;
  62. }
  63. }
  64. return m;
  65. }
  66. /**
  67. * Check if the state can be terminated. That is there are no more
  68. * tokens required in the input stream.
  69. * @return true if the model can terminate without further input
  70. */
  71. public boolean terminate() {
  72. switch (model.type) {
  73. case '+':
  74. if ((value == 0) && !(model).empty()) {
  75. return false;
  76. }
  77. case '*':
  78. case '?':
  79. return (next == null) || next.terminate();
  80. case '|':
  81. for (ContentModel m = (ContentModel)model.content ; m != null ; m = m.next) {
  82. if (m.empty()) {
  83. return (next == null) || next.terminate();
  84. }
  85. }
  86. return false;
  87. case '&': {
  88. ContentModel m = (ContentModel)model.content;
  89. for (int i = 0 ; m != null ; i++, m = m.next) {
  90. if ((value & (1L << i)) == 0) {
  91. if (!m.empty()) {
  92. return false;
  93. }
  94. }
  95. }
  96. return (next == null) || next.terminate();
  97. }
  98. case ',': {
  99. ContentModel m = (ContentModel)model.content;
  100. for (int i = 0 ; i < value ; i++, m = m.next);
  101. for (; (m != null) && m.empty() ; m = m.next);
  102. if (m != null) {
  103. return false;
  104. }
  105. return (next == null) || next.terminate();
  106. }
  107. default:
  108. return false;
  109. }
  110. }
  111. /**
  112. * Check if the state can be terminated. That is there are no more
  113. * tokens required in the input stream.
  114. * @return the only possible element that can occur next
  115. */
  116. public Element first() {
  117. switch (model.type) {
  118. case '*':
  119. case '?':
  120. case '|':
  121. case '&':
  122. return null;
  123. case '+':
  124. return model.first();
  125. case ',': {
  126. ContentModel m = (ContentModel)model.content;
  127. for (int i = 0 ; i < value ; i++, m = m.next);
  128. return m.first();
  129. }
  130. default:
  131. return model.first();
  132. }
  133. }
  134. /**
  135. * Advance this state to a new state. An exception is thrown if the
  136. * token is illegal at this point in the content model.
  137. * @return next state after reducing a token
  138. */
  139. public ContentModelState advance(Object token) {
  140. switch (model.type) {
  141. case '+':
  142. if (model.first(token)) {
  143. return new ContentModelState(model.content,
  144. new ContentModelState(model, next, value + 1)).advance(token);
  145. }
  146. if (value != 0) {
  147. if (next != null) {
  148. return next.advance(token);
  149. } else {
  150. return null;
  151. }
  152. }
  153. break;
  154. case '*':
  155. if (model.first(token)) {
  156. return new ContentModelState(model.content, this).advance(token);
  157. }
  158. if (next != null) {
  159. return next.advance(token);
  160. } else {
  161. return null;
  162. }
  163. case '?':
  164. if (model.first(token)) {
  165. return new ContentModelState(model.content, next).advance(token);
  166. }
  167. if (next != null) {
  168. return next.advance(token);
  169. } else {
  170. return null;
  171. }
  172. case '|':
  173. for (ContentModel m = (ContentModel)model.content ; m != null ; m = m.next) {
  174. if (m.first(token)) {
  175. return new ContentModelState(m, next).advance(token);
  176. }
  177. }
  178. break;
  179. case ',': {
  180. ContentModel m = (ContentModel)model.content;
  181. for (int i = 0 ; i < value ; i++, m = m.next);
  182. if (m.first(token) || m.empty()) {
  183. if (m.next == null) {
  184. return new ContentModelState(m, next).advance(token);
  185. } else {
  186. return new ContentModelState(m,
  187. new ContentModelState(model, next, value + 1)).advance(token);
  188. }
  189. }
  190. break;
  191. }
  192. case '&': {
  193. ContentModel m = (ContentModel)model.content;
  194. boolean complete = true;
  195. for (int i = 0 ; m != null ; i++, m = m.next) {
  196. if ((value & (1L << i)) == 0) {
  197. if (m.first(token)) {
  198. return new ContentModelState(m,
  199. new ContentModelState(model, next, value | (1L << i))).advance(token);
  200. }
  201. if (!m.empty()) {
  202. complete = false;
  203. }
  204. }
  205. }
  206. if (complete) {
  207. if (next != null) {
  208. return next.advance(token);
  209. } else {
  210. return null;
  211. }
  212. }
  213. break;
  214. }
  215. default:
  216. if (model.content == token) {
  217. if (next == null && (token instanceof Element) &&
  218. ((Element)token).content != null) {
  219. return new ContentModelState(((Element)token).content);
  220. }
  221. return next;
  222. }
  223. // PENDING: Currently we don't correctly deal with optional start
  224. // tags. This can most notably be seen with the 4.01 spec where
  225. // TBODY's start and end tags are optional.
  226. // Uncommenting this and the PENDING in ContentModel will
  227. // correctly skip the omit tags, but the delegate is not notified.
  228. // Some additional API needs to be added to track skipped tags,
  229. // and this can then be added back.
  230. /*
  231. if ((model.content instanceof Element)) {
  232. Element e = (Element)model.content;
  233. if (e.omitStart() && e.content != null) {
  234. return new ContentModelState(e.content, next).advance(
  235. token);
  236. }
  237. }
  238. */
  239. }
  240. // We used to throw this exception at this point. However, it
  241. // was determined that throwing this exception was more expensive
  242. // than returning null, and we could not justify to ourselves why
  243. // it was necessary to throw an exception, rather than simply
  244. // returning null. I'm leaving it in a commented out state so
  245. // that it can be easily restored if the situation ever arises.
  246. //
  247. // throw new IllegalArgumentException("invalid token: " + token);
  248. return null;
  249. }
  250. }