- java.lang.Object
-
- javax.swing.text.html.HTMLEditorKit.ParserCallback
-
- javax.swing.text.html.HTMLDocument.HTMLReader
-
- Enclosing class:
- HTMLDocument
public class HTMLDocument.HTMLReader extends HTMLEditorKit.ParserCallback
An HTML reader to load an HTML document with an HTML element structure. This is a set of callbacks from the parser, implemented to create a set of elements tagged with attributes. The parse builds up tokens (ElementSpec) that describe the element subtree desired, and burst it into the document under the protection of a write lock using the insert method on the document outer class.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:- Block
- Build the structure like it's specified in the stream. This produces elements that contain other elements.
- Paragraph
- Like block except that it's expected that the element will be used with a paragraph view so a paragraph element won't need to be synthesized.
- Character
- Contribute the element as an attribute that will start and stop at arbitrary text locations. This will ultimately be mixed into a run of text, with all of the currently flattened HTML character elements.
- Special
- Produce an embedded graphical element.
- Form
- Produce an element that is like the embedded graphical element, except that it also has a component model associated with it.
- Hidden
- Create an element that is hidden from view when the document is being viewed read-only, and visible when the document is being edited. This is useful to keep the model from losing information, and used to store things like comments and unrecognized tags.
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.HTML tags and assigned actions Tag Action HTML.Tag.ACharacterAction HTML.Tag.ADDRESSCharacterAction HTML.Tag.APPLETHiddenAction HTML.Tag.AREAAreaAction HTML.Tag.BCharacterAction HTML.Tag.BASEBaseAction HTML.Tag.BASEFONTCharacterAction HTML.Tag.BIGCharacterAction HTML.Tag.BLOCKQUOTEBlockAction HTML.Tag.BODYBlockAction HTML.Tag.BRSpecialAction HTML.Tag.CAPTIONBlockAction HTML.Tag.CENTERBlockAction HTML.Tag.CITECharacterAction HTML.Tag.CODECharacterAction HTML.Tag.DDBlockAction HTML.Tag.DFNCharacterAction HTML.Tag.DIRBlockAction HTML.Tag.DIVBlockAction HTML.Tag.DLBlockAction HTML.Tag.DTParagraphAction HTML.Tag.EMCharacterAction HTML.Tag.FONTCharacterAction HTML.Tag.FORMAs of 1.4 a BlockAction HTML.Tag.FRAMESpecialAction HTML.Tag.FRAMESETBlockAction HTML.Tag.H1ParagraphAction HTML.Tag.H2ParagraphAction HTML.Tag.H3ParagraphAction HTML.Tag.H4ParagraphAction HTML.Tag.H5ParagraphAction HTML.Tag.H6ParagraphAction HTML.Tag.HEADHeadAction HTML.Tag.HRSpecialAction HTML.Tag.HTMLBlockAction HTML.Tag.ICharacterAction HTML.Tag.IMGSpecialAction HTML.Tag.INPUTFormAction HTML.Tag.ISINDEXIsndexAction HTML.Tag.KBDCharacterAction HTML.Tag.LIBlockAction HTML.Tag.LINKLinkAction HTML.Tag.MAPMapAction HTML.Tag.MENUBlockAction HTML.Tag.METAMetaAction HTML.Tag.NOFRAMESBlockAction HTML.Tag.OBJECTSpecialAction HTML.Tag.OLBlockAction HTML.Tag.OPTIONFormAction HTML.Tag.PParagraphAction HTML.Tag.PARAMHiddenAction HTML.Tag.PREPreAction HTML.Tag.SAMPCharacterAction HTML.Tag.SCRIPTHiddenAction HTML.Tag.SELECTFormAction HTML.Tag.SMALLCharacterAction HTML.Tag.STRIKECharacterAction HTML.Tag.SCharacterAction HTML.Tag.STRONGCharacterAction HTML.Tag.STYLEStyleAction HTML.Tag.SUBCharacterAction HTML.Tag.SUPCharacterAction HTML.Tag.TABLEBlockAction HTML.Tag.TDBlockAction HTML.Tag.TEXTAREAFormAction HTML.Tag.THBlockAction HTML.Tag.TITLETitleAction HTML.Tag.TRBlockAction HTML.Tag.TTCharacterAction HTML.Tag.UCharacterAction HTML.Tag.ULBlockAction HTML.Tag.VARCharacterAction Once </html> is encountered, the Actions are no longer notified.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classHTMLDocument.HTMLReader.BlockActionAction assigned by default to handle the Block task of the reader.classHTMLDocument.HTMLReader.CharacterActionAction assigned by default to handle the Character task of the reader.classHTMLDocument.HTMLReader.FormActionAction to support forms by building all of the elements used to represent form controls.classHTMLDocument.HTMLReader.HiddenActionAction assigned by default to handle the Hidden task of the reader.classHTMLDocument.HTMLReader.IsindexActionAction assigned by default to handle the Isindex task of the reader.classHTMLDocument.HTMLReader.ParagraphActionAction assigned by default to handle the Paragraph task of the reader.classHTMLDocument.HTMLReader.PreActionAction assigned by default to handle the Pre block task of the reader.classHTMLDocument.HTMLReader.SpecialActionAction assigned by default to handle the Special task of the reader.classHTMLDocument.HTMLReader.TagActionAn action to be performed in response to parsing a tag.
-
Field Summary
Fields Modifier and Type Field Description protected MutableAttributeSetcharAttrCurrent character attribute set.protected Vector<DefaultStyledDocument.ElementSpec>parseBufferBuffer to keep building elements.-
Fields declared in class javax.swing.text.html.HTMLEditorKit.ParserCallback
IMPLIED
-
-
Constructor Summary
Constructors Constructor Description HTMLReader(int offset)Constructs an HTMLReader using default pop and push depth and no tag to insert.HTMLReader(int offset, int popDepth, int pushDepth, HTML.Tag insertTag)Constructs an HTMLReader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddContent(char[] data, int offs, int length)Adds some text with the current character attributes.protected voidaddContent(char[] data, int offs, int length, boolean generateImpliedPIfNecessary)Adds some text with the current character attributes.protected voidaddSpecialElement(HTML.Tag t, MutableAttributeSet a)Adds content that is basically specified entirely in the attribute set.protected voidblockClose(HTML.Tag t)Adds an instruction to the parse buffer to close out a block element of the given type.protected voidblockOpen(HTML.Tag t, MutableAttributeSet attr)Adds an instruction to the parse buffer to create a block element with the given attributes.voidflush()The last method called on the reader.voidhandleEndOfLineString(String eol)This is invoked after the stream has been parsed, but beforeflush.voidhandleEndTag(HTML.Tag t, int pos)Callback from the parser.voidhandleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)Callback from the parser.voidhandleStartTag(HTML.Tag t, MutableAttributeSet a, int pos)Callback from the parser.voidhandleText(char[] data, int pos)Called by the parser to indicate a block of text was encountered.protected voidpopCharacterStyle()Pops a previously pushed character style off the stack to return to a previous style.protected voidpreContent(char[] data)Adds the given content that was encountered in a PRE element.protected voidpushCharacterStyle()Pushes the current character style on a stack in preparation for forming a new nested character style.protected voidregisterTag(HTML.Tag t, HTMLDocument.HTMLReader.TagAction a)Registers a handler for the given tag.protected voidtextAreaContent(char[] data)Adds the given content to the textarea document.-
Methods declared in class javax.swing.text.html.HTMLEditorKit.ParserCallback
handleComment, handleError
-
-
-
-
Field Detail
-
parseBuffer
protected Vector<DefaultStyledDocument.ElementSpec> parseBuffer
Buffer to keep building elements.
-
charAttr
protected MutableAttributeSet charAttr
Current character attribute set.
-
-
Constructor Detail
-
HTMLReader
public HTMLReader(int offset)
Constructs an HTMLReader using default pop and push depth and no tag to insert.- Parameters:
offset- the starting offset
-
HTMLReader
public HTMLReader(int offset, int popDepth, int pushDepth, HTML.Tag insertTag)Constructs an HTMLReader.- Parameters:
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)
-
-
Method Detail
-
flush
public void flush() throws BadLocationExceptionThe last method called on the reader. It allows any pending changes to be flushed into the document. Since this is currently loading synchronously, the entire set of changes are pushed in at this point.- Overrides:
flushin classHTMLEditorKit.ParserCallback- Throws:
BadLocationException- if the given position does not represent a valid location in the associated document.
-
handleText
public void handleText(char[] data, int pos)Called by the parser to indicate a block of text was encountered.- Overrides:
handleTextin classHTMLEditorKit.ParserCallback- Parameters:
data- a datapos- a position
-
handleStartTag
public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos)
Callback from the parser. Route to the appropriate handler for the tag.- Overrides:
handleStartTagin classHTMLEditorKit.ParserCallback- Parameters:
t- an HTML taga- a set of attributespos- a position
-
handleEndTag
public void handleEndTag(HTML.Tag t, int pos)
Callback from the parser. Route to the appropriate handler for the tag.- Overrides:
handleEndTagin classHTMLEditorKit.ParserCallback- Parameters:
t- an HTML tagpos- a position
-
handleSimpleTag
public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
Callback from the parser. Route to the appropriate handler for the tag.- Overrides:
handleSimpleTagin classHTMLEditorKit.ParserCallback- Parameters:
t- an HTML taga- a set of attributespos- a position
-
handleEndOfLineString
public void handleEndOfLineString(String eol)
This is invoked after the stream has been parsed, but beforeflush.eolwill be one of \n, \r or \r\n, which ever is encountered the most in parsing the stream.- Overrides:
handleEndOfLineStringin classHTMLEditorKit.ParserCallback- Parameters:
eol- value of eol- Since:
- 1.3
-
registerTag
protected void registerTag(HTML.Tag t, HTMLDocument.HTMLReader.TagAction a)
Registers a handler for the given tag. By default all of the well-known tags will have been registered. This can be used to change the handling of a particular tag or to add support for custom tags.- Parameters:
t- an HTML taga- tag action handler
-
pushCharacterStyle
protected void pushCharacterStyle()
Pushes the current character style on a stack in preparation for forming a new nested character style.
-
popCharacterStyle
protected void popCharacterStyle()
Pops a previously pushed character style off the stack to return to a previous style.
-
textAreaContent
protected void textAreaContent(char[] data)
Adds the given content to the textarea document. This method gets called when we are in a textarea context. Therefore all text that is seen belongs to the text area and is hence added to the TextAreaDocument associated with the text area.- Parameters:
data- the given content
-
preContent
protected void preContent(char[] data)
Adds the given content that was encountered in a PRE element. This synthesizes lines to hold the runs of text, and makes calls to addContent to actually add the text.- Parameters:
data- the given content
-
blockOpen
protected void blockOpen(HTML.Tag t, MutableAttributeSet attr)
Adds an instruction to the parse buffer to create a block element with the given attributes.- Parameters:
t- an HTML tagattr- the attribute set
-
blockClose
protected void blockClose(HTML.Tag t)
Adds an instruction to the parse buffer to close out a block element of the given type.- Parameters:
t- the HTML tag
-
addContent
protected void addContent(char[] data, int offs, int length)Adds some text with the current character attributes.- Parameters:
data- the content to addoffs- the initial offsetlength- the length
-
addContent
protected void addContent(char[] data, int offs, int length, boolean generateImpliedPIfNecessary)Adds some text with the current character attributes.- Parameters:
data- the content to addoffs- the initial offsetlength- the lengthgenerateImpliedPIfNecessary- whether to generate implied paragraphs
-
addSpecialElement
protected void addSpecialElement(HTML.Tag t, MutableAttributeSet a)
Adds content that is basically specified entirely in the attribute set.- Parameters:
t- an HTML taga- the attribute set
-
-