2013-04-22 47 views
0

我試圖將已發佈的Google文檔加載到JEditorPaneJEditorPane使用Google文檔顯示原始HTML

以下是文檔:link

這裏是一個JEditorPane如何呈現它:

rendered

我的意見 - 從圖像:

  1. 的HTML,正在獲取正常。
  2. JEditorPane支持至少一些 CSS(注意頂部的陰影欄)。
  3. JEditorPane在HTML源代碼中的第二個<style type="text/css">塊中變得非常困惑。是否因爲<style>位於<div>而不是<head>
  4. 在代碼中的某些空格處有奇怪的工件(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(); 
    } 
} 
+0

的Java只支持HTML 3.2 – 2013-04-22 05:09:14

+0

嘗試從頭部 – 2013-04-22 05:19:39

+0

@SriHarshaChilakapati我會嘗試刪除腳本標記刪除腳本標記。將正則表達式足夠了(類似''),還是我需要使用完整的DOM解析器? – wchargin 2013-04-23 19:20:39

回答

0

LoboBrowser API。

例子。

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);    
    } 

} 
+0

謝謝,但我真的不想要一個完整的瀏覽器;相反,我只想渲染一個頁面。我可以做到這一點嗎?另外我不想包含一個巨人(16MB對我的項目來說非常大)依賴。 – wchargin 2013-04-23 13:53:55

+0

我可以使用底層Cobra庫來達到這個目的嗎?另外,是否可以將Cobra庫從3.7MB簡化爲渲染器(不需要JS等)? (即使沒有,3.7MB也好於16MB) – wchargin 2013-04-23 19:23:06

+0

顯然[Cobra和LoboBrowser已死](http://stackoverflow.com/a/9904090/732016),並且已經爲我(和其他人)產生了錯誤。你有另外一個建議嗎? – wchargin 2013-04-24 00:26:18