Home
Download
Lobo Browser
Cobra Toolkit
  Download
  Getting Started
  HTML Parser
  API Docs
JavaFX/Java
Source Code
Contact Us
SF Services
Donations
Thanks

 
SourceForge.net Logo
Lobo@SF
 
 
Support This Project
 
 

Cobra: Getting Started

This page provides an overview of the Cobra API and tips for getting started.

Test Program

Cobra comes with a test program that shows how a URL is rendered and its HTML DOM as a tree representation. It may be run as follows:

set CLASSPATH=cobra.jar;js.jar
java org.lobobrowser.html.test.TestEntry

A few other test tools are available, such as the ParserTest class which may be used to test the Cobra HTML parser independently of the renderer.

API Documentation

Cobra API documentation is available online and also in the installed distribution.

HTML Parser Usage

See the HTML DOM Parser information page.

Simple Rendering Engine Usage

A Swing component, HtmlPanel, can render a HTML document, as follows:
import org.lobobrowser.html.parser.*;
import org.lobobrowser.html.test.*;
import org.lobobrowser.html.gui.*;
import org.lobobrowser.html.*;
import org.w3c.dom.*;
...
HtmlPanel panel = new HtmlPanel();
// This panel should be added to a JFrame or
// another Swing component.
UserAgentcontext ucontext = new SimpleUserAgentContext();
SimpleHtmlRendererContext rcontext = new SimpleHtmlRendererContext(panel, ucontext);
// Note that document builder should receive both contexts.
DocumentBuilderImpl dbi = new DocumentBuilderImpl(ucontext, rcontext);
// A documentURI should be provided to resolve relative URIs.
Document document = dbi.parse(new InputSourceImpl(documentReader, documentURI));
// Now set document in panel. This is what causes the document to render.
panel.setDocument(document, rcontext);

Incremental Rendering

Cobra supports incremental HTML rendering. The document does not need to be loaded fully to be rendered. This is particularly useful when there are external scripts that need to load before parsing completes.

Instead of parsing the whole document before passing it to HtmlPanel, you can create an empty one by calling createDocument instead. This will return an instance of HTMLDocumentImpl. Next, you set the document in HtmlPanel by calling setDocument. Finally, you parse the document (which should be done outside the GUI dispatch thread) by calling HTMLDocumentImpl.load.

If you need to listen on document modification notifications as the document loads, see the addDocumentNotificationListener method.

The navigate method of SimpleHtmlRendererContext implements a simple incremental HTML rendering routine.

Examples

  1. Bare minimum renderer.
    This example contains the minimum necessary code to open a JFrame and render a HTML page (our browser home page in this case) with Cobra.

    BareMinimumTest.java

  2. Parsing and rendering with preferred width.
    This example is more elaborate and it shows (1) how to disable most Cobra logging, (2) how the caller can parse a HTML page and create a document instance, (3) how to create a window with a preferred width, whose preferred size is determined by the HTML content, and (4) how to override Cobra routines to open a new browser window on Javascript window.open() calls.

    BarebonesTest.java

Interface Implementations

Note that Cobra is a low-level HTML engine. Browser functionality such as navigation, cookies, HTTP authentication and so on are not part of Cobra. Simple implementations of context interfaces are provided with Cobra, but they generally need to be at least extended to implement necessary browser functionality.

The Lobo Browser API (GPL) is a separate API library we provide. This is a browser API as opposed to an HTML API. It uses Cobra as part of its HTML extension. In situations where users need to embed a browser in a Swing application, without concern for the way browser features are implemented, the Lobo Browser API is a good alternative to Cobra.

Help

If you require community assistance, please visit our Help Forum.

If you find something doesn't work quite the way it should, please submit a bug report or a feature request.

See Also

Support The Project