Codehaus     ExoLab     OpenEJB     OpenJMS     OpenORB     Castor     Tyrex     
 

Main
  Home
  About
  Features
  Download
  API
  Schema
  Castor Forums
  Mailing Lists
  CVS / JIRA
  Support
  CastorWiki
  RSS news feed

XML
  Using XML
  Source Generator
  Schema Support
  XML Mapping
  XML FAQ
  Custom Handlers

JDO
  Introduction
  Using JDO
  JDO Config
  Types
  JDO Mapping
  JDO FAQ
  JDO Examples
  JDO How-Tos
  Other Features

Advanced JDO
  OQL
  Trans. & Locks
  Design
  KeyGen
  Long Trans.
  Nested Attrs.
  Pooling Examples
  LOBs
  Best practice

More
  Presentations
  The Examples
  3rd Party Tools
  JDO Tests
  XML Tests
  Configuration
  Tips & Tricks
  Full JavaDoc
  CastorWiki
  
  

About
  License
  Contributors
  Marketplace
  Status, Todo
  Changelog
  Library
  Contact
  Project Name

  



Using the Source Code Generator

Documentation Author(s):
Keith Visco
Arnaud Blandin

API Reference: The Source Builder API


Introduction
Castor source generator Ant task
Source Generator Options
    Command Line Options
        Collection Types
    Advanced Options
        Bound Properties
        Class Creation/Mapping
        The 'element' method
        The 'type' method
        Setting a super class
        Mapping XML namespaces to Java packages
        Generate equals() method
        Maps java primitive types to wrapper object
Binding File
    <binding> element
    <include> element
    <package> element
    <namingXML> element
    <componentBinding> element
    <java-class> element
    <interface> element
    <member> element
    todo
Non-trivial real world example
    Background
    The Supply Chain XSD
    Binding file? - IT IS REQUIRED!
XML Schema


Introduction

Castor's Source Code Generator creates a set of Java classes which represent an object model for an XML Schema (W3C XML Schema, 20010502 Recommendation)1, as well as the necessary Class Descriptors used by the marshalling framework to obtain information about the generated classes. Currently the generated source files will need to be compiled. We will be adding an Ant taskdef eventually to handle this automatically.

Example:
java org.exolab.castor.builder.SourceGenerator -i foo-schema.xsd \
    -package com.xyz
        

This will generate a set of source files from the the XML Schema, foo-schema.xsd, and place them in the com/xyz/* package.

To compile the generated classes simply run javac, or your favorite compiler:

javac com/xyz/*.java

Created class will have marshal and unmarshal methods which are used to go back and forth between XML and an Object instance.

Castor source generator Ant task

An alternative to using the command line as shown in the previous section, the Castor Source Generator Ant Task can be used to call the source generator for class generation. Below is an example of how to use this task from within an Ant target definition named 'castor:gen:src':

  <target name="castor:gen:src" depends="init"
             description="Generate Java source files from XSD.">

    <taskdef name="castor-srcgen"
                classname="org.exolab.castor.tools.ant.taskdefs.CastorSourceGenTask"
                classpathref="castor.class.path" />
    <mkdir dir="generated" />
    <castor-srcgen file="src/schema/sample.xsd"
                      todir="generated-source"
                      package="org.castor.example.schema"
                      types="j2"
                      warnings="false" />
  </target>
        

The only requirement is that the castor-<version>-srcgen-ant-task.jar must be on the CLASSPATH.

Source Generator Options


Tip:For additional information about the Source Generator and options, You can download the Source Generator User Document(PDF). Note: The binding-file is not dicussed in that document.

The source code generator has a number of different options which may be set. Some of these are done using the command line, and others are done using a properties file located at

"org/exolab/castor/builder/castorbuilder.properties"
.

Command Line Options

OptionArgsDescriptionOptional?
ifilename The input XML Schema file Required
packagepackage-name The package for the generated source Optional
destpath The destination in which to put the generated source Optional
line-separatorunix | mac | win Sets the line separator style for the desired platform. This is useful if you are generating source on one platform, but will be compiling/modifying on another platform. Optional
typestype-factory Sets which type factory to use. This is useful if you you want JDK 1.2 collections instead of JDK 1.1 or if you want to pass in your own FieldInfoFactory. (see Collection types) Optional
h
Shows the help/usage screen Optional
f
Forces the source generator to supress all non-fatal errors, such as overwriting of pre-existing files. Optional
nodesc
Do not generate the class descriptors Optional
nomarshall
Do not generate the marshalling framework methods (marshall, unmarshall, validate) Optional
testable
Generate the specific methods used by the Castor Marshalling Framework Optional
sax1
Generate marshalling methods that are using SAX1 framework (default is false). Optional
binding-fileThe binding file name. Allows the setting of a Binding File to allow a fine-grained control the classes generated Optional
generateImportedSchemas
Generates automatically sources for imported XML Schemas (default is false). Optional

Collection Types

The source code generator has the ability to use the following types of collections when generating source code:

-Java 1.1 (default): java.util.Vector
-Java 1.2: use the option -types j2, collection type is java.util.Collection
-ODMG 3.0: use the option -types odmg, collection tyoe is odmg.DArray
You can also write your own FieldInfoFactory to handle specific collection types. All you have to do is to pass in the fully qualified name of that FieldInfoFactory: -types com.personal.MyCoolFactory

Advanced Options

Bound Properties

Since version: 0.8.9

Bound properties are "properties" of a class, which when updated the class will send out a java.beans.PropertyChangeEvent to all registered java.beans.PropertyChangeListeners.

To enable bound properties uncomment the appropriate line in the "org/exolab/castor/builder/castorbuilder.properties" file:

# To enable bound properties uncomment the following line. Please
# note that currently *all* fields will be treated as bound properties
# when enabled. This will change in the future when we introduce
# fine grained control over each class and it's properties.
#
#org.exolab.castor.builder.boundproperties=true

When enabled, all properties will be treated as bound properties. For each class that is generated a setPropertyChangeListener method is created as follows:

    
    /**
     * Registers a PropertyChangeListener with this class.
     * @param pcl The PropertyChangeListener to register.
    **/
    
    public void addPropertyChangeListener
        (java.beans.PropertyChangeListener pcl)
    {
        propertyChangeListeners.addElement(pcl);
    } //-- void addPropertyChangeListener
                 
Whenever a property of the class is changed, a PropertyChangeEvent will be sent to all registered listeners. The property name, the old value, and the new value will be set in the PropertyChangeEvent.

Note: To prevent unnecessary overhead, if the property is a collection, the old value will be null.

Class Creation/Mapping

Since version: 0.8.9

The source generator can treat the XML Schema structures such as complexType and element in two main ways. The first, and currently default method is called the "element" method. The other is called the "type" method.

The 'element' method

The "element" method creates classes for all elements whose type is a complexType. Abstract classes are created for all top-level complexTypes. Any elements whose type is a top-level type will have a new class create that extends the abstract class which was generated for that top-level complexType.

Classes are not created for elements whose type is a simpleType.

The 'type' method

The "type" method creates classes for all top-level complexTypes, or elements that contain an "anonymous" (in-lined) complexType.

Classes will not be generated for elements whose type is a top-level type.

More information on this is forth coming.

To change the "method" of class creation simple edit the castorbuilder.properties file:

# Java class mapping of <xsd:element>'s and <xsd:complexType>'s
#
#org.exolab.castor.builder.javaclassmapping=element

Setting a super class

Since version: 0.8.10

The source generator enables the user to set a super class to all the generated classes (of course class descriptors are not concerned by this option)

To set up the super class, edit the castorbuilder.properties file :

# This property allows one to specify the super class of *all*
# generated classes
#
#org.exolab.castor.builder.superclass=com.xyz.BaseObject

Mapping XML namespaces to Java packages

Since version: 0.9.0

An XML Schema instance is identified by a namespace. For data-binding purposes, especially code generation it may be necessary to map namespaces to Java packages.

This is needed for imported schema in order for Castor to generate the correct imports during code generation for the primary schema.

To allow the mapping between namespaces and Java packages , edit the castorbuilder.properties file :

# XML namespace mapping to Java packages
#
#org.exolab.castor.builder.nspackages=\
   http://www.xyz.com/schemas/project=com.xyz.schemas.project,\
   http://www.xyz.com/schemas/person=com.xyz.schemas.person

Generate equals() method

Since version: 0.9.1

The Source Generator can override the 'equals()' method for the generated objects.

Note: hashcode() is not currently overriden.

To generate the equals() method , edit the castorbuilder.properties file :

# Set to true if you want to generate the equals method
# for each generated class.
# false by default
#org.exolab.castor.builder.equalsmethod=true

Maps java primitive types to wrapper object

Since version 0.9.4

It may be convenient to use java objects instead of primitives, the Source Generator provides a way to do it. Thus the following mapping can be used:

-boolean to java.lang.Boolean
-byte to java.lang.Byte
-double to java.lang.Double
-float to java.lang.Float
-int and integer to java.lang.Integer
-long to java.lang.Long
-short to java.lang.Short

To enable this property, edit the castor builder.properties file:

# Set to true if you want to use Object Wrappers instead
# of primitives (e.g Float instead of float).
# false by default.
#org.exolab.castor.builder.primitivetowrapper=false

Binding File

It may appear that the default binding used to generate the Java Object Model from an XML schema doesn't meet one's expectation. For instance, the default binding won't deal with naming collision problems that can appear since XML Schema allows an element declaration and a complexType definition to both share the same name. From a Java standpoint, it will result in the creation of two classes with the same qualified name: the second class generated will simply overwrite the first one. Another example is when one user wants to change the default datatype binding provided by Castor or to add validation rules by implementing his own validator and passing it to the SourceGenerator.

The Binding declaration is an XML based language that allows the user to define such control on the generated classes. The aim of this section is to provide an overview of the binding file as well as a definition of the several XML components used to define this binding file. A more in-depth presentation will be available soon in the Source Generator User Document(PDF).

<binding> element

<binding
    defaultBindingType = (element|type)>
    (include*,
     package*,
     namingXML?,
     elementBinding*,
     attributeBinding,
     complexTypeBinding,
     groupBinding)
</binding>

The binding element is the root element and contains the binding information. The attribute

defaultBindingType
controls the Class creation type


Tip: Be careful when using defaultBindingType attribute as it will override the binding type specified in the castorbuilder.properties file.

<include> element

<include
    URI = xsd:anyURI/>

-URI:The URI of the binding file to include.

This element allows one to include the binding declaration defined in another file. This allows re-usability of Binding files defined for various XML Schemas.

<package> element

<package>
    name = xsd:string
    (namespace|schemaLocation) = xsd:string>
</package>

-name:A fully qualified java package name.
-namespace:An XML namespace that will be mapped to the package name defined by the name element.
-schemaLocation:A URL that locates the schema to be mapped to the package name defined by the name element.

The targetNamespace attribute of an XML Schema identifies the namespace in which the XML language is defined. Such language namespace is defined in the java language as package declaration. The <package/> element allows you to define the mapping between an XML namespace and a Java package. Moreover XML Schema allows you to factorize the definition of an XML Schema identified by a unique namespace by including several XML Schemas to build one XML Schema using the <xsd:include/> element (Please make sure you understand the difference between <xsd:include/> and <xsd:import/>). <xsd:include/> relies on the URI of the included XML schema and it can be needed to keep the structure hierarchy defined in XML Schema in the Java package generated. Thus the binding file allows to define the mapping between a schemaLocation attribute and a Java package.

<namingXML> element

<namingXML>
   (elementName,complexTypeName,modelGroupName)
</namingXML>

<elementName|complexTypeName|modelGroupName>
    (prefix?, suffix?) = xsd:string
</elementName|complexTypeName|modelGroupName>

-prefix:The prefix to add to the names of the generated classes.
-suffix:The suffix to append to the the names of the generated classes

One of the aim of the binding file is to avoid naming collisions. Indeed XML Schema allows elements and complexTypes to share the same name which results in name collisions when generated the sources. Defining a binding for every element and complexType that share the same name is sometimes not a convenient solution (for instance the BPML XML Schema or the UDDI v2.0 XML Schema use the same names for top-level complexTypes and top-level elements). The aim of the <naming/> XML element is to define a prefix and a suffix for the names of the classes generated for an element, a complexType or a model group definition.
Note:It is not possible to control the names of the classes generated to represent nested model groups (all, choice, sequence).

<componentBinding> element

<elementBinding|attributeBinding|complexTypeBinding|groupBinding
    name = xsd:string>
   ((java-class|interface|member),
     elementBinding*,
     attributeBinding*,
     complexTypeBinding*,
     groupBinding*)
</elementBinding|attributeBinding|complexTypeBinding|groupBinding>

-name:The name of the XML schema component for which we are defining a binding.

These elements are the tenets of the binding file since they contain the binding definition for an XML Schema element, attribute, complexType and modelGroup definition. The first child element (<java-class/>, <interface> or <member>) will determine the type of binding one is defining. Please note that defining a <java-class> binding on an XML Schema attribute will have absolutely no effect.

The binding file being written from an XML Schema point of view; there are two distinct ways to define the XML Schema component for which we are defining a binding. First we can define it throught the name attribute.

The value of the name attribute uniquely identifies the XML Schema Component. It can refer to the top-level component using the NCName of that component or it can use a location language based on XPath3. The grammar of that language can be defined by the following BNF:

            [1]Path         ::= LocationPath('/'LocationPath)*
            [2]LocationPath ::= (Complex|ModelGroup|Attribute|Element)
            [3]Complex      ::= 'complexType:'NCName
            [4]ModelGroup   ::= 'group:'NCName
            [5]Attribute    ::= '@'NCName
            [6]Element      ::= NCName
         

The second option to identify a XML Schema Component is to embed its binding definition inside its parent binding definition.

For instance, the following binding definition will be equivalent and will identify the element 'foo' defined in the top-level complexType 'fooType'.

<elementBinding name="complexType:fooType/foo>
   <member name="MyFoo" handler="mypackage.myHandler"/>
</elementBinding>

<complexTypeBinding name="fooType">
   <elementBinding name="foo>
      <member name="MyFoo" handler="mypackage.myHandler"/>
   </elementBinding>
<complexTypeBinding>

<java-class> element

<java-class
    name? = xsd:string
    package? = xsd:string
    final? = xsd:boolean
    abstract? = xsd:boolean
    equals? = xsd:boolean
    bound? = xsd:boolean >
    (implements*,extends?)
</java-class>

-name:The name of the class that will be generated.
-package:The package of the class to be generated. if set, this option overrides the mapping defined in the <package/> element.
-final:If true, the generated class will be final.
-abstract:If true, the generated class will be abstract.
-equals:If true, the generated class will implement the equals method.
-bound:If true, the generated class will implement the bound properties.

This element defines all the options for the class to be generated.

<interface> element

<interface>
    name = xsd:string
</interface>

-name:The name of the interface that will be generated.

This element specifies the name of the interface to be generated for an XML schema component.

NOTE: this element is not yet implemented.

<member> element

 <member
  name? = xsd:string
  java-type? = xsd:string
  wrapper? = xsd:boolean
  handler? = xsd:string
  collection? = (array|vector|arraylist|hashtable|collection|odmg|set|map)
  validator? = xsd:string/>

-name:The name of the class member that will be generated.
-java-type:the fully qualified name of the java type.
-wrapper:If true, a wrapper object will be generated in case the java type is a java primitive.
-handler:The fully qualified name of the FieldHandler to use.
-collection:If the schema component can occur more than once then this attribute allows to specify the collection to use to represent the component in Java.
-validator:The fully qualified name of the FieldValidator to use.

This element represents the binding for class member. It allows the definition of its name and java type as well as an implementation of FieldHandler to help the Marshalling framework in handling that member. Defining a validator is also possible. The names given for the validator and the fieldHandler must be fully qualified.

todo

-

 <enumBinding>
    (enumClassName|enumMemberName)
 </enumBinding>

This element will allow more control on the type safe enumerations generated to represent an XML Schema simpleType enumeration

-
<javadoc/>

the javadoc element will allow one to enter the necessary javadoc that represents the generated classes or members.

Non-trivial real world example

Background

Two companies wish to trade with each other using a Supply Chain messaging system. This system sends and receives Purchase Orders and Order Receipt messages. After many months of discussion they have finally decided upon the structure of the Version 1.0 of their message XSD and both are presently developing solutions for it. One of the companies decides to use Java and Castor XML supprt for (un)marshalling and Castor's code generator to accelerate their development process.

The Supply Chain XSD

supplyChainV1.0.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
              elementFormDefault="qualified" 
              attributeFormDefault="unqualified">
    <xs:element name="Data">
      <xs:annotation>
        <xs:documentation>This section contains the supply chain message 
		  data</xs:documentation>
      </xs:annotation>
      <xs:complexType>
        <xs:choice>
          <xs:element name="PurchaseOrder">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="LineItem" type="LineItemType" maxOccurs="unbounded"/>
              </xs:sequence>
              <xs:attribute name="OrderNumber" type="xs:string" use="required"/>
            </xs:complexType>
          </xs:element>
          <xs:element name="OrderReceipt">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="LineItem" type="ReceiptLineItemType" maxOccurs="unbounded"/>
              </xs:sequence>
              <xs:attribute name="OrderNumber" type="xs:string" use="required"/>
            </xs:complexType>
    	  </xs:element>
    	</xs:choice>
      </xs:complexType>
    </xs:element>
    <xs:complexType name="SkuType">
      <xs:annotation>
        <xs:documentation>Contains Product Identifier</xs:documentation>
      </xs:annotation>
      <xs:sequence>
        <xs:element name="Number" type="xs:integer"/>
        <xs:element name="ID" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
    <xs:complexType name="ReceiptSkuType">
      <xs:annotation>
        <xs:documentation>Contains Product Identifier</xs:documentation>
      </xs:annotation>
      <xs:complexContent>
        <xs:extension base="SkuType">
          <xs:sequence>
            <xs:element name="InternalID" type="xs:string"/>
          </xs:sequence>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="LineItemType">
      <xs:sequence>
        <xs:element name="Sku" type="SkuType"/>
        <xs:element name="Value" type="xs:double"/>
        <xs:element name="BillingInstructions" type="xs:string"/>
        <xs:element name="DeliveryDate" type="xs:date"/>
        <xs:element name="Number" type="xs:integer"/>
      </xs:sequence>
    </xs:complexType>
    <xs:complexType name="ReceiptLineItemType">
      <xs:sequence>
        <xs:element name="Sku" type="ReceiptSkuType"/>
        <xs:element name="Value" type="xs:double"/>
        <xs:element name="PackingDescription" type="xs:string"/>
        <xs:element name="ShipDate" type="xs:dateTime"/>
        <xs:element name="Number" type="xs:integer"/>
      </xs:sequence>
    </xs:complexType>
</xs:schema>

Binding file? - IT IS REQUIRED!

If you run the Castor CodeGenerator on the above XSD you end up with the following set of classes. [You also get lots of warning messages with the present 0.99 version.]

Data.java
DataDescriptor.java
LineItem.java
LineItemDescriptor.java
LineItemType.java
LineItemTypeDescriptor.java
OrderReceipt.java
OrderReceiptDescriptor.java
PurchaseOrder.java
PurchaseOrderDescriptor.java
ReceiptLineItemType.java
ReceiptLineItemTypeDescriptor.java
ReceiptSkuType.java
ReceiptSkuTypeDescriptor.java
Sku.java
SkuDescriptor.java
SkuType.java
SkuTypeDescriptor.java

The problem here is that there are two different elements with the same name in different locations in the XSD. This causes a java code generation conflict. Castor uses the element name as the name of the class. So the second class generated for the LineItem definition, which is different than the first, overwrites the first class generated.

A binding file is therefore necessary to help the Castor code generator differentiate between these generated classes. [i.e. You can 'bind' an element in the XSD to a differently named class file that you want to generate. Thus keeping different elements seperate]


Tip:

The warning messages for Castor 0.99+ are very usefull in assisting you in your creation of the binding file. For the example the warning messages for the example are;
Warning: A class name generation conflict has occured between element '/Data/OrderReceipt/LineItem' and element '/Data/PurchaseOrder/LineItem'. Please use a Binding file to solve this problem.Continue anyway [not recommended] (y|n|?)y
Warning: A class name generation conflict has occured between element '/Data/OrderReceipt/LineItem' and element '/Data/PurchaseOrder/LineItem'. Please use a Binding file to solve this problem.Continue anyway [not recommended] (y|n|?)y
Warning: A class name generation conflict has occured between element '/Data/OrderReceipt/LineItem' and element '/Data/PurchaseOrder/LineItem'. Please use a Binding file to solve this problem.Continue anyway [not recommended] (y|n|?)y
Warning: A class name generation conflict has occured between element 'complexType:ReceiptLineItemType/Sku' and element 'complexType:LineItemType/Sku'. Please use a Binding file to solve this problem.Continue anyway [not recommended] (y|n|?)y
Warning: A class name generation conflict has occured between element 'complexType:ReceiptLineItemType/Sku' and element 'complexType:LineItemType/Sku'. Please use a Binding file to solve this problem.Continue anyway [not recommended] (y|n|?)y
Warning: A class name generation conflict has occured between element 'complexType:ReceiptLineItemType/Sku' and element 'complexType:LineItemType/Sku'. Please use a Binding file to solve this problem.Continue anyway [not recommended] (y|n|?)y

The following binding file definition will overcome the naming issues for the generated classes.

binding.xml
<binding xmlns="http://www.castor.org/SourceGenerator/Binding" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://www.castor.org/SourceGenerator/Binding
                C:\\Castor\\xsd\\binding.xsd" 
            defaultBinding="element">

   <elementBinding name="/Data/PurchaseOrder/LineItem">
      <java-class name="PurchaseOrderLineItem"/>
   </elementBinding>
	
   <elementBinding name="/Data/OrderReceipt/LineItem">
      <java-class name="OrderReceiptLineItem"/>
   </elementBinding>
	
   <elementBinding name="complexType:ReceiptLineItemType/Sku">
      <java-class name="OrderReceiptSku"/>
   </elementBinding>
	
   <elementBinding name="complexType:LineItemType/Sku">
      <java-class name="PurchaseOrderSku"/>
   </elementBinding>	
	
</binding>

Things to notice in the above binding.xml file are that the name path used is relative to the root of the XSD NOT the root of the target XML. Also notice that the two complex types have the "complexType:" prefix to identify them, and then the name path relative to the root of the XSD.

The new list of generated classes is:

Data.java
DataDescriptor.java
LineItem.java
LineItemDescriptor.java
LineItemType.java
LineItemTypeDescriptor.java
OrderReceipt.java
OrderReceiptDescriptor.java
OrderReceiptLineItem.java
OrderReceiptLineItemDescriptor.java
OrderReceiptSku.java
OrderReceiptSkuDescriptor.java
PurchaseOrder.java
PurchaseOrderDescriptor.java
PurchaseOrderLineItem.java
PurchaseOrderLineItemDescriptor.java
PurchaseOrderSku.java
PurchaseOrderSkuDescriptor.java
ReceiptLineItemType.java
ReceiptLineItemTypeDescriptor.java
ReceiptSkuType.java
ReceiptSkuTypeDescriptor.java
Sku.java
SkuDescriptor.java
SkuType.java
SkuTypeDescriptor.java

The developers can now use these generated classes with Castor to (un)marshal the supply chain messages sent by their business partner.

XML Schema

The input file to the source code generator is an XML Schema2. The current supported version is the W3C XML Schema Recommendation1. For more information about XML Schema Support, check the XML Schema page


1Castor 0.9.4.1 uses XML Schema Recommendation 20010502

2XML Schema is a W3C Recommendation

3XPath is a W3C Recommendation
 
   
  
   
 


Copyright ? 1999-2005 ExoLab Group, Intalio Inc., and Contributors. All rights reserved.
 
Java, EJB, JDBC, JNDI, JTA, Sun, Sun Microsystems are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and in other countries. XML, XML Schema, XSLT and related standards are trademarks or registered trademarks of MIT, INRIA, Keio or others, and a product of the World Wide Web Consortium. All other product names mentioned herein are trademarks of their respective owners.