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.
In addition to the methods provided by Document and StyledDocument for mutating an HTMLDocument, HTMLDocument provides a number of convenience methods. The following methods can be used to insert HTML content into an existing document.
setInnerHTML(Element, String)setOuterHTML(Element, String)insertBeforeStart(Element, String)insertAfterStart(Element, String)insertBeforeEnd(Element, String)insertAfterEnd(Element, String)The following examples illustrate using these methods. Each example assumes the HTML document is initialized in the following way:
JEditorPane p = new JEditorPane();
p.setContentType("text/html");
p.setText("..."); // Document text is provided below.
HTMLDocument d = (HTMLDocument) p.getDocument();
With the following HTML content:
<html>
<head>
<title>An example HTMLDocument</title>
<style type="text/css">
div { background-color: silver; }
ul { color: blue; }
</style>
</head>
<body>
<div id="BOX">
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</div>
</body>
</html>
All the methods for modifying an HTML document require an Element. Elements can be obtained from an HTML document by using
the method getElement(Element e, Object attribute, Object
value). It returns the first descendant element that contains the
specified attribute with the given value, in depth-first order.
For example, d.getElement(d.getDefaultRootElement(),
StyleConstants.NameAttribute, HTML.Tag.P) returns the first
paragraph element.
A convenient shortcut for locating elements is the method getElement(String); returns an element whose ID
attribute matches the specified value. For example,
d.getElement("BOX") returns the DIV
element.
The getIterator(HTML. method can also be used for
finding all occurrences of the specified HTML tag in the
document.
Elements can be inserted before or after the existing children
of any non-leaf element by using the methods
insertAfterStart and insertBeforeEnd.
For example, if e is the DIV element,
d.insertAfterStart(e, "<ul><li>List
Item</li></ul>") inserts the list before the first
paragraph, and d.insertBeforeEnd(e, "<ul><li>List
Item</li></ul>") inserts the list after the last
paragraph. The DIV block becomes the parent of the
newly inserted elements.
Sibling elements can be inserted before or after any element by
using the methods insertBeforeStart and
insertAfterEnd. For example, if e is the
DIV element, d.insertBeforeStart(e,
"<ul><li>List Item</li></ul>") inserts the list
before the DIV element, and d.insertAfterEnd(e,
"<ul><li>List Item</li></ul>") inserts the list
after the DIV element. The newly inserted elements
become siblings of the DIV element.
Elements and all their descendants can be replaced by using the
methods setInnerHTML and setOuterHTML.
For example, if e is the DIV element,
d.setInnerHTML(e, "<ul><li>List
Item</li></ul>") replaces all children paragraphs with
the list, and d.setOuterHTML(e, "<ul><li>List
Item</li></ul>") replaces the DIV element
itself. In latter case the parent of the list is the
BODY element.
The following table shows the example document and the results of various methods described above.
| Example | insertAfterStart |
insertBeforeEnd |
insertBeforeStart |
insertAfterEnd |
setInnerHTML |
setOuterHTML |
|---|---|---|---|---|---|---|
|
Paragraph 1 Paragraph 2 |
Paragraph 1 Paragraph 2 |
Paragraph 1 Paragraph 2
|
Paragraph 1 Paragraph 2 |
Paragraph 1 Paragraph 2
|
|
|
Warning
Serialized objects of this class will
not be compatible with future Swing releases. The current
serialization support is appropriate for short term storage or RMI
between applications running the same version of Swing. As of 1.4,
support for long term storage of all JavaBeans
has been added to the
java.beans package. Please see java..
| Modifier and Type | Class and Description |
|---|---|
| public class | HTMLDocument.
An element that represents a structural block of HTML. |
| private static class | HTMLDocument.
Document that allows you to set the maximum length of the text. |
| public class | HTMLDocument.
An HTML reader to load an HTML document with an HTML element structure. |
| public abstract static class | HTMLDocument.
An iterator to iterate over a particular type of tag. |
| pack-priv static class | HTMLDocument.
An iterator to iterate over a particular type of tag. |
| public class | HTMLDocument.
An element that represents a chunk of text that has a set of HTML character level attributes assigned to it. |
| pack-priv static class | HTMLDocument.
Used by StyleSheet to determine when to avoid removing HTML.Tags matching StyleConstants. |
| Modifier and Type | Field and Description |
|---|---|
| public static final String | AdditionalComments
Document property key value. |
| pack-priv URL | base
The location to resolve relative URLs against. |
| private String | baseTarget
BASE tag's TARGET attribute value |
| private static AttributeSet | contentAttributeSet
Used for inserts when a null AttributeSet is supplied. |
| private boolean | |
| pack-priv boolean | hasBaseTag
does the document have base tag |
| private static final String | I18NProperty
I18N property key.
|
| private boolean | insertInBody
Indicates that direct insertion to body section takes place. |
| pack-priv static String | MAP_PROPERTY
Property Maps are registered under, will be a Hashtable. |
| private static final int | |
| private static char[] | |
| private HTMLEditorKit. | parser
The parser that is used when inserting html into the existing document. |
| private boolean | |
| private HashMap | |
| private static final int | |
| pack-priv static final String | StyleType
Document property key value. |
| pack-priv static final String | TokenThreshold
Document property for the number of tokens to buffer before building an element subtree to represent them. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | HTMLDocument(StyleSheet
the styles styles)Constructs an HTML document with the default content storage implementation and the specified style/attribute storage mechanism. |
| public | HTMLDocument(AbstractDocument.
the container for the content c,the styles styles)Constructs an HTML document with the given content storage implementation and the given style/attribute storage mechanism. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| protected void | create(DefaultStyledDocument.
the new contents of the document dataOverrides javax. Replaces the contents of the document with the given element specifications. |
| protected Element | Returns: the elementthe parent element parent, AttributeSet the attributes a)Overrides javax. Creates a document branch element, that can contain other elements. |
| protected AbstractDocument. | Returns: the element baseOverrides javax. Creates the root element to be used to represent the default document structure. |
| protected Element | Returns: the new elementthe parent element parent, AttributeSet the attributes for the element a, int the beginning of the range (must be at least 0) p0, int the end of the range (must be at least p0) p1)Overrides javax. Creates a document leaf element that directly represents text (doesn't have any children). |
| private Element | |
| protected void | fireChangedUpdate(DocumentEvent
the event e)Overrides javax. Notifies all listeners that have registered interest for notification on this event type. |
| protected void | fireUndoableEditUpdate(UndoableEditEvent
the event e)Overrides javax. Notifies all listeners that have registered interest for notification on this event type. |
| public URL | |
| pack-priv String | |
| pack-priv String | Returns: the content type language used for the style sheetsReturns the content type language used for style sheets. |
| public Element | Returns: the element with the specifiedAttribute
or null if it can't be found,
or null if id is nullthe string representing the desired id)AttributeReturns the element that has the given id |
| public Element | Returns: the element with the specifiedAttribute
and the specified value, or null
if it can't be foundthe root element where the search begins e, Object the desired attribute, Object Attributethe values for the specified value)AttributeReturns the child element of |
| private Element | Returns: the element with the specifiedAttribute
and the specified value, or null
if it can't be foundthe root element where the search begins e, Object the desired attribute, Object Attributethe values for the specified value, boolean searchLeafAttributes)AttributeReturns the child element of |
| public HTMLDocument. | Returns: theIterator for the given HTML tagthe requested tHTML.TagFetches an iterator for the specified HTML tag. |
| pack-priv Map | |
| pack-priv Enumeration | Returns: the enumerated list of maps, ornull
if the maps are not an instance of HashtableReturns an |
| public HTMLEditorKit. | Returns: the parser used for text insertionReturns the parser that is used when inserting HTML into the existing document. |
| public boolean | Returns: true if unknown tags are to be preserved when parsingReturns the behavior the parser observes when encountering unknown tags. |
| public HTMLEditorKit. | Returns: the reader used by the parser to load the documentthe starting position pos)Fetches the reader for the parser to use when loading the document with HTML. |
| public HTMLEditorKit. | Returns: the reader used by the parser to load the documentthe starting position pos, int the number of popDepth, int ElementSpec.EndTagTypes
to generate before insertingthe number of pushDepth, HTML.ElementSpec.StartTagTypes
with a direction of ElementSpec.JoinNextDirection
that should be generated before inserting,
but after the end tags have been generatedthe first tag to start inserting into document insertTagReturns the reader for the parser to use to load the document with HTML. |
| pack-priv HTMLEditorKit. | Returns: the reader used by the parser to load the documentthe number of popDepth, int ElementSpec.EndTagTypes
to generate before insertingthe number of pushDepth, HTML.ElementSpec.StartTagTypes
with a direction of ElementSpec.JoinNextDirection
that should be generated before inserting,
but after the end tags have been generatedthe first tag to start inserting into document insertTag,false if all the Elements after insertTag should
be inserted; otherwise insertTag will be inserted insertInsertTag)Fetches the reader for the parser to use to load the document with HTML. |
| public StyleSheet | Returns: theStyleSheetFetches the |
| public int | Returns: the number of tokens to bufferGets the number of tokens to buffer before trying to update the documents element structure. |
| pack-priv boolean | |
| protected void | insert(int
the starting offset offset, DefaultStyledDocument.the element data dataOverrides javax. Inserts new elements in bulk. |
| public void | insertAfterEnd(Element
the element the content is inserted after elem, String the string to be parsed and inserted after htmlText)elemInserts the HTML specified as a string after the end of the given element. |
| public void | insertAfterStart(Element
the branch element to be the root for the new text elem, String the string to be parsed and assigned to htmlText)elemInserts the HTML specified as a string at the start of the element. |
| public void | insertBeforeEnd(Element
the element to be the root for the new text elem, String the string to be parsed and assigned to htmlText)elemInserts the HTML specified as a string at the end of the element. |
| public void | insertBeforeStart(Element
the element the content is inserted before elem, String the string to be parsed and inserted before htmlText)elemInserts the HTML specified as a string before the start of the given element. |
| private void | insertHTML(Element parent, int offset, String html, boolean wantsTrailingNewline)
Inserts a string of HTML into the document at the given position. |
| protected void | insertUpdate(AbstractDocument.
a description of the document change chng,the attributes attr)Overrides javax. Updates document structure as a result of text insertion. |
| private void | |
| pack-priv boolean | Returns: true if document will be viewed in a frame, otherwise falseReturns true if the document will be viewed in a frame. |
| pack-priv static boolean | Returns: true if there is a match, false otherwisethe attributes to be matched attr, HTML.the value to be matched tagReturns true if |
| pack-priv void | |
| public void | processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent
the event e)Processes |
| pack-priv void | |
| private void | |
| private void | removeElements(Element e, int index, int count, int start, int end)
Called to remove child Elements when the end is not touched. |
| private void | removeElementsAtEnd(Element e, int index, int count, int start, int end)
Called to remove child elements of |
| pack-priv void | |
| private void | replace(AbstractDocument.
This is used by |
| public void | |
| pack-priv void | setDefaultStyleSheetType(String
the content type language for the style sheets contentType)Sets the content type language used for style sheets that do not explicitly specify the type. |
| pack-priv void | setFrameDocumentState(boolean
true if the document will be viewed in a frame,
otherwise false frameDoc)Sets a boolean state about whether the document will be viewed in a frame. |
| public void | setInnerHTML(Element
the branch element whose children will be replaced elem, String the string to be parsed and assigned to htmlText)elemReplaces the children of the given element with the contents specified as an HTML string. |
| public void | setOuterHTML(Element
the element to replace elem, String the string to be parsed and inserted in place of htmlText)elemReplaces the given element in the parent with the contents specified as an HTML string. |
| public void | setParagraphAttributes(int
the offset into the paragraph (must be at least 0) offset, int the number of characters affected (must be at least 0) length, AttributeSet the attributes s, boolean whether to replace existing attributes, or merge them replace)Overrides javax. Implements javax. Sets attributes for a paragraph. |
| public void | setParser(HTMLEditorKit.
the parser to be used for text insertion parserSets the parser that is used by the methods that insert html
into the existing document, such as |
| public void | setPreservesUnknownTags(boolean
true if unknown tags should be
saved in the model, otherwise tags are dropped preservesTags)Determines how unknown tags are handled by the parser. |
| public void | setTokenThreshold(int
the number of tokens to buffer n)Sets the number of tokens to buffer before trying to update the documents element structure. |
| private void | updateFrame(Element
a FRAME element whose SRC attribute will be updated element, String a string specifying the new value for the SRC attribute url)Updates the Frame elements |
| private void | updateFrameSet(Element
the frameset element to remove element, String the value for the SRC attribute for the
new frame that will replace the frameset url)Replaces a frameset branch Element with a frame leaf element. |
| private void |