RSS 2.0
Sign In
# Saturday, 16 February 2008

Hello again!

To see first part about jxom please read.

I'm back with jxom (Java xml object model). I've finally managed to create an xslt that generates java code from jxom document.

Will you ask why it took as long as a week to produce it?

There are two answers:
1. My poor talents.
2. I've virtually created two implementations.

My first approach was to directly generate java text from xml. I was a truly believer that this is the way. I've screwed things up on that way, as when you're starting to deal with indentations, formatting and reformatting of text you're generating you will see things are not that simple. Well, it was a naive approach.

I could finish it, however at some point I've realized that its complexity is not composable from complexity of its  parts, but increases more and more. This is not permissible for a such simple task. Approach is bad. Point.

An alternative I've devised is simple and in fact more natural than naive approach. This is a two stage generation:
  a) generate sequence of tokens - serializer;
  b) generate and then print a sequence of lines - streamer.

Tokens (item()*) are either control words (xs:QName), or literals (xs:string).

I've defined following control tokens:

Token Description
t:indent indents following content.
t:unindent unindents following content.
t:line-indent resets indentation for one line.
t:new-line new line token.
t:terminator separates token sequences.
t:code marks line as code (default line type).
t:doc marks line as documentation comment.
t:begin-doc marks line as begin of documentation comment.
t:end-doc marks line as end of documentation comment.
t:comment marks line as comment.

Thus an input for the streamer looks like:

<xsl:sequence select="'public'"/>
<xsl:sequence select="' '"/>
<xsl:sequence select="'class'"/>
<xsl:sequence select="' '"/>
<xsl:sequence select="'A'"/>
<xsl:sequence select="$t:new-line"/>
<xsl:sequence select="'{'"/>
<xsl:sequence select="$t:new-line"/>
<xsl:sequence select="$t:indent"/>
<xsl:sequence select="'public'"/>
<xsl:sequence select="' '"/>
<xsl:sequence select="'int'"/>
<xsl:sequence select="' '"/>
<xsl:sequence select="'a'"/>
<xsl:sequence select="';'"/>
<xsl:sequence select="$t:unindent"/>
<xsl:sequence select="$t:new-line"/>
<xsl:sequence select="'}'"/>
<xsl:sequence select="$t:new-line"/>

Streamer receives a sequence of tokens and transforms it in a sequence of lines.

One beautiful thing about tokens is that streamer can easily perform line breaks in order to keep page width, and another convenient thing is that code generating tokens should not track indentation level, as it just uses t:indent, t:unindent control tokens to increase and decrease current indentation.

The way the code is built allows mimic any code style. I've followed my favorite one. In future I'll probably add options controlling code style. In my todo list there still are several features I want to implement, such as line breaker to preserve page width, and type qualification optimizer (optional feature) to reduce unnecessary type qualifications.

Current implementation can be found at jxom.zip. It contains:

File Description
java.xsd jxom xml schema.
java-serializer-main.xslt transformation entry point.
java-serializer.xslt generates tokens for top level constructs.
java-serializer-statements.xslt generates tokens for statements.
java-serializer-expressions.xslt generates tokens for expressions.
java-streamer.xslt converts tokens into lines.
DataAdapter.xml sample jxom document.

This was my first experience with xslt 2.0. I feel very pleased with what it can do. The only missed feature is indirect function call (which I do not want to model with dull template matching approach).

Note that in spite that xslt I've built is platform independed I want to point out that I was experimenting with saxon 9. Several times I've relied on efficient tail call implementation (see t:cumulative-integer-sum), which otherwise will lead to xslt stack overflow.

I shall be pleased to see your feedback on the subject.

Wednesday, 03 December 2008 19:11:14 UTC
Looks great! I'm quite interested in using it to translate a number of C# interfaces and enumerations into Java. How about putting together a short section on how to invoke the various XSLT files? I can generate the appropriate XML files per the example on the other page but not sure about the XSLT steps required afterwards.

Thanks!
Brian
Thursday, 04 December 2008 04:36:14 UTC
Brian,

you're quite right, we shall add mode description on the jxom use.

Meanwhile you may start from java-serializer-main.xslt.

This sample:
a) recieves input in jxom scheme;
b) optimizes type qualifiers;
c) optimizes unreachable code (in java it's an error to have unreachable code);
d) serializes output as java text.

There are other services not presented in this sample:
a) refactoring complex expressions;
b) fixing up variable names
(when you're composing input from several blocks it
may have dublicate variables; fixup resolves this);
c) state machine generator.

It will take a little time to grasp jxom scheme.
The root is "unit" element.
Use any xml schema aware editor (e.g. Visual Studio) with java.xsd.

Thanks.
Vladimir Nesterovsky
Wednesday, 10 December 2008 06:34:09 UTC
The example showing the Java interface & XML equivalent are good but it would be better if it also contained a concrete class that extended another class/implemented an interface, an enumeration or two, etc. Working from the XSD isn't as clear as having a concrete example. Looking forward to your expanded useage section.

Regards,
Brian
Brian
Wednesday, 10 December 2008 09:12:18 UTC
Brian,

I've uploaded jxom version containing examples for enum declaration and a class that corresponds that defined in the A simple cache for Java application.

We should probably need to introduce several test xslts to demonstrate other features (like name normalization).

Thanks
Vladimir Nesterovsky
Wednesday, 23 December 2009 15:03:34 UTC
Hey, Great Job done by your folks this is really very helpful stuff. Just wanted to know if the kind of license you guys are planning to release this thing.
Satya Suman
Tuesday, 26 January 2010 19:32:26 UTC
Hello Satya Suman!

What a shame! I've missed your comment.
Sorry.

At present languages xom are shared "as is".
You may use it for your own needs.

Vladimir Nesterovsky
Tuesday, 26 October 2010 13:28:35 UTC
Thanx, really useful!
easyrider
All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview
Archive
<2024 March>
SunMonTueWedThuFriSat
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456
Statistics
Total Posts: 386
This Year: 2
This Month: 0
This Week: 0
Comments: 938
Locations of visitors to this page
Disclaimer
The opinions expressed herein are our own personal opinions and do not represent our employer's view in anyway.

© 2024, Nesterovsky bros
All Content © 2024, Nesterovsky bros
DasBlog theme 'Business' created by Christoph De Baene (delarou)