下面列出了javax.swing.text.html.HTMLEditorKit#createDefaultDocument ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) throws Exception {
int N = 10;
for (int i = 0; i < N; i++) {
HTMLEditorKit kit = new HTMLEditorKit();
Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
htmlReader.flush();
CharArrayWriter writer = new CharArrayWriter(1000);
kit.write(writer, doc, 0, doc.getLength());
writer.flush();
String result = writer.toString();
if (!result.contains("<tt><a")) {
throw new RuntimeException("The <a> and <tt> tags are swapped");
}
}
}
public static void main(String[] args) throws Exception {
ParserCB cb = new ParserCB();
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
htmlDoc.getParser().parse(new StringReader(text), cb, true);
synchronized (lock) {
if (!isCallbackInvoked) {
lock.wait(5000);
}
}
if (!isCallbackInvoked) {
throw new RuntimeException("Test Failed: ParserCallback.handleText() is not invoked for text - " + text);
}
if (exception != null) {
throw exception;
}
}
public static void main(String[] args) throws Exception {
ParserCB cb = new ParserCB();
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
htmlDoc.getParser().parse(new StringReader(text), cb, true);
synchronized (lock) {
if (!isCallbackInvoked) {
lock.wait(5000);
}
}
if (!isCallbackInvoked) {
throw new RuntimeException("Test Failed: ParserCallback.handleText() is not invoked for text - " + text);
}
if (exception != null) {
throw exception;
}
}
public static void main(String[] args) throws Exception {
int N = 10;
for (int i = 0; i < N; i++) {
HTMLEditorKit kit = new HTMLEditorKit();
Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
htmlReader.flush();
CharArrayWriter writer = new CharArrayWriter(1000);
kit.write(writer, doc, 0, doc.getLength());
writer.flush();
String result = writer.toString();
if (!result.contains("<tt><a")) {
throw new RuntimeException("The <a> and <tt> tags are swapped");
}
}
}
/** 构造方法 */
public HTMLDocumentEditor() {
/** 设置主窗体标题 */
super("HTMLDocumentEditor");
HTMLEditorKit editorKit = new HTMLEditorKit();
/** 创建默认文档指向网页引用document */
document = (HTMLDocument) editorKit.createDefaultDocument();
// 强制SWINGSET实现跨平台,不改变风格
try {
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());
// 如果你想用系统的界面风格替代,请注释掉上一行代码,而取消下一行代码的注释:
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc) {
// 产生异常,则显示错误消息:加载L&F错误
System.err.println("Error loading L&F: " + exc);
}
// 调用初始化方法
init();
}
public static void main(String[] args) throws Exception {
int N = 10;
for (int i = 0; i < N; i++) {
HTMLEditorKit kit = new HTMLEditorKit();
Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
htmlReader.flush();
CharArrayWriter writer = new CharArrayWriter(1000);
kit.write(writer, doc, 0, doc.getLength());
writer.flush();
String result = writer.toString();
if (!result.contains("<tt><a")) {
throw new RuntimeException("The <a> and <tt> tags are swapped");
}
}
}
public static void main(String[] args) throws Exception {
ParserCB cb = new ParserCB();
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
htmlDoc.getParser().parse(new StringReader(text), cb, true);
synchronized (lock) {
if (!isCallbackInvoked) {
lock.wait(5000);
}
}
if (!isCallbackInvoked) {
throw new RuntimeException("Test Failed: ParserCallback.handleText() is not invoked for text - " + text);
}
if (exception != null) {
throw exception;
}
}
public static void main(String[] args) throws Exception {
int N = 10;
for (int i = 0; i < N; i++) {
HTMLEditorKit kit = new HTMLEditorKit();
Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
htmlReader.flush();
CharArrayWriter writer = new CharArrayWriter(1000);
kit.write(writer, doc, 0, doc.getLength());
writer.flush();
String result = writer.toString();
if (!result.contains("<tt><a")) {
throw new RuntimeException("The <a> and <tt> tags are swapped");
}
}
}
public static void main(String[] args) throws Exception {
int N = 10;
for (int i = 0; i < N; i++) {
HTMLEditorKit kit = new HTMLEditorKit();
Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
htmlReader.flush();
CharArrayWriter writer = new CharArrayWriter(1000);
kit.write(writer, doc, 0, doc.getLength());
writer.flush();
String result = writer.toString();
if (!result.contains("<tt><a")) {
throw new RuntimeException("The <a> and <tt> tags are swapped");
}
}
}
public static void main(String[] args) throws Exception {
ParserCB cb = new ParserCB();
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
htmlDoc.getParser().parse(new StringReader(text), cb, true);
synchronized (lock) {
if (!isCallbackInvoked) {
lock.wait(5000);
}
}
if (!isCallbackInvoked) {
throw new RuntimeException("Test Failed: ParserCallback.handleText() is not invoked for text - " + text);
}
if (exception != null) {
throw exception;
}
}
public static void main(String[] args) throws Exception {
int N = 10;
for (int i = 0; i < N; i++) {
HTMLEditorKit kit = new HTMLEditorKit();
Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
htmlReader.flush();
CharArrayWriter writer = new CharArrayWriter(1000);
kit.write(writer, doc, 0, doc.getLength());
writer.flush();
String result = writer.toString();
if (!result.contains("<tt><a")) {
throw new RuntimeException("The <a> and <tt> tags are swapped");
}
}
}
public static void main(String[] args) {
String htmlDoc = "<pre><p> </pre>";
try {
HTMLEditorKit kit = new HTMLEditorKit();
Class c = Class.forName(
"javax.swing.text.html.parser.ParserDelegator");
HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
parser.parse(new CharArrayReader(htmlDoc.toCharArray()),
htmlReader, true);
htmlReader.flush();
CharArrayWriter writer = new CharArrayWriter(1000);
kit.write(writer, doc, 0, doc.getLength());
writer.flush();
} catch (Exception ex) {
throw new RuntimeException("Test Failed " + ex);
}
}
private static String convertToRTF(final String htmlStr) {
OutputStream os = new ByteArrayOutputStream();
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
RTFEditorKit rtfEditorKit = new RTFEditorKit();
String rtfStr = null;
String tempStr = htmlStr.replace("</font>", "#END_FONT#").replace("<br>", "#NEW_LINE#");
InputStream is = new ByteArrayInputStream(tempStr.getBytes());
try {
Document doc = htmlEditorKit.createDefaultDocument();
htmlEditorKit.read(is, doc, 0);
rtfEditorKit.write(os, doc, 0, doc.getLength());
rtfStr = os.toString();
rtfStr = rtfStr.replace("#NEW_LINE#", "\\line ");
rtfStr = rtfStr.replace("#END_FONT#", "\\cf0 ");
} catch (IOException | BadLocationException e) {
e.printStackTrace();
}
return rtfStr;
}
public static void main(String[] args) throws Exception {
int N = 10;
for (int i = 0; i < N; i++) {
HTMLEditorKit kit = new HTMLEditorKit();
Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
htmlReader.flush();
CharArrayWriter writer = new CharArrayWriter(1000);
kit.write(writer, doc, 0, doc.getLength());
writer.flush();
String result = writer.toString();
if (!result.contains("<tt><a")) {
throw new RuntimeException("The <a> and <tt> tags are swapped");
}
}
}
public static void main(String[] args) throws Exception {
ParserCB cb = new ParserCB();
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
htmlDoc.getParser().parse(new StringReader(text), cb, true);
synchronized (lock) {
if (!isCallbackInvoked) {
lock.wait(5000);
}
}
if (!isCallbackInvoked) {
throw new RuntimeException("Test Failed: ParserCallback.handleText() is not invoked for text - " + text);
}
if (exception != null) {
throw exception;
}
}
private void setEnhancedOutputStyle(JTextPane pane) {
pane.setContentType("text/html");
pane.setEditable(false);
pane.setEnabled(true);
HTMLEditorKit editorKit = new HTMLEditorKit();
HTMLDocument defaultDocument = (HTMLDocument) editorKit.createDefaultDocument();
pane.setEditorKit(editorKit);
pane.setDocument(defaultDocument);
StyleSheet css = editorKit.getStyleSheet();
for (String entry : style.getFormattedStyle())
css.addRule(entry);
}
public void openDocument() {
try {
File current = new File(".");
JFileChooser chooser = new JFileChooser(current);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new HTMLFileFilter());
int approval = chooser.showSaveDialog(this);
if (approval == JFileChooser.APPROVE_OPTION) {
currentFile = chooser.getSelectedFile();
setTitle(currentFile.getName());
FileReader fr = new FileReader(currentFile);
Document oldDoc = textPane.getDocument();
if (oldDoc != null)
oldDoc.removeUndoableEditListener(undoHandler);
HTMLEditorKit editorKit = new HTMLEditorKit();
document = (HTMLDocument) editorKit.createDefaultDocument();
editorKit.read(fr, document, 0);
document.addUndoableEditListener(undoHandler);
textPane.setDocument(document);
resetUndoManager();
}
} catch (BadLocationException ble) {
System.err.println("BadLocationException: " + ble.getMessage());
} catch (FileNotFoundException fnfe) {
System.err.println("FileNotFoundException: " + fnfe.getMessage());
} catch (IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}
}
private JComponent createPanel() {
System.setProperty("awt.useSystemAAFontSettings", "on");
final JEditorPane editorPane = new JEditorPane();
HTMLEditorKit kit = new HTMLEditorKit();
editorPane.setEditorKit(kit);
editorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
editorPane.setFont(new Font("Arial", Font.PLAIN, 12));
editorPane.setPreferredSize(new Dimension(350, 180));
editorPane.setEditable(false);
editorPane.setContentType("text/html");
editorPane.setBackground(new Color(234, 241, 248));
// add some styles to the html
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule(".firstCol {margin-left: 25px; }");
styleSheet.addRule(".secondCol {color: blue; }");
Document doc = kit.createDefaultDocument();
editorPane.setDocument(doc);
editorPane.setText(
"<html>" +
"<body>" +
"<table border='0px' BGCOLOR=\"#EAF1F8\">" +
"<tr><td colspan=2>" +
"<img src='" + ImageUtil.getImageURL("logo").toExternalForm() + "'>" +
"</td></tr>" +
"<tr><td class=\"firstCol\"><b>" + VERSION + "</b></td><td class=\"secondCol\">" +
VERSION_NO +
"</td></tr>" +
"<tr><td class=\"firstCol\"><b>" + BUILD + "</b></td><td class=\"secondCol\">" +
ReleaseInfo.getBuildNumber() + " (" + BUILD_DATE + ")" +
"</td></tr>" +
"<tr><td class=\"firstCol\"><b>" + SITE + "</b></td><td class=\"secondCol\">"+
"<a href=\"" + SITE_VALUE + "\">" + SITE_SMALL_VALUE +
"</a></td></tr>" +
"<tr><td class=\"firstCol\"><b>" + COPYRIGHT + "</b></td><td class=\"secondCol\">" +
DEVELOPER +
"</td></tr>" +
"</table>" +
"</body>" +
"</html>"
);
// Add Hyperlink listener to process hyperlinks
editorPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(final HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
EventQueue.invokeLater(new Runnable() {
public void run() {
SwingUtilities.getWindowAncestor(editorPane).setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
editorPane.setToolTipText(e.getURL().toExternalForm());
}
});
} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
EventQueue.invokeLater(new Runnable() {
public void run() {
SwingUtilities.getWindowAncestor(editorPane).setCursor(Cursor.getDefaultCursor());
editorPane.setToolTipText(null);
}
});
} else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
FileUtil.openUrl(e.getURL().toString(), AboutAction.class);
}
}
});
editorPane.addMouseListener(mouseListener);
JScrollPane sp = new JScrollPane(editorPane);
return sp;
}
private JComponent createPanel(String html) {
System.setProperty("awt.useSystemAAFontSettings", "on");
final JEditorPane editorPane = new JEditorPane();
HTMLEditorKit kit = new HTMLEditorKit();
editorPane.setEditorKit(kit);
editorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
editorPane.setFont(new Font("Arial", Font.PLAIN, 12));
editorPane.setPreferredSize(new Dimension(350, 120));
editorPane.setEditable(false);
editorPane.setContentType("text/html");
editorPane.setBackground(new Color(234, 241, 248));
Document doc = kit.createDefaultDocument();
editorPane.setDocument(doc);
editorPane.setText(html);
// Add Hyperlink listener to process hyperlinks
editorPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(final HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
EventQueue.invokeLater(new Runnable() {
public void run() {
SwingUtilities.getWindowAncestor(editorPane).setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
editorPane.setToolTipText(e.getURL().toExternalForm());
}
});
} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
EventQueue.invokeLater(new Runnable() {
public void run() {
SwingUtilities.getWindowAncestor(editorPane).setCursor(Cursor.getDefaultCursor());
editorPane.setToolTipText(null);
}
});
} else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
FileUtil.openUrl(e.getURL().toString(), AboutAction.class);
}
}
});
editorPane.addMouseListener(mouseListener);
JScrollPane sp = new JScrollPane(editorPane);
return sp;
}
private Component createContentPanel() {
contentGbc = new GridBagConstraints();
contentPanel = new JPanel(new GridBagLayout()) {
@Override
public Dimension getPreferredSize() {
return new Dimension(getParent().getWidth(), super.getPreferredSize().height);
}
};
contentPanel.setBackground(Colors.WHITE);
jEditorPane = new JEditorPane() {
@Override
public Dimension getPreferredSize() {
return new Dimension(getParent().getWidth(), super.getPreferredSize().height);
}
};
jEditorPane.setEditable(false);
contentGbc.gridx = 0;
contentGbc.gridy = 0;
contentGbc.weightx = 1.0f;
contentGbc.fill = GridBagConstraints.HORIZONTAL;
contentPanel.add(jEditorPane, contentGbc);
// add filler at bottom
contentGbc.gridy += 1;
contentGbc.weighty = 1.0f;
contentGbc.fill = GridBagConstraints.BOTH;
contentPanel.add(new JLabel(), contentGbc);
// prepare contentGbc for feedback form
contentGbc.gridy += 1;
contentGbc.weighty = 0.0f;
contentGbc.fill = GridBagConstraints.HORIZONTAL;
scrollPane = new ExtendedJScrollPane(contentPanel);
scrollPane.setBorder(null);
HTMLEditorKit kit = new HTMLEditorKit();
jEditorPane.setEditorKit(kit);
jEditorPane.setMargin(new Insets(0, 0, 0, 0));
Document doc = kit.createDefaultDocument();
jEditorPane.setDocument(doc);
jEditorPane.setText(String.format(INFO_TEMPLATE, NO_TUTORIAL_SELECTED));
jEditorPane.addHyperlinkListener(e -> {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
ActionStatisticsCollector.INSTANCE.log(ActionStatisticsCollector.TYPE_GETTING_STARTED,
TUTORIAL_BROWSER_DOCK_KEY, "open_remote_url");
RMUrlHandler.handleUrl(e.getURL().toString());
}
});
return scrollPane;
}