Everything Java Apache Geospatial Open Source. Hello Shinning Stars!!! Vincent Massol, Raphael Luta, Santiago Gala, Carsten Z.

Tuesday, July 06, 2004

Up until now I am putting in practice XMLBeans, Ojb, and Castor. Let's just say I like diversity. So from the bottom up it would be described this way. We have a database schema which I need to represent in xml. So I use Ojb 1.0.0 ReverseDB tool (org.apache.ojb.tools.mapping.reversedb.Main in the db-ojb-1.0.0-tools.jar) to create a xml and all the JavaBeans that I need for Castor. This xml is very nice, it describes all the tables, columns, primary and foreign keys, relations, collections that exist in our database schema. This will help me when using Castor.

I need a mapping file for Castor so I wrote a small xslt to convert my database xml into a mapping xml. Here it is :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Warning the key-generator="SEQ" has to be added manually to mapping.xml -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<key-generator name="SEQUENCE" alias="SEQ">
<param name="sequence" value="seq_id"/>
<xsl:template match="class-descriptor">
<xsl:if test="field-descriptor[@primarykey='true']">
<xsl:attribute name="identity"><xsl:for-each select="field-descriptor[@primarykey='true']"><xsl:value-of select="concat(@name, ' ')"/></xsl:for-each></xsl:attribute>
<xsl:attribute name="name"><xsl:value-of select="concat('db.finaixm.', @class)"/></xsl:attribute>
<xsl:attribute name="table"><xsl:value-of select="@table"/></xsl:attribute>
<xsl:template match="field-descriptor">
<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
<xsl:attribute name="type"><xsl:choose><xsl:when test="@jdbc-type = 'DECIMAL'">long</xsl:when><xsl:when test="@jdbc-type = 'VARCHAR'">string</xsl:when><xsl:when test="@jdbc-type = 'TIMESTAMP'">timestamp</xsl:when></xsl:choose></xsl:attribute>
<xsl:attribute name="name"><xsl:value-of select="@column"/></xsl:attribute>

Here is a quick Ant task to run it,

<target name="finaixm-mapping">
<echo>Building Castor mapping file</echo>
<xslt style="xslt/oracle2mapping.xslt" in="oracle.xml" out="etc/mapping.xml" />

I also want to manipulate xml files that conform to a given xml schema. So I use XMLBeans to read the schema and create a parser and custom API for the given schema. Practical! So I've got JavaBeans on both sides, all I need to do now is apply business logic to some Velocity templates that can import xml to the database and export from the database to xml. Thanks to the Open Source community.

No comments: