Wednesday, September 29, 2010

JavaScript Code Model

I was recently looking for a JAXB-like API for JavaScript which would allow converting between XML and JavaScript objects on the client. Such tool would be extremely useful when working with schema-based XML documents in JavaScript clients.

Apparently, there's no such thing. So I've started thinking about writing one. To put it frankly, the idea fascinated me so much that I simply could not help starting experimenting.

Today I've started with a code model for JavaScript. I'm writing an API which would allow generating syntactically correct JavaScript from Java. This will be somewhat similar to the code model library used in JAXB/XJC, only implemented for JavaScript. I took the ECMAScript specification and a couple of grammars and wrote over 50 interfaces which model the grammar of the JavaScript code. Here's a couple of examples of what I got so far:

JSProgram program = codeModel.program();

JSNumericLiteral one = codeModel.lit(1);
JSFunctionDeclaration factorial =
program.declare("factorial");
JSVariable x = factorial.parameter("x");
factorial.body()._return(
x.le(one).cond(one,
x.mul(codeModel.call(factorial).args(x.minus(one)))));


This should produce the code of the factorial function like:

function factorial(x)
{ return x <= 1 ? 1 : x * factorial(x - 1); }

The conditional could be rolled out into an if statement:

JSIfStatement _if = factorial.body()._if(x.le(one));
_if._then()._return(one);
_if._else()._return(
x.mul(codeModel.call(factorial).args(x.minus(one))));

I need this to be able to generate XML-JSON mappings in correct JavaScript syntax. String concatenation just does not feel right.

But what's probably the most important is that it's great fun to work on this. :)

Labels:

Sunday, September 26, 2010

Juneloop - tool for programmatic manipulation of JNLP files

We recently needed to create a JNLP file for Java Web Start programmatically from a template. Basically we needed to add a property element to an existing JNLP file. There were also similar requirements from a project partner, so all in all it goes into a direction of programmatic manipulation of JNLP files.

Since JNLP is defined in DTDs (1.5, 6.0) I though this would be a good JAXB project (and a good demo case for Maven JAXB2 Plugin). All I needed to do is compile JNLP DTDs with XJC, add a couple of tests and here you go:

Juneloop, a tool for programmatic manipulation of JNLP files.

With Juneloop you can:
  • unmarshal JNLP resources;
  • create or modify JNLP object structures programmatically;
  • marshal JNLP object structures.
See Juneloop documentation here:

Labels: , , , , , ,

Long Time No See

It was quite a while that I posted last time to this blog. In this time I did a huge amount of work on my open-source projects (visit confluence.highsource.org and see for yourself).

Now I've decided to come back to this blog and to use it to post announcements and updates about my OSS activities. New projects, new releases, new initiatives.

I'm certainly not hoping for a huge audience (I'm bad in marketing anyway), I just need an instrument which would help me to keep track of my developments. An maybe this will help you to find one or another useful tool.

Labels: