Publishing RPGIV Generated Invoices with Cocoon
When I first tried out Cocoon I was impressed, but I wasn't sure how I could take advantage of its capabilities on the iSeries. One of the best ways to try out a new technology is to build a proof-of-concept prototype that demonstrates that technology's capability. During the process, you will come to understand how the technology fits in your business, and you are likely to see some weaknesses. I decided to publish invoices generated by an RPGIV program.
The prototype takes an RPGIV generated XML invoice to a PDF document that is displayed in the browser. First, the RPGIV program generates the XML invoice document. When a user requests the invoice from their browser, Cocoon parses the XML invoice using the Xerces parser and locates a stylesheet reference. Next Cocoon transforms the document using the Xalan XSLT processor. The transformation combines the XML invoice document with XSL-FO elements. Finally, Cocoon uses FOP to render the combined document, which is displayed as a PDF file in your browser.
The first thing I created for the prototype was an Integrated File System (IFS) support toolkit. That toolkit, which is included with the download files, uses the Unix APIs. The procedures in the toolkit can create, open, close, read from, write to, and set the authority of IFS files and are found in the UNIX module. Those procedures also supply an interface that makes it easy to perform the supported tasks. The toolkit packaged in a save file. Copy the save file to your iSeries system with FTP and restore the library Cocoon. The FTP commands should look like this:
quote site namefmt 1
Next, I created a program that uses the IFS toolkit to create a file in Cocoon's publishing path. The test module is TSTXML, which builds a well-formed XML document. That document is stored using the ISO-8859-1 character set, also known as Latin-1, which all XML parsers support. After building writing out a document declaration and stylesheet reference, the test program writes an XML processing instruction that tells Cocoon this document to transform the document using an XSLT processor. Following this heading information is the body of the invoice, which is contained in the invoice root element.
After mocking up a test XML invoice, I tested transformation using the Xalan XSLT processor from a DOS command prompt. The command to do this looks something like (but on one line):
Java -classpath "d:\xalan\bin\xalan.jar;d:\xalan\bin\xerces.jar"
org.apache.xalan.xslt.Process -IN invoice.xml -XSL invoice-fo.xsl -OUT
invoice.fo -INDENT 2
Once I was satisfied with that transformation, I ran FOP from a command line using an Ant task. FOP is an Apache XML sub-project, and Ant is a Jakarta Apache sub-project. FOP is a print formatter that is driven by XSL Formatting Objects, Ant is a build utility that facilitates the scripting of Java related tasks. To set up the Ant task I used a version of the runtests.bat file found in the docs/examples and created a custom build.xml file.
After making sure that all of the processes worked individually, I placed my XML invoice and associated XSLT document into Tomcat's Cocoon context on my iSeries system. Next, I tested by entering the URL of the XML invoice, followed by a parameter that tells Cocoon to launch the PDF viewer. Here are the results of that test:
The biggest challenge I encountered while creating this prototype were slight variations in the various Apache sub-project releases. For example, I used a table with a header and footer to build the body of the invoice, which worked great with FOP version .17. When I went to render the same document using Cocoon's .15 version of FOP, I found that table headers, footers, and column span were not supported. I found similar differences in the various XML parsers and XSLT processors.