Johnny's Software Saloon

Weblog where I discuss things that really interest me. Things like Java software development, Ruby, Ruby on Rails, Macintosh software, Cocoa, Eclipse IDE, OOP, content management, XML technologies, CSS and XSLT document styling, artificial intelligence, standard document formats, and cool non-computing technologies.

My Photo
Name:
Location: Germantown, Maryland, United States

I like writing software, listening to music (mostly country and rock but a little of everything), walking around outside, reading (when I have the time), relaxing in front of my TV watching my TiVo, playing with my cat, and riding around in my hybrid gas/electric car.

Saturday, August 07, 2004

WebRowSet (Java 2 Platform SE v1.5.0) - reads SQL data, outputs data in XML format

The new version of Java is introducing a new feature that will be a boon to a lot of the programmers out there in IT departments or working as consultants who get marching orders to "create an online report" or "create a data entry/reporting system based on the appearance of this existing paper form".

The Java javax.sql.rowset.WebRowSet (Java 2 Platform SE v1.5.0) class provides a simple way of reading back XML data from an SQL database.

From there, you can do any of the standard things that you can do with data that is in XML format. Generate a Swing/AWT GUI or PDF (using FOP), generate a chart/graph (using Batik [by generating SVG representation of histograms, line, charts, pie charts, and legends/titles] or JFreeChart), create an MS-Word or MS-Excel WordprocessingML or SpreadsheetML document, or render an HTML or XML web page to display the data or even let someone render it.

Generally, you can get the data into the right format for those things by just writing an XSLT stylesheet. Probably not a very long XSLT stylesheet either. You can accomplish some pretty miraculous transformations by just writing one to five pages of XSLT. It's very adroit at changing XML elements and attributes and text into another format; sorting, filtering, rearranging, and inserting/removing content along the way.

After all, that's what presentation is. Presentation format, data representation format - same thing. Just a little bit of transformation from a computer file, through some programs, on the way to your eyes. Or from your keyboard, to some data entry fields/controls, to some objects or structures, into some computer file or database table someplace. So it is little wonder a little presentation customization language based on data transformation would be a big help here. Big surprise maybe. But little wonder.

It is easy to convert data in XML format into a document in HTML or XHTML format. You just need to know HTML and, if you want to do it cleanly, some CSS.

It's a little more work to create an XSL-FO document, which you need to do to create PDF from XML, than it is to create an HTML document. There are three reasons for this, in my experience.

First, XSL-FO is a wordier format than HTML - so you are going to do about twice to three times as much typing compared with creating an HTML document. Two, the diagnostic messages you get back during XSL-FO sometimes are a little hard to interpret and reading the XSL-FO output (typically you can call this a .fo file) is harder than reading an HTML format. Third, the XSL-FO document format is concerned with page layout and most HTML pages, to my chagrin at least when I am printing a page off a web site that has a sidebar on it I don't want to print, do not struggle to do precise page layout at all - just very free-form screen layout.

Stylesheets are one of the most important technologies to come out for the web in the 1990s. Right behind HTML and XML, I would say.

Someone was just asking me this morning what was a good way to get XML formatted data so they could run XSLT on it and transform it into HTML or something that could be converted into a PDF report. This is one way to do it, if you want to read the data from a database via a high level interface layered over JDBC.

It's not hard to convert an SQL result set that comes back from an SQL query into XML format using Java. Why write any code if you don't have to, though - right???

So, got an SQL database and want good displayable and/or printable output? Why not generate a report by just using a WebRowSet object to get the data and give it to you in XML format, convert it by passing it and an XSLT stylesheet you write yourself to an XSLT processor like the one built into Java 2 (Xalan, part of JAXP and also available from the Apache web site), and then taking that resulting XML file and feeding it into the program you intended to read and use it.

Is it that easy?

Yep.

Of course if you lack confidence but have a lot of money and time, I could help you shop for some packages that cost over $100,000...

There is also another way to use all of this technology, except for the WebRowSet. That way is to use Apache Cocoon. Cocoon can already read SQL data in an XML format similar to the XML format that WebRowSet returns. Cocoon already has a way to hook up that XML data to your custom made XSLT stylesheet. Cocoon will let you take that output and route it to FOP (if you want a PDF document to come out) or through JTidy (if you want an HTML or XHTML document to come out). No Java code to write then, just create a couple short configuration files and write yourself a an XSLT stylesheet or two and you are done.

The other nice thing about Cocoon is that it runs on J2SE JDK 1.4 so you do not have to wait for JDK 1.5 to come out and stabilize. JDK 1.4 has been out for a couple of years. It's stable now.

You can download Cocoon's source distribution, compile it, download Tomcat 5's binary distribution and install it, throw the cocoon.war file you just generated into the Tomcat webapps directory, start up Tomcat, and be running Cocoon's examples and reading them all in less than half an hour. In the short term that might be a better way to create a solution than waiting for the JDK 1.5 technology. In the long run, you can always reuse your stylesheets from Java programs when JDK 1.5 has at long last arrived and become stable.


0 Comments:

Post a Comment

<< Home

Related pages & news