Class Parser
- All Implemented Interfaces:
DTDConstants
- Direct Known Subclasses:
DocumentParser
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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected DTDThe dtd.protected booleanThis flag determines whether or not the Parser will be strict in enforcing SGML compatibility.Fields declared in interface DTDConstants
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, SYSTEMModifier and TypeFieldDescriptionstatic final intThe DTD constant corresponds to ANYstatic final intThe DTD constant corresponds to CDATAstatic final intThe DTD constant corresponds to CONREFstatic final intThe DTD constant corresponds to CURRENTstatic final intThe DTD constant corresponds to DEFAULTstatic final intThe DTD constant corresponds to EMPTYstatic final intThe DTD constant corresponds to ENDTAGstatic final intThe DTD constant corresponds to ENTITIESstatic final intThe DTD constant corresponds to ENTITYstatic final intThe DTD constant corresponds to FIXEDstatic final intThe DTD constant corresponds to GENERALstatic final intThe DTD constant corresponds to IDstatic final intThe DTD constant corresponds to IDREFstatic final intThe DTD constant corresponds to IDREFSstatic final intThe DTD constant corresponds to IMPLIEDstatic final intThe DTD constant corresponds to MDstatic final intThe DTD constant corresponds to MODELstatic final intThe DTD constant corresponds to MSstatic final intThe DTD constant corresponds to NAMEstatic final intThe DTD constant corresponds to NAMESstatic final intThe DTD constant corresponds to NMTOKENstatic final intThe DTD constant corresponds to NMTOKENSstatic final intThe DTD constant corresponds to NOTATIONstatic final intThe DTD constant corresponds to NUMBERstatic final intThe DTD constant corresponds to NUMBERSstatic final intThe DTD constant corresponds to NUTOKENstatic final intThe DTD constant corresponds to NUTOKENSstatic final intThe DTD constant corresponds to PARAMETERstatic final intThe DTD constant corresponds to PIstatic final intThe DTD constant corresponds to PUBLICstatic final intThe DTD constant corresponds to RCDATAstatic final intThe DTD constant corresponds to REQUIREDstatic final intThe DTD constant corresponds to SDATAstatic final intThe DTD constant corresponds to STARTTAGstatic final intThe DTD constant corresponds to SYSTEM -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidendTag(boolean omitted) Handle an end tag.protected voidInvokes the error handler with the 1st, 2nd and 3rd error message argument "?".protected voidInvokes the error handler with the 2nd and 3rd error message argument "?".protected voidInvokes the error handler with the 3rd error message argument "?".protected voidInvokes the error handler.protected voidRemoves the current attributes.protected SimpleAttributeSetReturns attributes for the current tag.protected intReturns the line number of the line currently being parsed.protected intReturns 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 voidCalled 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 TagElementMakes a TagElement.protected TagElementMakes a TagElement.protected voidmarkFirstTime(Element elem) Marks the first time a tag has been seen in a documentvoidParse an HTML stream, given a DTD.Parses the Document Type Declaration markup declaration.protected booleanparseMarkupDeclarations(StringBuffer strBuff) Parse markup declarations.protected voidstartTag(TagElement tag) Handle a start tag.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Creates and returns a copy of this object.booleanIndicates whether some other object is "equal to" this one.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.Finalization is deprecated and subject to removal in a future release.final Class<?> getClass()Returns the runtime class of thisObject.inthashCode()Returns a hash code value for this object.final voidnotify()Wakes up a single thread that is waiting on this object's monitor.final voidWakes up all threads that are waiting on this object's monitor.toString()Returns a string representation of the object.final voidwait()Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final voidwait(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.final voidwait(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
-
Field Details
-
dtd
The dtd. -
strict
protected boolean strictThis 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.
-
-
Constructor Details
-
Parser
-
-
Method Details
-
getCurrentLine
protected int getCurrentLine()Returns the line number of the line currently being parsed.- Returns:
- the line number of the line currently being parsed
-
makeTag
Makes a TagElement.- Parameters:
elem- the element storing the tag definitionfictional- the value of the flag "fictional" to be set for the tag- Returns:
- the created
TagElement
-
makeTag
Makes a TagElement.- Parameters:
elem- the element storing the tag definition- Returns:
- the created
TagElement
-
getAttributes
Returns attributes for the current tag.- Returns:
SimpleAttributeSetcontaining the attributes
-
flushAttributes
protected void flushAttributes()Removes the current attributes. -
handleText
protected void handleText(char[] text) Called when PCDATA is encountered.- Parameters:
text- the section text
-
handleTitle
protected void handleTitle(char[] text) Called when an HTML title tag is encountered.- Parameters:
text- the title text
-
handleComment
protected void handleComment(char[] text) Called when an HTML comment is encountered.- Parameters:
text- the comment being handled
-
handleEOFInComment
protected void handleEOFInComment()Called when the content terminates without closing the HTML comment. -
handleEmptyTag
Called when an empty tag is encountered.- Parameters:
tag- the tag being handled- Throws:
ChangedCharSetException- if the document charset was changed
-
handleStartTag
Called when a start tag is encountered.- Parameters:
tag- the tag being handled
-
handleEndTag
Called when an end tag is encountered.- Parameters:
tag- the tag being handled
-
handleError
An error has occurred.- Parameters:
ln- the number of line containing the errormsg- the error message
-
error
-
error
-
error
-
error
Invokes the error handler with the 1st, 2nd and 3rd error message argument "?".- Parameters:
err- the error type
-
startTag
Handle 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
-
endTag
protected 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
-
markFirstTime
Marks the first time a tag has been seen in a document- Parameters:
elem- the element represented by the tag
-
parseDTDMarkup
Parses 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
-
parseMarkupDeclarations
Parse 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; otherwisefalse- Throws:
IOException- if an I/O error occurs
-
parse
Parse an HTML stream, given a DTD.- Parameters:
in- the reader to read the source from- Throws:
IOException- if an I/O error occurs
-
getCurrentPos
protected int getCurrentPos()Returns the current position.- Returns:
- the current position
-