Writing your own templates | ||
We do not discuss in detail here, how to write Velocity templates, but we want to give some ideas on writing templates to be used with AXgen. Before you start you may wish to take a look at the Jakarta Velocity site to get familiar with Velocity concepts.. Furthermore you may wish to take a closer look at AXgen's API doc, especially the de.armax.ax.devtool.uml package containg our UML abstraction layer to learn about the capabilities and ideas of the metamodel classes you may use in the templates. | ||
Developing a Template | ||
We choosed Velocity because it provides powefull means by giving access to arbitrary Java objects inside your templates allowing a great flexibility generating code from UML metamodels. You may access the UML metamodel through AXgen's own UML abstraction layer from inside the templates, providing easy access to model elements and their different properties. As AXgen is independent of a specific UML implementation, your templates will equally be UML implementation independant. | ||
AXgen iterates the model and starts generation for each model element that matches certain criteria (type of the element, values of some attributes, ...). The current model element is accessible in the templates through the velocity variable "$axelement", this variable may contain an AXclass or an AXpackage depending on the configuration. This it what it may look like with Velocity: | ||
#set ($classname = "$axelement.getName()") #set ($attribs = $axelement.getAttributes()) #set ($assocs = $axelement.getXToNAssociations(false)) |
||
Looking at the code snippet you notice that the first assignment is done with quotes. The result is that $classname is a String variable whereas $attribs is a List of AXattribute objects and $assocs is a List of AXassociation objects (presuming that "$axelement" contains an instance of AXclass). | ||
The next snippet shows you how to iterate over a List and use if statements to produce code. | ||
#foreach ($attrib in $attribs) #if ($attrib.getType().isVoidType() == false) private $attrib.getType().getQualifiedName() $attrib.getNameForVariable(); #end #end |
||
For more complex code, AXgen allows you to define a helper class accessible in the templates through the "$helper" variable. The helper class may contain arbitrary methods that can be used in the template. By default, AXgen uses AXojbHelper as helper class. Feel free to write your own helper class to to do any formatting or transformation operations. For details on helper classes see the API doc for AXgen and the API doc for AXojbHelper. | ||
As mentioned before this is just a crude introduction to Velocity. If you miss some explanation please give us feedback. It may also be interesting to have a look at the templates shipped with AXgen to get some ideas on writing templates. | ||
See the Use AXgen section to learn how to configure AXgen to use your own templates. |