javax.swing.text.html
Class HTMLDocument

java.lang.Object
  extended by javax.swing.text.AbstractDocument
      extended by javax.swing.text.DefaultStyledDocument
          extended by javax.swing.text.html.HTMLDocument
All Implemented Interfaces:
Serializable, Document, StyledDocument

public class HTMLDocument
extends DefaultStyledDocument

Represents the HTML document that is constructed by defining the text and other components (images, buttons, etc) in HTML language. This class can becomes the default document for 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.

See Also:
Serialized Form

Nested Class Summary
 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.
 
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
          A key for document properies.
 
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  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.
 
Methods inherited from class javax.swing.text.DefaultStyledDocument
addStyle, create, getBackground, getCharacterElement, getDefaultRootElement, getFont, getForeground, getLogicalStyle, getParagraphElement, getStyle, getStyleNames, insert, removeStyle, removeUpdate, setCharacterAttributes, setLogicalStyle, setParagraphAttributes, styleChanged
 
Methods inherited from class javax.swing.text.AbstractDocument
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
 
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
addDocumentListener, addUndoableEditListener, createPosition, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, remove, removeDocumentListener, removeUndoableEditListener, render
 

Field Detail

AdditionalComments

public static final String AdditionalComments
A key for document properies. The value for the key is a Vector of Strings of comments not found in the body.

See Also:
Constant Field Values
Constructor Detail

HTMLDocument

public HTMLDocument()
Constructs an HTML document using the default buffer size and a default StyleSheet.


HTMLDocument

public HTMLDocument(StyleSheet styles)
Constructs an HTML document with the default content storage implementation and the specified style/attribute storage mechanism.

Parameters:
styles - - the style sheet

HTMLDocument

public HTMLDocument(AbstractDocument.Content c,
                    StyleSheet styles)
Constructs an HTML document with the given content storage implementation and the given style/attribute storage mechanism.

Parameters:
c - - the document's content
styles - - the style sheet
Method Detail

getStyleSheet

public StyleSheet getStyleSheet()
Gets the style sheet with the document display rules (CSS) that were specified in the HTML document.

Returns:
- the style sheet

createDefaultRoot

protected AbstractDocument.AbstractElement createDefaultRoot()
This method creates a root element for the new document.

Overrides:
createDefaultRoot in class DefaultStyledDocument
Returns:
the new default root

createLeafElement

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. The run has attributes described by a.

Overrides:
createLeafElement in class AbstractDocument
Parameters:
parent - - the parent element
a - - the attributes for the element
p0 - - the beginning of the range >= 0
p1 - - the end of the range >= p0
Returns:
the new element
See Also:
AbstractDocument.LeafElement

createBranchElement

protected Element createBranchElement(Element parent,
                                      AttributeSet a)
This method returns an HTMLDocument.BlockElement object representing the attribute set a and attached to parent.

Overrides:
createBranchElement in class AbstractDocument
Parameters:
parent - - the parent element
a - - the attributes for the element
Returns:
the new element
See Also:
AbstractDocument.BranchElement

getParser

public HTMLEditorKit.Parser getParser()
Returns the parser used by this HTMLDocument to insert HTML.

Returns:
the parser used by this HTMLDocument to insert HTML.

setParser

public void setParser(HTMLEditorKit.Parser p)
Sets the parser used by this HTMLDocument to insert HTML.

Parameters:
p - the parser to use

setTokenThreshold

public void setTokenThreshold(int n)
Sets the number of tokens to buffer before trying to display the Document.

Parameters:
n - the number of tokens to buffer

getTokenThreshold

public int getTokenThreshold()
Returns the number of tokens that are buffered before the document is rendered.

Returns:
the number of tokens buffered

getBase

public URL getBase()
Returns the location against which to resolve relative URLs. This is the document's URL if the document was loaded from a URL. If a base tag is found, it will be used.

Returns:
the base URL

setBase

public void setBase(URL u)
Sets the location against which to resolve relative URLs.

Parameters:
u - the new base URL

getPreservesUnknownTags

public boolean getPreservesUnknownTags()
Returns whether or not the parser preserves unknown HTML tags.

Returns:
true if the parser preserves unknown tags

setPreservesUnknownTags

public void setPreservesUnknownTags(boolean preservesTags)
Sets the behaviour of the parser when it encounters unknown HTML tags.

Parameters:
preservesTags - true if the parser should preserve unknown tags.

processHTMLFrameHyperlinkEvent

public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent event)

getIterator

public HTMLDocument.Iterator getIterator(HTML.Tag t)
Gets an iterator for the given HTML.Tag.

Parameters:
t - the requested HTML.Tag
Returns:
the Iterator

getReader

public HTMLEditorKit.ParserCallback getReader(int pos)
Gets the reader for the parser to use when loading the document with HTML.

Parameters:
pos - - the starting position
Returns:
- the reader

getReader

public 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.

Parameters:
pos - - the starting position
popDepth - - the number of EndTagTypes to generate before inserting
pushDepth - - 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 document
Returns:
- the reader

getInsertingReader

public HTMLEditorKit.ParserCallback getInsertingReader(int pos,
                                                       int popDepth,
                                                       int pushDepth,
                                                       HTML.Tag insertTag,
                                                       Element parent)
                                                throws IllegalStateException
Gets the reader for the parser to use when inserting the HTML fragment into the document. Checks if the parser is present, sets the parent in the element stack and removes any actions for BODY (it can be only one body in a HTMLDocument).

Parameters:
pos - - the starting position
popDepth - - the number of EndTagTypes to generate before inserting
pushDepth - - 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 document
parent - the element that will be the parent in the document. HTML parsing includes checks for the parent, so it must be available.
Returns:
- the reader
Throws:
IllegalStateException - if the parsert is not set.

getElement

public Element getElement(Element e,
                          Object attribute,
                          Object value)
Gets the child element that contains the attribute with the value or null. Not thread-safe.

Parameters:
e - - the element to begin search at
attribute - - the desired attribute
value - - the desired value
Returns:
the element found with the attribute and value specified or null if it is not found.

getElement

public Element getElement(String attrId)
Returns the element that has the given id Attribute (for instance, <p id ='my paragraph >'). If it is not found, null is returned. The HTML tag, having this attribute, is not checked by this method and can be any. The method is not thread-safe.

Parameters:
attrId - - the value of the attribute id to look for
Returns:
the element that has the given id.

setInnerHTML

public void setInnerHTML(Element elem,
                         String htmlText)
                  throws BadLocationException,
                         IOException
Replaces the children of the given element with the contents of the string. The document must have an HTMLEditorKit.Parser set. This will be seen as at least two events, n inserts followed by a remove.

Parameters:
elem - - the brance element whose children will be replaced
htmlText - - the string to be parsed and assigned to element.
Throws:
BadLocationException
IOException
IllegalArgumentException - - if elem is a leaf
IllegalStateException - - if an HTMLEditorKit.Parser has not been set

setOuterHTML

public void setOuterHTML(Element elem,
                         String htmlText)
                  throws BadLocationException,
                         IOException
Replaces the given element in the parent with the string. 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. This will be seen as at least two events, n inserts followed by a remove. The HTMLEditorKit.Parser must be set.

Parameters:
elem - - the branch element whose parent will be replaced
htmlText - - the string to be parsed and assigned to elem
Throws:
BadLocationException
IOException
IllegalStateException - - if parser is not set

insertBeforeStart

public void insertBeforeStart(Element elem,
                              String htmlText)
                       throws BadLocationException,
                              IOException
Inserts the string before the start of the given element. The parser must be set.

Parameters:
elem - - the element to be the root for the new text.
htmlText - - the string to be parsed and assigned to elem
Throws:
BadLocationException
IOException
IllegalStateException - - if parser has not been set

insertBeforeEnd

public void insertBeforeEnd(Element elem,
                            String htmlText)
                     throws BadLocationException,
                            IOException
Inserts the string at the end of the element. If elem's children are leaves, and the character at elem.getEndOffset() - 1 is a newline, then it will be inserted before the newline. The parser must be set.

Parameters:
elem - - the element to be the root for the new text
htmlText - - the text to insert
Throws:
BadLocationException
IOException
IllegalStateException - - if parser is not set

insertAfterEnd

public void insertAfterEnd(Element elem,
                           String htmlText)
                    throws BadLocationException,
                           IOException
Inserts the string after the end of the given element. The parser must be set.

Parameters:
elem - - the element to be the root for the new text
htmlText - - the text to insert
Throws:
BadLocationException
IOException
IllegalStateException - - if parser is not set

insertAfterStart

public void insertAfterStart(Element elem,
                             String htmlText)
                      throws BadLocationException,
                             IOException
Inserts the string at the start of the element. The parser must be set.

Parameters:
elem - - the element to be the root for the new text
htmlText - - the text to insert
Throws:
BadLocationException
IOException
IllegalStateException - - if parser is not set

insertUpdate

protected void insertUpdate(AbstractDocument.DefaultDocumentEvent evt,
                            AttributeSet att)
Overridden to tag content with the synthetic HTML.Tag.CONTENT tag.

Overrides:
insertUpdate in class DefaultStyledDocument
Parameters:
evt - the DocumentEvent describing the change
att - the attributes for the change