【发布时间】:2013-04-14 22:01:28
【问题描述】:
我正在尝试将已发布的 Google 文档加载到 JEditorPane。
这是文档:link。
以下是 JEditorPane 的呈现方式:
我对图片的观察:
- 正在正确提取 HTML。
-
JEditorPane至少支持一些 CSS(注意顶部的阴影条)。 -
JEditorPane在 HTML 源代码中的第二个<style type="text/css">块中非常困惑。是因为<style>在<div>内,而不是<head>? - 在代码中的某些空格处存在奇怪的伪影(U+00C2,十进制 194;带有抑扬符的拉丁文大写字母 A),实际上是普通的
U+0020空格。这可能与字节顺序有关吗? (我已经验证了这些字符实际上是通过printlning 每一行来获取的。)
我已经阅读了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
-
尝试从头部删除脚本标签
-
@SriHarshaChilakapati 我将尝试删除脚本标签。正则表达式就足够了(类似于
<script>.*?</script>)还是我需要使用完整的DOM解析器? -
Regex 应该足够了,我认为但对此没有任何其他想法。我曾经在编辑器窗格中显示 JavaDoc 时遇到过这种情况并放弃了它。您也可以尝试将编辑器工具包更改为 HTMLEditorKit。也许它可以工作。
标签: java css url jtextpane jeditorpane