我试图将已发布的Google文档加载到JEditorPane
。JEditorPane使用Google文档显示原始HTML
以下是文档:link。
这里是一个JEditorPane如何呈现它:
我的意见 - 从图像:
- 的HTML,正在获取正常。
JEditorPane
支持至少一些 CSS(注意顶部的阴影栏)。JEditorPane
在HTML源代码中的第二个<style type="text/css">
块中变得非常困惑。是否因为<style>
位于<div>
而不是<head>
?- 在代码中的某些空格处有奇怪的工件(U + 00C2,十进制194;拉丁大写字母A,带有回纹),它们实际上是香草
U+0020
空间。这可能与字节顺序有关吗? (我已验证的人物实际上是获取这种方式,通过println
荷兰国际集团的每一行。)
我读过关于这个问题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();
}
}
的Java只支持HTML 3.2 – 2013-04-22 05:09:14
尝试从头部 – 2013-04-22 05:19:39
@SriHarshaChilakapati我会尝试删除脚本标记删除脚本标记。将正则表达式足够了(类似''),还是我需要使用完整的DOM解析器? – wchargin 2013-04-23 19:20:39