- All Implemented Interfaces:
- DTDConstants
- Direct Known Subclasses:
- DocumentParser
public class Parser extends Object implements 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.
- See Also:
- DTD,- TagElement,- SimpleAttributeSet
- 
Field SummaryFields Modifier and Type Field Description protected DTDdtdThe dtd.protected booleanstrictThis flag determines whether or not the Parser will be strict in enforcing SGML compatibility.Fields declared in interface javax.swing.text.html.parser.DTDConstantsANY, 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
- 
Constructor Summary
- 
Method SummaryModifier and Type Method Description protected voidendTag(boolean omitted)Handle an end tag.protected voiderror(String err)Invokes the error handler with the 1st, 2nd and 3rd error message argument "?".protected voiderror(String err, String arg1)Invokes the error handler with the 2nd and 3rd error message argument "?".protected voiderror(String err, String arg1, String arg2)Invokes the error handler with the 3rd error message argument "?".protected voiderror(String err, String arg1, String arg2, String arg3)Invokes the error handler.protected voidflushAttributes()Removes the current attributes.protected SimpleAttributeSetgetAttributes()Returns attributes for the current tag.protected intgetCurrentLine()protected intgetCurrentPos()Returns the current position.protected voidhandleComment(char[] text)Called when an HTML comment is encountered.protected voidhandleEmptyTag(TagElement tag)Called when an empty tag is encountered.protected voidhandleEndTag(TagElement tag)Called when an end tag is encountered.protected voidhandleEOFInComment()Called when the content terminates without closing the HTML comment.protected voidhandleError(int ln, String msg)An error has occurred.protected voidhandleStartTag(TagElement tag)Called when a start tag is encountered.protected voidhandleText(char[] text)Called when PCDATA is encountered.protected voidhandleTitle(char[] text)Called when an HTML title tag is encountered.protected TagElementmakeTag(Element elem)Makes a TagElement.protected TagElementmakeTag(Element elem, boolean fictional)Makes a TagElement.protected voidmarkFirstTime(Element elem)Marks the first time a tag has been seen in a documentvoidparse(Reader in)Parse an HTML stream, given a DTD.StringparseDTDMarkup()Parses the Document Type Declaration markup declaration.protected booleanparseMarkupDeclarations(StringBuffer strBuff)Parse markup declarations.protected voidstartTag(TagElement tag)Handle a start tag.
- 
Field Details
- 
Constructor Details- 
ParserCreates parser with the specifieddtd.- Parameters:
- dtd- the dtd.
 
 
- 
- 
Method Details- 
getCurrentLineprotected int getCurrentLine()- Returns:
- the line number of the line currently being parsed
 
- 
makeTagMakes a TagElement.- Parameters:
- elem- the element storing the tag definition
- fictional- the value of the flag "- fictional" to be set for the tag
- Returns:
- the created TagElement
 
- 
makeTagMakes a TagElement.- Parameters:
- elem- the element storing the tag definition
- Returns:
- the created TagElement
 
- 
getAttributesReturns attributes for the current tag.- Returns:
- SimpleAttributeSetcontaining the attributes
 
- 
flushAttributesprotected void flushAttributes()Removes the current attributes.
- 
handleTextprotected void handleText(char[] text)Called when PCDATA is encountered.- Parameters:
- text- the section text
 
- 
handleTitleprotected void handleTitle(char[] text)Called when an HTML title tag is encountered.- Parameters:
- text- the title text
 
- 
handleCommentprotected void handleComment(char[] text)Called when an HTML comment is encountered.- Parameters:
- text- the comment being handled
 
- 
handleEOFInCommentprotected void handleEOFInComment()Called when the content terminates without closing the HTML comment.
- 
handleEmptyTagCalled when an empty tag is encountered.- Parameters:
- tag- the tag being handled
- Throws:
- ChangedCharSetException- if the document charset was changed
 
- 
handleStartTagCalled when a start tag is encountered.- Parameters:
- tag- the tag being handled
 
- 
handleEndTagCalled when an end tag is encountered.- Parameters:
- tag- the tag being handled
 
- 
handleErrorAn error has occurred.- Parameters:
- ln- the number of line containing the error
- msg- the error message
 
- 
errorInvokes the error handler.- Parameters:
- err- the error type
- arg1- the 1st error message argument
- arg2- the 2nd error message argument
- arg3- the 3rd error message argument
 
- 
errorInvokes the error handler with the 3rd error message argument "?".- Parameters:
- err- the error type
- arg1- the 1st error message argument
- arg2- the 2nd error message argument
 
- 
errorInvokes the error handler with the 2nd and 3rd error message argument "?".- Parameters:
- err- the error type
- arg1- the 1st error message argument
 
- 
errorInvokes the error handler with the 1st, 2nd and 3rd error message argument "?".- Parameters:
- err- the error type
 
- 
startTagHandle a start tag. The new tag is pushed onto the tag stack. The attribute list is checked for required attributes.- Parameters:
- tag- the tag
- Throws:
- ChangedCharSetException- if the document charset was changed
 
- 
endTagprotected void endTag(boolean omitted)Handle an end tag. The end tag is popped from the tag stack.- Parameters:
- omitted-- trueif the tag is no actually present in the document, but is supposed by the parser
 
- 
markFirstTimeMarks the first time a tag has been seen in a document- Parameters:
- elem- the element represented by the tag
 
- 
parseDTDMarkupParses the Document Type Declaration markup declaration. Currently ignores it.- Returns:
- the string representation of the markup declaration
- Throws:
- IOException- if an I/O error occurs
 
- 
parseMarkupDeclarationsParse markup declarations. Currently only handles the Document Type Declaration markup. Returns true if it is a markup declaration false otherwise.- Parameters:
- strBuff- the markup declaration
- Returns:
- trueif this is a valid markup declaration; otherwise- false
- Throws:
- IOException- if an I/O error occurs
 
- 
parseParse an HTML stream, given a DTD.- Parameters:
- in- the reader to read the source from
- Throws:
- IOException- if an I/O error occurs
 
- 
getCurrentPosprotected int getCurrentPos()Returns the current position.- Returns:
- the current position
 
 
-