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