Castor XML - Code generator bindings
Documentation Author(s): Werner Guttmann
Introduction Binding File <binding> element <include> element <package> element <namingXML> element <componentBinding> element <java-class> element <member> element Not implemented yet
<enumBinding>
<javadoc>
<interface> element Non-trivial real world example Background The Supply Chain XSD Binding file? - IT IS REQUIRED!
Introduction
Blah
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> |
|
This element defines all the options for the class to be generated, including
common properties such as class name, package name, etc.
|
- | 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. |
For instance, the following binding definition instructs the source generator
to generate a class CustomTest for a global element named 'test', replacing
the default class name Test with CustomTest.
<elementBinding name="/test">
<java-class name="CustomTest" final="true"/>
</elementBinding> |
|
In addition to above properties, it is possible to define that the class generated
will extend a class given and/or implement one or more interfaces.
For instance, the following binding definition instructs the source generator
to generate a class TestWithInterface that implements the interface
org.castor.sample.SomeInterface in addition to
java.io.Serializable.
<elementBinding name="/test">
<java-class name="TestWithInterface">
<implements>org.castor.sample.SomeInterface</implements>
</java-class>
</elementBinding> |
|
The subsequent binding definition instructs the source generator
to generate a class TestWithExtendsAndInterface that implements the interface
org.castor.sample.SomeInterface in addition to
java.io.Serializable, and extends from (a possible) abstract base class
SomeAbstractBaseClass.
<elementBinding name="/test">
<java-class name="TestWithExtendsAndInterface">
<extends>org.castor.sample.SomeAbstractBaseClass</extends>
<implements>org.castor.sample.SomeInterface</implements>
</java-class>
</elementBinding> |
|
The generated class SomeAbstractBaseClass will have a class signature
identical to ...
...
public class TestWithExtendsAndInterface
extends SomeAbstractBaseClass
implements SomeInterface, java.io.Serializable {
...
|
|
<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|sortedset)
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.
Not implemented yet
<enumBinding>
<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.
<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.
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.
|