ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Template-Based Code Generation with Apache Velocity, Part 2
Subject:   It's proven to work in practice
Date:   2004-06-11 06:34:25
From:   Jesco
This article precisely describes the code generation system of Poseidon for UML, which I happened to help write early in 2002.


The IOM of Poseidon is the UML 1.4 model, which is a real mess to traverse in a language like Velocity. We decided that we need an intermediate level of objects that the templates should access, rather than the raw UML model. Our PSM of the UML model pre-processes the information, prepares it for the templates, and so we called the classes "PreparedElements". A PreparedAttribute represents the original UML attribute, has a reference to it, and provides useful methods for Java code generation through Velocity. The classes of the PSM resemble the original classes of the UML metamodel (see an image at Gentleware's web site but provide Java-specific helper methods such as "getTypeAsString", which returns the type's name, filtered for Java-suitability (such as handling of space characters).


To provide more flexibility, we found it useful to choose different templates based on properties of the IOM objects, such as the UML stereotype. So we have a mapping of (element type + stereotype) to template file. And finally, the name of the file is also encoded in the template, so that it is easy to change the way the file name is produced without touching the program code itself. As you can access Java objects from a template, we have a line like $fileNameHolder.setFileName("${className}.cpp") in the C++ class template. The generation system writes to a temporary file first; after that, the object that we put in the Velocity context under the name "fileNameHolder" has been given the final file name; and so we change the temporary file name to the final name.


1 to 1 of 1
  1. It's proven to work in practice
    2006-01-10 08:07:32  mustafabulut [View]

1 to 1 of 1