public class Parser extends Objectimplements DTDConstants
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 |
|---|---|
protected DTD |
dtd
|
protected boolean |
strict
This flag determines whether or not the Parser will be strict in enforcing SGML compatibility.
|
ANY, CDATA, CONREF, CURRENT, DEFAULT, EMPTY, ENDTAG, ENTITIES, ENTITY, FIXED, GENERAL, ID, IDREF, IDREFS, IMPLIED, MD, MODEL, MS, NAME, NAMES, NMTOKEN, NMTOKENS, NOTATION, NUMBER, NUMBERS, NUTOKEN, NUTOKENS, PARAMETER, PI, PUBLIC, RCDATA, REQUIRED, SDATA, STARTTAG, SYSTEM| Modifier and Type | Method and Description |
|---|---|
protected void |
endTag(boolean omitted)
Handle an end tag.
|
protected void |
error(String
|
protected void |
error(String
|
protected void |
error(String
|
protected void |
error(String
Invoke the error handler.
|
protected void |
flushAttributes()
|
protected SimpleAttributeSet |
getAttributes()
|
protected int |
getCurrentLine()
|
protected int |
getCurrentPos()
|
protected void |
handleComment(char[] text)
Called when an HTML comment is encountered.
|
protected void |
handleEmptyTag(TagElement
Called when an empty tag is encountered.
|
protected void |
handleEndTag(TagElement
Called when an end tag is encountered.
|
protected void |
handleEOFInComment()
|
protected void |
handleError(int ln, String
An error has occurred.
|
protected void |
handleStartTag(TagElement
Called when a start tag is encountered.
|
protected void |
handleText(char[] text)
Called when PCDATA is encountered.
|
protected void |
handleTitle(char[] text)
Called when an HTML title tag is encountered.
|
protected TagElement |
makeTag(Element
|
protected TagElement |
makeTag(Element
Makes a TagElement.
|
protected void |
markFirstTime(Element
Marks the first time a tag has been seen in a document
|
void |
parse(Reader
Parse an HTML stream, given a DTD.
|
String |
parseDTDMarkup()
Parses th Document Declaration Type markup declaration.
|
protected boolean |
parseMarkupDeclarations(StringBuffer
Parse markup declarations.
|
protected void |
startTag(TagElement
Handle a start tag.
|
protected DTDdtd
protected boolean strict
public Parser(DTDdtd)
protected int getCurrentLine()
protected TagElementmakeTag(Element elem, boolean fictional)
protected TagElementmakeTag(Element elem)
protected SimpleAttributeSetgetAttributes()
protected void flushAttributes()
protected void handleText(char[] text)
protected void handleTitle(char[] text)
protected void handleComment(char[] text)
protected void handleEOFInComment()
protected void handleEmptyTag(TagElementtag) throws ChangedCharSetException
ChangedCharSetException
protected void handleStartTag(TagElementtag)
protected void handleEndTag(TagElementtag)
protected void handleError(int ln,
String msg)
protected void error(Stringerr, String arg1, String arg2, String arg3)
protected void error(Stringerr)
protected void startTag(TagElementtag) throws ChangedCharSetException
ChangedCharSetException
protected void endTag(boolean omitted)
protected void markFirstTime(Elementelem)
public StringparseDTDMarkup() throws IOException
IOException
protected boolean parseMarkupDeclarations(StringBufferstrBuff) throws IOException
IOException
public void parse(Readerin) throws IOException
IOException
protected int getCurrentPos()