The <Gen-it> Manual

 

 

 

 

Wrong GEN-IT?
Look at
www.codagen.com

 

Next >

1. Introduction

So how does it work, this "code generation" thing? Well, that's not an easy question. Simply said it's:
"You take a XML file describing your database, create a XSL template that is specific for the code you want to create, blend them together using a XML parser and there's your code."
But if you have never generated code before or described your database in XML or seen an XSL transformation from nearby, then this answer won't make anything clearer. It will probably raise more questions like "How do I get a XML file describing my database?", "What kind of code can I generate?", "How do I create a XSL template?" and "Where do I get an XML parser and how do I use it?".

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.
We know that almost every CASE tool can generate this kind of code, but for now we just want the example to be simple.

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.
Note that each table in your database corresponds to a <Class> element in the XML file and each field within that table corresponds to a <Attribute> element. Within these elements there are other elements describing the Class (table) name and the Attribute (fielda) names, datatype and stereotype (the NULL allowed indicator).

The next thing you need is a XSL template. This step is kind of complicated, so we’ll use a little magic for now and just provide the XSL template:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform version="1.0">
<xsl:output method="text" encoding="us-ascii" indent="yes"/> <xsl:template match="/">
    <xsl:for-each select="//Class">public class <xsl:value-of select="Gen-it:CapFirst(Class.name)"/>
{
<xsl:for-each select="Attribute">
    <xsl:apply-templates select="." mode="datatype"/><xsl:apply-templates select="." mode="variable"/>
= new <xsl:apply-templates select="." mode="newdatatype"/>;
        </xsl:for-each>
    <xsl:for-each select="Attribute">
public void set<xsl:value-of select="Gen-it:CapFirst(Attribute.name)"/>(
<xsl:apply-templates select="." mode="datatype"/>value)
{
    <xsl:apply-templates select="." mode="variable"/> = value;
}
public <xsl:apply-templates select="." mode="datatype"/>
get<xsl:value-of select="Gen-it:CapFirst(Attribute.name)"/>()
{
return <xsl:apply-templates select="." mode="variable"/>;
}</xsl:for-each>
}
    </xsl:for-each>
</xsl:template>

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:


Next >