java.lang.Object javax.swing.text.AbstractDocument javax.swing.text.DefaultStyledDocument javax.swing.text.html.HTMLDocument
public class HTMLDocument
A document that models HTML. The purpose of this model is to support both browsing and editing. As a result, the structure described by an HTML document is not exactly replicated by default. The element structure that is modeled by default, is built by the class HTMLDocument.HTMLReader, which implements the HTMLEditorKit.ParserCallback protocol that the parser expects. To change the structure one can subclass HTMLReader, and reimplement the method getReader(int) to return the new reader implementation. The documentation for HTMLReader should be consulted for the details of the default structure created. The intent is that the document be non-lossy (although reproducing the HTML format may result in a different format).
The document models only HTML, and makes no attempt to store view attributes in it. The elements are identified by the StyleContext.NameAttribute attribute, which should always have a value of type HTML.Tag that identifies the kind of element. Some of the elements (such as comments) are synthesized. The HTMLFactory uses this attribute to determine what kind of view to build.
This document supports incremental loading. The TokenThreshold property controls how much of the parse is buffered before trying to update the element structure of the document. This property is set by the EditorKit so that subclasses can disable it.
The Base property determines the URL against which relative URLs are resolved. By default, this will be the Document.StreamDescriptionProperty if the value of the property is a URL. If a <BASE> tag is encountered, the base will become the URL specified by that tag. Because the base URL is a property, it can of course be set directly.
The default content storage mechanism for this document is a gap buffer (GapContent). Alternatives can be supplied by using the constructor that takes a Content implementation.
Nested Class Summary | |
---|---|
class |
HTMLDocument.BlockElement
An element that represents a structural block of HTML. |
class |
HTMLDocument.HTMLReader
An HTML reader to load an HTML document with an HTML element structure. |
static class |
HTMLDocument.Iterator
An iterator to iterate over a particular type of tag. |
class |
HTMLDocument.RunElement
An element that represents a chunk of text that has a set of HTML character level attributes assigned to it. |
Nested classes/interfaces inherited from class javax.swing.text. DefaultStyledDocument |
---|
DefaultStyledDocument.AttributeUndoableEdit , DefaultStyledDocument.ElementBuffer , DefaultStyledDocument.ElementSpec , DefaultStyledDocument.SectionElement |
Nested classes/interfaces inherited from class javax.swing.text. AbstractDocument |
---|
AbstractDocument.AbstractElement , AbstractDocument.AttributeContext , AbstractDocument.BranchElement , AbstractDocument.Content , AbstractDocument.DefaultDocumentEvent , AbstractDocument.ElementEdit , AbstractDocument.LeafElement |
Field Summary | |
---|---|
static String |
AdditionalComments
Document property key value. |
Fields inherited from class javax.swing.text. DefaultStyledDocument |
---|
buffer , BUFFER_SIZE_DEFAULT |
Fields inherited from class javax.swing.text. AbstractDocument |
---|
BAD_LOCATION , BidiElementName , ContentElementName , ElementNameAttribute , listenerList , ParagraphElementName , SectionElementName |
Fields inherited from interface javax.swing.text. Document |
---|
StreamDescriptionProperty , TitleProperty |
Constructor Summary | |
---|---|
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. |
Method Summary | |
---|---|
protected void |
create
(
DefaultStyledDocument.ElementSpec
[] data) Replaces the contents of the document with the given element specifications. |
protected Element |
createBranchElement
(
Element
parent,
AttributeSet
a) Creates a document branch element, that can contain other elements. |
protected AbstractDocument.AbstractElement |
createDefaultRoot
() Creates the root element to be used to represent the default document structure. |
protected Element |
createLeafElement
(
Element
parent,
AttributeSet
a, int p0, int p1) Creates a document leaf element that directly represents text (doesn't have any children). |
protected void |
fireChangedUpdate
(
DocumentEvent
e) Notifies all listeners that have registered interest for notification on this event type. |
protected void |
fireUndoableEditUpdate
(
UndoableEditEvent
e) Notifies all listeners that have registered interest for notification on this event type. |
URL |
getBase
() Returns the location to resolve relative URLs against. |
Element |
getElement
(
Element
e,
Object
attribute,
Object
value) Returns the child element of e that contains the attribute, attribute with value value, or null if one isn't found. |
Element |
getElement
(
String
id) Returns the element that has the given id Attribute. |
HTMLDocument.Iterator |
getIterator
(
HTML.Tag
t) Fetches an iterator for the specified HTML tag. |
HTMLEditorKit.Parser |
getParser
() Returns the parser that is used when inserting HTML into the existing document. |
boolean |
getPreservesUnknownTags
() Returns the behavior the parser observes when encountering unknown tags. |
HTMLEditorKit.ParserCallback |
getReader
(int pos) Fetches 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) Returns the reader for the parser to use to load the document with HTML. |
StyleSheet |
getStyleSheet
() Fetches the StyleSheet with the document-specific display rules (CSS) that were specified in the HTML document itself. |
int |
getTokenThreshold
() Gets the number of tokens to buffer before trying to update the documents element structure. |
protected void |
insert
(int offset,
DefaultStyledDocument.ElementSpec
[] data) Inserts new elements in bulk. |
void |
insertAfterEnd
(
Element
elem,
String
htmlText) Inserts the HTML specified as a string after the the end of the given element. |
void |
insertAfterStart
(
Element
elem,
String
htmlText) Inserts the HTML specified as a string at the start of the element. |
void |
insertBeforeEnd
(
Element
elem,
String
htmlText) Inserts the HTML specified as a string at the end of the element. |
void |
insertBeforeStart
(
Element
elem,
String
htmlText) Inserts the HTML specified as a string before the start of the given element. |
protected void |
insertUpdate
(
AbstractDocument.DefaultDocumentEvent
chng,
AttributeSet
attr) Updates document structure as a result of text insertion. |
void |
processHTMLFrameHyperlinkEvent
(
HTMLFrameHyperlinkEvent
e) Processes HyperlinkEvents that are generated by documents in an HTML frame. |
void |
setBase
(
URL
u) Sets the location to resolve relative URLs against. |
void |
setInnerHTML
(
Element
elem,
String
htmlText) Replaces the children of the given element with the contents specified as an HTML string. |
void |
setOuterHTML
(
Element
elem,
String
htmlText) Replaces the given element in the parent with the contents specified as an HTML string. |
void |
setParagraphAttributes
(int offset, int length,
AttributeSet
s, boolean replace) Sets attributes for a paragraph. |
void |
setParser
(
HTMLEditorKit.Parser
parser) Sets the parser that is used by the methods that insert html into the existing document, such as setInnerHTML, and setOuterHTML. |
void |
setPreservesUnknownTags
(boolean preservesTags) Determines how unknown tags are handled by the parser. |
void |
setTokenThreshold
(int n) Sets the number of tokens to buffer before trying to update the documents element structure. |
Methods inherited from class javax.swing.text. DefaultStyledDocument |
---|
addDocumentListener , addStyle , getBackground , getCharacterElement , getDefaultRootElement , getFont , getForeground , getLogicalStyle , getParagraphElement , getStyle , getStyleNames , removeDocumentListener , removeStyle , removeUpdate , setCharacterAttributes , setLogicalStyle , styleChanged |
Methods inherited from class javax.swing.text. AbstractDocument |
---|
addUndoableEditListener , createPosition , dump , fireInsertUpdate , fireRemoveUpdate , getAsynchronousLoadPriority , getAttributeContext , getBidiRootElement , getContent , getCurrentWriter , getDocumentFilter , getDocumentListeners , getDocumentProperties , getEndPosition , getLength , getListeners , getProperty , getRootElements , getStartPosition , getText , getText , getUndoableEditListeners , insertString , postRemoveUpdate , putProperty , readLock , readUnlock , remove , removeUndoableEditListener , render , replace , setAsynchronousLoadPriority , setDocumentFilter , setDocumentProperties , writeLock , writeUnlock |
Methods inherited from class java.lang. Object |
---|
clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait |
Methods inherited from interface javax.swing.text. Document |
---|
addUndoableEditListener , createPosition , getEndPosition , getLength , getProperty , getRootElements , getStartPosition , getText , getText , insertString , putProperty , remove , removeUndoableEditListener , render |
Field Detail |
---|
public static final String AdditionalComments
Constructor Detail |
---|
public HTMLDocument()
public HTMLDocument(StyleSheet styles)
public HTMLDocument(AbstractDocument.Content c, StyleSheet styles)
Method Detail |
---|
public HTMLEditorKit.ParserCallback getReader(int pos)
public HTMLEditorKit.ParserCallback getReader(int pos, int popDepth, int pushDepth, HTML.Tag insertTag)
This is a convenience method for getReader(int, int, int, HTML.Tag, TRUE).
public URL getBase()
public void setBase(URL u)
This also sets the base of the StyleSheet to be u as well as the base of the document.
protected void insert(int offset, DefaultStyledDocument.ElementSpec[] data) throws BadLocationException
protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr)
protected void create(DefaultStyledDocument.ElementSpec[] data)
public void setParagraphAttributes(int offset, int length, AttributeSet s, boolean replace)
This method is thread safe, although most Swing methods are not. Please see
How to Use Threads
Threads and Swing
for more information.
public StyleSheet getStyleSheet()
public HTMLDocument.Iterator getIterator(HTML.Tag t)
protected Element createLeafElement(Element parent, AttributeSet a, int p0, int p1)
protected Element createBranchElement(Element parent, AttributeSet a)
protected AbstractDocument.AbstractElement createDefaultRoot()
public void setTokenThreshold(int n)
public int getTokenThreshold()
public void setPreservesUnknownTags(boolean preservesTags)
public boolean getPreservesUnknownTags()
public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent e)
If the target is _parent, then it deletes the parent element, which is a <FRAMESET> element, and inserts a new <FRAME> element, and sets its HTML.Attribute.SRC attribute to have a value equal to the destination URL and fire a RemovedUpdate and InsertUpdate.
If the target is _top, this method does nothing. In the implementation of the view for a frame, namely the FrameView, the processing of _top is handled. Given that _top implies replacing the entire document, it made sense to handle this outside of the document that it will replace.
If the target is a named frame, then the element hierarchy is searched for an element with a name equal to the target, its HTML.Attribute.SRC attribute is updated and a ChangedUpdate event is fired.
public void setParser(HTMLEditorKit.Parser parser)
HTMLEditorKit.createDefaultDocument will set the parser for you. If you create an HTMLDocument by hand, be sure and set the parser accordingly.
public HTMLEditorKit.Parser getParser()
public void setInnerHTML(Element elem, String htmlText) throws BadLocationException, IOException
This will be seen as at least two events, n inserts followed by a remove.
For this to work correcty, the document must have an HTMLEditorKit.Parser set. This will be the case if the document was created from an HTMLEditorKit via the createDefaultDocument method.
public void setOuterHTML(Element elem, String htmlText) throws BadLocationException, IOException
This will be seen as at least two events, n inserts followed by a remove.
When replacing a leaf this will attempt to make sure there is a newline present if one is needed. This may result in an additional element being inserted. Consider, if you were to replace a character element that contained a newline with <img> this would create two elements, one for the image, ane one for the newline.
If you try to replace the element at length you will most likely end up with two elements, eg setOuterHTML(getCharacterElement (getLength()), "blah") will result in two leaf elements at the end, one representing 'blah', and the other representing the end element.
For this to work correcty, the document must have an HTMLEditorKit.Parser set. This will be the case if the document was created from an HTMLEditorKit via the createDefaultDocument method.
public void insertAfterStart(Element elem, String htmlText) throws BadLocationException, IOException
For this to work correcty, the document must have an HTMLEditorKit.Parser set. This will be the case if the document was created from an HTMLEditorKit via the createDefaultDocument method.
public void insertBeforeEnd(Element elem, String htmlText) throws BadLocationException, IOException
If elem's children are leaves, and the character at a elem.getEndOffset() - 1 is a newline, this will insert before the newline so that there isn't text after the newline.
For this to work correcty, the document must have an HTMLEditorKit.Parser set. This will be the case if the document was created from an HTMLEditorKit via the createDefaultDocument method.
public void insertBeforeStart(Element elem, String htmlText) throws BadLocationException, IOException
For this to work correcty, the document must have an HTMLEditorKit.Parser set. This will be the case if the document was created from an HTMLEditorKit via the createDefaultDocument method.
public void insertAfterEnd(Element elem, String htmlText) throws BadLocationException, IOException
For this to work correcty, the document must have an HTMLEditorKit.Parser set. This will be the case if the document was created from an HTMLEditorKit via the createDefaultDocument method.
public Element getElement(String id)
public Element getElement(Element e, Object attribute, Object value)
protected void fireChangedUpdate(DocumentEvent e)
protected void fireUndoableEditUpdate(UndoableEditEvent e)