Unfortunately there are many badly implemented HTML parsers out there, and as a result there are many badly formatted HTML files. This parser attempts to parse most HTML files. This means that the implementation sometimes deviates from the SGML specification in favor of HTML.
The parser treats \r and \r\n as \n. Newlines after starttags and before end tags are ignored just as specified in the SGML/HTML specification.
The html spec does not specify how spaces are to be coalesced very well. Specifically, the following scenarios are not discussed (note that a space should be used here, but I am using   to force the space to be displayed):
'<b>blah <i> <strike> foo' which can be treated as: '<b>blah <i><strike>foo'
as well as: '<p><a href="xx"> <em>Using</em></a></p>' which appears to be treated as: '<p><a href="xx"><em>Using</em></a></p>'
If strict is false, when a tag that breaks flow,
(TagElement.breaksFlows) or trailing whitespace is
encountered, all whitespace will be ignored until a non whitespace
character is encountered. This appears to give behavior closer to
the popular browsers.
DTD, TagElement, SimpleAttributeSet
| Modifier and Type | Field and Description |
|---|---|
| private SimpleAttributeSet | |
| private char[] | |
| private int | |
| private static final char[] | cp1252Map
array for mapping numeric references in range 130-159 to displayable Unicode characters. |
| private int | crCount
Number of \r's encountered. |
| private int | crlfCount
Number of \r\n's encountered. |
| private int | currentBlockStartPos
The start position of the current block. |
| private int | |
| protected DTD | dtd
The dtd. |
| private static final String | |
| private boolean | ignoreSpace
The html spec does not specify how spaces are coalesced very well. |
| private Reader | |
| private TagElement | |
| private int | lastBlockStartPos
Start position of the last block. |
| private TagElement | |
| private int | |
| private int | lfCount
Number of \n's encountered. |
| private int | |
| private int | |
| private Element | |
| private static final char[] | |
| private static final char[] | |
| private boolean | |
| private boolean | |
| private boolean | |
| private boolean | |
| private boolean | |
| private TagStack | |
| private static final String | |
| private char[] | |
| protected boolean | strict
This flag determines whether or not the Parser will be strict in enforcing SGML compatibility. |
| private int | |
| private char[] | |
| private int |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| protected void | endTag(boolean
true if the tag is no actually present in the
document, but is supposed by the parserHandle an end tag. |
| protected void | |
| protected void | |
| protected void | |
| protected void | |
| pack-priv void | |
| protected void | |
| protected SimpleAttributeSet | Returns: SimpleAttributeSet containing the attributesReturns attributes for the current tag. |
| pack-priv int | Returns: the start position of the current blockReturns the start position of the current block. |
| pack-priv char[] | |
| pack-priv char[] | |
| protected int | Returns: the line number of the line currently being parsedReturns the line number of the line currently being parsed. |
| protected int | |
| pack-priv String | |
| pack-priv String | |
| protected void | |
| protected void | |
| protected void | |
| protected void | |
| protected void | handleError(int
the number of line containing the error ln, String the error message msg)An error has occurred. |
| protected void | |
| protected void | |
| pack-priv void | |
| protected void | |
| pack-priv boolean | |
| pack-priv boolean | |
| pack-priv void | |
| protected TagElement | |
| protected TagElement | |
| private char[] | Returns: a char array corresponding to the reference code.the code of numeric character reference. c)Converts numeric character reference to char array. |
| protected void | markFirstTime(Element
the element represented by the tag elem)Marks the first time a tag has been seen in a document |
| public synchronized void | |
| pack-priv void | |
| pack-priv String | |
| pack-priv void | |
| pack-priv void | |
| public String | Returns: the string representation of the markup declarationParses the Document Type Declaration markup declaration. |
| private char[] | |
| pack-priv boolean | |
| pack-priv void | |
| pack-priv void | |
| protected boolean | Returns: true if this is a valid markup declaration;
otherwise falsethe markup declaration strBuff)Parse markup declarations. |
| pack-priv void | |
| pack-priv void | |
| private int | |
| pack-priv void | |
| pack-priv void | |
| protected void | |
| pack-priv int |
| attributes | back to summary |
|---|---|
| private SimpleAttributeSet attributes | |
| buf | back to summary |
|---|---|
| private char[] buf | |
| ch | back to summary |
|---|---|
| private int ch | |
| cp1252Map | back to summary |
|---|---|
| private static final char[] cp1252Map array for mapping numeric references in range 130-159 to displayable Unicode characters. | |
| crCount | back to summary |
|---|---|
| private int crCount Number of \r's encountered. A \r\n will not increment this. | |
| crlfCount | back to summary |
|---|---|
| private int crlfCount Number of \r\n's encountered. | |
| currentBlockStartPos | back to summary |
|---|---|
| private int currentBlockStartPos The start position of the current block. Block is overloaded here, it really means the current start position for the current comment, tag, text. Use getBlockStartPosition to access this. | |
| currentPosition | back to summary |
|---|---|
| private int currentPosition | |
| dtd | back to summary |
|---|---|
| protected DTD dtd The dtd. | |
| END_COMMENT | back to summary |
|---|---|
| private static final String END_COMMENT | |
| ignoreSpace | back to summary |
|---|---|
| private boolean ignoreSpace The html spec does not specify how spaces are coalesced very well. If strict == false, ignoreSpace is used to try and mimic the behavior of the popular browsers. The problematic scenarios are: '<b>blah <i> <strike> foo' which can be treated as: '<b>blah <i><strike>foo' as well as: '<p><a href="xx"> <em>Using</em></a></p>' which appears to be treated as: '<p><a href="xx"><em>Using</em></a></p>' When a tag that breaks flow, or trailing whitespace is encountered ignoreSpace is set to true. From then on, all whitespace will be ignored. ignoreSpace will be set back to false the first time a non whitespace character is encountered. This appears to give behavior closer to the popular browsers. | |
| in | back to summary |
|---|---|
| private Reader in | |
| last | back to summary |
|---|---|
| private TagElement last | |
| lastBlockStartPos | back to summary |
|---|---|
| private int lastBlockStartPos Start position of the last block. | |
| lastFormSent | back to summary |
|---|---|
| private TagElement lastFormSent | |
| len | back to summary |
|---|---|
| private int len | |
| lfCount | back to summary |
|---|---|
| private int lfCount Number of \n's encountered. A \r\n will not increment this. | |
| ln | back to summary |
|---|---|
| private int ln | |
| pos | back to summary |
|---|---|
| private int pos | |
| recent | back to summary |
|---|---|
| private Element recent | |
| SCRIPT_END_TAG | back to summary |
|---|---|
| private static final char[] SCRIPT_END_TAG | |
| SCRIPT_END_TAG_UPPER_CASE | back to summary |
|---|---|
| private static final char[] SCRIPT_END_TAG_UPPER_CASE | |
| seenBody | back to summary |
|---|---|
| private boolean seenBody | |
| seenHead | back to summary |
|---|---|
| private boolean seenHead | |
| seenHtml | back to summary |
|---|---|
| private boolean seenHtml | |
| skipTag | back to summary |
|---|---|
| private boolean skipTag | |
| space | back to summary |
|---|---|
| private boolean space | |
| stack | back to summary |
|---|---|
| private TagStack stack | |
| START_COMMENT | back to summary |
|---|---|
| private static final String START_COMMENT | |
| str | back to summary |
|---|---|
| private char[] str | |
| strict | back to summary |
|---|---|
| protected boolean strict This flag determines whether or not the Parser will be strict in enforcing SGML compatibility. If false, it will be lenient with certain common classes of erroneous HTML constructs. Strict or not, in either case an error will be recorded. | |
| strpos | back to summary |
|---|---|
| private int strpos | |
| text | back to summary |
|---|---|
| private char[] text | |
| textpos | back to summary |
|---|---|
| private int textpos | |
| Parser | back to summary |
|---|---|
| public Parser(DTD dtd) Creates parser with the specified
| |
| addString | back to summary |
|---|---|
| pack-priv void addString(int c) Add a char to the string buffer. | |
| endTag | back to summary |
|---|---|
| protected void endTag(boolean omitted) Handle an end tag. The end tag is popped from the tag stack.
| |
| error | back to summary |
|---|---|
| protected void error(String err, String arg1, String arg2, String arg3) Invokes the error handler. | |
| error | back to summary |
|---|---|
| protected void error(String err, String arg1, String arg2) Invokes the error handler with the 3rd error message argument "?". | |
| error | back to summary |
|---|---|
| protected void error(String err, String arg1) Invokes the error handler with the 2nd and 3rd error message argument "?". | |
| error | back to summary |
|---|---|
| protected void error(String err) Invokes the error handler with the 1st, 2nd and 3rd error message argument "?".
| |
| errorContext | back to summary |
|---|---|
| pack-priv void errorContext() throws ChangedCharSetException Error context. Something went wrong, make sure we are in the document's body context | |
| flushAttributes | back to summary |
|---|---|
| protected void flushAttributes() Removes the current attributes. | |
| getAttributes | back to summary |
|---|---|
| protected SimpleAttributeSet getAttributes() Returns attributes for the current tag.
| |
| getBlockStartPosition | back to summary |
|---|---|
| pack-priv int getBlockStartPosition() Returns the start position of the current block. Block is overloaded here, it really means the current start position for the current comment tag, text, block.... This is provided for subclassers that wish to know the start of the current block when called with one of the handleXXX methods.
| |
| getChars | back to summary |
|---|---|
| pack-priv char[] getChars(int pos) | |
| getChars | back to summary |
|---|---|
| pack-priv char[] getChars(int pos, int endPos) | |
| getCurrentLine | back to summary |
|---|---|
| protected int getCurrentLine() Returns the line number of the line currently being parsed.
| |
| getCurrentPos | back to summary |
|---|---|
| protected int getCurrentPos() Returns the current position.
| |
| getEndOfLineString | back to summary |
|---|---|
| pack-priv String getEndOfLineString() Returns the end of line string. This will return the end of line string that has been encountered the most, one of \r, \n or \r\n. | |
| getString | back to summary |
|---|---|
| pack-priv String getString(int pos) Get the string that's been accumulated. | |
| handleComment | back to summary |
|---|---|
| protected void handleComment(char[] text) Called when an HTML comment is encountered.
| |
| handleEmptyTag | back to summary |
|---|---|
| protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException Called when an empty tag is encountered.
| |
| handleEndTag | back to summary |
|---|---|
| protected void handleEndTag(TagElement tag) Called when an end tag is encountered.
| |
| handleEOFInComment | back to summary |
|---|---|
| protected void handleEOFInComment() Called when the content terminates without closing the HTML comment. | |
| handleError | back to summary |
|---|---|
| protected void handleError(int ln, String msg) An error has occurred.
| |
| handleStartTag | back to summary |
|---|---|
| protected void handleStartTag(TagElement tag) Called when a start tag is encountered.
| |
| handleText | back to summary |
|---|---|
| protected void handleText(char[] text) Called when PCDATA is encountered.
| |
| handleText | back to summary |
|---|---|
| pack-priv void handleText(TagElement tag) Output text. | |
| handleTitle | back to summary |
|---|---|
| protected void handleTitle(char[] text) Called when an HTML title tag is encountered.
| |
| ignoreElement | back to summary |
|---|---|
| pack-priv boolean ignoreElement(Element elem) | |
| legalElementContext | back to summary |
|---|---|
| pack-priv boolean legalElementContext(Element elem) throws ChangedCharSetException Create a legal content for an element. | |
| legalTagContext | back to summary |
|---|---|
| pack-priv void legalTagContext(TagElement tag) throws ChangedCharSetException Create a legal context for a tag. | |
| makeTag | back to summary |
|---|---|
| protected TagElement makeTag(Element elem, boolean fictional) Makes a TagElement.
| |
| makeTag | back to summary |
|---|---|
| protected TagElement makeTag(Element elem) Makes a TagElement.
| |
| mapNumericReference | back to summary |
|---|---|
| private char[] mapNumericReference(int c) Converts numeric character reference to char array. Normally the code in a reference should be always converted to the Unicode character with the same code, but due to wide usage of Cp1252 charset most browsers map numeric references in the range 130-159 (which are control chars in Unicode set) to displayable characters with other codes.
| |
| markFirstTime | back to summary |
|---|---|
| protected void markFirstTime(Element elem) Marks the first time a tag has been seen in a document
| |
| parse | back to summary |
|---|---|
| public synchronized void parse(Reader in) throws IOException Parse an HTML stream, given a DTD.
| |
| parseAttributeSpecificationList | back to summary |
|---|---|
| pack-priv void parseAttributeSpecificationList(Element elem) throws IOException Parse attribute specification List. [31] 327:17 | |
| parseAttributeValue | back to summary |
|---|---|
| pack-priv String parseAttributeValue(boolean lower) throws IOException Parse attribute value. [33] 331:1
| |
| parseComment | back to summary |
|---|---|
| pack-priv void parseComment() throws IOException Parse a comment. [92] 391:7 | |
| parseContent | back to summary |
|---|---|
| pack-priv void parseContent() throws IOException Parse Content. [24] 320:1 | |
| parseDTDMarkup | back to summary |
|---|---|
| public String parseDTDMarkup() throws IOException Parses the Document Type Declaration markup declaration. Currently ignores it.
| |
| parseEntityReference | back to summary |
|---|---|
| private char[] parseEntityReference() throws IOException Parse an entity reference. [59] 350:17 | |
| parseIdentifier | back to summary |
|---|---|
| pack-priv boolean parseIdentifier(boolean lower) throws IOException Parse identifier. Uppercase characters are folded to lowercase when lower is true. Returns false if no identifier is found. [55] 346:17 | |
| parseInvalidTag | back to summary |
|---|---|
| pack-priv void parseInvalidTag() throws IOException Parse an invalid tag. | |
| parseLiteral | back to summary |
|---|---|
| pack-priv void parseLiteral(boolean replace) throws IOException Parse literal content. [46] 343:1 and [47] 344:1 | |
| parseMarkupDeclarations | back to summary |
|---|---|
| protected boolean parseMarkupDeclarations(StringBuffer strBuff) throws IOException Parse markup declarations. Currently only handles the Document Type Declaration markup. Returns true if it is a markup declaration false otherwise.
| |
| parseScript | back to summary |
|---|---|
| pack-priv void parseScript() throws IOException | |
| parseTag | back to summary |
|---|---|
| pack-priv void parseTag() throws IOException Parse a start or end tag.
| |
| readCh | back to summary |
|---|---|
| private int readCh() throws IOException | |
| resetStrBuffer | back to summary |
|---|---|
| pack-priv void resetStrBuffer() | |
| skipSpace | back to summary |
|---|---|
| pack-priv void skipSpace() throws IOException Skip space. [5] 297:5 | |
| startTag | back to summary |
|---|---|
| protected void startTag(TagElement tag) throws ChangedCharSetException Handle a start tag. The new tag is pushed onto the tag stack. The attribute list is checked for required attributes.
| |
| strIndexOf | back to summary |
|---|---|
| pack-priv int strIndexOf(char target) | |