Xml Xsl FOP

One of the ‘new’ features that was requested in our app was the forwarding of screens to email addresses. We discussed this and adding a simple pdf version of the screen as attachment to the mail was ok.
As at the moment our application isn’t xml based (just Html with custom Javascript,Dojo and DWR) I suggested to first ‘xmlize’ our messages and then convert this xml to a pdf. At previous customers I used Velocity for performing the Xml serialization. But before I suggested this solution I read up on Groovy as I had read somewhere that it has first class support for Xml. And indeed after checking it out. You have a simple MarkupBuilder object that handles a lot for you.


import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)

xml.Blog(id:"${blog.id}",type:"${blog.type}"){
Content("${blog.content}"){}
}

output = writer.toString()

This small piece of code will generate the following Xml:



Just some content

As I installed the Groovy plugin in Eclipse I get errors when my Xml is false. Not bad at all.
The really nice thing about this is that you just initialize your Groovy engine in Java. You add your Java object (in my example blog) and you can use it as if it was a Groovy object. Then I return the output to my Java code.

Sweet!

As this generates Xml I used a simple Xsl stylesheet to generate an intermediate Xml. So all objects get transformed into this intermediate Xml. This xml gets transformed in to FOP xml and with the nice Apache FOP library I generate a simple pdf.

Another added value is that for changing my pdf or Xml I don’t have to restart my application, so no downtime!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.