HTMLDocument
public class HTMLDocument.HTMLReader extends HTMLEditorKit.ParserCallback
The reader can be configured by registering actions (of type HTMLDocument.HTMLReader.TagAction
) that describe how to handle the action. The idea behind the actions provided is that the most natural text editing operations can be provided if the element structure boils down to paragraphs with runs of some kind of style in them. Some things are more naturally specified structurally, so arbitrary structure should be allowed above the paragraphs, but will need to be edited with structural actions. The implication of this is that some of the HTML elements specified in the stream being parsed will be collapsed into attributes, and in some cases paragraphs will be synthesized. When HTML elements have been converted to attributes, the attribute key will be of type HTML.Tag, and the value will be of type AttributeSet so that no information is lost. This enables many of the existing actions to work so that the user can type input, hit the return key, backspace, delete, etc and have a reasonable result. Selections can be created, and attributes applied or removed, etc. With this in mind, the work done by the reader can be categorized into the following kinds of tasks:
Currently, <APPLET>, <PARAM>, <MAP>, <AREA>, <LINK>, <SCRIPT> and <STYLE> are unsupported.
The assignment of the actions described is shown in the following table for the tags defined in HTML.Tag
.
Tag | Action |
---|---|
HTML.Tag.A |
CharacterAction |
HTML.Tag.ADDRESS |
CharacterAction |
HTML.Tag.APPLET |
HiddenAction |
HTML.Tag.AREA |
AreaAction |
HTML.Tag.B |
CharacterAction |
HTML.Tag.BASE |
BaseAction |
HTML.Tag.BASEFONT |
CharacterAction |
HTML.Tag.BIG |
CharacterAction |
HTML.Tag.BLOCKQUOTE |
BlockAction |
HTML.Tag.BODY |
BlockAction |
HTML.Tag.BR |
SpecialAction |
HTML.Tag.CAPTION |
BlockAction |
HTML.Tag.CENTER |
BlockAction |
HTML.Tag.CITE |
CharacterAction |
HTML.Tag.CODE |
CharacterAction |
HTML.Tag.DD |
BlockAction |
HTML.Tag.DFN |
CharacterAction |
HTML.Tag.DIR |
BlockAction |
HTML.Tag.DIV |
BlockAction |
HTML.Tag.DL |
BlockAction |
HTML.Tag.DT |
ParagraphAction |
HTML.Tag.EM |
CharacterAction |
HTML.Tag.FONT |
CharacterAction |
HTML.Tag.FORM |
As of 1.4 a BlockAction |
HTML.Tag.FRAME |
SpecialAction |
HTML.Tag.FRAMESET |
BlockAction |
HTML.Tag.H1 |
ParagraphAction |
HTML.Tag.H2 |
ParagraphAction |
HTML.Tag.H3 |
ParagraphAction |
HTML.Tag.H4 |
ParagraphAction |
HTML.Tag.H5 |
ParagraphAction |
HTML.Tag.H6 |
ParagraphAction |
HTML.Tag.HEAD |
HeadAction |
HTML.Tag.HR |
SpecialAction |
HTML.Tag.HTML |
BlockAction |
HTML.Tag.I |
CharacterAction |
HTML.Tag.IMG |
SpecialAction |
HTML.Tag.INPUT |
FormAction |
HTML.Tag.ISINDEX |
IsndexAction |
HTML.Tag.KBD |
CharacterAction |
HTML.Tag.LI |
BlockAction |
HTML.Tag.LINK |
LinkAction |
HTML.Tag.MAP |
MapAction |
HTML.Tag.MENU |
BlockAction |
HTML.Tag.META |
MetaAction |
HTML.Tag.NOFRAMES |
BlockAction |
HTML.Tag.OBJECT |
SpecialAction |
HTML.Tag.OL |
BlockAction |
HTML.Tag.OPTION |
FormAction |
HTML.Tag.P |
ParagraphAction |
HTML.Tag.PARAM |
HiddenAction |
HTML.Tag.PRE |
PreAction |
HTML.Tag.SAMP |
CharacterAction |
HTML.Tag.SCRIPT |
HiddenAction |
HTML.Tag.SELECT |
FormAction |
HTML.Tag.SMALL |
CharacterAction |
HTML.Tag.STRIKE |
CharacterAction |
HTML.Tag.S |
CharacterAction |
HTML.Tag.STRONG |
CharacterAction |
HTML.Tag.STYLE |
StyleAction |
HTML.Tag.SUB |
CharacterAction |
HTML.Tag.SUP |
CharacterAction |
HTML.Tag.TABLE |
BlockAction |
HTML.Tag.TD |
BlockAction |
HTML.Tag.TEXTAREA |
FormAction |
HTML.Tag.TH |
BlockAction |
HTML.Tag.TITLE |
TitleAction |
HTML.Tag.TR |
BlockAction |
HTML.Tag.TT |
CharacterAction |
HTML.Tag.U |
CharacterAction |
HTML.Tag.UL |
BlockAction |
HTML.Tag.VAR |
CharacterAction |
Once </html> is encountered, the Actions are no longer notified.
Modifier and Type | Class | Description |
---|---|---|
class |
HTMLDocument.HTMLReader.BlockAction |
Action assigned by default to handle the Block task of the reader. |
class |
HTMLDocument.HTMLReader.CharacterAction |
Action assigned by default to handle the Character task of the reader. |
class |
HTMLDocument.HTMLReader.FormAction |
Action to support forms by building all of the elements used to represent form controls. |
class |
HTMLDocument.HTMLReader.HiddenAction |
Action assigned by default to handle the Hidden task of the reader. |
class |
HTMLDocument.HTMLReader.IsindexAction |
Action assigned by default to handle the Isindex task of the reader. |
class |
HTMLDocument.HTMLReader.ParagraphAction |
Action assigned by default to handle the Paragraph task of the reader. |
class |
HTMLDocument.HTMLReader.PreAction |
Action assigned by default to handle the Pre block task of the reader. |
class |
HTMLDocument.HTMLReader.SpecialAction |
Action assigned by default to handle the Special task of the reader. |
class |
HTMLDocument.HTMLReader.TagAction |
An action to be performed in response to parsing a tag. |
Modifier and Type | Field | Description |
---|---|---|
protected MutableAttributeSet |
charAttr |
Current character attribute set. |
protected Vector |
parseBuffer |
Buffer to keep building elements. |
IMPLIED
Constructor | Description |
---|---|
HTMLReader |
Constructs an HTMLReader using default pop and push depth and no tag to insert. |
HTMLReader |
Constructs an HTMLReader. |
Modifier and Type | Method | Description |
---|---|---|
protected void |
addContent |
Adds some text with the current character attributes. |
protected void |
addContent |
Adds some text with the current character attributes. |
protected void |
addSpecialElement |
Adds content that is basically specified entirely in the attribute set. |
protected void |
blockClose |
Adds an instruction to the parse buffer to close out a block element of the given type. |
protected void |
blockOpen |
Adds an instruction to the parse buffer to create a block element with the given attributes. |
void |
flush() |
The last method called on the reader. |
void |
handleEndOfLineString |
This is invoked after the stream has been parsed, but before flush . |
void |
handleEndTag |
Callback from the parser. |
void |
handleSimpleTag |
Callback from the parser. |
void |
handleStartTag |
Callback from the parser. |
void |
handleText |
Called by the parser to indicate a block of text was encountered. |
protected void |
popCharacterStyle() |
Pops a previously pushed character style off the stack to return to a previous style. |
protected void |
preContent |
Adds the given content that was encountered in a PRE element. |
protected void |
pushCharacterStyle() |
Pushes the current character style on a stack in preparation for forming a new nested character style. |
protected void |
registerTag |
Registers a handler for the given tag. |
protected void |
textAreaContent |
Adds the given content to the textarea document. |
handleComment, handleError
protected Vector<DefaultStyledDocument.ElementSpec> parseBuffer
protected MutableAttributeSet charAttr
public HTMLReader(int offset)
offset
- the starting offsetpublic HTMLReader(int offset, int popDepth, int pushDepth, HTML.Tag insertTag)
offset
- the starting offsetpopDepth
- how many parents to ascend before insert new elementpushDepth
- how many parents to descend (relative to popDepth) before insertinginsertTag
- a tag to insert (may be null)public void flush() throws BadLocationException
flush
in class HTMLEditorKit.ParserCallback
BadLocationException
- if the given position does not represent a valid location in the associated document.public void handleText(char[] data, int pos)
handleText
in class HTMLEditorKit.ParserCallback
data
- a datapos
- a positionpublic void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos)
handleStartTag
in class HTMLEditorKit.ParserCallback
t
- an HTML taga
- a set of attributespos
- a positionpublic void handleEndTag(HTML.Tag t, int pos)
handleEndTag
in class HTMLEditorKit.ParserCallback
t
- an HTML tagpos
- a positionpublic void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
handleSimpleTag
in class HTMLEditorKit.ParserCallback
t
- an HTML taga
- a set of attributespos
- a positionpublic void handleEndOfLineString(String eol)
flush
. eol
will be one of \n, \r or \r\n, which ever is encountered the most in parsing the stream.handleEndOfLineString
in class HTMLEditorKit.ParserCallback
eol
- value of eolprotected void registerTag(HTML.Tag t, HTMLDocument.HTMLReader.TagAction a)
t
- an HTML taga
- tag action handlerprotected void pushCharacterStyle()
protected void popCharacterStyle()
protected void textAreaContent(char[] data)
data
- the given contentprotected void preContent(char[] data)
data
- the given contentprotected void blockOpen(HTML.Tag t, MutableAttributeSet attr)
t
- an HTML tagattr
- the attribute setprotected void blockClose(HTML.Tag t)
t
- the HTML tagprotected void addContent(char[] data, int offs, int length)
data
- the content to addoffs
- the initial offsetlength
- the lengthprotected void addContent(char[] data, int offs, int length, boolean generateImpliedPIfNecessary)
data
- the content to addoffs
- the initial offsetlength
- the lengthgenerateImpliedPIfNecessary
- whether to generate implied paragraphsprotected void addSpecialElement(HTML.Tag t, MutableAttributeSet a)
t
- an HTML taga
- the attribute set
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/javax/swing/text/html/HTMLDocument.HTMLReader.html