Introduction

As the information system becomes more and more complex, it is increasingly difficult to maintain a single repository that has the definitions of all the business objects that are valid within the scope of your project.

In our case, we have to maintain Object Definitions in the following environments:

  • TIB/Repository: all the meta data definitions are stored here for use by Integration Manager, Message Broker, Active Database, ...
  • Castor mapping files: contains the decriptions of the xml files that Castor XML has to parse within the scope of our project.
  • Java Beans: our business logic is implemented within an application server and all our java developpers have to handle java beans. Each java bean has getters and setters that are related to the properties of the business object.

On projects with large repositories this situation can quickly become a nightmare, especially when a change occurs in some business object. It is for that very reason that a single repository is nearly compulsory. Therefore we decided to build a source generator that is capable of taking a business object from any of the previous environnements and generating the object description in the othere two environnements.

To sum it up, the sourcegenerator is capable of:

  • Converting a java bean into a mapping file for Castor and schema definition for the TIB/Repository
  • Converting a schema definition from the repository into a mapping file for Castor XML and a java bean
  • Converting a Castor XML mapping file into a java bean and schema definition for the TIB/Repository
  • Parsing an XML file into a java bean, Castor XML mapping file and schema definition for the TIB/Repository

Why use the generator?

Comming up next ....

General Guidelines

Comming up next ....

Raccoon XML

Introduction

Today there are many ways to represent a Data Bean, starting from the simple source code going to the TIB/Repository passing by a Castor XML mapping file.

Raccoon too has it's representation of a Data Bean, it's called a RaccoonBean that has certain properties. Anyway this RaccoonBean has a unique representation in the xml world that is very simple to use. This XML representation can be used to generate either:

  • The java source code
  • The TIB/Repository import/export file
  • The Castor XML mapping file
A very simple example is:
          
<container name="Test XML String">
  <raccoonBean name="raccoon.test.RaccoonBean">
    <field name="name" type="java.lang.String" />
    <field name="friends" type="java.util.Vector" colType="java.lang.String"/>
    <field name="family"  type="java.util.HashMap" colType="java.lang.String"/>
    <field name="elements" type="java.util.ArrayList" colType="raccoon.test.RaccoonField"/>
  </raccoonBean>
  <raccoonBean name="raccoon.test.RaccoonField">
    <field name="name" type="java.lang.String" />
    <field name="age" type="int" />
  </raccoonBean>
</container>
        

This XML Source file can be used to generate

  • The java source code of RaccoonBean and RaccoonField.
  • The TIB/Repository import/export file
  • The Castor XML mapping file

Castor XML

This section briefly explains how to use the Source Generator between a Raccoon JavaBean and a Castor XML mapping file. For more information related to Castor XML please visit the Intalio site: Castor XML

Introduction

Today Castor XML has become an invaluable tool on daily basis, this is certainly the case for Raccoon.

As for us, whenever we use Castor XML we have already designed and usually implemented our java beans, what annoys us the most is the mapping file that we have to write for all our beans. We therefore decided to include into Raccoon a service for our favorite XML mapping tool that will:

  • automatically generate a mapping file for Castor XML from a java bean that complies to the Raccoon specification. See the section Generating a Mapping File For Castor
  • automatically generate all the java source code from a Castor mapping file, the java bean generated complies to the Raccoon specification, of course!

Generating a Mapping File For Castor

Usually when using Castor the developper has his java bean already specified and not a Castor Mapping File. Well it's our case anyway :o)

Getting the mapping file for castor is very easy as you can see for yourself hereafter.

import raccoon.core.exceptions.ComponentException;
import raccoon.core.exceptions.RaccoonException;
import raccoon.tools.generator.SrcFactory;

public class SampleApp {
  public static void main(String args[]) {

    try {
      SrcFactory sourceGen = new SrcFactory();

      sourceGen.setPropertyFileName("raccoon");
      sourceGen.start();

      String marshalled = sourceGen.classToCastor(Team.class);
      System.out.println("Castor XML Mapping File is:\n" + marshalled);
    } catch (RaccoonException re) {
      re.printStackTrace();
    }
  }
}
        

Where Team is a valid java bean containing getters, setters and adders. The result obtained in our sample case can be viewed by clicking on the link: Castor XML Mapping

Customizing the behaviour

The following elements found in the file raccoon.tools.generator.generator.properties can now be customized:

  • Castor DTD line found under the property castor.dtd
  • The bind-xml line found under the property castor.bind-xml. Accepted values are upper or lower, default value is lower

    This affects the xml element type, if upper is set then the xml element will be bind-xml="PropertyName". If lower is set then the xml element will be bind-xml="propertyName".
  • Castor mapping node value line found under the property castor.mapping.type Accepted values are attribute or element, default value is element.

    This affects the xml node type, if attribute is set then the xml element will be node="attribute". If element is set then the xml element will be node="element".

Generating Source Files From a Castor Mapping File

Using the previous Castor Mapping file to generate the source code can be done quite easily, a sample application is given hereafter:

import raccoon.core.exceptions.ComponentException;
import raccoon.core.exceptions.RaccoonException;
import raccoon.tools.generator.SrcFactory;

public static void main(String args[]) {


  try {
    SrcFactory sourceGen = new SrcFactory();

    sourceGen.setPropertyFileName("raccoon");
    sourceGen.start();

    String result[] = sourceGen.castorToSource("mapping.xml");

    System.out.println("Final Source is");
    for (int i=0;i<result.length;i++) {
      System.out.println(result[i]);
    }
  } catch (RaccoonException re) {
    re.printStackTrace();
  }
}
	
	    

Where mapping.xml is a valid Castor XML mapping file. The result obtained in our sample case can be viewed by clicking on one of the following links: TestTeam, TestPerson.

Eventually the reader may prefer viewing the associated javadoc: TestTeam, TestPerson