我查看了 JDK 7 的来源:DTD
is no more store in an attribute but in dtd
. 来自 sun 包的 AppContext 不应该被 JRE 本身之外的类访问。因此,使用 的来源,我自己编写了加载 DTD。之后,自定义标签可以很容易地添加到 DTD 中。javax.swing.text.html.parser.ParserDelegator
sun.awt.AppContext
javax.swing.text.html.parser.ParserDelegator
MyParserDelegator
下面的代码还包含来自http://java-sl.com/custom_tag_html_kit.html的其他类,以获得一个工作示例。
import java.awt.Component;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.ComponentView;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTMLEditorKit.Parser;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;
import javax.swing.text.html.StyleSheet;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.html.parser.DocumentParser;
import javax.swing.text.html.parser.ParserDelegator;
public class CustomTag {
public static String htmlText = "<html>\n" + "<body>\n" + "<p>\n" + "Text before button\n" + "<button>Text for <button> tag</button>\n"
+ "Text after button\n" + "</p>\n" + "</body>\n" + "</html>";
JEditorPane edit = new JEditorPane();
public CustomTag() {
JFrame frame = new JFrame("Custom tag in HTMLEditorKit");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(edit));
edit.setEditorKit(new MyHTMLEditorKit());
edit.setText(htmlText);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) throws Exception {
new CustomTag();
}
}
class MyHTMLEditorKit extends HTMLEditorKit {
public MyHTMLEditorKit() {
super();
}
@Override
public Document createDefaultDocument() {
StyleSheet styles = getStyleSheet();
StyleSheet ss = new StyleSheet();
ss.addStyleSheet(styles);
MyHTMLDocument doc = new MyHTMLDocument(ss);
doc.setParser(getParser());
doc.setAsynchronousLoadPriority(4);
doc.setTokenThreshold(100);
return doc;
}
@Override
public ViewFactory getViewFactory() {
return new MyHTMLFactory();
}
Parser defaultParser;
@Override
protected Parser getParser() {
if (defaultParser == null) {
defaultParser = new MyParserDelegator();
}
return defaultParser;
}
class MyHTMLFactory extends HTMLFactory implements ViewFactory {
public MyHTMLFactory() {
super();
}
@Override
public View create(Element element) {
HTML.Tag kind = (HTML.Tag) (element.getAttributes().getAttribute(javax.swing.text.StyleConstants.NameAttribute));
if (kind instanceof HTML.UnknownTag && element.getName().equals("button")) {
return new ComponentView(element) {
@Override
protected Component createComponent() {
JButton button = new JButton("Button : text unknown");
try {
int start = getElement().getStartOffset();
int end = getElement().getEndOffset();
String text = getElement().getDocument().getText(start, end - start);
button.setText(text);
} catch (BadLocationException e) {
e.printStackTrace();
}
return button;
}
};
}
return super.create(element);
}
}
}
class MyHTMLDocument extends HTMLDocument {
public MyHTMLDocument(StyleSheet styles) {
super(styles);
}
@Override
public HTMLEditorKit.ParserCallback getReader(int pos) {
Object desc = getProperty(Document.StreamDescriptionProperty);
if (desc instanceof URL) {
setBase((URL) desc);
}
return new MyHTMLReader(pos);
}
class MyHTMLReader extends HTMLDocument.HTMLReader {
public MyHTMLReader(int offset) {
super(offset);
}
@Override
public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
if (t.toString().equals("button")) {
registerTag(t, new BlockAction());
}
super.handleStartTag(t, a, pos);
}
}
}
class MyParserDelegator extends Parser {
private DTD _dtd;
public MyParserDelegator() {
String nm = "html32";
try {
_dtd = DTD.getDTD(nm);
createDTD(_dtd, nm);
javax.swing.text.html.parser.Element div = _dtd.getElement("div");
_dtd.defineElement("button", div.getType(), true, true, div.getContent(), null, null, div.getAttributes());
} catch (IOException e) {
// (PENDING) UGLY!
System.out.println("Throw an exception: could not get default dtd: " + nm);
}
}
protected static DTD createDTD(DTD dtd, String name) {
InputStream in = null;
try {
String path = name + ".bdtd";
in = ParserDelegator.class.getResourceAsStream(path);
if (in != null) {
dtd.read(new DataInputStream(new BufferedInputStream(in)));
DTD.putDTDHash(name, dtd);
}
} catch (Exception e) {
System.out.println(e);
}
return dtd;
}
@Override
public void parse(Reader r, ParserCallback cb, boolean ignoreCharSet) throws IOException {
new DocumentParser(_dtd).parse(r, cb, ignoreCharSet);
}
}