| Sign In/My Account | View Cart |
Template-Based Code Generation with Apache Velocity, Part 2
Pages: 1, 2, 3, 4
To provide a real example of code generation, let's consider the class diagram in Figure 6.

Figure 6. Class diagram for Order/OrderItem/Article
We can represent it with XML in the following way:
<?xml version="1.0" encoding="UTF-8"?>
<!-- order.xml -->
<Content>
<Class name="Order">
<Attribute name="number" type="integer"/>
<Attribute name="date" type="date"/>
</Class>
<Class name="OrderItem">
<Attribute name="number" type="integer"/>
<Attribute name="quantity" type="integer"/>
</Class>
<Class name="Article">
<Attribute name="code" type="integer"/>
<Attribute name="description" type="integer"/>
</Class>
<Association name="">
<Role class="Order" multiplicity="1" name=""
type="start" navigable="true"/>
<Role class="OrderItem" multiplicity="*" name=""
type="end" navigable="true"/>
</Association>
<Association name="">
<Role class="OrderItem" multiplicity="*" name=""
type="start" navigable="false"/>
<Role class="Article" multiplicity="1" name=""
type="end" navigable="true"/>
</Association>
</Content>
Then we can create an importer that creates the Internal Object Model starting from that XML information -- inside of the source code for this article (in the "Resources" section below) you can find an implementation of such an importer. Finally, we can create a main program having three command-line parameters: the input file, an alias for the importer implementation, and an alias for the exporter implementation. Here's the code:
import com.codegenerator.velocity.*;
import java.util.*;
public class Generator {
public static void main(String args[]) throws Exception {
if (args.length!=3) {
System.out.println(
"Syntax: Generator <input-file> <importer> <exporter>");
System.exit(1);
}
Importer importer = null;
if (args[1].equals("XML"))
importer = new XMLImporter(args[0]);
else {
System.out.println("Importer " + args[1] +
" non found!");
System.exit(1);
}
Exporter exporter = null;
if (args[2].equals("IOMVelocityExporter"))
exporter = new IOMVelocityExporter();
else if (args[2].equals("PSMVelocityExporter"))
exporter = new PSMVelocityExporter();
else {
System.out.println("Exporter " + args[2] +
" non found!");
System.exit(1);
}
importer.start();
IOMNavigator navigator = new IOMNavigator(exporter);
navigator.start();
}
}
Note: We will see later what PSMVelocityExporter is.
Launching the application in this way:
java com.codegenerator.Generator order.xml XML IOMVelocityExporter
creates, by using IOMVelocityExporter, the Java
classes indicated by order.xml: Order,
OrderItem, and Article. The generator, for example, creates
OrderItem.java as the following:
// Generated by IOMVelocityExporter
import java.util.*;
public abstract class OrderItem {
// number
private int number;
public int getNumber() {
return this.number;
}
public void setNumber(int number) {
this.number = number;
}
// quantity
private int quantity;
public int getQuantity() {
return this.quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
// Association
// OrderItem -- Order
// Navigable : true
// Multiplicity: 1
public abstract Order getOrder();
public abstract void setOrder(Order order);
// Association
// OrderItem -- Article
// Navigable : true
// Multiplicity: 1
public abstract Article getArticle();
public abstract void setArticle(Article article);
}
Note: The classes are defined as abstract because the implementations of the association methods have not been specified.