javax.swing.text.html

Class HTMLDocument

Implemented Interfaces:
Document, Serializable, StyledDocument

public class HTMLDocument
extends DefaultStyledDocument

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.

See Also:
Serialized Form

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_SIZE_DEFAULT, buffer

Fields inherited from class javax.swing.text.AbstractDocument

BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, ParagraphElementName, SectionElementName, listenerList

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(String id)
Returns the element that has the given id Attribute.
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.
Iterator<E>
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, create, createDefaultRoot, getBackground, getCharacterElement, getDefaultRootElement, getFont, getForeground, getLogicalStyle, getParagraphElement, getStyle, getStyleNames, insert, insertUpdate, removeDocumentListener, removeStyle, removeUpdate, setCharacterAttributes, setLogicalStyle, setParagraphAttributes, styleChanged

Methods inherited from class javax.swing.text.AbstractDocument

addDocumentListener, addUndoableEditListener, createBranchElement, createLeafElement, createPosition, dump, extends EventListener> T[] getListeners, fireChangedUpdate, fireInsertUpdate, fireRemoveUpdate, fireUndoableEditUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDefaultRootElement, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getParagraphElement, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, insertString, insertUpdate, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeDocumentListener, removeUndoableEditListener, removeUpdate, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlock

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

AdditionalComments

public static final String AdditionalComments
Document property key value. The value for the key will be a Vector of Strings that are comments not found in the body.
Field Value:
"AdditionalComments"

Constructor Details

HTMLDocument

public HTMLDocument()
Constructs an HTML document using the default buffer size and a default StyleSheet. This is a convenience method for the constructor HTMLDocument(Content, StyleSheet).
Usages and Demos :

View More Examples of HTMLDocument()
   1:         content = "<html><head><base href=\"" + new File(blogDir, "index.html").toURL() + "\"><style type=\"text/css\">body { font: Lucida Grande, Verdana, Arial; } h1 { font-size: 16px; } h6 { color: gray; font-weight: bold; font-size: 9px; }</style></head><body>" + content + "</body></html>";
   2:         HTMLDocument doc = new HTMLDocument();
   3:         doc.setBase(new File(blogDir, "index.html").toURL());
   4:         htmlPane.setContentType("text/html");
   5:         htmlPane.read(new StringReader(content), doc);

View Full Code Here
   1:         {
   2:           jTextPane1.read( inputStream, new HTMLDocument() );
   3:         }
   4:         catch( IOException ioe )
   5:         {

View Full Code Here
   1:         
   2:             HTMLDocument doc = new HTMLDocument();
   3:             try {
   4:                 doc.remove(0, doc.getLength());
   5:             } catch (BadLocationException e) {

View Full Code Here
   1:     protected JTextComponent createEditor() {
   2:         HTMLDocument doc = new HTMLDocument();
   3:         JTextPane p = new JTextPane(doc);
   4:         p.setEditorKit(new HTMLEditorKit());
   5:         p.setDragEnabled(true);

View Full Code Here

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 container for the content
styles - the styles

HTMLDocument

public HTMLDocument(StyleSheet styles)
Constructs an HTML document with the default content storage implementation and the specified style/attribute storage mechanism. This is a convenience method for the constructor HTMLDocument(Content, StyleSheet).
Parameters:
styles - the styles
Usages and Demos :

View More Examples of HTMLDocument(StyleSheet styles)
   1: 
   2:         HTMLDocument doc = new HTMLDocument(ss);
   3:         doc.setParser(new ParserDelegator());
   4:         doc.setAsynchronousLoadPriority(4);
   5:         doc.setTokenThreshold(100);

View Full Code Here

Method Details

create

protected void create(DefaultStyledDocument.ElementSpec[] data)
Replaces the contents of the document with the given element specifications. This is called before insert if the loading is done in bursts. This is the only method called if loading the document entirely in one burst.
Overrides:
create in interface DefaultStyledDocument
Parameters:
data - the new contents of the document

createBranchElement

protected Element createBranchElement(Element parent,
                                      AttributeSet a)
Creates a document branch element, that can contain other elements. This is implemented to return an element of type HTMLDocument.BlockElement.
Overrides:
createBranchElement in interface AbstractDocument
Parameters:
parent - the parent element
a - the attributes
Returns:
the element

createDefaultRoot

protected AbstractDocument.AbstractElement createDefaultRoot()
Creates the root element to be used to represent the default document structure.
Overrides:
createDefaultRoot in interface DefaultStyledDocument
Returns:
the element base

createLeafElement

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). This is implemented to return an element of type HTMLDocument.RunElement.
Overrides:
createLeafElement in interface AbstractDocument
Parameters:
parent - the parent element
a - the attributes for the element
p0 - the beginning of the range (must be at least 0)
p1 - the end of the range (must be at least p0)
Returns:
the new element

fireChangedUpdate

protected void fireChangedUpdate(DocumentEvent e)
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.
Overrides:
fireChangedUpdate in interface AbstractDocument
Parameters:
e - the event

fireUndoableEditUpdate

protected void fireUndoableEditUpdate(UndoableEditEvent e)
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.
Overrides:
fireUndoableEditUpdate in interface AbstractDocument
Parameters:
e - the event

getBase

public URL getBase()
Returns the location to resolve relative URLs against. By default this will be the document's URL if the document was loaded from a URL. If a base tag is found and can be parsed, it will be used as the base location.
Returns:
the base location

getElement

public Element getElement(String id)
Returns the element that has the given id Attribute. If the element can't be found, null is returned. Note that this method works on an Attribute, not a character tag. In the following HTML snippet: <a id="HelloThere"> the attribute is 'id' and the character tag is 'a'. This is a convenience method for getElement(RootElement, HTML.Attribute.id, id). This is not thread-safe.
Parameters:
id - the string representing the desired Attribute
Returns:
the element with the specified Attribute or null if it can't be found, or null if id is null
Since:
1.3

getElement

public 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. This is not thread-safe.
Parameters:
e - the root element where the search begins
attribute - the desired Attribute
value - the values for the specified Attribute
Returns:
the element with the specified Attribute and the specified value, or null if it can't be found
Since:
1.3

getIterator

public Iterator<E> getIterator(HTML.Tag t)
Fetches an iterator for the specified HTML tag. This can be used for things like iterating over the set of anchors contained, or iterating over the input elements.
Parameters:
t - the requested HTML.Tag
Returns:
the Iterator for the given HTML tag
See Also:
HTML.Tag
Usages and Demos :

View More Examples of getIterator(HTML.Tag t)
   1:     HTMLEditorKit htmlKit = new HTMLEditorKit();
   2:     HTMLDocument htmlDoc = (HTMLDocument)htmlKit.createDefaultDocument();
   3:     HTMLEditorKit.Parser parser = new ParserDelegator();
   4:         ...
   5: 
   6:     for (HTMLDocument.Iterator iterator = 
   7:         ...
   8:            htmlDoc.getIterator(HTML.Tag.A);
   9:          iterator.isValid();

View Full Code Here
   1: import javax.swing.text.html.HTML;
   2: import javax.swing.text.html.HTMLDocument;
   3: import java.awt.*;
   4:         ...
   5: 
   6:   private HTMLDocument.Iterator getIterator(HTML.Tag tag) {
   7:     Document doc = text.getDocument();
   8:         ...
   9:     if (doc instanceof HTMLDocument) {
  10:       HTMLDocument html = (HTMLDocument) doc;
  11:         ...
  12:       return html.getIterator(tag);

View Full Code Here

getParser

public HTMLEditorKit.Parser getParser()
Returns the parser that is used when inserting HTML into the existing document.
Returns:
the parser used for text insertion
Since:
1.3
Usages and Demos :

View More Examples of getParser()
   1:         HTMLEditorKit k = new HTMLEditorKit();
   2:         HTMLDocument doc = (HTMLDocument) k.createDefaultDocument();
   3:         ...
   4:         Parser parser = doc.getParser();
   5:         HrefConverter p = new HrefConverter(originalSuite);
   6:         doc.setAsynchronousLoadPriority(-1);
   7:         try {

View Full Code Here

getPreservesUnknownTags

public boolean getPreservesUnknownTags()
Returns the behavior the parser observes when encountering unknown tags.
Returns:
true if unknown tags are to be preserved when parsing
See Also:
HTML.Tag

getReader

public HTMLEditorKit.ParserCallback getReader(int pos)
Fetches the reader for the parser to use when loading the document with HTML. This is implemented to return an instance of HTMLDocument.HTMLReader. Subclasses can reimplement this method to change how the document gets structured if desired. (For example, to handle custom tags, or structurally represent character style elements.)
Parameters:
pos - the starting position
Returns:
the reader used by the parser to load the document

getReader

public 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. This is implemented to return an instance of HTMLDocument.HTMLReader. Subclasses can reimplement this method to change how the document gets structured if desired. (For example, to handle custom tags, or structurally represent character style elements.)

This is a convenience method for getReader(int, int, int, HTML.Tag, TRUE).

Parameters:
popDepth - the number of ElementSpec.EndTagTypes to generate before inserting
pushDepth - the number of ElementSpec.StartTagTypes with a direction of ElementSpec.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 used by the parser to load the document

getStyleSheet

public StyleSheet getStyleSheet()
Fetches the StyleSheet with the document-specific display rules (CSS) that were specified in the HTML document itself.
Returns:
the StyleSheet
Usages and Demos :

View More Examples of getStyleSheet()
   1: import javax.swing.text.StyleConstants;
   2: import javax.swing.text.html.HTMLDocument;
   3: import javax.swing.text.html.StyleSheet;
   4:         ...
   5:           EditorKit ek = html_.getEditorKitForContentType("text/html");
   6:           HTMLDocument dk = (HTMLDocument) ek.createDefaultDocument();
   7:           ek.read(new StringReader(_source), dk, 0);
   8:         ...
   9: 
  10:           updateStyles(dk.getStyleSheet());

View Full Code Here
   1: import javax.swing.text.View;
   2: import javax.swing.text.html.HTMLDocument;
   3: 
   4:         ...
   5:     try {
   6:       HTMLDocument doc = null;
   7:       if (component instanceof JEditorPane) {
   8:         ...
   9:         if (((JEditorPane)component).getDocument() instanceof HTMLDocument) {
  10:           doc = (HTMLDocument)((JEditorPane)component).getDocument();
  11:         ...
  12:       if (doc != null) {
  13:         doc.getStyleSheet().loadRules(new java.io.StringReader(stylesheet),

View Full Code Here
   1: import javax.swing.text.View;
   2: import javax.swing.text.html.HTMLDocument;
   3: 
   4:         ...
   5:     try {
   6:       HTMLDocument doc = null;
   7:       if (component instanceof JEditorPane) {        
   8:         ...
   9:         if (((JEditorPane)component).getDocument() instanceof HTMLDocument) {
  10:           doc = (HTMLDocument) ((JEditorPane)component).getDocument();
  11:         ...
  12:       if (doc != null) {
  13:         doc.getStyleSheet().loadRules(

View Full Code Here
   1: import javax.swing.event.ChangeListener;
   2: import javax.swing.text.html.HTMLDocument;
   3: import java.awt.*;
   4:         ...
   5: 
   6:                 HTMLDocument doc = (HTMLDocument)textcomp.getStyledDocument();
   7:         ...
   8:                 doc.getStyleSheet().loadRules(new StringReader(css.toString()), null);
   9:             }

View Full Code Here
   1:         EditorKit    ek=html_.getEditorKitForContentType("text/html");
   2:         HTMLDocument dk=(HTMLDocument)ek.createDefaultDocument();
   3:         ek.read(new StringReader(_source),dk,0);
   4:         ...
   5: 
   6:         updateStyles(dk.getStyleSheet());
   7: 
   8:         ...
   9:           EditorKit    ek=html_.getEditorKitForContentType("text/html");
  10:           HTMLDocument dk=(HTMLDocument)ek.createDefaultDocument();
  11:           dk.setBase(url_);
  12:         ...
  13:           
  14:           updateStyles(dk.getStyleSheet());

View Full Code Here

getTokenThreshold

public int getTokenThreshold()
Gets the number of tokens to buffer before trying to update the documents element structure. The default value is Integer.MAX_VALUE.
Returns:
the number of tokens to buffer

insert

protected void insert(int offset,
                      DefaultStyledDocument.ElementSpec[] data)
            throws BadLocationException
Inserts new elements in bulk. This is how elements get created in the document. The parsing determines what structure is needed and creates the specification as a set of tokens that describe the edit while leaving the document free of a write-lock. This method can then be called in bursts by the reader to acquire a write-lock for a shorter duration (i.e. while the document is actually being altered).
Overrides:
insert in interface DefaultStyledDocument
Parameters:
offset - the starting offset
data - the element data
Throws:
BadLocationException - if the given position does not represent a valid location in the associated document.

insertAfterEnd

public void insertAfterEnd(Element elem,
                           String htmlText)
            throws BadLocationException,
                   IOException
Inserts the HTML specified as a string after the the end of the given 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.

Parameters:
elem - the element to be the root for the new text
htmlText - the string to be parsed and assigned to elem
Throws:
IllegalStateException - if an HTMLEditorKit.Parser has not been set on the document
Since:
1.3

insertAfterStart

public void insertAfterStart(Element elem,
                             String htmlText)
            throws BadLocationException,
                   IOException
Inserts the HTML specified as a string at the start of the 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.

Parameters:
elem - the branch element to be the root for the new text
htmlText - the string to be parsed and assigned to elem
Throws:
IllegalStateException - if an HTMLEditorKit.Parser has not been set on the document
Since:
1.3

insertBeforeEnd

public void insertBeforeEnd(Element elem,
                            String htmlText)
            throws BadLocationException,
                   IOException
Inserts the HTML specified as a string at the end of the element.

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.

Parameters:
elem - the element to be the root for the new text
htmlText - the string to be parsed and assigned to elem
Throws:
IllegalStateException - if an HTMLEditorKit.Parser has not been set on the document
Since:
1.3
Usages and Demos :

View More Examples of insertBeforeEnd(Element elem,String htmlText)
   1: 
   2: import javax.swing.text.html.HTMLDocument;
   3: 
   4:         ...
   5: 
   6:       HTMLDocument doc =
   7:         ...
   8:         (HTMLDocument) new javax.swing.text.html.HTMLEditorKit().createDefaultDocument();
   9: 
  10:         ...
  11:                             + getResultString(_action.getState()) + "<br>");
  12:         doc.insertBeforeEnd(root, "<br><br>");

View Full Code Here
   1: 
   2: import javax.swing.text.html.HTMLDocument;
   3: import javax.swing.text.Element;
   4:         ...
   5: 
   6:       HTMLDocument doc =
   7:         ...
   8:         (HTMLDocument) new javax.swing.text.html.HTMLEditorKit().createDefaultDocument();
   9: 
  10:         ...
  11: 
  12:         doc.insertBeforeEnd(root, "<b>Test: </b>" + _test.getName() + "<br>");

View Full Code Here
   1: 
   2: import javax.swing.text.html.HTMLDocument;
   3: 
   4:         ...
   5: 
   6:       HTMLDocument doc =
   7:         ...
   8:         (HTMLDocument) new javax.swing.text.html.HTMLEditorKit().createDefaultDocument();
   9: 
  10:         ...
  11:         {
  12:           doc.insertBeforeEnd(root, "<b>Server: </b>" + serverStr + "<br>");

View Full Code Here
   1: import javax.swing.event.HyperlinkListener;
   2: import javax.swing.text.html.HTMLDocument;
   3: import javax.swing.text.html.HTMLEditorKit;
   4:         ...
   5: 
   6:     protected HTMLDocument doc;
   7:     protected ArrayList actionListeners;
   8:         ...
   9:         setEditorKit(htmlKit);
  10:         doc = (HTMLDocument)htmlKit.createDefaultDocument();
  11:         setStyledDocument(doc);
  12:         ...
  13:             Element body = doc.getElement("body");
  14:             doc.insertBeforeEnd(body,text);

View Full Code Here

insertBeforeStart

public void insertBeforeStart(Element elem,
                              String htmlText)
            throws BadLocationException,
                   IOException
Inserts the HTML specified as a string before the start of the given 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.

Parameters:
elem - the element to be the root for the new text
htmlText - the string to be parsed and assigned to elem
Throws:
IllegalStateException - if an HTMLEditorKit.Parser has not been set on the document
Since:
1.3

insertUpdate

protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng,
                            AttributeSet attr)
Updates document structure as a result of text insertion. This will happen within a write lock. This implementation simply parses the inserted content for line breaks and builds up a set of instructions for the element buffer.
Overrides:
insertUpdate in interface DefaultStyledDocument
Parameters:
chng - a description of the document change
attr - the attributes

processHTMLFrameHyperlinkEvent

public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent e)
Processes HyperlinkEvents that are generated by documents in an HTML frame. The HyperlinkEvent type, as the parameter suggests, is HTMLFrameHyperlinkEvent. In addition to the typical information contained in a HyperlinkEvent, this event contains the element that corresponds to the frame in which the click happened (the source element) and the target name. The target name has 4 possible values:
  • _self
  • _parent
  • _top
  • a named frame
If target is _self, the action is to change the value of the HTML.Attribute.SRC attribute and fires a ChangedUpdate event.

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.

Parameters:
e - the event
Usages and Demos :

View More Examples of processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent e)
   1:     HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
   2:     HTMLDocument doc = (HTMLDocument)pane.getDocument();
   3:         ...
   4:     doc.processHTMLFrameHyperlinkEvent(evt);
   5:     } else {
   6:     try {
   7:     pane.setPage(e.getURL());

View Full Code Here
   1: import javax.swing.event.HyperlinkListener;
   2: import javax.swing.text.html.HTMLDocument;
   3: import javax.swing.text.html.HTMLFrameHyperlinkEvent;
   4:         ...
   5:                             HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
   6:                             HTMLDocument doc = (HTMLDocument)pane.getDocument();
   7:         ...
   8:                             doc.processHTMLFrameHyperlinkEvent(evt);
   9:                         } else {

View Full Code Here
   1:         HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)he;
   2:         HTMLDocument doc = (HTMLDocument)pane.getDocument();
   3:         ...
   4:         doc.processHTMLFrameHyperlinkEvent(evt);
   5:       } else {
   6:         try {
   7:           pane.setPage(he.getURL());

View Full Code Here
   1: import javax.swing.event.HyperlinkListener;
   2: import javax.swing.text.html.HTMLDocument;
   3: import javax.swing.text.html.HTMLFrameHyperlinkEvent;
   4:         ...
   5:                     HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
   6:                     HTMLDocument doc = (HTMLDocument) pane.getDocument();
   7:         ...
   8:                     doc.processHTMLFrameHyperlinkEvent(evt);
   9:                 } else {

View Full Code Here
   1: import javax.swing.event.ListSelectionListener;
   2: import javax.swing.text.html.HTMLDocument;
   3: import javax.swing.text.html.HTMLFrameHyperlinkEvent;
   4:         ...
   5:             HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
   6:             HTMLDocument doc = (HTMLDocument) pane.getDocument();
   7:         ...
   8:             doc.processHTMLFrameHyperlinkEvent(evt);
   9:          }

View Full Code Here

setBase

public void setBase(URL u)
Sets the location to resolve relative URLs against. By default this will be the document's URL if the document was loaded from a URL. If a base tag is found and can be parsed, it will be used as the base location.

This also sets the base of the StyleSheet to be u as well as the base of the document.

Parameters:
u - the desired base URL
Usages and Demos :

View More Examples of setBase(URL u)
   1: import javax.swing.text.BadLocationException;
   2: import javax.swing.text.html.HTMLDocument;
   3: import javax.swing.text.html.HTMLEditorKit;
   4:         ...
   5: 
   6:     protected HTMLDocument createDefaultDocument(HTMLEditorKit kit) {
   7:         StyleSheet styles = kit.getStyleSheet();
   8:         ...
   9: 
  10:         HTMLDocument doc = new HTMLDocument(ss);
  11:         doc.setParser(new ParserDelegator());
  12:         ...
  13:         doc.putProperty("IgnoreCharsetDirective",Boolean.TRUE);
  14:         doc.setBase(collateralURL);

View Full Code Here
   1:         EditorKit    ek=html_.getEditorKitForContentType("text/html");
   2:         HTMLDocument dk=(HTMLDocument)ek.createDefaultDocument();
   3:         ek.read(new StringReader(_source),dk,0);
   4:         ...
   5:           EditorKit    ek=html_.getEditorKitForContentType("text/html");
   6:           HTMLDocument dk=(HTMLDocument)ek.createDefaultDocument();
   7:         ...
   8:           dk.setBase(url_);
   9:           dk.putProperty("IgnoreCharsetDirective", new Boolean(true));

View Full Code Here
   1: 
   2:     final HTMLDocument hd=(HTMLDocument)kit_.createDefaultDocument();
   3: 
   4:         ...
   5:       {
   6:         hd.setBase(_url);
   7:         hd.putProperty("IgnoreCharsetDirective",Boolean.TRUE);
   8: 
   9:         kit_.read

View Full Code Here

setInnerHTML

public void setInnerHTML(Element elem,
                         String htmlText)
            throws BadLocationException,
                   IOException
Replaces the children of the given element with the contents specified as an HTML string.

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.

Parameters:
elem - the branch element whose children will be replaced
htmlText - the string to be parsed and assigned to elem
Throws:
IllegalArgumentException - if elem is a leaf
IllegalStateException - if an HTMLEditorKit.Parser has not been defined
Since:
1.3
Usages and Demos :

View More Examples of setInnerHTML(Element elem,String htmlText)
   1: import javax.swing.text.Element;
   2: import javax.swing.text.html.HTMLDocument;
   3: import org.omegat.core.threads.CommandThread;
   4:         ...
   5:         {
   6:             HTMLDocument doc = (HTMLDocument) m_editorPane.getDocument();
   7:             int nts = CommandThread.core.getNumberofTranslatedSegments();
   8:         }
   9:         catch( Exception e ) { }

View Full Code Here

setOuterHTML

public void setOuterHTML(Element elem,
                         String htmlText)
            throws BadLocationException,
                   IOException
Replaces the given element in the parent with the contents specified as an HTML string.

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.

Parameters:
elem - the branch element whose children will be replaced
htmlText - the string to be parsed and assigned to elem
Throws:
IllegalStateException - if an HTMLEditorKit.Parser has not been set
Since:
1.3

setParagraphAttributes

public void setParagraphAttributes(int offset,
                                   int length,
                                   AttributeSet s,
                                   boolean replace)
Sets attributes for a paragraph.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Specified by:
setParagraphAttributes in interface StyledDocument
Overrides:
setParagraphAttributes in interface DefaultStyledDocument
Parameters:
offset - the offset into the paragraph (must be at least 0)
length - the number of characters affected (must be at least 0)
s - the attributes
replace - whether to replace existing attributes, or merge them
Usages and Demos :

View More Examples of setParagraphAttributes(int offset,int length,AttributeSet s,boolean replace)
   1: import javax.swing.text.html.HTML;
   2: import javax.swing.text.html.HTMLDocument;
   3: import javax.swing.text.html.HTMLEditorKit;
   4:         ...
   5:         Document doc = getDocument();
   6:         if (!(doc instanceof HTMLDocument)) {
   7:             return;
   8:         ...
   9:         }
  10:         HTMLDocument hdoc = (HTMLDocument)doc;
  11:         int start = getSelectionStart();
  12:         ...
  13: 
  14:         hdoc.setParagraphAttributes(start, end - start, newAttrs, true);

View Full Code Here

setParser

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

HTMLEditorKit.createDefaultDocument will set the parser for you. If you create an HTMLDocument by hand, be sure and set the parser accordingly.

Parameters:
parser - the parser to be used for text insertion
Since:
1.3

setPreservesUnknownTags

public void setPreservesUnknownTags(boolean preservesTags)
Determines how unknown tags are handled by the parser. If set to true, unknown tags are put in the model, otherwise they are dropped.
Parameters:
preservesTags - true if unknown tags should be saved in the model, otherwise tags are dropped
See Also:
HTML.Tag
Usages and Demos :

View More Examples of setPreservesUnknownTags(boolean preservesTags)
   1: import javax.swing.text.Document;
   2: import javax.swing.text.html.HTMLDocument;
   3: import javax.swing.text.html.HTMLEditorKit;
   4:         ...
   5:             editorPane.setEditorKit(new HTMLEditorKit());
   6:             HTMLDocument htmlDocument = (HTMLDocument)editorPane.getDocument();
   7:         ...
   8:             htmlDocument.setPreservesUnknownTags(true);

View Full Code Here

setTokenThreshold

public void setTokenThreshold(int n)
Sets the number of tokens to buffer before trying to update the documents element structure.
Parameters:
n - the number of tokens to buffer
Usages and Demos :

View More Examples of setTokenThreshold(int n)
   1: import javax.swing.text.BadLocationException;
   2: import javax.swing.text.html.HTMLDocument;
   3: import javax.swing.text.html.HTMLEditorKit;
   4:         ...
   5: 
   6:     protected HTMLDocument createDefaultDocument(HTMLEditorKit kit) {
   7:         StyleSheet styles = kit.getStyleSheet();
   8:         ...
   9: 
  10:         HTMLDocument doc = new HTMLDocument(ss);
  11:         doc.setParser(new ParserDelegator());
  12:         ...
  13:         doc.setAsynchronousLoadPriority(4);
  14:         doc.setTokenThreshold(100);

View Full Code Here