APIdia Launch Partner
Top Description Fields Constructors Methods
javax.swing.text.html.parser

public Class DocumentParser

extends Parser
Class Inheritance
Imports
javax.swing.text.SimpleAttributeSet, .ChangedCharSetException, javax.swing.text.html.HTMLEditorKit, .HTML, java.util.*, java.io.*, java.net.*

A Parser for HTML Documents (actually, you can specify a DTD, but you should really only use this class with the html dtd in swing). Reads an InputStream of HTML and invokes the appropriate methods in the ParserCallback class. This is the default parser used by HTMLEditorKit to parse HTML url's.

This will message the callback for all valid tags, as well as tags that are implied but not explicitly specified. For example, the html string (<p>blah) only has a p tag defined. The callback will see the following methods:

  1. handleStartTag(html, ...)
  2. handleStartTag(head, ...)
  3. handleEndTag(head)
  4. handleStartTag(body, ...)
  5. handleStartTag(p, ...)
  6. handleText(...)
  7. handleEndTag(p)
  8. handleEndTag(body)
  9. handleEndTag(html)
The items in italic are implied, that is, although they were not explicitly specified, to be correct html they should have been present (head isn't necessary, but it is still generated). For tags that are implied, the AttributeSet argument will have a value of Boolean.TRUE for the key HTMLEditorKit.ParserCallback.IMPLIED.

HTML.Attributes defines a type safe enumeration of html attributes. If an attribute key of a tag is defined in HTML.Attribute, the HTML.Attribute will be used as the key, otherwise a String will be used. For example <p foo=bar class=neat> has two attributes. foo is not defined in HTML.Attribute, where as class is, therefore the AttributeSet will have two values in it, HTML.Attribute.CLASS with a String value of 'neat' and the String key 'foo' with a String value of 'bar'.

The position argument will indicate the start of the tag, comment or text. Similar to arrays, the first character in the stream has a position of 0. For tags that are implied the position will indicate the location of the next encountered tag. In the first example, the implied start body and html tags will have the same position as the p tag, and the implied end p, html and body tags will all have the same position.

As html skips whitespace the position for text will be the position of the first valid character, eg in the string '\n\n\nblah' the text 'blah' will have a position of 3, the newlines are skipped.

For attributes that do not have a value, eg in the html string <foo blah> the attribute blah does not have a value, there are two possible values that will be placed in the AttributeSet's value:

Once the stream has been parsed, the callback is notified of the most likely end of line string. The end of line string will be one of \n, \r or \r\n, which ever is encountered the most in parsing the stream.

Author
Sunita Mani

Field Summary

Modifier and TypeField and Description
private HTMLEditorKit.ParserCallback
private static final boolean
private boolean
private int
private int
private int
private int
private int
private boolean
Inherited from javax.swing.text.html.parser.Parser:
dtdstrict

Constructor Summary

AccessConstructor and Description
public
DocumentParser(DTD
the dtd.
dtd
)

Creates document parser with the specified dtd.

Method Summary

Modifier and TypeMethod and Description
private void
protected void
handleComment(char[] text)

Overrides javax.swing.text.html.parser.Parser.handleComment.

Called when an HTML comment is encountered.

protected void
protected void
protected void
handleError(int ln, String errorMsg)

Overrides javax.swing.text.html.parser.Parser.handleError.

An error has occurred.

protected void
protected void
handleText(char[] data)

Overrides javax.swing.text.html.parser.Parser.handleText.

Handle Text.

public void
parse(Reader
the reader to read the source from
in
,
HTMLEditorKit.ParserCallback
the callback
callback
,
boolean
if true the charset is ignored
ignoreCharSet
)

Parse an HTML stream, given a DTD.

Inherited from javax.swing.text.html.parser.Parser:
addStringendTagerrorerrorerrorerrorerrorContextflushAttributesgetAttributesgetBlockStartPositiongetCharsgetCharsgetCurrentLinegetCurrentPosgetEndOfLineStringgetStringhandleEOFInCommenthandleTexthandleTitleignoreElementlegalElementContextlegalTagContextmakeTagmakeTagmarkFirstTimeparseparseAttributeSpecificationListparseAttributeValueparseCommentparseContentparseDTDMarkupparseIdentifierparseInvalidTagparseLiteralparseMarkupDeclarationsparseScriptparseTagresetStrBufferskipSpacestartTagstrIndexOf

Field Detail

callbackback to summary
private HTMLEditorKit.ParserCallback callback
debugFlagback to summary
private static final boolean debugFlag
ignoreCharSetback to summary
private boolean ignoreCharSet
inbodyback to summary
private int inbody
inheadback to summary
private int inhead
inscriptback to summary
private int inscript
instyleback to summary
private int instyle
intitleback to summary
private int intitle
seentitleback to summary
private boolean seentitle

Constructor Detail

DocumentParserback to summary
public DocumentParser(DTD dtd)

Creates document parser with the specified dtd.

Parameters
dtd:DTD

the dtd.

Method Detail

debugback to summary
private void debug(String msg)
handleCommentback to summary
protected void handleComment(char[] text)

Overrides javax.swing.text.html.parser.Parser.handleComment.

Doc from javax.swing.text.html.parser.Parser.handleComment.

Called when an HTML comment is encountered.

handleEmptyTagback to summary
protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException

Overrides javax.swing.text.html.parser.Parser.handleEmptyTag.

Handle Empty Tag.

Exceptions
ChangedCharSetException:

Doc from javax.swing.text.html.parser.Parser.handleEmptyTag.

if the document charset was changed

handleEndTagback to summary
protected void handleEndTag(TagElement tag)

Overrides javax.swing.text.html.parser.Parser.handleEndTag.

Handle End Tag.

handleErrorback to summary
protected void handleError(int ln, String errorMsg)

Overrides javax.swing.text.html.parser.Parser.handleError.

Doc from javax.swing.text.html.parser.Parser.handleError.

An error has occurred.

handleStartTagback to summary
protected void handleStartTag(TagElement tag)

Overrides javax.swing.text.html.parser.Parser.handleStartTag.

Handle Start Tag.

handleTextback to summary
protected void handleText(char[] data)

Overrides javax.swing.text.html.parser.Parser.handleText.

Handle Text.

parseback to summary
public void parse(Reader in, HTMLEditorKit.ParserCallback callback, boolean ignoreCharSet) throws IOException

Parse an HTML stream, given a DTD.

Parameters
in:Reader

the reader to read the source from

callback:HTMLEditorKit.ParserCallback

the callback

ignoreCharSet:boolean

if true the charset is ignored

Exceptions
IOException:
if an I/O error occurs