用于Scrollable窗格的纯Java HTML查看器/渲染器 [英] Pure Java HTML viewer/renderer for use in a Scrollable pane

查看:222
本文介绍了用于Scrollable窗格的纯Java HTML查看器/渲染器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有哪些纯Java HTML查看器和渲染器可用?要求如下:


  • 它应该实现 JComponent 接口放入 Scrollable 窗格。

  • 它应该是一个免费的解决方案;开源是一个优点。

  • 它作为Maven工件的可用性是一个优点。


I只知道一些组件和项目,其中一些现在已经不存在了:


解决方案

自Java 8开始,您可以使用JavaFX的 WebView组件,它也可以是在Swing中使用



代码非常简单:

  JFXPanel jfxPanel = new JFXPanel(); //可滚动JCompenent 
Platform.runLater(() - > {// FX组件需要由JavaFX
管理WebView webView = new WebView();
webView.getEngine()。 loadContent(< html> Hello World!);
webView.getEngine()。load(http://www.stackoverflow.com/);
jfxPanel.setScene(new Scene webView));
});

它由WebKit引擎支持(版本取决于JRE并且是最新的)。
但请记住,它不是一个完整的浏览器,所以不要指望支持HTML5音频/视频。
否则,它运行的HTML + CSS + JS与浏览器一样好。



从技术上讲,底层引擎是C ++,而不是本地Java。
但它捆绑在Oracle的官方JRE中,无需库,零配置,与Java FX一样跨平台,并且正在进行更新和维护。



<对于大多数使用情况来说,与原生Java一样好,我认为?





下面已经过时了,因为我们现在已经有了Java的WebView。


试过 Cobra / Lobo CSSBox 飞碟,全是纯Java。其他人不论是本地的还是商业的。

内容:即时生成的简单HTML(如字符串),嵌入式CSS 2.1,无JS。

短篇小说:飞碟最简单易用,渲染效果最正确,但您最好完全控制内容。



长篇小说:

CSSBox似乎更加活跃,但看起来似乎依赖于一些第三方库。例如,演示依赖于使用apache xerces的nekohtml,它改变了默认的Java 1.7 sax解析器的工作方式并破坏了我的程序,但是当我强制它使用内置在xerces中的java时,我得到 ClassCastException(InlineBox to BlockBox)。最后无法让它工作。 Plus仍然没有找到一种方法来替换现有的 BrowserCanvas 中的文档。



不再维护眼镜蛇,必须手动修复不兼容问题,才能使其在1.7版本中正常运行。还需要抢mozilla犀牛(不使用任何JS),但就是这样。之后,它非常流畅,只需要让Logger隐藏油漆信息即可。渲染是正确的,速度是公平的 - 只要文档很简单。当你开始使用较不常见的标签或更复杂的布局时,Cobra就会快速分崩离析。

飞碟在撰写时拥有最好的CSS支持(2011年2月)。安装非常简单(例如,不需要设置像cobo这样的文档或像cssbox这样的domparser)几乎没有依赖性 - 这也意味着没有JavaScript。但飞碟对你的食物非常严格。源必须是格式良好的XML,例如样式和脚本可能必须包装在CDATA中,并且如果使用html实体,则必须声明DTD(因此不包含html5文档类型)。但是,如果您嵌入的内容可以控制,那么它可能是您的最佳选择。


What pure Java HTML viewers and renderers are available? The requirements are:

  • It should implement the JComponent interface to be placed into Scrollable pane.
  • It should be preferably a free solution; open source is a plus.
  • Its availability as Maven artifact is a plus.

I know only of a few components and projects, some of which are now defunct:

解决方案

Since Java 8, you can use JavaFX's WebView Component, which can also be used in Swing.

Code is as simple as:

JFXPanel jfxPanel = new JFXPanel(); // Scrollable JCompenent
Platform.runLater( () -> { // FX components need to be managed by JavaFX
   WebView webView = new WebView();
   webView.getEngine().loadContent( "<html> Hello World!" );
   webView.getEngine().load( "http://www.stackoverflow.com/" );
   jfxPanel.setScene( new Scene( webView ) );
});

It is backed by the WebKit engine (version depends on JRE and is reasonably up to date). But keep in mind that it is not a full browser, so don't count on support of, say, HTML5 audio/video. Otherwise, it runs HTML + CSS + JS as good as your browser.

Technically, the underlying engine is C++, not native Java. But it is bundled in Oracle's official JRE, requires no library, has zero config, is as cross-platform as Java FX, and is actively updated and maintained.

As good as native Java for most use cases, I think?


The information below is outdated, seeing that we now have WebView in Java.

Tried Cobra/Lobo, CSSBox, and Flying Saucer, all pure Java. Others are either native or commercial.

Content: Simple HTML generated on the fly (as string), embedded CSS 2.1, no JS.

Short story: Flying Saucer is simplest to use and render is most correct, but you better have full control over content. Otherwise look for a native solution.

Long story:

CSSBox seems to be more active, however it seems to depends on some 3rd party libraries. For example the demo depends on nekohtml which use apache xerces which changed the way the default Java 1.7 sax parser works and broke my program, but when I force it to use java's built in xerces I get ClassCastException (InlineBox to BlockBox). Can't get it to work at the end. Plus still haven't found a way to replace the document in an existing BrowserCanvas.

Cobra is no longer maintained, have to manually fix an incompatibility issue to make it works in 1.7. Also need to grab mozilla Rhino (not using any JS) but that is all. After that it is fairly smooth, just need to ask Logger to hide paint messages. Render is correct and speed is fair - as long as the document is simple. When you start to use less common tags or more complicated layout, Cobra falls apart pretty quickly.

Flying Saucer has the best CSS support of the three as of writing (Feb 2011). Setup is very easy (e.g. no need to setup document like cobo or domparser like cssbox) has few dependency - which also means no javascript. But Flying Saucer is very strict about what you feed it. The source must be a well-formed XML, for example style and script may have to be wrapped in CDATA and if you use html entities you must declare DTD (so no html5 doctype). However if you are embedding content that you can control then it may be your best choice.

这篇关于用于Scrollable窗格的纯Java HTML查看器/渲染器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆