Simple Bean Generator

Chris Greenhalgh, 2006-03-24; last updated 2006-04-05

Introduction

To make it easier to create the simple classes used as data holders in EQUIP2 I am defining a simple XML schema to describe such object(s), which can be used to generate java bean classes and EQUIP2 helper classes using XSLT.

Bean description schema

Sample/representative bean description (based on the existing hand-crafted class):
<?xml version="1.0"?> 
<bean package="equip2.core.test" class="TestObject1" id="id">
<description>test object 1 - simple bean-style java object with 'id' and 'info' properties (String).
'id' is used as the identity of the object (ie unique primary key) (specified in TestObject1_helper).
</description>
<property name="id" javatype="java.lang.String">
<description>a value - this is used as the identity of this object.</description>
</property>
<property name="info" javatype="java.lang.String">
<description>a value</description>>
</property>
</bean>

Note that the identity and descriptions are optional.

See also ../etc/equip2.core.test.TestObject2.xml

Note that the standard Java primitive & common types (e.g. for use as javatype attribute) are:

boolean
true/false
byte
8 bit signed int
short
16 bit signed int
int
32 bit signed int
long
64 bit signed int
char
16 bit signed int and unicode character
float
32 bit float
double
64 bit float
java.lang.String
Immutable string
java.util.Vector
Variable size vector/array of java.lang.Object references
java.util.Hashtable
Map from keys to values (one value per key)
java.lang.Boolean
Boxed boolean (can be null), subclass of java.lang.Object
java.lang.Byte
Boxed byte
java.lang.Short
Boxed short
java.lang.Integer
Boxed int
java.lang.Long
Boxed long
java.lang.Character
Boxed char
java.lang.Float
Boxed float
java.lang.Double
Boxed double

Usage

Usage (windows) to create the java bean and helper class is (EQUIP2_HOME is equip2 top-level directory):
java equip2.tools.j2se.xsltTransform %EQUIP2_HOME%/etc/bean2java.xsl BEANDEFFILE.xml OUTFILE.java
java equip2.tools.j2se.xsltTransform %EQUIP2_HOME%/etc/bean2javahelper.xsl BEANDEFFILE.xml OUTFILE_helper.java
See for example, the tools_test ant target which reads ../etc/equip2.core.test.TestObject2.xml and generates ../generated/equip2/core/test/TestObject2.java and ../generated/equip2/core/test/TestObject2_helper.java.

Hibernate-related extensions

I have extended the basic language to also generate Hibernate mapping files. This requires (in some cases) additional information to be added. The following file is representative:
<?xml version="1.0"?> 
<bean package="dof2.db" class="Player" id="ID" primarykey="ID" primarykeygenerator="assigned" table="player">
<!-- primarykey is optional; if not specified an internal long-typed primary key
column called 'hibernatekey' is created -->
<!-- primarykeygenerator is optional; default is 'native' (at present)-->
<!-- table is optional; default table name is the same as the class name -->
<description>The player...</description>
<type name="IDType" javatype="java.lang.String" maxlength="20"/>
<!-- a type element is just a type alias for later use -->
<!-- maxlength is optional, and only required in most cases for short strings -->
<property name="ID" type="IDType">
<!-- javatype (and optionally maxlength) can be specified here in the property,
or determined from the identified type element -->
<description>Primary key...</description>
</property>
<property name="nickName" javatype="java.lang.String" maxlength="8">
<!-- like this, for example -->
<description>Player-chosen nickname</description>
</property>
<property name="fullName" javatype="java.lang.String">
<!-- with no specified maxlength Hibernate will use its own default (256?) -->
<description>Player-chosen full name.</description>
</property>
<property name="currentMissions" javatype="java.util.Set" elementtype="IDType">
<!-- set-valued properties will mapped to Hibernate sets. Currently only sets of
primitive types or strings are supported. -->
<description>Set of current mission IDs.</description>
</property>
<linkedclass name="friends" class="Player_Friend" linkproperty="playerID">
<!-- this element will generate a convenience method in the Java bean called
getFriendsTemplate, which will return a Player_Friend template instance
with the playerID property set appropriately, to allow linked (referencing)
instances to be retrieved using a dataspace match() on the template. -->
<description>"Friends" of this player.</description>
</linkedclass>
</bean>
Usage (windows) to create the Hibernate mapping file is (EQUIP2_HOME is equip2 top-level directory):
java equip2.tools.j2se.xsltTransform %EQUIP2_HOME%/etc/bean2hibernatehbm.xsl BEANDEFFILE.xml OUTFILE.hbm.xml

Issues/extensions

Change Log

2006-04-05
2006-03-24