2013-04-22 83 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

相关问题