JavaTM 2 Platform
Standard Ed. 6

javax.swing.text.html.parser
类 Parser

java.lang.Object
  继承者 javax.swing.text.html.parser.Parser
所有已实现的接口:
DTDConstants
直接已知子类:
DocumentParser

public class Parser
extends Object
implements DTDConstants

一个简单的 DTD 驱动的 HTML 解析器。该解析器从 InputStream 读取 HTML 文件,并在遇到标记和数据时调用各种方法(在子类中应重写这些方法)。

遗憾的是,存在许多不良实现的 HTML 解析器,结果导致许多不良格式的 HTML 文件。此解析器尝试解析大多数 HTML 文件。这意味着该实现有时偏离有利于 HTML 的 SGML 规范。

该解析器将 \r 和 \r\n 视为 \n。开始标记之后和结束标记之前的换行被忽略,正如 SGML/HTML 规范中指定的那样。

html 规范不指定如何很好地组合空格。明确地说,不讨论下面的情形(注意,此处应使用空格,但我使用了   来强制显示空格):

'<b>blah <i> <strike> foo' 可以被视为:'<b>blah <i><strike>foo'

以及:'<p><a href="xx"> <em>Using</em></a></p>' 好像被视为:'<p><a href="xx"><em>Using</em></a></p>'

如果 strict 为 false,在遇到中断流的标记 (TagElement.breaksFlows) 或尾部空白时,如果未遇到非空白字符,所有空白都将被忽略。看上去该行为与流行的浏览器的行为更接近。

另请参见:
DTD, TagElement, SimpleAttributeSet

字段摘要
protected  DTD dtd
           
protected  boolean strict
          此标记确定 Parser 在强制执行 SGML 兼容性方面是否严格。
 
从接口 javax.swing.text.html.parser.DTDConstants 继承的字段
ANY, CDATA, CONREF, CURRENT, DEFAULT, EMPTY, ENDTAG, ENTITIES, ENTITY, FIXED, GENERAL, ID, IDREF, IDREFS, IMPLIED, MD, MODEL, MS, NAME, NAMES, NMTOKEN, NMTOKENS, NOTATION, NUMBER, NUMBERS, NUTOKEN, NUTOKENS, PARAMETER, PI, PUBLIC, RCDATA, REQUIRED, SDATA, STARTTAG, SYSTEM
 
构造方法摘要
Parser(DTD dtd)
           
 
方法摘要
protected  void endTag(boolean omitted)
          处理结束标记。
protected  void error(String err)
           
protected  void error(String err, String arg1)
           
protected  void error(String err, String arg1, String arg2)
           
protected  void error(String err, String arg1, String arg2, String arg3)
          调用错误处理程序。
protected  void flushAttributes()
           
protected  SimpleAttributeSet getAttributes()
           
protected  int getCurrentLine()
           
protected  int getCurrentPos()
           
protected  void handleComment(char[] text)
          在遇到 HTML 注释时调用。
protected  void handleEmptyTag(TagElement tag)
          在遇到空标记时调用。
protected  void handleEndTag(TagElement tag)
          在遇到结束标记时调用。
protected  void handleEOFInComment()
           
protected  void handleError(int ln, String msg)
          发生了一个错误。
protected  void handleStartTag(TagElement tag)
          在遇到开始标记时调用。
protected  void handleText(char[] text)
          在遇到 PCDATA 时调用。
protected  void handleTitle(char[] text)
          在遇到 HTML 标题标记时调用。
protected  TagElement makeTag(Element elem)
           
protected  TagElement makeTag(Element elem, boolean fictional)
          制作 TagElement。
protected  void markFirstTime(Element elem)
          标记在文档中首次看到标记
 void parse(Reader in)
          在给定 DTD 的情况下,解析 HTML 流。
 String parseDTDMarkup()
          解析 Document Type Declaration 标记声明。
protected  boolean parseMarkupDeclarations(StringBuffer strBuff)
          解析标记声明。
protected  void startTag(TagElement tag)
          处理开始标记。
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

dtd

protected DTD dtd

strict

protected boolean strict
此标记确定 Parser 在强制执行 SGML 兼容性方面是否严格。如果为 false,则对某些 HTML 构造有误的通用类比较宽松。无论是否严格,都会记录一个错误。

构造方法详细信息

Parser

public Parser(DTD dtd)
方法详细信息

getCurrentLine

protected int getCurrentLine()
返回:
当前解析行的行号

makeTag

protected TagElement makeTag(Element elem,
                             boolean fictional)
制作 TagElement。


makeTag

protected TagElement makeTag(Element elem)

getAttributes

protected SimpleAttributeSet getAttributes()

flushAttributes

protected void flushAttributes()

handleText

protected void handleText(char[] text)
在遇到 PCDATA 时调用。


handleTitle

protected void handleTitle(char[] text)
在遇到 HTML 标题标记时调用。


handleComment

protected void handleComment(char[] text)
在遇到 HTML 注释时调用。


handleEOFInComment

protected void handleEOFInComment()

handleEmptyTag

protected void handleEmptyTag(TagElement tag)
                       throws ChangedCharSetException
在遇到空标记时调用。

抛出:
ChangedCharSetException

handleStartTag

protected void handleStartTag(TagElement tag)
在遇到开始标记时调用。


handleEndTag

protected void handleEndTag(TagElement tag)
在遇到结束标记时调用。


handleError

protected void handleError(int ln,
                           String msg)
发生了一个错误。


error

protected void error(String err,
                     String arg1,
                     String arg2,
                     String arg3)
调用错误处理程序。


error

protected void error(String err,
                     String arg1,
                     String arg2)

error

protected void error(String err,
                     String arg1)

error

protected void error(String err)

startTag

protected void startTag(TagElement tag)
                 throws ChangedCharSetException
处理开始标记。将新标记推送到标记堆栈。在属性列表中检查所需的属性。

抛出:
ChangedCharSetException

endTag

protected void endTag(boolean omitted)
处理结束标记。结束标记从标记堆栈中弹出。


markFirstTime

protected void markFirstTime(Element elem)
标记在文档中首次看到标记


parseDTDMarkup

public String parseDTDMarkup()
                      throws IOException
解析 Document Type Declaration 标记声明。当前忽略它。

抛出:
IOException

parseMarkupDeclarations

protected boolean parseMarkupDeclarations(StringBuffer strBuff)
                                   throws IOException
解析标记声明。当前仅处理 Document Type Declaration 标记。如果为标记声明,则返回 true,否则返回 false。

抛出:
IOException

parse

public void parse(Reader in)
           throws IOException
在给定 DTD 的情况下,解析 HTML 流。

抛出:
IOException

getCurrentPos

protected int getCurrentPos()

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见

版权所有 2008 Sun Microsystems, Inc. 保留所有权利。请遵守GNU General Public License, version 2 only