- java.lang.Object
-
- javax.swing.text.html.parser.Parser
-
- javax.swing.text.html.parser.DocumentParser
-
- All Implemented Interfaces:
- DTDConstants
public class DocumentParser extends Parser
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:
- handleStartTag(html, ...)
- handleStartTag(head, ...)
- handleEndTag(head)
- handleStartTag(body, ...)
- handleStartTag(p, ...)
- handleText(...)
- handleEndTag(p)
- handleEndTag(body)
- handleEndTag(html)
Boolean.TRUE
for the keyHTMLEditorKit.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. Similiar 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 attributeblah
does not have a value, there are two possible values that will be placed in the AttributeSet's value:- If the DTD does not contain an definition for the element, or the
definition does not have an explicit value then the value in the
AttributeSet will be
HTML.NULL_ATTRIBUTE_VALUE
. - If the DTD contains an explicit value, as in:
<!ATTLIST OPTION selected (selected) #IMPLIED>
this value from the dtd (in this case selected) will be used.
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.
-
-
Field Summary
-
Fields inherited from interface javax.swing.text.html.parser.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, SYSTEM
-
-
Constructor Summary
Constructors Constructor and Description DocumentParser(DTD dtd)
-
Method Summary
Methods Modifier and Type Method and Description protected void
handleComment(char[] text)
Called when an HTML comment is encountered.protected void
handleEmptyTag(TagElement tag)
Handle Empty Tag.protected void
handleEndTag(TagElement tag)
Handle End Tag.protected void
handleError(int ln, String errorMsg)
An error has occurred.protected void
handleStartTag(TagElement tag)
Handle Start Tag.protected void
handleText(char[] data)
Handle Text.void
parse(Reader in, HTMLEditorKit.ParserCallback callback, boolean ignoreCharSet)
-
Methods inherited from class javax.swing.text.html.parser.Parser
endTag, error, error, error, error, flushAttributes, getAttributes, getCurrentLine, getCurrentPos, handleEOFInComment, handleTitle, makeTag, makeTag, markFirstTime, parse, parseDTDMarkup, parseMarkupDeclarations, startTag
-
-
-
-
Constructor Detail
-
DocumentParser
public DocumentParser(DTD dtd)
-
-
Method Detail
-
parse
public void parse(Reader in, HTMLEditorKit.ParserCallback callback, boolean ignoreCharSet) throws IOException
- Throws:
IOException
-
handleStartTag
protected void handleStartTag(TagElement tag)
Handle Start Tag.- Overrides:
handleStartTag
in classParser
-
handleComment
protected void handleComment(char[] text)
Description copied from class:Parser
Called when an HTML comment is encountered.- Overrides:
handleComment
in classParser
-
handleEmptyTag
protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException
Handle Empty Tag.- Overrides:
handleEmptyTag
in classParser
- Throws:
ChangedCharSetException
-
handleEndTag
protected void handleEndTag(TagElement tag)
Handle End Tag.- Overrides:
handleEndTag
in classParser
-
handleText
protected void handleText(char[] data)
Handle Text.- Overrides:
handleText
in classParser
-
handleError
protected void handleError(int ln, String errorMsg)
Description copied from class:Parser
An error has occurred.- Overrides:
handleError
in classParser
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
22/12/2024 09:15:45 Cette version de la page est en cache (à la date du 22/12/2024 09:15:45) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 06/12/2006, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-javax/swing/text/html/parser/DocumentParser.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.