Browse DevX
Sign up for e-mail newsletters from DevX


Getting Tidy with Eclipse : Page 2

Eclipse's extensibility makes it the perfect collaborator. Learn how to build custom views in Eclipse that integrate with other useful public libraries. For example, in this article, you'll learn how to create a split pane view in Eclipse that cleans malformed HTML files using JTidy.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Cleaning up HTML with JTidy
Taking existing HTML and executing JTidy against it is a very simple process. I create a FileInputStream to read in the file and a FileOutputStream to write the result of JTidy. With an instance of Tidy, I simply call the parse method with my FileInputStream and FileOutputStream. Finally, I close both streams. Here is the code:

Tidy tidy = new Tidy(); BufferedInputStream in = new BufferedInputStream(newFileInputStream("foo.html")); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("bar.html")); tidy.parse(in, out); in.close(); out.close();

The generated output can vary significantly depending on the HTML in your input file. If the HTML is good there will likely be little change. However, if the HTML is bad JTidy might even throw an exception. Either way, pay close attention to the messages JTidy writes for information on what it's changing and why. You'll often find that it issues several warnings for things that it considers wrong, but can't fix automatically.

Rendering HTML with Eclipse
In the past, whenever I wanted to render HTML in a Java application it was a real pain. The built-in HTML renderer wasn't very good and really didn't reflect what the HTML might look like in a mainstream browser. With Eclipse this is incredibly easy thanks to SWT. If you aren't aware of SWT, it is the widget toolkit provided by Eclipse that performs much the same function as Swing. However, unlike Swing, SWT is just a thin Java wrapper on top of native UI widgets. I mention this because SWT's browser widget follows suit by providing a thin wrapper on top of a native browser. This approach has many obvious benefits not the least of which is that the browser widget will render exactly the same as the native browser.

Making use of the SWT browser to render HTML takes only two lines of code. First, I instantiate an instance of the browser. Then, I ask the browser to render a URL. The code is as follows:

Browser browser = new Browser(parent, SWT.NONE); browser.setUrl("http://www.devx.com");

The constructor for the browser widget takes two parameters. The first parameter is the parent composite in which the browser widget will be contained. The second parameter is any style bits you want to provide.

Thanks for your registration, follow us on our social networks to keep up-to-date