|
![]() |
The <Gen-it> Manual |
|
1. Introduction So how does it work, this "code generation" thing? Well,
that's not an easy question. Simply said it's: In this manual we'll try to answer all of these questions (and more) using the "<Gen-it> Sample Case Online Store". As a prerequisite of this manual you should be familiar with XML. If you are not, check our links page. Many of the XML related websites have one or more tutorials on XML. We'll start with a "bare bones" example. Let's assume that
you have a database (containing tables with fields) and you want to
generate Java classes for each table. Each class should contain private
attributes that correspond to the fields in the represented table and
Get and Set functions for each attribute. The first thing we do is to create a XML file describing your database. That should look something like this (although your database probably contains more then one table): <?xml version="1.0" encoding="UTF-8"?> <Gen-it> <Class> <Class.name>Product</Class.name> <Attribute> <Attribute.name>productid</Attribute.name> <Attribute.datatype>NUMERIC(10,0)</Attribute.datatype> <Attribute.stereotype>PK</Attribute.stereotype> </Attribute> <Attribute> <Attribute.name>productname</Attribute.name> <Attribute.datatype>VARCHAR(100)</Attribute.datatype> <Attribute.stereotype>NOT NULL</Attribute.stereotype> </Attribute> <Attribute> <Attribute.name>description</Attribute.name> <Attribute.datatype>TEXT</Attribute.datatype> <Attribute.stereotype>NULL</Attribute.stereotype> </Attribute> <Attribute> <Attribute.name>price</Attribute.name> <Attribute.datatype>NUMERIC(9,2)</Attribute.datatype> <Attribute.stereotype>NOT NULL</Attribute.stereotype> </Attribute> </Class> </Gen-it> This file is stored as MyDatabase.xml. The next thing you need is a XSL template. This step is kind of complicated, so well use a little magic for now and just provide the XSL template: <?xml version="1.0"
encoding="iso-8859-1"?> This file is stored as MyTemplate.xsl. As has been said before, you need a XML parser to create the source code. For our example we'll use Saxon (see our links page). To use Saxon you have to type the following on a command line: Saxon -o output.java MyDatabase.xml MyTemplate.xsl This will produce a file named output.java with the following content: public class Product
{
Long lngProductid = new Long(0) ;
String strProductname = new String() ;
String strDescription = new String() ;
Long lngPrice = new Long(0) ;
public void setProductid(Long value)
{
lngProductid = value;
}
public Long getProductid()
{
return lngProductid;
}
public void setProductname(String value)
{
strProductname = value;
}
public String getProductname()
{
return strProductname;
}
public void setDescription(String value)
{
strDescription = value;
}
public String getDescription()
{
return strDescription;
}
public void setPrice(Long value)
{
lngPrice = value;
}
public Long getPrice()
{
return lngPrice;
}
}
That's it. We have generated our first Java class using <Gen-it>. The next figure illustrates the followed process: |
|||||||||||||