public class HTMLDocument extends DefaultStyledDocument
HTMLDocument.HTMLReader建成,实现,预计
HTMLEditorKit.ParserCallback协议分析器。改变结构的一类
HTMLReader,和执行方法
getReader(int)返回新的读写器的实现。对于
HTMLReader文件应为细节的默认结构创建咨询。其意图是,文件是无损的(虽然复制HTML格式可能会导致不同的格式)。
文档模型的HTML,而没有尝试存储视图属性中。元素是由StyleContext.NameAttribute属性标识,这应该有一个值类型HTML.Tag标识元素种类。合成的一些元素(如评论)。的HTMLFactory使用此属性确定什么样的视图建立。
本文档支持增量加载。的TokenThreshold属性控制多少的解析缓存之前试图更新文件中的元件结构。这个属性是由EditorKit使子类可以禁用它。
Base属性确定的URL与相对URL解析。默认情况下,这将是Document.StreamDescriptionProperty如果属性的值是一个URL。如果遇到了一个“基地”标签,该基地将成为该标签指定的网址。因为基础的网址是一个属性,它可以直接设置。
默认的内容存储机制,这个文件是一个间隙缓冲(GapContent)。替代品可以使用,以Content执行构造函数提供。
除了通过文件和styleddocument提供变异的HTML文档的方法,这提供了一些便利的方法。可以使用以下方法插入HTML内容到一个已经存在的文件。
setInnerHTML(Element, String)setOuterHTML(Element, String)insertBeforeStart(Element, String)insertAfterStart(Element, String)insertBeforeEnd(Element, String)insertAfterEnd(Element, String)下面的例子说明了使用这些方法。每个示例HTML文档是以下面的方式初始化:
jeditorpane P =新jeditorpane();p.setcontenttype(“text/html”);p.settext(“…”);/ /文档文本的下方。这a(这getdocument())P.;
下面的HTML内容:
<
<标题>
例HtmlDocument
<风格类型=“文本 css”>
div的背景颜色:银;} {UL {颜色:红色;}< /样式>< /头>
<鱼>
第1段:第段第2段:第段< / DIV >< /身体>< / HTML >
鱼>
风格类型=“文本>
标题>
所有的方法修改HTML文档需要Element。元素可以采用的方法getElement(Element e, Object attribute, Object value)从HTML文档中获得。它返回包含给定值的指定属性的第一个子代元素,一个深度优先顺序。例如,d.getElement(d.getDefaultRootElement(), StyleConstants.NameAttribute, HTML.Tag.P)返回第一个段落元素。
定位元件的方便快捷的方法是getElement(String);返回一个元素的属性指定的值相匹配ID。例如,d.getElement("BOX")返回DIV元。
的getIterator(HTML.Tag t)方法也可以用于寻找指定的HTML文档中的所有事件。
元素能被插入之前或之后的任何现有的孩子非叶单元采用的方法insertAfterStart和insertBeforeEnd。例如,如果e是DIV元,d.insertAfterStart(e, "<ul><li>List Item</li></ul>")插入列表的第一款前,和d.insertBeforeEnd(e, "<ul><li>List Item</li></ul>")插入列表的最后一段后。的DIV块成为新插入的元素的父元素。
同级元素可以插入之前或使用方法insertBeforeStart和insertAfterEnd任何元素之后。例如,如果e是DIV元,d.insertBeforeStart(e, "<ul><li>List Item</li></ul>")插入列表的DIV元素之前,和d.insertAfterEnd(e, "<ul><li>List Item</li></ul>")插入列表的DIV元后。新插入的元素成为了DIV元的兄弟姐妹。
元素和他们的后代可以通过使用方法setInnerHTML和setOuterHTML取代。例如,如果e是DIV元,d.setInnerHTML(e, "<ul><li>List Item</li></ul>")替换所有孩子段落列表,和d.setOuterHTML(e, "<ul><li>List Item</li></ul>")取代DIV元素本身。在后一种情况下的列表的父是BODY元。
下表显示了上面所述的示例文档和各种方法的结果。
| 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
|
|
|
警告:序列化该类的对象与以后的Swing版本不兼容。当前的序列化支持适用于短期贮藏或RMI运行相同Swing版本的应用程序之间。为1.4,为所有JavaBeans™长期存储的支持已被添加到java.beans包。请看XMLEncoder。
| Modifier and Type | Class and Description |
|---|---|
class |
HTMLDocument.BlockElement
一个元素代表一个HTML的结构块。
|
class |
HTMLDocument.HTMLReader
一个HTML阅读器加载HTML文档和HTML元素的结构。
|
static class |
HTMLDocument.Iterator
迭代器遍历一个特定类型的标签。
|
class |
HTMLDocument.RunElement
一个元素代表一个块,一组HTML字符级属性分配给它的文本。
|
DefaultStyledDocument.AttributeUndoableEdit, DefaultStyledDocument.ElementBuffer, DefaultStyledDocument.ElementSpec, DefaultStyledDocument.SectionElementAbstractDocument.AbstractElement, AbstractDocument.AttributeContext, AbstractDocument.BranchElement, AbstractDocument.Content, AbstractDocument.DefaultDocumentEvent, AbstractDocument.ElementEdit, AbstractDocument.LeafElement| Modifier and Type | Field and Description |
|---|---|
static String |
AdditionalComments
文档属性键值。
|
buffer, BUFFER_SIZE_DEFAULTBAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementNameStreamDescriptionProperty, TitleProperty| Constructor and Description |
|---|
HTMLDocument()
使用默认缓冲区大小和默认
StyleSheet构建一个HTML文档。
|
HTMLDocument(AbstractDocument.Content c, StyleSheet styles)
构造一个给定的内容存储的实现和给定的样式属性的HTML文档存储机制。
|
HTMLDocument(StyleSheet styles)
构造一个默认内容存储的实现和指定的样式属性的存储机制的HTML文档。
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
create(DefaultStyledDocument.ElementSpec[] data)
用给定的元素规范替换文档的内容。
|
protected Element |
createBranchElement(Element parent, AttributeSet a)
创建一个文档分支元素,可以包含其他元素。
|
protected AbstractDocument.AbstractElement |
createDefaultRoot()
创建用于表示默认文档结构的根元素。
|
protected Element |
createLeafElement(Element parent, AttributeSet a, int p0, int p1)
创建直接表示文本的文档叶元素(没有任何子元素)。
|
protected void |
fireChangedUpdate(DocumentEvent e)
通知所有的听众,关于这一事件的类型通知已注册的兴趣。
|
protected void |
fireUndoableEditUpdate(UndoableEditEvent e)
通知所有的听众,关于这一事件的类型通知已注册的兴趣。
|
URL |
getBase()
返回解决相对网址的位置。
|
Element |
getElement(Element e, Object attribute, Object value)
返回
e包含该属性的子元素,与价值
value
attribute,或
null如果没有发现。
|
Element |
getElement(String id)
返回元素具有特定的身份
Attribute。
|
HTMLDocument.Iterator |
getIterator(HTML.Tag t)
获取指定的HTML标记一个迭代器。
|
HTMLEditorKit.Parser |
getParser()
返回时使用HTML解析器,插入到现有的文件。
|
boolean |
getPreservesUnknownTags()
返回在遇到未知标签时所观察到的行为。
|
HTMLEditorKit.ParserCallback |
getReader(int pos)
获取分析器使用时加载文档和HTML的读者。
|
HTMLEditorKit.ParserCallback |
getReader(int pos, int popDepth, int pushDepth, HTML.Tag insertTag)
返回解析器使用加载文档和HTML的读者。
|
StyleSheet |
getStyleSheet()
取
StyleSheet与文件具体的显示规则(CSS)被指定在HTML文档本身。
|
int |
getTokenThreshold()
获取缓冲区的数量,然后尝试更新文档元素结构。
|
protected void |
insert(int offset, DefaultStyledDocument.ElementSpec[] data)
批量插入新元素。
|
void |
insertAfterEnd(Element elem, String htmlText)
插入指定为结束的给定元素在一个字符串的HTML。
|
void |
insertAfterStart(Element elem, String htmlText)
插入指定的字符串的HTML元素的开始。
|
void |
insertBeforeEnd(Element elem, String htmlText)
插入指定为底的元素的HTML字符串。
|
void |
insertBeforeStart(Element elem, String htmlText)
插入指定为在给定字符串的HTML元素的开始。
|
protected void |
insertUpdate(AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr)
更新文本插入的结果文档结构。
|
void |
processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent e)
HyperlinkEvents过程是由一个HTML框架文件的生成。
|
void |
setBase(URL u)
设置位置,以解决相对网址。
|
void |
setInnerHTML(Element elem, String htmlText)
将给定元素的内容指定为HTML字符串的孩子。
|
void |
setOuterHTML(Element elem, String htmlText)
将给定的元素与内容的HTML字符串指定为母。
|
void |
setParagraphAttributes(int offset, int length, AttributeSet s, boolean replace)
设置段落的属性。
|
void |
setParser(HTMLEditorKit.Parser parser)
设置解析器的方法插入HTML到现有的文件,如
setInnerHTML,和
setOuterHTML。
|
void |
setPreservesUnknownTags(boolean preservesTags)
确定如何未知标签由解析器处理。
|
void |
setTokenThreshold(int n)
设置缓冲区的数量,然后尝试更新文档元素结构。
|
addDocumentListener, addStyle, getBackground, getCharacterElement, getDefaultRootElement, getFont, getForeground, getLogicalStyle, getParagraphElement, getStyle, getStyleNames, removeDocumentListener, removeElement, removeStyle, removeUpdate, setCharacterAttributes, setLogicalStyle, styleChangedaddUndoableEditListener, createPosition, dump, fireInsertUpdate, fireRemoveUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, insertString, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlockclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitaddUndoableEditListener, createPosition, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, remove, removeUndoableEditListener, renderpublic static final String AdditionalComments
public HTMLDocument()
StyleSheet构建一个HTML文档。这是一个构造函数
HTMLDocument(Content, StyleSheet)便利的方法。
public HTMLDocument(StyleSheet styles)
HTMLDocument(Content, StyleSheet)便利的方法。
styles -风格
public HTMLDocument(AbstractDocument.Content c, StyleSheet styles)
c为内容的容器
styles -风格
public HTMLEditorKit.ParserCallback getReader(int pos)
HTMLDocument.HTMLReader实例。子类可以重载这个方法改变文档结构如果需要。(例如,用于处理自定义标签,或结构表示字符样式元素。)
pos -起始位置
public HTMLEditorKit.ParserCallback getReader(int pos, int popDepth, int pushDepth, HTML.Tag insertTag)
HTMLDocument.HTMLReader实例。子类可以重载这个方法改变文档结构如果需要。(例如,处理标签,或结构代表性风格元素。)
这是一个getReader(int, int, int, HTML.Tag, TRUE)便利的方法。
popDepth -
ElementSpec.EndTagTypes数产生之前插入
pushDepth具有方向
ElementSpec.JoinNextDirection应生成之前插入
ElementSpec.StartTagTypes数量,但结束后的标签已生成
insertTag开始插入文档的第一个标签
public URL getBase()
public void setBase(URL u)
这套的StyleSheet基地被u以及文档的基础。
u -所需的基础URL
protected void insert(int offset,
DefaultStyledDocument.ElementSpec[] data)
throws BadLocationException
insert 方法重写,继承类
DefaultStyledDocument
offset -起始偏移
data -元素数据
BadLocationException -如果给定的位置不在相关文件表示一个有效的位置。
protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr)
insertUpdate 方法重写,继承类
DefaultStyledDocument
chng -描述文件的改变
attr -属性
protected void create(DefaultStyledDocument.ElementSpec[] data)
create 方法重写,继承类
DefaultStyledDocument
data -该文件的新内容
public void setParagraphAttributes(int offset,
int length,
AttributeSet s,
boolean replace)
这种方法是线程安全的,虽然大多数摇摆方法不是。更多信息请见Concurrency in Swing。
setParagraphAttributes 接口
StyledDocument
setParagraphAttributes 方法重写,继承类
DefaultStyledDocument
offset -偏移到段落(至少0个)
length的字符数的影响(至少0个)
s -属性
replace -是否替换现有的属性,或者将他们合并
public StyleSheet getStyleSheet()
StyleSheet与文件具体的显示规则(CSS)被指定在HTML文档本身。
StyleSheet
public HTMLDocument.Iterator getIterator(HTML.Tag t)
t -要求的
HTML.Tag
Iterator
HTML.Tag
protected Element createLeafElement(Element parent, AttributeSet a, int p0, int p1)
HTMLDocument.RunElement。
createLeafElement 方法重写,继承类
AbstractDocument
parent -父元素
a -该元素的属性
p0 -范围的开始(至少0个)
p1 -范围的结束(必须至少为P0)
protected Element createBranchElement(Element parent, AttributeSet a)
HTMLDocument.BlockElement。
createBranchElement 方法重写,继承类
AbstractDocument
parent -父元素
a -属性
protected AbstractDocument.AbstractElement createDefaultRoot()
createDefaultRoot 方法重写,继承类
DefaultStyledDocument
public void setTokenThreshold(int n)
n -令牌的数量来缓冲
public int getTokenThreshold()
Integer.MAX_VALUE。
public void setPreservesUnknownTags(boolean preservesTags)
preservesTags -如果未知的标签应该被保存在模型中,否则标签掉了
HTML.Tag
public boolean getPreservesUnknownTags()
HTML.Tag
public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent e)
HyperlinkEvents过程是由一个HTML框架文件的生成。的
HyperlinkEvent类型作为参数的建议,是
HTMLFrameHyperlinkEvent。除了包含在
HyperlinkEvent典型的信息,该事件包含对应于该点击发生框架元(源元件)和目标名称。目标的名字有4个可能的值:
HTML.Attribute.SRC属性值和火灾
ChangedUpdate事件。
如果目标是_parent,然后删除父元素,这是一个
<框架>
的元素,并插入一个新的
<框架>
的元素,并将其
HTML.Attribute.SRC属性有一个值等于目标URL和消防
RemovedUpdate和
InsertUpdate。
框架>
框架>
如果目标是_top,这方法不。在视图的实现框架,即FrameView,_top的加工处理。鉴于_top意味着更换整个文档,因此处理以外的文件,它将取代。
如果目标是一个名为框架,那么元素层次搜索与一个名字等于目标元素,其HTML.Attribute.SRC属性更新和ChangedUpdate事件触发。
e -事件
public void setParser(HTMLEditorKit.Parser parser)
setInnerHTML,和
setOuterHTML。
HTMLEditorKit.createDefaultDocument会为你设置解析器。如果你创建一HTMLDocument,务必设置相应的解析器。
parser -分析器用于文本插入
public HTMLEditorKit.Parser getParser()
public void setInnerHTML(Element elem, String htmlText) throws BadLocationException, IOException
这将被看作是至少两个事件,N插入后面跟着一个删除。
考虑下面的结构(的elem参数在大胆)。
<鱼>
|
/ \< < >
鱼>
调用setInnerHTML(elem, "<ul><li>")结果如下结构(新的元素在红)。
<鱼>
|
\
\
<李>李>
鱼>
参数elem不得叶元,否则IllegalArgumentException抛出。如果elem或htmlText参数null,不进行任何更改的文件。
这项工作是正确的,该文件必须有一个HTMLEditorKit.Parser集。这将是如果文档的创建一个通过createDefaultDocument HTMLEditorKit执行方法。
elem -分支元素的儿童将被取代
htmlText -被解析并分配到
elem字符串
IllegalArgumentException -如果
elem是叶
IllegalStateException -如果一个
HTMLEditorKit.Parser没有定义
BadLocationException
IOException
public void setOuterHTML(Element elem, String htmlText) throws BadLocationException, IOException
这将被看作是至少两个事件,N插入后面跟着一个删除。
当更换叶这将试图确保如果需要有一个换行符的礼物。这可能会导致插入的一个额外的元素。考虑一下,如果你被取代,包含一个
这将创建两个元素的特征元素,一个形象,一个换行符。
如果你试图在你最有可能最终会有两个元素替换的元素,如setOuterHTML(getCharacterElement (getLength()), "blah")将导致最后两叶元素,一个代表'废话',和其他代表的端元。
考虑下面的结构(的elem参数在大胆)。
<鱼>
|
/ \< < >
鱼>
调用setOuterHTML(elem, "<ul><li>")结果如下结构(新的元素在红)。
<鱼>
|
\
<李>李>
鱼>
如果elem或htmlText参数null,不进行任何更改的文件。
这项工作必须有一个正确的文档解析器设置HTMLEditorKit执行。这将是如果文档的创建一个通过createDefaultDocument HTMLEditorKit执行方法。
elem -元素取代
htmlText -被解析并插到位的
elem字符串
IllegalStateException HTMLEditorKit执行解析器尚未确定。
BadLocationException
IOException
public void insertAfterStart(Element elem, String htmlText) throws BadLocationException, IOException
考虑下面的结构(的elem参数在大胆)。
<鱼>
|
/ \< < >
鱼>
调用insertAfterStart(elem, "<ul><li>")结果如下结构(新的元素在红)。
<鱼>
|
/ | \
/
<李>李>
鱼>
不像insertBeforeStart方法,新的元素成为指定元素的孩子,没有兄弟姐妹。
参数elem不得叶元,否则IllegalArgumentException抛出。如果elem或htmlText参数null,不进行任何更改的文件。
这项工作是正确的,该文件必须有一个HTMLEditorKit.Parser集。这将是如果文档的创建一个通过createDefaultDocument HTMLEditorKit执行方法。
elem -分支元素是新的文本的根
htmlText -被解析并分配到
elem字符串
IllegalArgumentException -如果
elem是叶
IllegalStateException HTMLEditorKit执行。解析器尚未设置文档
BadLocationException
IOException
public void insertBeforeEnd(Element elem, String htmlText) throws BadLocationException, IOException
如果elem的孩子离开,并在elem.getEndOffset() - 1字符是换行符,这将在换行前,没有文本换行后。
考虑下面的结构(的elem参数在大胆)。
<鱼>
|
/ \< < >
鱼>
调用insertBeforeEnd(elem, "<ul><li>")结果如下结构(新的元素在红)。
<鱼>
|
/ | \
\
<李>李>
鱼>
不像insertAfterEnd方法,新的元素成为指定元素的孩子,没有兄弟姐妹。
参数elem不得叶元,否则IllegalArgumentException抛出。如果elem或htmlText参数null,不进行任何更改的文件。
这项工作是正确的,该文件必须有一个HTMLEditorKit.Parser集。这将是如果文档的创建一个通过createDefaultDocument HTMLEditorKit执行方法。
elem -元素是新的文本的根
htmlText -被解析并分配到
elem字符串
IllegalArgumentException -如果
elem是叶
IllegalStateException HTMLEditorKit执行。解析器尚未设置文档
BadLocationException
IOException
public void insertBeforeStart(Element elem, String htmlText) throws BadLocationException, IOException
考虑下面的结构(的elem参数在大胆)。
<鱼>
|
/ \< < >
鱼>
调用insertBeforeStart(elem, "<ul><li>")结果如下结构(新的元素在红)。
<鱼>
/ \
/ \
<李>李>
鱼>
不像insertAfterStart方法,新的元素成为指定元素的兄弟姐妹,没有孩子。
如果elem或htmlText参数null,不进行任何更改的文件。
这项工作是正确的,该文件必须有一个HTMLEditorKit.Parser集。这将是如果文档的创建一个通过createDefaultDocument HTMLEditorKit执行方法。
elem -元素的内容之前插入
htmlText -被解析并
elem之前插入字符串
IllegalStateException HTMLEditorKit执行。解析器尚未设置文档
BadLocationException
IOException
public void insertAfterEnd(Element elem, String htmlText) throws BadLocationException, IOException
考虑下面的结构(的elem参数在大胆)。
<鱼>
|
/ \< < >
鱼>
调用insertAfterEnd(elem, "<ul><li>")结果如下结构(新的元素在红)。
<鱼>
/ \
/ \
<李>李>
鱼>
不像insertBeforeEnd方法,新的元素成为指定元素的兄弟姐妹,没有孩子。
如果elem或htmlText参数null,不进行任何更改的文件。
这项工作是正确的,该文件必须有一个HTMLEditorKit.Parser集。这将是如果文档的创建一个通过createDefaultDocument HTMLEditorKit执行方法。
elem -元素内容后插入
htmlText -被解析并插在
elem字符串
IllegalStateException HTMLEditorKit执行。解析器尚未设置文档
BadLocationException
IOException
public Element getElement(String id)
Attribute。如果元素不能被发现,
null返回。请注意,此方法在
Attribute作品,不字符标记。在下面的HTML片段:
<a id="HelloThere">属性ID和字符的标签是“A”。这是一个
getElement(RootElement, HTML.Attribute.id, id)便利的方法。这不是线程安全的。
id代表所需的
Attribute字符串
Attribute或
null如果不能被发现,或
null如果
id是
null
HTML.Attribute
public Element getElement(Element e, Object attribute, Object value)
e包含该属性的子元素,与价值
value
attribute,或
null如果没有发现。这不是线程安全的。
e -根元素的搜索开始的地方
attribute -所需的
Attribute
value为指定的值
Attribute
Attribute和指定的
value元素,或
null如果不能被发现
HTML.Attribute
protected void fireChangedUpdate(DocumentEvent e)
fireChangedUpdate 方法重写,继承类
AbstractDocument
e -事件
EventListenerList
protected void fireUndoableEditUpdate(UndoableEditEvent e)
fireUndoableEditUpdate 方法重写,继承类
AbstractDocument
e -事件
EventListenerList
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.