我正在尝试将已发布的 Google 文档加载到 JEditorPane
中.
这是文档:link .
下面是 JEditorPane 是如何呈现它的:
我对图像的观察:
- 正在正确获取 HTML。
-
JEditorPane
至少支持一些 CSS(注意顶部的阴影条)。 -
JEditorPane
第二次真的很困惑<style type="text/css">
block 在 HTML 源代码中。是不是因为<style>
在<div>
里面而不是<head>
? - 代码中的某些空间有奇怪的工件(U+00C2,十进制 194;带抑扬音符的拉丁大写字母 A),实际上是 Vanilla
U+0020
空间。这可能与字节顺序有关吗? (我已经通过println
ing 每一行验证了字符实际上是以这种方式获取的。)
我读过 this StackOverflow post关于这个主题并实现了它,但这并没有解决问题。
我还注意到 CSS 支持总体上是稀疏的(例如,呈现 http://www.stackoverflow.com
会产生带有大量蓝框的不良结果)但没有公开实际的 HTML 代码或工件。
使用 JTextPane
而不是 JEditorPane
产生相同的结果。
将 DTD 添加到文档顶部(尝试了 XHTML 4.1 Transitional 和 HTML5 的 <!DOCTYPE html>
)也不起作用。
关于为什么会发生这种情况以及我该如何解决它有什么想法吗?
为了尽快获得更好的帮助,这是我的 SSCCE:
public class GoogleDocSSCCE extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame();
GoogleDocSSCCE gdv = new GoogleDocSSCCE();
gdv.docId = "1jG_rNCfVSD8yhHB9ZgA5YicXK_yDOl9T-fItIgmKa-o";
gdv.refreshDocument();
frame.setContentPane(gdv);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private final JEditorPane docPane;
private String docId;
private static final String PREFIX = "https://docs.google.com/document/d/";
private static final String SUFFIX = "/pub";
public GoogleDocSSCCE() {
super(new BorderLayout());
docPane = new JEditorPane();
docPane.setEditable(false);
docPane.setContentType("text/html");
add(new JScrollPane(docPane), BorderLayout.CENTER);
JButton btnRefresh = new JButton("Refresh Document");
btnRefresh.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
refreshDocument();
}
});
add(btnRefresh, BorderLayout.NORTH);
}
public void refreshDocument() {
if (docId == null || docId.isEmpty()) {
docPane.setText(new String());
return;
}
docPane.setText("<html><body>Loading...</body></html>");
new Thread(new Runnable() {
@Override
public void run() {
boolean success = false;
try {
URL u = new URL(PREFIX + docId + SUFFIX);
InputStream stream = u.openStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(stream));
StringBuilder sbDocument = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sbDocument.append(line);
sbDocument.append('\n');
}
docPane.setText(sbDocument.toString());
success = true;
} catch (MalformedURLException e) {
JOptionPane.showMessageDialog(GoogleDocSSCCE.this,
"The given URL is malformed.",
"Error Reading Google Document",
JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} catch (IOException e) {
JOptionPane.showMessageDialog(GoogleDocSSCCE.this,
"Unable to read the document.",
"Error Reading Google Document",
JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} finally {
if (!success) {
// We failed.
docPane.setText(new String());
}
}
}
}).start();
}
}
最佳答案
参见 LoboBrowser应用程序接口(interface)。
例子。
import org.lobobrowser.gui.*;
import org.lobobrowser.main.*;
import javax.swing.*;
public class Browser extends JFrame {
public Browser(string docid)
{
FramePanel browser = new FramePanel();
add(browser);
browser.navigate("https://docs.google.com/document/d/" + docid + "/pub/");
}
public static void main(String[] args)
{
Browser b = new Browser("1jG_rNCfVSD8yhHB9ZgA5YicXK_yDOl9T-fItIgmKa-o");
b.setSize(400, 400);
b.setVisible(true);
}
}
关于java - JEditorPane 显示带有 Google Docs 的原始 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16139702/