1. /*
  2. * @(#)Parser.java 1.18 01/11/29
  3. *
  4. * Copyright 2002 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. import javax.swing.text.SimpleAttributeSet;
  9. import javax.swing.text.html.HTML;
  10. import javax.swing.text.ChangedCharSetException;
  11. import java.io.*;
  12. import java.util.Hashtable;
  13. import java.util.Properties;
  14. import java.util.Vector;
  15. import java.util.Enumeration;
  16. import java.net.URL;
  17. import sun.misc.MessageUtils;
  18. /**
  19. * A simple DTD-driven HTML parser. The parser reads an
  20. * HTML file from an InputStream and calls various methods
  21. * (which should be overridden in a subclass) when tags and
  22. * data are encountered.
  23. * <p>
  24. * Unfortunately there are many badly implemented HTML parsers
  25. * out there, and as a result there are many badly formatted
  26. * HTML files. This parser attempts to parse most HTML files.
  27. * This means that the implementation sometimes deviates from
  28. * the SGML specification in favor of HTML.
  29. * <p>
  30. * The parser treats \r and \r\n as \n. Newlines after starttags
  31. * and before end tags are ignored just as specified in the SGML/HTML
  32. * specification.
  33. *
  34. * @see DTD
  35. * @see Tag
  36. * @see SimpleAttributeSet
  37. * @version 1.18, 11/29/01
  38. * @author Arthur van Hoff
  39. * @author Sunita Mani
  40. */
  41. public
  42. class Parser implements DTDConstants {
  43. private char text[] = new char[1024];
  44. private int textpos = 0;
  45. private TagElement last;
  46. private boolean space;
  47. private char str[] = new char[128];
  48. private int strpos = 0;
  49. protected DTD dtd = null;
  50. private int ch;
  51. private int ln;
  52. private Reader in;
  53. private Element recent;
  54. private TagStack stack;
  55. private boolean skipTag = false;
  56. private TagElement lastFormSent = null;
  57. private SimpleAttributeSet attributes = new SimpleAttributeSet();
  58. // State for <html>, <head> and <body>. Since people like to slap
  59. // together HTML documents without thinking, occasionally they
  60. // have multiple instances of these tags. These booleans track
  61. // the first sightings of these tags so they can be safely ignored
  62. // by the parser if repeated.
  63. private boolean seenHtml = false;
  64. private boolean seenHead = false;
  65. private boolean seenBody = false;
  66. /**
  67. * This flag determines whether or not the Parser will be strict
  68. * in enforcing SGML compatibility. If false, it will be lenient
  69. * with certain common classes of erroneous HTML constructs.
  70. * Strict or not, in either case an error will be recorded.
  71. *
  72. */
  73. protected boolean strict = false;
  74. /** Number of \r\n's encountered. */
  75. private int crlfCount;
  76. /** Number of \r's encountered. A \r\n will not increment this. */
  77. private int crCount;
  78. /** Number of \n's encountered. A \r\n will not increment this. */
  79. private int lfCount;
  80. public Parser(DTD dtd) {
  81. this.dtd = dtd;
  82. }
  83. /**
  84. * @return the line number of the line currently being parsed
  85. */
  86. protected int getCurrentLine() {
  87. return ln;
  88. }
  89. /**
  90. * Makes a TagElement.
  91. */
  92. protected TagElement makeTag(Element elem, boolean fictional) {
  93. return new TagElement(elem, fictional);
  94. }
  95. protected TagElement makeTag(Element elem) {
  96. return makeTag(elem, false);
  97. }
  98. protected SimpleAttributeSet getAttributes() {
  99. return attributes;
  100. }
  101. protected void flushAttributes() {
  102. attributes.removeAttributes(attributes);
  103. }
  104. /**
  105. * Called when PCDATA is encountered.
  106. */
  107. protected void handleText(char text[]) {
  108. }
  109. /**
  110. * Called when an HTML title tag is encountered.
  111. */
  112. protected void handleTitle(char text[]) {
  113. // default behavior is to call handleText. Subclasses
  114. // can override if necessary.
  115. handleText(text);
  116. }
  117. /**
  118. * Called when an HTML comment is encountered.
  119. */
  120. protected void handleComment(char text[]) {
  121. }
  122. protected void handleEOFInComment() {
  123. // We've reached EOF. Our recovery strategy is to
  124. // see if we have more than one line in the comment;
  125. // if so, we pretend that the comment was an unterminated
  126. // single line comment, and reparse the lines after the
  127. // first line as normal HTML content.
  128. int commentEndPos = strIndexOf('\n');
  129. if (commentEndPos >= 0) {
  130. handleComment(getChars(0, commentEndPos));
  131. try {
  132. in.close();
  133. in = new CharArrayReader(getChars(commentEndPos + 1));
  134. ch = '>';
  135. } catch (IOException e) {
  136. error("ioexception");
  137. }
  138. resetStrBuffer();
  139. } else {
  140. // no newline, so signal an error
  141. error("eof.comment");
  142. }
  143. }
  144. /**
  145. * Called when an empty tag is encountered.
  146. */
  147. protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException {
  148. }
  149. /**
  150. * Called when a start tag is encountered.
  151. */
  152. protected void handleStartTag(TagElement tag) {
  153. }
  154. /**
  155. * Called when an end tag is encountered.
  156. */
  157. protected void handleEndTag(TagElement tag) {
  158. }
  159. /**
  160. * An error has occurred.
  161. */
  162. protected void handleError(int ln, String msg) {
  163. Thread.dumpStack();
  164. System.out.println("**** " + stack);
  165. System.out.println("line " + ln + ": error: " + msg);
  166. System.out.println();
  167. }
  168. /**
  169. * Output text.
  170. */
  171. void handleText(TagElement tag) {
  172. if (tag.breaksFlow()) {
  173. space = false;
  174. }
  175. if (textpos == 0) {
  176. if ((!space) || (stack == null) || last.breaksFlow() ||
  177. !stack.advance(dtd.pcdata)) {
  178. last = tag;
  179. space = false;
  180. return;
  181. }
  182. }
  183. if (space) {
  184. // enlarge buffer if needed
  185. if (textpos + 1 > text.length) {
  186. char newtext[] = new char[text.length + 200];
  187. System.arraycopy(text, 0, newtext, 0, text.length);
  188. text = newtext;
  189. }
  190. // output pending space
  191. text[textpos++] = ' ';
  192. space = false;
  193. }
  194. char newtext[] = new char[textpos];
  195. System.arraycopy(text, 0, newtext, 0, textpos);
  196. // Handles cases of bad html where the title tag
  197. // was getting lost when we did error recovery.
  198. if (tag.getElement().getName().equals("title")) {
  199. handleTitle(newtext);
  200. } else {
  201. handleText(newtext);
  202. }
  203. textpos = 0;
  204. last = tag;
  205. space = false;
  206. }
  207. /**
  208. * Invoke the error handler.
  209. */
  210. protected void error(String err, String arg1, String arg2,
  211. String arg3) {
  212. // big hack, but this should never get used...
  213. handleError (ln, err + arg1 + arg2 + arg3);
  214. }
  215. protected void error(String err, String arg1, String arg2) {
  216. error(err, arg1, arg2, "?");
  217. }
  218. protected void error(String err, String arg1) {
  219. error(err, arg1, "?", "?");
  220. }
  221. protected void error(String err) {
  222. error(err, "?", "?", "?");
  223. }
  224. /**
  225. * Handle a start tag. The new tag is pushed
  226. * onto the tag stack. The attribute list is
  227. * checked for required attributes.
  228. */
  229. protected void startTag(TagElement tag) throws ChangedCharSetException {
  230. Element elem = tag.getElement();
  231. // If the tag is an empty tag and texpos != 0
  232. // this implies that there is text before the
  233. // start tag that needs to be processed before
  234. // handling the tag.
  235. //
  236. if (!elem.isEmpty() || textpos != 0) {
  237. handleText(tag);
  238. } else {
  239. // this variable gets updated in handleText().
  240. // Since in this case we do not call handleText()
  241. // we need to update it here.
  242. //
  243. last = tag;
  244. }
  245. // check required attributes
  246. for (AttributeList a = elem.atts ; a != null ; a = a.next) {
  247. if ((a.modifier == REQUIRED) && ((attributes.isEmpty()) || (!attributes.isDefined(a.name)))) {
  248. error("req.att ", a.getName(), elem.getName());
  249. }
  250. }
  251. if (elem.isEmpty()) {
  252. handleEmptyTag(tag);
  253. } else if (elem.getName().equals("form")) {
  254. handleStartTag(tag);
  255. } else {
  256. recent = elem;
  257. stack = new TagStack(tag, stack);
  258. handleStartTag(tag);
  259. }
  260. }
  261. /**
  262. * Handle an end tag. The end tag is popped
  263. * from the tag stack.
  264. */
  265. protected void endTag(boolean omitted) {
  266. handleText(stack.tag);
  267. if (omitted && !stack.elem.omitEnd()) {
  268. error("end.missing", stack.elem.getName());
  269. } else if (!stack.terminate()) {
  270. error("end.unexpected", stack.elem.getName());
  271. }
  272. // handle the tag
  273. handleEndTag(stack.tag);
  274. stack = stack.next;
  275. recent = (stack != null) ? stack.elem : null;
  276. }
  277. boolean ignoreElement(Element elem) {
  278. String stackElement = stack.elem.getName();
  279. String elemName = elem.getName();
  280. /* We ignore all elements that are not valid in the context of
  281. a table except <td>, <th> (these we handle in
  282. legalElementContext()) and #pcdata. We also ignore the
  283. <font> tag in the context of <ul> and <ol> We additonally
  284. ignore the <meta> and the <style> tag if the body tag has
  285. been seen. **/
  286. if ((elemName.equals("html") && seenHtml) ||
  287. (elemName.equals("head") && seenHead) ||
  288. (elemName.equals("body") && seenBody)) {
  289. return true;
  290. }
  291. if (elemName.equals("dt") || elemName.equals("dd")) {
  292. TagStack s = stack;
  293. while (s != null && !s.elem.getName().equals("dl")) {
  294. s = s.next;
  295. }
  296. if (s == null) {
  297. return true;
  298. }
  299. }
  300. if (((stackElement.equals("table")) &&
  301. (!elemName.equals("#pcdata")) && (!elemName.equals("input"))) ||
  302. ((elemName.equals("font")) &&
  303. (stackElement.equals("ul") || stackElement.equals("ol"))) ||
  304. (elemName.equals("meta") && stack != null) ||
  305. elemName.equals("style") ||
  306. (stackElement.equals("table") && elemName.equals("a"))) {
  307. return true;
  308. }
  309. return false;
  310. }
  311. /**
  312. * Marks the first time a tag has been seen in a document
  313. */
  314. protected void markFirstTime(Element elem) {
  315. String elemName = elem.getName();
  316. if (elemName.equals("html")) {
  317. seenHtml = true;
  318. } else if (elemName.equals("head")) {
  319. seenHead = true;
  320. } else if (elemName.equals("body")) {
  321. seenBody = true;
  322. }
  323. }
  324. /**
  325. * Create a legal content for an element.
  326. */
  327. boolean legalElementContext(Element elem) throws ChangedCharSetException {
  328. // System.out.println("-- legalContext -- " + elem);
  329. // Deal with the empty stack
  330. if (stack == null) {
  331. // System.out.println("-- stack is empty");
  332. if (elem != dtd.html) {
  333. // System.out.println("-- pushing html");
  334. startTag(makeTag(dtd.html, true));
  335. return legalElementContext(elem);
  336. }
  337. return true;
  338. }
  339. // Is it allowed in the current context
  340. if (stack.advance(elem)) {
  341. // System.out.println("-- legal context");
  342. markFirstTime(elem);
  343. return true;
  344. }
  345. boolean insertTag = false;
  346. // The use of all error recovery strategies are contingent
  347. // on the value of the strict property.
  348. //
  349. // These are commonly occuring errors. if insertTag is true,
  350. // then we want to adopt an error recovery strategy that
  351. // involves attempting to insert an additional tag to
  352. // legalize the context. The two errors addressed here
  353. // are:
  354. // 1) when a <td> or <th> is seen soon after a <table> tag.
  355. // In this case we insert a <tr>.
  356. // 2) when any other tag apart from a <tr> is seen
  357. // in the context of a <tr>. In this case we would
  358. // like to add a <td>. If a <tr> is seen within a
  359. // <tr> context, then we will close out the current
  360. // <tr>.
  361. //
  362. // This insertion strategy is handled later in the method.
  363. // The reason for checking this now, is that in other cases
  364. // we would like to apply other error recovery strategies for example
  365. // ignoring tags.
  366. //
  367. // In certain cases it is better to ignore a tag than try to
  368. // fix the situation. So the first test is to see if this
  369. // is what we need to do.
  370. //
  371. String stackElemName = stack.elem.getName();
  372. String elemName = elem.getName();
  373. if (!strict &&
  374. ((stackElemName.equals("table") && elemName.equals("td")) ||
  375. (stackElemName.equals("table") && elemName.equals("th")) ||
  376. (stackElemName.equals("tr") && !elemName.equals("tr")))){
  377. insertTag = true;
  378. }
  379. if (!strict && !insertTag && (stack.elem.getName() != elem.getName() ||
  380. elem.getName().equals("body"))) {
  381. if (skipTag = ignoreElement(elem)) {
  382. error("tag.ignore", elem.getName());
  383. return skipTag;
  384. }
  385. }
  386. // Check for anything after the start of the table besides tr, td, th
  387. // or caption, and if those aren't there, insert the <tr> and call
  388. // legalElementContext again.
  389. if (!strict && stackElemName.equals("table") &&
  390. !elemName.equals("tr") && !elemName.equals("td") &&
  391. !elemName.equals("th") && !elemName.equals("caption")) {
  392. Element e = dtd.getElement("tr");
  393. TagElement t = makeTag(e, true);
  394. legalTagContext(t);
  395. startTag(t);
  396. error("start.missing", elem.getName());
  397. return legalElementContext(elem);
  398. }
  399. // They try to find a legal context by checking if the current
  400. // tag is valid in an enclosing context. If so
  401. // close out the tags by outputing end tags and then
  402. // insert the curent tag. If the tags that are
  403. // being closed out do not have an optional end tag
  404. // specification in the DTD then an html error is
  405. // reported.
  406. //
  407. if (!insertTag && stack.terminate() && (!strict || stack.elem.omitEnd())) {
  408. for (TagStack s = stack.next ; s != null ; s = s.next) {
  409. if (s.advance(elem)) {
  410. while (stack != s) {
  411. endTag(true);
  412. }
  413. return true;
  414. }
  415. if (!s.terminate() || (strict && !s.elem.omitEnd())) {
  416. break;
  417. }
  418. }
  419. }
  420. // Check if we know what tag is expected next.
  421. // If so insert the tag. Report an error if the
  422. // tag does not have its start tag spec in the DTD as optional.
  423. //
  424. Element next = stack.first();
  425. if (next != null && (!strict || next.omitStart()) &&
  426. !(next==dtd.head && elem==dtd.pcdata) ) {
  427. // System.out.println("-- omitting start tag: " + next);
  428. TagElement t = makeTag(next, true);
  429. legalTagContext(t);
  430. startTag(t);
  431. if (!next.omitStart()) {
  432. error("start.missing", elem.getName());
  433. }
  434. return legalElementContext(elem);
  435. }
  436. // Traverse the list of expected elements and determine if adding
  437. // any of these elements would make for a legal context.
  438. //
  439. if (!strict) {
  440. ContentModel content = stack.contentModel();
  441. Vector elemVec = new Vector();
  442. if (content != null) {
  443. content.getElements(elemVec);
  444. for (Enumeration v = elemVec.elements(); v.hasMoreElements();) {
  445. Element e = (Element)v.nextElement();
  446. // Ensure that this element has not been included as
  447. // part of the exclusions in the DTD.
  448. //
  449. if (stack.excluded(e.getIndex())) {
  450. continue;
  451. }
  452. boolean reqAtts = false;
  453. for (AttributeList a = e.getAttributes(); a != null ; a = a.next) {
  454. if (a.modifier == REQUIRED) {
  455. reqAtts = true;
  456. break;
  457. }
  458. }
  459. // Ensure that no tag that has required attributes
  460. // gets inserted.
  461. //
  462. if (reqAtts) {
  463. continue;
  464. }
  465. ContentModel m = e.getContent();
  466. if (m != null && m.first(elem)) {
  467. // System.out.println("-- adding a legal tag: " + e);
  468. TagElement t = makeTag(e, true);
  469. legalTagContext(t);
  470. startTag(t);
  471. error("start.missing", e.getName());
  472. return legalElementContext(elem);
  473. }
  474. }
  475. }
  476. }
  477. // Check if the stack can be terminated. If so add the appropriate
  478. // end tag. Report an error if the tag being ended does not have its
  479. // end tag spec in the DTD as optional.
  480. //
  481. if (stack.terminate() && (stack.elem != dtd.body) && (!strict || stack.elem.omitEnd())) {
  482. // System.out.println("-- omitting end tag: " + stack.elem);
  483. if (!stack.elem.omitEnd()) {
  484. error("end.missing", elem.getName());
  485. }
  486. endTag(true);
  487. return legalElementContext(elem);
  488. }
  489. // At this point we know that something is screwed up.
  490. return false;
  491. }
  492. /**
  493. * Create a legal context for a tag.
  494. */
  495. void legalTagContext(TagElement tag) throws ChangedCharSetException {
  496. if (legalElementContext(tag.getElement())) {
  497. markFirstTime(tag.getElement());
  498. return;
  499. }
  500. // Avoid putting a block tag in a flow tag.
  501. if (tag.breaksFlow() && (stack != null) && !stack.tag.breaksFlow()) {
  502. endTag(true);
  503. legalTagContext(tag);
  504. return;
  505. }
  506. // Avoid putting something wierd in the head of the document.
  507. for (TagStack s = stack ; s != null ; s = s.next) {
  508. if (s.tag.getElement() == dtd.head) {
  509. while (stack != s) {
  510. endTag(true);
  511. }
  512. endTag(true);
  513. legalTagContext(tag);
  514. return;
  515. }
  516. }
  517. // Everything failed
  518. error("tag.unexpected", tag.getElement().getName());
  519. }
  520. /**
  521. * Error context. Something went wrong, make sure we are in
  522. * the document's body context
  523. */
  524. void errorContext() throws ChangedCharSetException {
  525. for (; (stack != null) && (stack.tag.getElement() != dtd.body) ; stack = stack.next) {
  526. handleEndTag(stack.tag);
  527. }
  528. if (stack == null) {
  529. legalElementContext(dtd.body);
  530. startTag(makeTag(dtd.body, true));
  531. }
  532. }
  533. /**
  534. * Add a char to the string buffer.
  535. */
  536. void addString(int c) {
  537. if (strpos == str.length) {
  538. char newstr[] = new char[str.length + 128];
  539. System.arraycopy(str, 0, newstr, 0, str.length);
  540. str = newstr;
  541. }
  542. str[strpos++] = (char)c;
  543. }
  544. /**
  545. * Get the string that's been accumulated.
  546. */
  547. String getString(int pos) {
  548. char newStr[] = new char[strpos - pos];
  549. System.arraycopy(str, pos, newStr, 0, strpos - pos);
  550. strpos = pos;
  551. return new String(newStr);
  552. }
  553. char[] getChars(int pos) {
  554. char newStr[] = new char[strpos - pos];
  555. System.arraycopy(str, pos, newStr, 0, strpos - pos);
  556. strpos = pos;
  557. return newStr;
  558. }
  559. char[] getChars(int pos, int endPos) {
  560. char newStr[] = new char[endPos - pos];
  561. System.arraycopy(str, pos, newStr, 0, endPos - pos);
  562. // REMIND: it's not clear whether this version should set strpos or not
  563. // strpos = pos;
  564. return newStr;
  565. }
  566. void resetStrBuffer() {
  567. strpos = 0;
  568. }
  569. int strIndexOf(char target) {
  570. for (int i = 0; i < strpos; i++) {
  571. if (str[i] == target) {
  572. return i;
  573. }
  574. }
  575. return -1;
  576. }
  577. /**
  578. * Skip space.
  579. * [5] 297:5
  580. */
  581. void skipSpace() throws IOException {
  582. while (true) {
  583. switch (ch) {
  584. case '\n':
  585. ln++;
  586. ch = readCh();
  587. lfCount++;
  588. break;
  589. case '\r':
  590. ln++;
  591. if ((ch = readCh()) == '\n') {
  592. ch = readCh();
  593. crlfCount++;
  594. }
  595. else {
  596. crCount++;
  597. }
  598. break;
  599. case ' ':
  600. case '\t':
  601. ch = readCh();
  602. break;
  603. default:
  604. return;
  605. }
  606. }
  607. }
  608. /**
  609. * Parse identifier. Uppercase characters are folded
  610. * to lowercase when lower is true. Returns falsed if
  611. * no identifier is found. [55] 346:17
  612. */
  613. boolean parseIdentifier(boolean lower) throws IOException {
  614. switch (ch) {
  615. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  616. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  617. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  618. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  619. case 'Y': case 'Z':
  620. if (lower) {
  621. ch = 'a' + (ch - 'A');
  622. }
  623. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  624. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  625. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  626. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  627. case 'y': case 'z':
  628. break;
  629. default:
  630. return false;
  631. }
  632. while (true) {
  633. addString(ch);
  634. switch (ch = readCh()) {
  635. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  636. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  637. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  638. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  639. case 'Y': case 'Z':
  640. if (lower) {
  641. ch = 'a' + (ch - 'A');
  642. }
  643. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  644. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  645. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  646. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  647. case 'y': case 'z':
  648. case '0': case '1': case '2': case '3': case '4':
  649. case '5': case '6': case '7': case '8': case '9':
  650. case '.': case '-':
  651. case '_': // not officially allowed
  652. break;
  653. default:
  654. return true;
  655. }
  656. }
  657. }
  658. /**
  659. * Parse an entity reference. [59] 350:17
  660. */
  661. private char[] parseEntityReference() throws IOException {
  662. int pos = strpos;
  663. if ((ch = readCh()) == '#') {
  664. int n = 0;
  665. ch = readCh();
  666. if ((ch >= '0') && (ch <= '9')) {
  667. while ((ch >= '0') && (ch <= '9')) {
  668. n = (n * 10) + ch - '0';
  669. ch = readCh();
  670. }
  671. switch (ch) {
  672. case '\n':
  673. ln++;
  674. ch = readCh();
  675. lfCount++;
  676. break;
  677. case '\r':
  678. ln++;
  679. if ((ch = readCh()) == '\n') {
  680. ch = readCh();
  681. crlfCount++;
  682. }
  683. else {
  684. crCount++;
  685. }
  686. break;
  687. case ';':
  688. ch = readCh();
  689. break;
  690. }
  691. char data[] = {(char)n};
  692. return data;
  693. }
  694. addString('#');
  695. if (!parseIdentifier(false)) {
  696. error("ident.expected");
  697. strpos = pos;
  698. char data[] = {'&', '#'};
  699. return data;
  700. }
  701. } else if (!parseIdentifier(false)) {
  702. char data[] = {'&'};
  703. return data;
  704. }
  705. switch (ch) {
  706. case '\n':
  707. ln++;
  708. ch = readCh();
  709. lfCount++;
  710. break;
  711. case '\r':
  712. ln++;
  713. if ((ch = readCh()) == '\n') {
  714. ch = readCh();
  715. crlfCount++;
  716. }
  717. else {
  718. crCount++;
  719. }
  720. break;
  721. case ';':
  722. ch = readCh();
  723. break;
  724. }
  725. String nm = getString(pos);
  726. Entity ent = dtd.getEntity(nm);
  727. // entities are case sensitive - however if strict
  728. // is false then we will try to make a match by
  729. // converting the string to all lowercase.
  730. //
  731. if (!strict && (ent == null)) {
  732. ent = dtd.getEntity(nm.toLowerCase());
  733. }
  734. if ((ent == null) || !ent.isGeneral()) {
  735. if (nm.length() == 0) {
  736. error("invalid.entref", nm);
  737. return new char[0];
  738. }
  739. /* given that there is not a match restore the entity reference */
  740. String str = "&" + nm;
  741. char b[] = new char[str.length()];
  742. str.getChars(0, b.length, b, 0);
  743. return b;
  744. }
  745. return ent.getData();
  746. }
  747. /**
  748. * Parse a comment. [92] 391:7
  749. */
  750. void parseComment() throws IOException {
  751. while (true) {
  752. int c = ch;
  753. switch (c) {
  754. case '-':
  755. /** Presuming that the start string of a comment "<!--" has
  756. already been parsed, the '-' character is valid only as
  757. part of a comment termination and further more it must
  758. be present in even numbers. Hence if strict is true, we
  759. presume the comment has been terminated and return.
  760. However if strict is false, then there is no even number
  761. requirement and this character can appear anywhere in the
  762. comment. The parser reads on until it sees the following
  763. pattern: "-->" or "--!>".
  764. **/
  765. if (!strict && (strpos != 0) && (str[strpos - 1] == '-')) {
  766. if ((ch = readCh()) == '>') {
  767. return;
  768. }
  769. if (ch == '!') {
  770. if ((ch = readCh()) == '>') {
  771. return;
  772. } else {
  773. /* to account for extra read()'s that happened */
  774. addString('-');
  775. addString('!');
  776. continue;
  777. }
  778. }
  779. break;
  780. }
  781. if ((ch = readCh()) == '-') {
  782. ch = readCh();
  783. if (strict || ch == '>') {
  784. return;
  785. }
  786. if (ch == '!') {
  787. if ((ch = readCh()) == '>') {
  788. return;
  789. } else {
  790. /* to account for extra read()'s that happened */
  791. addString('-');
  792. addString('!');
  793. continue;
  794. }
  795. }
  796. /* to account for the extra read() */
  797. addString('-');
  798. }
  799. break;
  800. case -1:
  801. handleEOFInComment();
  802. return;
  803. case '\n':
  804. ln++;
  805. ch = readCh();
  806. lfCount++;
  807. break;
  808. case '>':
  809. ch = readCh();
  810. break;
  811. case '\r':
  812. ln++;
  813. if ((ch = readCh()) == '\n') {
  814. ch = readCh();
  815. crlfCount++;
  816. }
  817. else {
  818. crCount++;
  819. }
  820. c = '\n';
  821. break;
  822. default:
  823. ch = readCh();
  824. break;
  825. }
  826. addString(c);
  827. }
  828. }
  829. /**
  830. * Parse literal content. [46] 343:1 and [47] 344:1
  831. */
  832. void parseLiteral(boolean replace) throws IOException {
  833. while (true) {
  834. int c = ch;
  835. switch (c) {
  836. case -1:
  837. error("eof.literal", stack.elem.getName());
  838. endTag(true);
  839. return;
  840. case '>':
  841. ch = readCh();
  842. int i = textpos - (stack.elem.name.length() + 2), j = 0;
  843. // match end tag
  844. if ((i >= 0) && (text[i++] == '<') && (text[i] == '/')) {
  845. while ((++i < textpos) &&
  846. (Character.toLowerCase(text[i]) == stack.elem.name.charAt(j++)));
  847. if (i == textpos) {
  848. textpos -= (stack.elem.name.length() + 2);
  849. if ((textpos > 0) && (text[textpos-1] == '\n')) {
  850. textpos--;
  851. }
  852. endTag(false);
  853. return;
  854. }
  855. }
  856. break;
  857. case '&':
  858. char data[] = parseEntityReference();
  859. if (textpos + data.length > text.length) {
  860. char newtext[] = new char[Math.max(textpos + data.length + 128, text.length * 2)];
  861. System.arraycopy(text, 0, newtext, 0, text.length);
  862. text = newtext;
  863. }
  864. System.arraycopy(data, 0, text, textpos, data.length);
  865. textpos += data.length;
  866. continue;
  867. case '\n':
  868. ln++;
  869. ch = readCh();
  870. lfCount++;
  871. break;
  872. case '\r':
  873. ln++;
  874. if ((ch = readCh()) == '\n') {
  875. ch = readCh();
  876. crlfCount++;
  877. }
  878. else {
  879. crCount++;
  880. }
  881. c = '\n';
  882. break;
  883. default:
  884. ch = readCh();
  885. break;
  886. }
  887. // output character
  888. if (textpos == text.length) {
  889. char newtext[] = new char[text.length + 128];
  890. System.arraycopy(text, 0, newtext, 0, text.length);
  891. text = newtext;
  892. }
  893. text[textpos++] = (char)c;
  894. }
  895. }
  896. /**
  897. * Parse attribute value. [33] 331:1
  898. */
  899. String parseAttributeValue(boolean lower) throws IOException {
  900. int delim = -1;
  901. // Check for a delimiter
  902. switch(ch) {
  903. case '\'':
  904. case '"':
  905. delim = ch;
  906. ch = readCh();
  907. break;
  908. }
  909. // Parse the rest of the value
  910. while (true) {
  911. int c = ch;
  912. switch (c) {
  913. case '\n':
  914. ln++;
  915. ch = readCh();
  916. lfCount++;
  917. if (delim < 0) {
  918. return getString(0);
  919. }
  920. break;
  921. case '\r':
  922. ln++;
  923. if ((ch = readCh()) == '\n') {
  924. ch = readCh();
  925. crlfCount++;
  926. }
  927. else {
  928. crCount++;
  929. }
  930. if (delim < 0) {
  931. return getString(0);
  932. }
  933. break;
  934. case '\t':
  935. if (delim < 0)
  936. c = ' ';
  937. case ' ':
  938. ch = readCh();
  939. if (delim < 0) {
  940. return getString(0);
  941. }
  942. break;
  943. case '>':
  944. case '<':
  945. if (delim < 0) {
  946. return getString(0);
  947. }
  948. ch = readCh();
  949. break;
  950. case '\'':
  951. case '"':
  952. ch = readCh();
  953. if (c == delim) {
  954. return getString(0);
  955. } else if (delim == -1) {
  956. error("attvalerr");
  957. if (strict || ch == ' ') {
  958. return getString(0);
  959. } else {
  960. continue;
  961. }
  962. }
  963. break;
  964. case '=':
  965. if (delim < 0) {
  966. /* In SGML a construct like <img src=/cgi-bin/foo?x=1>
  967. is considered invalid since an = sign can only be contained
  968. in an attributes value if the string is quoted.
  969. */
  970. error("attvalerr");
  971. /* If strict is true then we return with the string we have thus far.
  972. Otherwise we accept the = sign as part of the attribute's value and
  973. process the rest of the img tag. */
  974. if (strict) {
  975. return getString(0);
  976. }
  977. }
  978. ch = readCh();
  979. break;
  980. case '&':
  981. if (strict && delim < 0) {
  982. ch = readCh();
  983. break;
  984. }
  985. char data[] = parseEntityReference();
  986. for (int i = 0 ; i < data.length ; i++) {
  987. c = data[i];
  988. addString((lower && (c >= 'A') && (c <= 'Z')) ? 'a' + c - 'A' : c);
  989. }
  990. continue;
  991. case -1:
  992. return getString(0);
  993. default:
  994. if (lower && (c >= 'A') && (c <= 'Z')) {
  995. c = 'a' + c - 'A';
  996. }
  997. ch = readCh();
  998. break;
  999. }
  1000. addString(c);
  1001. }
  1002. }
  1003. /**
  1004. * Parse attribute specification List. [31] 327:17
  1005. */
  1006. void parseAttributeSpecificationList(Element elem) throws IOException {
  1007. while (true) {
  1008. skipSpace();
  1009. switch (ch) {
  1010. case '/':
  1011. case '>':
  1012. case '<':
  1013. case -1:
  1014. return;
  1015. case '-':
  1016. if ((ch = readCh()) == '-') {
  1017. ch = readCh();
  1018. parseComment();
  1019. strpos = 0;
  1020. } else {
  1021. error("invalid.tagchar", "-", elem.getName());
  1022. ch = readCh();
  1023. }
  1024. continue;
  1025. }
  1026. AttributeList att = null;
  1027. String attname = null;
  1028. String attvalue = null;
  1029. if (parseIdentifier(true)) {
  1030. attname = getString(0);
  1031. skipSpace();
  1032. if (ch == '=') {
  1033. ch = readCh();
  1034. skipSpace();
  1035. att = elem.getAttribute(attname);
  1036. // Bug ID 4102750
  1037. // Load the NAME of an Attribute Case Sensitive
  1038. // The case of the NAME must be intact
  1039. // MG 021898
  1040. attvalue = parseAttributeValue((att != null) && (att.type != CDATA) && (att.type != NOTATION) && (att.type != NAME));
  1041. // attvalue = parseAttributeValue((att != null) && (att.type != CDATA) && (att.type != NOTATION));
  1042. } else {
  1043. attvalue = attname;
  1044. att = elem.getAttributeByValue(attvalue);
  1045. if (att == null) {
  1046. att = elem.getAttribute(attname);
  1047. if (att != null) {
  1048. attvalue = att.getValue();
  1049. }
  1050. }
  1051. }
  1052. } else if (!strict && ch == ',') { // allows for comma separated attribute-value pairs
  1053. ch = readCh();
  1054. continue;
  1055. } else if (!strict && ch == '"') { // allows for quoted attributes
  1056. ch = readCh();
  1057. skipSpace();
  1058. if (parseIdentifier(true)) {
  1059. attname = getString(0);
  1060. if (ch == '"') {
  1061. ch = readCh();
  1062. }
  1063. skipSpace();
  1064. if (ch == '=') {
  1065. ch = readCh();
  1066. skipSpace();
  1067. att = elem.getAttribute(attname);
  1068. attvalue = parseAttributeValue((att != null) &&
  1069. (att.type != CDATA) &&
  1070. (att.type != NOTATION));
  1071. } else {
  1072. attvalue = attname;
  1073. att = elem.getAttributeByValue(attvalue);
  1074. if (att == null) {
  1075. att = elem.getAttribute(attname);
  1076. if (att != null) {
  1077. attvalue = att.getValue();
  1078. }
  1079. }
  1080. }
  1081. } else {
  1082. char str[] = {(char)ch};
  1083. error("invalid.tagchar", new String(str), elem.getName());
  1084. ch = readCh();
  1085. continue;
  1086. }
  1087. } else if (!strict && (attributes.isEmpty()) && (ch == '=')) {
  1088. ch = readCh();
  1089. skipSpace();
  1090. attname = elem.getName();
  1091. att = elem.getAttribute(attname);
  1092. attvalue = parseAttributeValue((att != null) &&
  1093. (att.type != CDATA) &&
  1094. (att.type != NOTATION));
  1095. } else if (!strict && (ch == '=')) {
  1096. ch = readCh();
  1097. skipSpace();
  1098. attvalue = parseAttributeValue(true);
  1099. error("attvalerr");
  1100. return;
  1101. } else {
  1102. char str[] = {(char)ch};
  1103. error("invalid.tagchar", new String(str), elem.getName());
  1104. if (!strict) {
  1105. ch = readCh();
  1106. continue;
  1107. } else {
  1108. return;
  1109. }
  1110. }
  1111. if (att != null) {
  1112. attname = att.getName();
  1113. } else {
  1114. error("invalid.tagatt", attname, elem.getName());
  1115. }
  1116. // Check out the value
  1117. if (attributes.isDefined(attname)) {
  1118. error("multi.tagatt", attname, elem.getName());
  1119. }
  1120. if (attvalue == null) {
  1121. attvalue = ((att != null) && (att.value != null)) ? att.value :
  1122. HTML.NULL_ATTRIBUTE_VALUE;
  1123. } else if ((att != null) && (att.values != null) && !att.values.contains(attvalue)) {
  1124. error("invalid.tagattval", attname, elem.getName());
  1125. }
  1126. HTML.Attribute attkey = HTML.getAttributeKey(attname);
  1127. if (attkey == null) {
  1128. attributes.addAttribute(attname, attvalue);
  1129. } else {
  1130. attributes.addAttribute(attkey, attvalue);
  1131. }
  1132. }
  1133. }
  1134. /**
  1135. * Parses th Document Declaration Type markup declaration.
  1136. * Currently ignores it.
  1137. */
  1138. public String parseDTDMarkup() throws IOException {
  1139. StringBuffer strBuff = new StringBuffer();
  1140. ch = readCh();
  1141. while(true) {
  1142. switch (ch) {
  1143. case '>':
  1144. ch = readCh();
  1145. return strBuff.toString();
  1146. case -1:
  1147. error("invalid.markup");
  1148. return strBuff.toString();
  1149. case '\n':
  1150. ln++;
  1151. ch = readCh();
  1152. lfCount++;
  1153. break;
  1154. case '"':
  1155. ch = readCh();
  1156. break;
  1157. case '\r':
  1158. ln++;
  1159. if ((ch = readCh()) == '\n') {
  1160. ch = readCh();
  1161. crlfCount++;
  1162. }
  1163. else {
  1164. crCount++;
  1165. }
  1166. break;
  1167. default:
  1168. strBuff.append((char)(ch & 0xFF));
  1169. ch = readCh();
  1170. break;
  1171. }
  1172. }
  1173. }
  1174. /**
  1175. * Parse markup declarations.
  1176. * Currently only handles the Document Type Declaration markup.
  1177. * Returns true if it is a markup declaration false otherwise.
  1178. */
  1179. protected boolean parseMarkupDeclarations(StringBuffer strBuff) throws IOException {
  1180. /* Currently handles only the DOCTYPE */
  1181. if ((strBuff.length() == "DOCTYPE".length()) &&
  1182. (strBuff.toString().toUpperCase().equals("DOCTYPE"))) {
  1183. parseDTDMarkup();
  1184. return true;
  1185. }
  1186. return false;
  1187. }
  1188. /**
  1189. * Parse an invalid tag.
  1190. */
  1191. void parseInvalidTag() throws IOException {
  1192. // ignore all data upto the close bracket '>'
  1193. while (true) {
  1194. skipSpace();
  1195. switch (ch) {
  1196. case '>':
  1197. case -1:
  1198. ch = readCh();
  1199. return;
  1200. case '<':
  1201. return;
  1202. default:
  1203. ch = readCh();
  1204. }
  1205. }
  1206. }
  1207. /**
  1208. * Parse a start or end tag.
  1209. */
  1210. void parseTag() throws IOException {
  1211. Element elem = null;
  1212. boolean net = false;
  1213. boolean warned = false;
  1214. boolean unknown = false;
  1215. switch (ch = readCh()) {
  1216. case '!':
  1217. switch (ch = readCh()) {
  1218. case '-':
  1219. // Parse comment. [92] 391:7
  1220. while (true) {
  1221. if (ch == '-') {
  1222. if (!strict || ((ch = readCh()) == '-')) {
  1223. ch = readCh();
  1224. if (!strict && ch == '-') {
  1225. ch = readCh();
  1226. }
  1227. // send over any text you might see
  1228. // before parsing and sending the
  1229. // comment
  1230. if (textpos != 0) {
  1231. char newtext[] = new char[textpos];
  1232. System.arraycopy(text, 0, newtext, 0, textpos);
  1233. handleText(newtext);
  1234. textpos = 0;
  1235. }
  1236. parseComment();
  1237. handleComment(getChars(0));
  1238. continue;
  1239. } else if (!warned) {
  1240. warned = true;
  1241. error("invalid.commentchar", "-");
  1242. }
  1243. }
  1244. skipSpace();
  1245. switch (ch) {
  1246. case '-':
  1247. continue;
  1248. case '>':
  1249. ch = readCh();
  1250. case -1:
  1251. return;
  1252. default:
  1253. ch = readCh();
  1254. if (!warned) {
  1255. warned = true;
  1256. error("invalid.commentchar",
  1257. String.valueOf((char)ch));
  1258. }
  1259. break;
  1260. }
  1261. }
  1262. default:
  1263. // deal with marked sections
  1264. StringBuffer strBuff = new StringBuffer();
  1265. while (true) {
  1266. strBuff.append((char)ch);
  1267. if (parseMarkupDeclarations(strBuff)) {
  1268. return;
  1269. }
  1270. switch(ch) {
  1271. case '>':
  1272. ch = readCh();
  1273. case -1:
  1274. error("invalid.markup");
  1275. return;
  1276. case '\n':
  1277. ln++;
  1278. ch = readCh();
  1279. lfCount++;
  1280. break;
  1281. case '\r':
  1282. ln++;
  1283. if ((ch = readCh()) == '\n') {
  1284. ch = readCh();
  1285. crlfCount++;
  1286. }
  1287. else {
  1288. crCount++;
  1289. }
  1290. break;
  1291. default:
  1292. ch = readCh();
  1293. break;
  1294. }
  1295. }
  1296. }
  1297. case '/':
  1298. // parse end tag [19] 317:4
  1299. switch (ch = readCh()) {
  1300. case '>':
  1301. ch = readCh();
  1302. case '<':
  1303. // empty end tag. either </> or </<
  1304. if (recent == null) {
  1305. error("invalid.shortend");
  1306. return;
  1307. }
  1308. elem = recent;
  1309. break;
  1310. default:
  1311. if (!parseIdentifier(true)) {
  1312. error("expected.endtagname");
  1313. return;
  1314. }
  1315. skipSpace();
  1316. switch (ch) {
  1317. case '>':
  1318. ch = readCh();
  1319. case '<':
  1320. break;
  1321. default:
  1322. error("expected", "'>'");
  1323. while ((ch != -1) && (ch != '\n') && (ch != '>')) {
  1324. ch = readCh();
  1325. }
  1326. if (ch == '>') {
  1327. ch = readCh();
  1328. }
  1329. break;
  1330. }
  1331. String elemStr = getString(0);
  1332. if (!dtd.elementExists(elemStr)) {
  1333. error("end.unrecognized", elemStr);
  1334. // Ignore RE before end tag
  1335. if ((textpos > 0) && (text[textpos-1] == '\n')) {
  1336. textpos--;
  1337. }
  1338. elem = dtd.getElement("unknown");
  1339. elem.name = elemStr;
  1340. unknown = true;
  1341. } else {
  1342. elem = dtd.getElement(elemStr);
  1343. }
  1344. break;
  1345. }
  1346. // If the stack is null, we're seeing end tags without any begin
  1347. // tags. Ignore them.
  1348. if (stack == null) {
  1349. error("end.extra.tag", elem.getName());
  1350. return;
  1351. }
  1352. // Ignore RE before end tag
  1353. if ((textpos > 0) && (text[textpos-1] == '\n')) {
  1354. // In a pre tag, if there are blank lines
  1355. // we do not want to remove the newline
  1356. // before the end tag. Hence this code.
  1357. //
  1358. if (stack.pre) {
  1359. if ((textpos > 1) && (text[textpos-2] != '\n')) {
  1360. textpos--;
  1361. }
  1362. } else {
  1363. textpos--;
  1364. }
  1365. }
  1366. // If the end tag is a form, since we did not put it
  1367. // on the tag stack, there is no corresponding start
  1368. // start tag to find. Hence do not touch the tag stack.
  1369. //
  1370. if (!strict && elem.getName().equals("form")) {
  1371. if (lastFormSent != null) {
  1372. handleEndTag(lastFormSent);
  1373. return;
  1374. } else {
  1375. // do nothing.
  1376. return;
  1377. }
  1378. }
  1379. if (unknown) {
  1380. // we will not see a corresponding start tag
  1381. // on the the stack. If we are seeing an
  1382. // end tag, lets send this on as an empty
  1383. // tag with the end tag attribute set to
  1384. // true.
  1385. TagElement t = makeTag(elem);
  1386. handleText(t);
  1387. attributes.addAttribute(HTML.Attribute.ENDTAG, "true");
  1388. handleEmptyTag(makeTag(elem));
  1389. unknown = false;
  1390. return;
  1391. }
  1392. // find the corresponding start tag
  1393. // A commonly occuring error appears to be the insertion
  1394. // of extra end tags in a table. The intent here is ignore
  1395. // such extra end tags.
  1396. //
  1397. if (!strict) {
  1398. String stackElem = stack.elem.getName();
  1399. if (stackElem.equals("table")) {
  1400. // If it isnt a valid end tag ignore it and return
  1401. //
  1402. if (!elem.getName().equals(stackElem)) {
  1403. error("tag.ignore", elem.getName());
  1404. return;
  1405. }
  1406. }
  1407. if (stackElem.equals("tr") ||
  1408. stackElem.equals("td")) {
  1409. if ((!elem.getName().equals("table")) &&
  1410. (!elem.getName().equals(stackElem))) {
  1411. error("tag.ignore", elem.getName());
  1412. return;
  1413. }
  1414. }
  1415. }
  1416. TagStack sp = stack;
  1417. while ((sp != null) && (elem != sp.elem)) {
  1418. sp = sp.next;
  1419. }
  1420. if (sp == null) {
  1421. error("unmatched.endtag", elem.getName());
  1422. return;
  1423. }
  1424. // People put font ending tags in the darndest places.
  1425. // Don't close other contexts based on them being between
  1426. // a font tag and the corresponding end tag. Instead,
  1427. // ignore the end tag like it doesn't exist and allow the end
  1428. // of the document to close us out.
  1429. String elemName = elem.getName();
  1430. if (stack != sp &&
  1431. (elemName.equals("font") ||
  1432. elemName.equals("center"))) {
  1433. // Since closing out a center tag can have real wierd
  1434. // effects on the formatting, make sure that tags
  1435. // for which omitting an end tag is legimitate
  1436. // get closed out.
  1437. //
  1438. if (elemName.equals("center")) {
  1439. while(stack.elem.omitEnd() && stack != sp) {
  1440. endTag(true);
  1441. }
  1442. if (stack.elem == elem) {
  1443. endTag(false);
  1444. }
  1445. }
  1446. return;
  1447. }
  1448. // People do the same thing with center tags. In this
  1449. // case we would like to close off the center tag but
  1450. // not necessarily all enclosing tags.
  1451. // end tags
  1452. while (stack != sp) {
  1453. endTag(true);
  1454. }
  1455. endTag(false);
  1456. return;
  1457. case -1:
  1458. error("eof");
  1459. return;
  1460. }
  1461. // start tag [14] 314:1
  1462. if (!parseIdentifier(true)) {
  1463. elem = recent;
  1464. if ((ch != '>') || (elem == null)) {
  1465. error("expected.tagname");
  1466. return;
  1467. }
  1468. } else {
  1469. String elemStr = getString(0);
  1470. if (elemStr.equals("image")) {
  1471. elemStr = new String("img");
  1472. }
  1473. /* determine if this element is part of the dtd. */
  1474. if (!dtd.elementExists(elemStr)) {
  1475. // parseInvalidTag();
  1476. error("tag.unrecognized ", elemStr);
  1477. elem = dtd.getElement("unknown");
  1478. elem.name = elemStr;
  1479. unknown = true;
  1480. } else {
  1481. elem = dtd.getElement(elemStr);
  1482. }
  1483. }
  1484. // Parse attributes
  1485. parseAttributeSpecificationList(elem);
  1486. switch (ch) {
  1487. case '/':
  1488. net = true;
  1489. case '>':
  1490. ch = readCh();
  1491. case '<':
  1492. break;
  1493. default:
  1494. error("expected", "'>'");
  1495. break;
  1496. }
  1497. if (!strict) {
  1498. if (elem.getName().equals("script")) {
  1499. error("javascript.unsupported");
  1500. }
  1501. }
  1502. // ignore RE after start tag
  1503. //
  1504. if (!elem.isEmpty()) {
  1505. if (ch == '\n') {
  1506. ln++;
  1507. lfCount++;
  1508. ch = readCh();
  1509. } else if (ch == '\r') {
  1510. ln++;
  1511. if ((ch = readCh()) == '\n') {
  1512. ch = readCh();
  1513. crlfCount++;
  1514. }
  1515. else {
  1516. crCount++;
  1517. }
  1518. }
  1519. }
  1520. // ensure a legal context for the tag
  1521. TagElement tag = makeTag(elem, false);
  1522. /** In dealing with forms, we have decided to treat
  1523. them as legal in any context. Also, even though
  1524. they do have a start and an end tag, we will
  1525. not put this tag on the stack. This is to deal
  1526. several pages in the web oasis that choose to
  1527. start and end forms in any possible location. **/
  1528. if (!strict && elem.getName().equals("form")) {
  1529. if (lastFormSent == null) {
  1530. lastFormSent = tag;
  1531. } else {
  1532. handleEndTag(lastFormSent);
  1533. lastFormSent = tag;
  1534. }
  1535. } else {
  1536. // Smlly, if a tag is unknown, we will apply
  1537. // no legalTagContext logic to it.
  1538. //
  1539. if (!unknown) {
  1540. legalTagContext(tag);
  1541. // If skip tag is true, this implies that
  1542. // the tag was illegal and that the error
  1543. // recovery strategy adopted is to ignore
  1544. // the tag.
  1545. if (!strict && skipTag) {
  1546. skipTag = false;
  1547. return;
  1548. }
  1549. }
  1550. }
  1551. startTag(tag);
  1552. if (!elem.isEmpty()) {
  1553. switch (elem.getType()) {
  1554. case CDATA:
  1555. parseLiteral(false);
  1556. break;
  1557. case RCDATA:
  1558. parseLiteral(true);
  1559. break;
  1560. default:
  1561. if (stack != null) {
  1562. stack.net = net;
  1563. }
  1564. break;
  1565. }
  1566. }
  1567. }
  1568. /**
  1569. * Parse Content. [24] 320:1
  1570. */
  1571. void parseContent() throws IOException {
  1572. Thread curThread = Thread.currentThread();
  1573. for (;;) {
  1574. if (curThread.isInterrupted()) {
  1575. curThread.interrupt(); // resignal the interrupt
  1576. break;
  1577. }
  1578. int c = ch;
  1579. switch (c) {
  1580. case '<':
  1581. parseTag();
  1582. continue;
  1583. case '/':
  1584. ch = readCh();
  1585. if ((stack != null) && stack.net) {
  1586. // null end tag.
  1587. endTag(false);
  1588. continue;
  1589. }
  1590. break;
  1591. case -1:
  1592. return;
  1593. case '&':
  1594. if (textpos == 0) {
  1595. if (!legalElementContext(dtd.pcdata)) {
  1596. error("unexpected.pcdata");
  1597. }
  1598. if (last.breaksFlow()) {
  1599. space = false;
  1600. }
  1601. }
  1602. char data[] = parseEntityReference();
  1603. if (textpos + data.length + 1 > text.length) {
  1604. char newtext[] = new char[Math.max(textpos + data.length + 128, text.length * 2)];
  1605. System.arraycopy(text, 0, newtext, 0, text.length);
  1606. text = newtext;
  1607. }
  1608. if (space) {
  1609. space = false;
  1610. text[textpos++] = ' ';
  1611. }
  1612. System.arraycopy(data, 0, text, textpos, data.length);
  1613. textpos += data.length;
  1614. continue;
  1615. case '\n':
  1616. ln++;
  1617. lfCount++;
  1618. ch = readCh();
  1619. if ((stack != null) && stack.pre) {
  1620. break;
  1621. }
  1622. space = true;
  1623. continue;
  1624. case '\r':
  1625. ln++;
  1626. c = '\n';
  1627. if ((ch = readCh()) == '\n') {
  1628. ch = readCh();
  1629. crlfCount++;
  1630. }
  1631. else {
  1632. crCount++;
  1633. }
  1634. if ((stack != null) && stack.pre) {
  1635. break;
  1636. }
  1637. space = true;
  1638. continue;
  1639. case '\t':
  1640. case ' ':
  1641. ch = readCh();
  1642. if ((stack != null) && stack.pre) {
  1643. break;
  1644. }
  1645. space = true;
  1646. continue;
  1647. default:
  1648. if (textpos == 0) {
  1649. if (!legalElementContext(dtd.pcdata)) {
  1650. error("unexpected.pcdata");
  1651. }
  1652. if (last.breaksFlow()) {
  1653. space = false;
  1654. }
  1655. }
  1656. ch = readCh();
  1657. break;
  1658. }
  1659. // enlarge buffer if needed
  1660. if (textpos + 2 > text.length) {
  1661. char newtext[] = new char[text.length + 128];
  1662. System.arraycopy(text, 0, newtext, 0, text.length);
  1663. text = newtext;
  1664. }
  1665. // output pending space
  1666. if (space) {
  1667. text[textpos++] = ' ';
  1668. space = false;
  1669. }
  1670. text[textpos++] = (char)c;
  1671. }
  1672. }
  1673. /**
  1674. * Returns the end of line string. This will return the end of line
  1675. * string that has been encountered the most, one of \r, \n or \r\n.
  1676. */
  1677. String getEndOfLineString() {
  1678. if (crlfCount >= crCount) {
  1679. if (lfCount >= crlfCount) {
  1680. return "\n";
  1681. }
  1682. else {
  1683. return "\r\n";
  1684. }
  1685. }
  1686. else {
  1687. if (crCount > lfCount) {
  1688. return "\r";
  1689. }
  1690. else {
  1691. return "\n";
  1692. }
  1693. }
  1694. }
  1695. /**
  1696. * Parse an HTML stream, given a DTD.
  1697. */
  1698. public synchronized void parse(Reader in) throws IOException {
  1699. this.in = in;
  1700. this.ln = 1;
  1701. seenHtml = false;
  1702. seenHead = false;
  1703. seenBody = false;
  1704. crCount = lfCount = crlfCount = 0;
  1705. try {
  1706. try {
  1707. ch = readCh();
  1708. text = new char[1024];
  1709. str = new char[128];
  1710. parseContent();
  1711. // NOTE: interruption may have occurred. Control flows out
  1712. // of here normally.
  1713. while (stack != null) {
  1714. endTag(true);
  1715. }
  1716. } finally {
  1717. in.close();
  1718. }
  1719. } catch (IOException e) {
  1720. errorContext();
  1721. error("ioexception");
  1722. throw e;
  1723. } catch (Exception e) {
  1724. errorContext();
  1725. error("exception", e.getClass().getName(), e.getMessage());
  1726. e.printStackTrace();
  1727. } catch (ThreadDeath e) {
  1728. errorContext();
  1729. error("terminated");
  1730. e.printStackTrace();
  1731. throw e;
  1732. } finally {
  1733. for (; stack != null ; stack = stack.next) {
  1734. handleEndTag(stack.tag);
  1735. }
  1736. text = null;
  1737. str = null;
  1738. }
  1739. }
  1740. /*
  1741. * Input cache. This is much faster than calling down to a synchronized
  1742. * method of BufferedReader for each byte. Measurements done 5/30/97
  1743. * show that there's no point in having a bigger buffer: Increasing
  1744. * the buffer to 8192 had no measurable impact for a program discarding
  1745. * one character at a time (reading from an http URL to a local machine).
  1746. */
  1747. private char buf[] = new char[256];
  1748. private int pos;
  1749. private int len;
  1750. /*
  1751. tracks position relative to the beginning of the
  1752. document.
  1753. */
  1754. private int currentPosition;
  1755. private final int readCh() throws IOException {
  1756. if (pos >= len) {
  1757. // This loop allows us to ignore interrupts if the flag
  1758. // says so
  1759. for (;;) {
  1760. try {
  1761. len = in.read(buf);
  1762. break;
  1763. } catch (InterruptedIOException ex) {
  1764. throw ex;
  1765. }
  1766. }
  1767. if (len <= 0) {
  1768. return -1; // eof
  1769. }
  1770. pos = 0;
  1771. }
  1772. ++currentPosition;
  1773. return buf[pos++];
  1774. }
  1775. protected int getCurrentPos() {
  1776. return currentPosition;
  1777. }
  1778. }