javax.swing.text.html
public class HTMLDocument extends DefaultStyledDocument
JEditorPane
after setting its
content type to "text/html". HTML document also serves as an intermediate
data structure when it is needed to parse HTML and then obtain the content of
the certain types of tags. This class also has methods for modifying the HTML
content.Modifier and Type | Class and Description |
---|---|
class |
HTMLDocument.BlockElement |
class |
HTMLDocument.HTMLReader
A reader to load an HTMLDocument with HTML structure.
|
static class |
HTMLDocument.Iterator
An iterator over a particular type of tag.
|
class |
HTMLDocument.RunElement
RunElement represents a section of text that has a set of
HTML character level attributes assigned to it.
|
DefaultStyledDocument.AttributeUndoableEdit, DefaultStyledDocument.ElementBuffer, DefaultStyledDocument.ElementSpec, DefaultStyledDocument.SectionElement
AbstractDocument.AbstractElement, AbstractDocument.AttributeContext, AbstractDocument.BranchElement, AbstractDocument.Content, AbstractDocument.DefaultDocumentEvent, AbstractDocument.ElementEdit, AbstractDocument.LeafElement
Modifier and Type | Field and Description |
---|---|
static String |
AdditionalComments
A key for document properies.
|
buffer, BUFFER_SIZE_DEFAULT
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
StreamDescriptionProperty, TitleProperty
Constructor and Description |
---|
HTMLDocument()
Constructs an HTML document using the default buffer size and a default
StyleSheet.
|
HTMLDocument(AbstractDocument.Content c,
StyleSheet styles)
Constructs an HTML document with the given content storage implementation
and the given style/attribute storage mechanism.
|
HTMLDocument(StyleSheet styles)
Constructs an HTML document with the default content storage
implementation and the specified style/attribute storage mechanism.
|
Modifier and Type | Method and Description |
---|---|
protected Element |
createBranchElement(Element parent,
AttributeSet a)
This method returns an HTMLDocument.BlockElement object representing the
attribute set a and attached to parent.
|
protected AbstractDocument.AbstractElement |
createDefaultRoot()
This method creates a root element for the new document.
|
protected Element |
createLeafElement(Element parent,
AttributeSet a,
int p0,
int p1)
This method returns an HTMLDocument.RunElement object attached to
parent representing a run of text from p0 to p1.
|
URL |
getBase()
Returns the location against which to resolve relative URLs.
|
Element |
getElement(Element e,
Object attribute,
Object value)
Gets the child element that contains the attribute with the value or null.
|
Element |
getElement(String attrId)
Returns the element that has the given id Attribute (for instance, <p id
='my paragraph >').
|
HTMLEditorKit.ParserCallback |
getInsertingReader(int pos,
int popDepth,
int pushDepth,
HTML.Tag insertTag,
Element parent)
Gets the reader for the parser to use when inserting the HTML fragment into
the document.
|
HTMLDocument.Iterator |
getIterator(HTML.Tag t)
Gets an iterator for the given HTML.Tag.
|
HTMLEditorKit.Parser |
getParser()
Returns the parser used by this HTMLDocument to insert HTML.
|
boolean |
getPreservesUnknownTags()
Returns whether or not the parser preserves unknown HTML tags.
|
HTMLEditorKit.ParserCallback |
getReader(int pos)
Gets the reader for the parser to use when loading the document with HTML.
|
HTMLEditorKit.ParserCallback |
getReader(int pos,
int popDepth,
int pushDepth,
HTML.Tag insertTag)
Gets the reader for the parser to use when loading the document with HTML.
|
StyleSheet |
getStyleSheet()
Gets the style sheet with the document display rules (CSS) that were specified
in the HTML document.
|
int |
getTokenThreshold()
Returns the number of tokens that are buffered before the document
is rendered.
|
void |
insertAfterEnd(Element elem,
String htmlText)
Inserts the string after the end of the given element.
|
void |
insertAfterStart(Element elem,
String htmlText)
Inserts the string at the start of the element.
|
void |
insertBeforeEnd(Element elem,
String htmlText)
Inserts the string at the end of the element.
|
void |
insertBeforeStart(Element elem,
String htmlText)
Inserts the string before the start of the given element.
|
protected void |
insertUpdate(AbstractDocument.DefaultDocumentEvent evt,
AttributeSet att)
Overridden to tag content with the synthetic HTML.Tag.CONTENT
tag.
|
void |
processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent event) |
void |
setBase(URL u)
Sets the location against which to resolve relative URLs.
|
void |
setInnerHTML(Element elem,
String htmlText)
Replaces the children of the given element with the contents of
the string.
|
void |
setOuterHTML(Element elem,
String htmlText)
Replaces the given element in the parent with the string.
|
void |
setParser(HTMLEditorKit.Parser p)
Sets the parser used by this HTMLDocument to insert HTML.
|
void |
setPreservesUnknownTags(boolean preservesTags)
Sets the behaviour of the parser when it encounters unknown HTML tags.
|
void |
setTokenThreshold(int n)
Sets the number of tokens to buffer before trying to display the
Document.
|
addStyle, create, getBackground, getCharacterElement, getDefaultRootElement, getFont, getForeground, getLogicalStyle, getParagraphElement, getStyle, getStyleNames, insert, removeStyle, removeUpdate, setCharacterAttributes, setLogicalStyle, setParagraphAttributes, styleChanged
addDocumentListener, addUndoableEditListener, createPosition, dump, fireChangedUpdate, fireInsertUpdate, fireRemoveUpdate, fireUndoableEditUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, insertString, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeDocumentListener, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlock
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addDocumentListener, addUndoableEditListener, createPosition, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, remove, removeDocumentListener, removeUndoableEditListener, render
public static final String AdditionalComments
public HTMLDocument()
public HTMLDocument(StyleSheet styles)
styles
- - the style sheetpublic HTMLDocument(AbstractDocument.Content c, StyleSheet styles)
c
- - the document's contentstyles
- - the style sheetpublic StyleSheet getStyleSheet()
protected AbstractDocument.AbstractElement createDefaultRoot()
createDefaultRoot
in class DefaultStyledDocument
protected Element createLeafElement(Element parent, AttributeSet a, int p0, int p1)
createLeafElement
in class AbstractDocument
parent
- - the parent elementa
- - the attributes for the elementp0
- - the beginning of the range >= 0p1
- - the end of the range >= p0AbstractDocument.LeafElement
protected Element createBranchElement(Element parent, AttributeSet a)
createBranchElement
in class AbstractDocument
parent
- - the parent elementa
- - the attributes for the elementAbstractDocument.BranchElement
public HTMLEditorKit.Parser getParser()
public void setParser(HTMLEditorKit.Parser p)
p
- the parser to usepublic void setTokenThreshold(int n)
n
- the number of tokens to bufferpublic int getTokenThreshold()
public URL getBase()
base
tag is found, it will be used.public void setBase(URL u)
u
- the new base URLpublic boolean getPreservesUnknownTags()
public void setPreservesUnknownTags(boolean preservesTags)
preservesTags
- true if the parser should preserve unknown tags.public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent event)
public HTMLDocument.Iterator getIterator(HTML.Tag t)
t
- the requested HTML.Tagpublic HTMLEditorKit.ParserCallback getReader(int pos)
pos
- - the starting positionpublic HTMLEditorKit.ParserCallback getReader(int pos, int popDepth, int pushDepth, HTML.Tag insertTag)
pos
- - the starting positionpopDepth
- - the number of EndTagTypes to generate before insertingpushDepth
- - the number of StartTagTypes with a direction
of JoinNextDirection that should be generated before inserting,
but after the end tags have been generated.insertTag
- - the first tag to start inserting into documentpublic HTMLEditorKit.ParserCallback getInsertingReader(int pos, int popDepth, int pushDepth, HTML.Tag insertTag, Element parent) throws IllegalStateException
pos
- - the starting positionpopDepth
- - the number of EndTagTypes to generate before insertingpushDepth
- - the number of StartTagTypes with a direction of
JoinNextDirection that should be generated before inserting, but
after the end tags have been generated.insertTag
- - the first tag to start inserting into documentparent
- the element that will be the parent in the document. HTML
parsing includes checks for the parent, so it must be available.IllegalStateException
- if the parsert is not set.public Element getElement(Element e, Object attribute, Object value)
e
- - the element to begin search atattribute
- - the desired attributevalue
- - the desired valuepublic Element getElement(String attrId)
attrId
- - the value of the attribute id to look forpublic void setInnerHTML(Element elem, String htmlText) throws BadLocationException, IOException
elem
- - the brance element whose children will be replacedhtmlText
- - the string to be parsed and assigned to element.BadLocationException
IOException
IllegalArgumentException
- - if elem is a leafIllegalStateException
- - if an HTMLEditorKit.Parser has not been setpublic void setOuterHTML(Element elem, String htmlText) throws BadLocationException, IOException
elem
- - the branch element whose parent will be replacedhtmlText
- - the string to be parsed and assigned to elemBadLocationException
IOException
IllegalStateException
- - if parser is not setpublic void insertBeforeStart(Element elem, String htmlText) throws BadLocationException, IOException
elem
- - the element to be the root for the new text.htmlText
- - the string to be parsed and assigned to elemBadLocationException
IOException
IllegalStateException
- - if parser has not been setpublic void insertBeforeEnd(Element elem, String htmlText) throws BadLocationException, IOException
elem
- - the element to be the root for the new texthtmlText
- - the text to insertBadLocationException
IOException
IllegalStateException
- - if parser is not setpublic void insertAfterEnd(Element elem, String htmlText) throws BadLocationException, IOException
elem
- - the element to be the root for the new texthtmlText
- - the text to insertBadLocationException
IOException
IllegalStateException
- - if parser is not setpublic void insertAfterStart(Element elem, String htmlText) throws BadLocationException, IOException
elem
- - the element to be the root for the new texthtmlText
- - the text to insertBadLocationException
IOException
IllegalStateException
- - if parser is not setprotected void insertUpdate(AbstractDocument.DefaultDocumentEvent evt, AttributeSet att)
insertUpdate
in class DefaultStyledDocument
evt
- the DocumentEvent
describing the changeatt
- the attributes for the change