Spec-Zone .ru
спецификации, руководства, описания, API
JavaTM 2 Platform
Standard Edition

javax.swing.text.html
Class HTMLDocument

java.lang.Object
  |
  +--javax.swing.text.AbstractDocument
        |
        +--javax.swing.text.DefaultStyledDocument
              |
              +--javax.swing.text.html.HTMLDocument

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 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 are (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 URL's are resolved against. 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

Inner Class Summary
 class HTMLDocument.BlockElement
          An element that represents a structual 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.
 
Inner classes inherited from class javax.swing.text.DefaultStyledDocument
DefaultStyledDocument.AttributeUndoableEdit, DefaultStyledDocument.ElementBuffer, DefaultStyledDocument.ElementSpec, DefaultStyledDocument.SectionElement
 
Inner classes 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
 
Constructor Summary
HTMLDocument()
          Constructs an html document.
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 given 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()
          Get the location to resolve relative url's against.
 HTMLDocument.Iterator getIterator(HTML.Tag t)
          Fetch an iterator for the following kind of html tag.
 boolean getPreservesUnknownTags()
           
 HTMLEditorKit.ParserCallback getReader(int pos)
          Fetch the reader for the parser to use to load the document with html.
 HTMLEditorKit.ParserCallback getReader(int pos, int popDepth, int pushDepth, HTML.Tag insertTag)
          Fetch the reader for the parser to use to load the document with html.
 StyleSheet getStyleSheet()
          Fetch the StyleSheet with the document-specific display rules(CSS) that were specified in the html document itself.
 int getTokenThreshold()
          Get 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.
protected  void insertUpdate(AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr)
          Updates document structure as a result of text insertion.
 void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent e)
          This method is responsible for processing HyperlinkEvent's that are generated by documents in an HTML frame.
 void setBase(URL u)
          Set the location to resolve relative url's against.
 void setParagraphAttributes(int offset, int length, AttributeSet s, boolean replace)
          Sets attributes for a paragraph.
 void setPreservesUnknownTags(boolean preservesTags)
          Sets how unknown tags are handled.
 void setTokenThreshold(int n)
          Set 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, getDocumentProperties, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeUndoableEditListener, render, setAsynchronousLoadPriority, setDocumentProperties, writeLock, writeUnlock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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.
Constructor Detail

HTMLDocument

public HTMLDocument()
Constructs an html document.

HTMLDocument

public HTMLDocument(StyleSheet styles)
Constructs an html document with the default content storage implementation and the given style/attribute storage mechanism.
Parameters:
styles - the styles

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
Method Detail

getReader

public HTMLEditorKit.ParserCallback getReader(int pos)
Fetch 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 get structured if desired (e.g. to handle custom tags, structurally represent character style elements, etc.).

getReader

public HTMLEditorKit.ParserCallback getReader(int pos,
                                              int popDepth,
                                              int pushDepth,
                                              HTML.Tag insertTag)
Fetch 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 get structured if desired (e.g. to handle custom tags, structurally represent character style elements, etc.).
Parameters:
popDepth - number of ElementSpec.EndTagType to generate before inserting.
pushDepth - number of ElementSpec.StartTagType with a direction of ElementSpec.JoinNextDirection that should be generated before inserting, but after the end tags have been generated.
insertTag - first tag to start inserting into document.

getBase

public URL getBase()
Get the location to resolve relative url's against. By default this will be the documents 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.

setBase

public void setBase(URL u)
Set the location to resolve relative url's against. By default this will be the documents 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.

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 class DefaultStyledDocument
Parameters:
offset - the starting offset
Throws:
BadLocationException - for an invalid starting offset
BadLocationException - if the given position does not represent a valid location in the associated document.
See Also:
StyledDocument#insert

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 class DefaultStyledDocument
Parameters:
chng - a description of the document change
attr - the attributes

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 class DefaultStyledDocument

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.

Overrides:
setParagraphAttributes in class DefaultStyledDocument
Parameters:
offset - the offset into the paragraph >= 0
length - the number of characters affected >= 0
s - the attributes
replace - whether to replace existing attributes, or merge them

getStyleSheet

public StyleSheet getStyleSheet()
Fetch the StyleSheet with the document-specific display rules(CSS) that were specified in the html document itself.

getIterator

public HTMLDocument.Iterator getIterator(HTML.Tag t)
Fetch an iterator for the following kind of html tag. This can be used for things like iterating over the set of anchors contained, iterating over the input elements, etc.

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

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 class 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 class DefaultStyledDocument
Returns:
the element base

setTokenThreshold

public void setTokenThreshold(int n)
Set the number of tokens to buffer before trying to update the documents element structure.

getTokenThreshold

public int getTokenThreshold()
Get the number of tokens to buffer before trying to update the documents element structure. By default, this will be Integer.MAX_VALUE.

setPreservesUnknownTags

public void setPreservesUnknownTags(boolean preservesTags)
Sets how unknown tags are handled. If set to true, unknown tags are put in the model, otherwise they are dropped.

getPreservesUnknownTags

public boolean getPreservesUnknownTags()
Returns:
true if unknown tags are to be preserved when parsing.

processHTMLFrameHyperlinkEvent

public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent e)
This method is responsible for processing HyperlinkEvent's 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, (i.e. the source element) and the target name. The target name has 4 possible values: 1) _self 2) _parent 3) _top 4) 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 element, and inserts a new element and sets its HTML.Attribute.SRC attribute to have a value equal to the destination url and fire an 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:
HTMLFrameHyperLinkEvent -  

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 class AbstractDocument
Parameters:
e - the event
See Also:
EventListenerList

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 class AbstractDocument
Parameters:
e - the event
See Also:
EventListenerList

JavaTM 2 Platform
Standard Edition

Submit a bug or feature
Java, Java 2D, and JDBC are a trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1999 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.