DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Quick-Prototype POJO With CamelCase Using XSLT
// If you've ever been frustrated trying to write an enormous POJO, here's a way to speed the
// process a little. It's far from perfect, and could use some tweaking, but this XSLT code will
// spit out a POJO much faster than you could do it by hand.
TableDDLs.xml
<schema>
<table name="tlkpDepartment">
<columns>
<column name="DeptCode" type="integer" null="no" primarykey="yes" />
<column name="DeptDesc" type="varchar"
size="30" null="yes" />
<column name="Active" type="integer" null="no" />
</columns>
<table name="tblAddress">
<columns>
<column name="AddressID" type="integer" null="no" />
<column name="Address1" type="varchar" size="150" null="no" />
<column name="Address2" type="varchar" size="150" null="no" />
<column name="City" type="varchar" size="50" null="no" />
<column name="State" type="varchar" size="2" null="yes" />
<column name="ZipCode" type="varchar" size="10" null="no" />
</columns>
</table>
</schema>
CreatePOJOs.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"/>
<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<!-- pass the document's children to our converter -->
<xsl:template match="/">
<xsl:call-template name="createPojo">
</xsl:call-template>
</xsl:template>
<xsl:template name="createPojo">
<xsl:for-each select="/schema/table">
/**
* <xsl:value-of select="@name" /> example of POJO output
*
*/
package pojo;
import java.sql.Date;
/**
* @author [your name here]
*
*/
public class <xsl:value-of select="@name" /> {
<xsl:for-each select="columns/column">
<xsl:variable name="datatype" select="@type" />
<xsl:variable name="columnName" select="@name" />
private <xsl:choose><xsl:when test="$datatype='integer'"> Long </xsl:when><xsl:when test="$datatype='varchar'"> String </xsl:when><xsl:when test="$datatype='date'"> Date </xsl:when></xsl:choose> <xsl:call-template name="camelCaseTheColumn"><xsl:with-param name="column" select="$columnName" /></xsl:call-template> = null;</xsl:for-each>
<xsl:for-each select="columns/column">
<xsl:variable name="datatype" select="@type" />
<xsl:variable name="columnName" select="@name" />
public<xsl:choose><xsl:when test="$datatype='integer'"> Long </xsl:when><xsl:when test="$datatype='varchar'"> String</xsl:when><xsl:when test="$datatype='date'"> Date</xsl:when></xsl:choose> get<xsl:value-of select="$columnName" />() {
return <xsl:call-template name="camelCaseTheColumn"><xsl:with-param name="column" select="$columnName" /></xsl:call-template>;
}
public void set<xsl:value-of select="$columnName" />(<xsl:choose><xsl:when test="$datatype='integer'">Long </xsl:when><xsl:when test="$datatype='varchar'">String </xsl:when><xsl:when test="$datatype='date'">Date </xsl:when></xsl:choose> <xsl:call-template name="camelCaseTheColumn"><xsl:with-param name="column" select="$columnName" /></xsl:call-template>) {
this.<xsl:call-template name="camelCaseTheColumn"><xsl:with-param name="column" select="$columnName" /></xsl:call-template> = <xsl:call-template name="camelCaseTheColumn"><xsl:with-param name="column" select="$columnName" /></xsl:call-template>;
}
</xsl:for-each>
}
-----------------------------------------------------------------------------------------------
</xsl:for-each>
</xsl:template>
<xsl:template name="getListOfAttributes">
<xsl:for-each select="/schema/table">
<xsl:for-each select="columns/column">
<xsl:variable name="columnName" select="@name" />
this.<xsl:call-template name="camelCaseTheColumn"><xsl:with-param name="column" select="$columnName" /></xsl:call-template> = null;</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template name="getAttributeDeclarations">
<xsl:for-each select="/schema/table">
<xsl:for-each select="columns/column">
<xsl:variable name="columnName" select="@name" />
protected String <xsl:call-template name="camelCaseTheColumn"><xsl:with-param name="column" select="$columnName" /></xsl:call-template> = null;</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template name="getSimpleAttributeList">
<xsl:for-each select="/schema/table">
<xsl:for-each select="columns/column">
<xsl:variable name="columnName" select="@name" />
<xsl:call-template name="camelCaseTheColumn"><xsl:with-param name="column" select="$columnName" /></xsl:call-template> = null;
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template name="outputTableAndColumns">
<xsl:for-each select="/schema/table">
<xsl:for-each select="columns/column">
ColumnName= <xsl:value-of select="@name" />
CamelCasedColumn: <xsl:call-template name="camelCaseTheColumn">
<xsl:with-param name="column" select="@name" />
</xsl:call-template>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template name="camelCaseTheColumn">
<xsl:param name="column"/>
<xsl:variable name='f' select='substring($column, 1, 1)'/>
<xsl:variable name='x' select='translate($f, $ucletters, $lcletters)'/>
<xsl:variable name='s' select='substring($column, 2)'/>
<xsl:value-of select="$x"/><xsl:value-of select="$s"/></xsl:template>
</xsl:stylesheet>





