Generating Java using XSLT

| 6 Comments | 1 TrackBack

We are working on yet another language migration tool and faced once again Java source code generation problem. Unfortunately Java doesn't have anything similar to .NET's CodeDOM, so we had to build own own Java generator. This time our development platform is XSLT 2.0. Yes, we are converting COOL:Gen (obscure 4GL model-based language) to Java using XSLT 2.0.

XSLT 2.0 rocks by the way. This is first time I write production code in XSLT 2.0 and this is amazing experience. Suddenly all is so easy, everything is possible, no hassle. Despite poor authoring support (Eclipse XSLT editor sucks, while Visual Studio 2008 with XSLT 2.0 schema is ok, but cannot run Saxon), lack of debugger and Saxon quirks I had a blast practicing XSLT 2.0 for real.

At first I started generating Java beans simple way: output mode="text" and producing Java sources as text. Obviously it sucked big way. I spent a week and got it done, but with way too cumbersome and fragile code. Generating code and simultaneously coping with Java syntax and formatting is hard. Additional layer of indirection was needed desperately.

One of smart guys I work with came with a simple but brilliant idea. Vladimir took Java 6 ANTLR grammar and converted it to XML Schema. Then he developed a generic serializer (also in XSLT 2.0 of course) that is able to convert XML document confirming to Java XML schema (he called it JXOM -  Java XML Object Model) into nicely formatted and optimized decent Java 6 source code.

Then I rebuilt my Java bean generator using JXOM instead in just one day. Building Java as XML is so much easier and cleaner, I believe it's even easier than using System.CodeDom in .NET (obviously CodeDom can do more than just generate C# or VB sources).

Anyway, anybody interested in Java generation - check out JXOM. This is really easy way to generate Java 9even Java 6.0) using XSLT. It's freely available and it just works. Here are more links:

  1. Java xml object model
  2. Xslt for the jxom (Java xml object model)
  3. jxom update

JXOM is ready to use, but still under active development. Any feedback is highly appreciated at Vladimir and Arthur Nesterovsky blog.

Related Blog Posts

1 TrackBack

TrackBack URL: http://www.tkachenko.com/cgi-bin/mt-tb.cgi/736

6 Comments

What's up with XSLT 2 for .NET? There still seems to be absolutely no light at the end of the tunnel.

How much the saxon is fast ?
I used AltovaXML but it is very slow when you transform big files :(

Hi,

have you tried using J# under VisualStudio and basing on it generate string with code using VJSharpCodeProvider? I have already finished my master project - system generating mobile applications (written under .net but generating Java and .Net applications). Of course it wasn't easy, but possible and VJSharpCodeProvider helped. When i find some time Iam gonna check jxom!

You might want to start looking at the new XSLT incubating project in eclipse. The next version we are releasing will have many usability improvements for the editor. It's not as robust as it could be, but we are always looking for comments on how to improve it.

http://www.eclipse.org/webtools/incubator

>> XSLT 2.0 rocks by the way. This is first time I write production code in XSLT 2.0 and this is amazing experience.

It's like a whole new world, isn't it :D

>> Despite poor authoring support (Eclipse XSLT editor sucks, while Visual Studio 2008 with XSLT 2.0 schema is ok, but cannot run Saxon), lack of debugger and Saxon quirks I had a blast practicing XSLT 2.0 for real.

This is cool.

Now, what could be possible would be to design an XML syntax for Java source, and patch the eclipse javac compiler to handle it. Then life would be even simpler. That is, provided java is hierarchical enough to have a workable schema.

Leave a comment