本文目录导读:

- 基础 JEditorPane + HTMLEditorKit 渲染测试
- 测试 Parser 方法:手动调用 HTMLEditorKit 的 Parser 并捕获回调
- 单元测试框架示例 (JUnit)
- 常见问题与调试技巧
这是一个关于 JEditorPane, HTMLEditorKit 和 Parser 的测试或实验类名称,根据这个类名,我推断你很可能是在尝试:
- 测试 Java Swing 中
JEditorPane如何解析和渲染 HTML。 - 测试
HTMLEditorKit的解析器(Parser)方法,例如手动调用ParserCallback或自定义解析行为。 - 遇到了无法正确解析某些 HTML 内容的问题,想要通过单元测试来排查。
下面我为你提供一个完整的测试示例,涵盖:
- 基础测试:JEditorPane 设置 HTML 内容并验证渲染。
- 测试 Parser 方法:通过
HTMLEditorKit.getParser()手动解析 HTML 并捕获回调。 - 测试自定义回调处理。
基础 JEditorPane + HTMLEditorKit 渲染测试
这是一个简单的测试类,用于验证 JEditorPane 能够正确显示简单的 HTML。
import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTMLDocument;
/**
* 测试 JEditorPane 使用 HTMLEditorKit 解析和渲染简单的 HTML 内容。
*/
public class JEditorPaneHTMLEditorKitParserMethodTesting {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
// 1. 创建 JEditorPane
JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKit(new HTMLEditorKit());
// 2. 设置简单的 HTML 内容
String htmlContent = """
<html>
<body>
<h1>Hello, Swing!</h1>
<p>This is a <b>bold</b> and <i>italic</i> text.</p>
<a href='https://example.com'>Click here</a>
</body>
</html>
""";
// 3. 设置内容并启用编辑(可选)
editorPane.setContentType("text/html");
editorPane.setText(htmlContent);
editorPane.setEditable(false); // 只读显示
// 4. 显示在 JFrame 中
JFrame frame = new JFrame("JEditorPane HTML Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(editorPane));
frame.setSize(400, 300);
frame.setVisible(true);
});
}
}
测试 Parser 方法:手动调用 HTMLEditorKit 的 Parser 并捕获回调
如果你需要深入测试解析器(例如自定义标签处理、获取所有链接等),可以这样:
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.html.HTML;
import javax.swing.text.MutableAttributeSet;
import java.io.StringReader;
import java.io.IOException;
/**
* 测试手动调用 HTMLEditorKit 的 Parser 并捕获回调事件。
*/
public class ParserMethodTesting {
public static void main(String[] args) {
String html = """
<html>
<body>
<h1>Title</h1>
<p>Paragraph with <a href="test.html">link</a></p>
<img src="image.png" alt="test">
</body>
</html>
""";
// 使用 HTMLEditorKit 提供的解析器
HTMLEditorKit.Parser parser = new HTMLEditorKit().getParser();
if (parser == null) {
// 在某些 JDK 版本中 parser 可能为 null,需要手动创建
// 或者使用 ParserDelegator
parser = new ParserDelegator();
}
StringReader reader = new StringReader(html);
try {
parser.parse(reader, new HTMLEditorKit.ParserCallback() {
@Override
public void handleText(char[] data, int pos) {
System.out.println("Text: " + new String(data));
}
@Override
public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
System.out.println("StartTag: " + t + " Attributes: " + a);
}
@Override
public void handleEndTag(HTML.Tag t, int pos) {
System.out.println("EndTag: " + t);
}
@Override
public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
System.out.println("SimpleTag (self-closing): " + t + " Attributes: " + a);
}
@Override
public void handleError(String errorMsg, int pos) {
System.err.println("Parse Error at " + pos + ": " + errorMsg);
}
}, true); // true 表示忽略字符集
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出示例:
StartTag: html Attributes:
StartTag: body Attributes:
StartTag: h1 Attributes:
Text: Title
EndTag: h1
StartTag: p Attributes:
Text: Paragraph with
StartTag: a Attributes: {href=test.html}
Text: link
EndTag: a
SimpleTag: img Attributes: {src=image.png, alt=test}
Text:
EndTag: p
EndTag: body
EndTag: html
单元测试框架示例 (JUnit)
如果你希望做自动化测试,可以这样写一个 JUnit 测试类:
import org.junit.jupiter.api.Test;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTML;
import javax.swing.text.MutableAttributeSet;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* JUnit 测试:验证 Parser 回调方法的调用是否符合预期。
*/
class JEditorPaneHTMLEditorKitParserMethodTestingTest {
@Test
void testParserExtractsAllTags() throws Exception {
String html = "<html><body><p>Hello <b>World</b></p></body></html>";
List<String> tagNames = new ArrayList<>();
HTMLEditorKit.Parser parser = new HTMLEditorKit().getParser();
if (parser == null) {
// 部分 JDK 需要显式加载 DTD,这里用 ParserDelegator 替代
parser = new javax.swing.text.html.parser.ParserDelegator();
}
parser.parse(new StringReader(html), new HTMLEditorKit.ParserCallback() {
@Override
public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
tagNames.add(t.toString());
}
@Override
public void handleEndTag(HTML.Tag t, int pos) {
tagNames.add("/" + t.toString());
}
@Override
public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
tagNames.add(t.toString() + " (self-closing)");
}
}, true);
// 验证标签顺序和类型
assertEquals(6, tagNames.size());
assertEquals("html", tagNames.get(0));
assertEquals("body", tagNames.get(1));
assertEquals("p", tagNames.get(2));
assertEquals("b", tagNames.get(3));
assertEquals("/b", tagNames.get(4));
assertEquals("/p", tagNames.get(5));
}
}
常见问题与调试技巧
-
Parser 为 null 的问题
- 在 JDK 11+ 中,
HTMLEditorKit.getParser()可能返回null。 - 解决方案:直接使用
new ParserDelegator()或手动加载 DTD:DTD dtd = DTD.getDTD("html32"); HTMLEditorKit.Parser parser = new ParserDelegator(dtd);
- 在 JDK 11+ 中,
-
HTML 内容不渲染 / 显示为纯文本
- 确保调用了
setContentType("text/html")。 - 检查 HTML 是否格式正确(例如必须包含
<html>标签)。 - 使用
setText()而不是直接操作 Document。
- 确保调用了
-
自定义标签处理
- 可以通过重写
ParserCallback来处理自定义标签,例如提取所有<a>链接。 - 示例:提取所有 href 属性。
- 可以通过重写
-
样式表 (CSS) 支持
- JEditorPane 的 HTML 解析器对 CSS 支持非常有限,仅支持内联样式和部分属性。
- 复杂的 CSS 可能无法正确渲染。
通过上面的示例,你可以:
- 基础测试:验证 JEditorPane 能否显示 HTML。
- Parser 方法测试:手动调用解析器并观察回调事件。
- 单元测试:确保解析器按预期工作。
如果你有具体的测试目标(解析 XHTML、处理特殊字符、测试性能),请提供更详细的描述,我可以给出针对性的解决方案。
版权声明:足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播