게시 된 Google 문서를 JEditorPane
으로로드하려고합니다.JEditorPane은 Google 문서로 원시 HTML을 표시합니다.
여기에 문서가 있습니다 : link.
여기 JEditorPane로 그것을 렌더링 방법은 다음과 같습니다 이미지에서
내 관찰 :
- html로 제대로 가져 오는 중입니다.
JEditorPane
은 적어도 일부를 지원합니다. CSS (맨 위에 음영 막대가 있음).JEditorPane
은 HTML 소스의 두 번째<style type="text/css">
블록에서 혼란스러워집니다.<style>
이<div>
안에 있고<head>
이 아니기 때문입니까?- 코드의 특정 공백에 실제로는 바닐라
U+0020
공백 인 홀수 아티팩트 (U + 00C2, 십진수 194, 곡절 기호가있는 라틴어 대문자 A)가 있습니다. 이 바이트 순서와 관련이 있을까요? 나는이 주제에 this StackOverflow post를 읽고 그것을 구현했습니다
(나는. 문자가 실제로 각 행을 보내고 println
하여이 방법을 인출하는 것을 확인했습니다)하지만,이 문제를 해결 아니에요.
CSS 지원이 전체적으로 드문 경우 (예 : http://www.stackoverflow.com
은 많은 파란색 상자가있는 바람직하지 않은 결과를 산출하지만 실제 HTML 코드 또는 아티팩트는 공개되지 않음)
JEditorPane
대신 JTextPane
을 사용하면 동일한 결과가 생성됩니다.
DTD를 문서 상단에 추가하면 (XHTML 4.1 Transitional과 HTML5의 <!DOCTYPE html>
모두 시도 됨) 작동하지 않습니다.
왜 이런 일이 발생하고 어떻게 해결할 수 있을지에 대한 의견이 있으십니까?
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();
}
}
자바는 HTML 3.2 –
내가 스크립트 태그를 제거하려고합니다 머리 –
@SriHarshaChilakapati에서 스크립트 태그를 제거하십시오 지원합니다. 정규 표현식으로 충분합니까 ('') 또는 전체 DOM 구문 분석기를 사용해야합니까? – wchargin