| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Source Code Generator Reference: For a more detailed version of the Source Generator documentation, please download the Source Generator User Document(PDF). IntroductionSource 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 XML Schema IntroductionCastor'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. Example: 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: Created class will have marshal and unmarshal methods which are used to go back and forth between XML and an Object instance. Source Generator Options 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 Command Line Options
Collection Types The source code generator has the ability to use the following types of collections when generating source code: Advanced OptionsBound PropertiesSince 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: When enabled, all properties will be treated as bound properties. For each class that is generated a setPropertyChangeListener method is created as follows: Note: To prevent unnecessary overhead, if the property is a collection, the old value will be null. Class Creation/MappingSince 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' methodThe "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' methodThe "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: Setting a super classSince 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 : Mapping XML namespaces to Java packagesSince 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 : Generate equals() methodSince 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 : Maps java primitive types to wrapper objectSince 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: To enable this property, edit the castor builder.properties file: Binding FileIt 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> elementThe binding element is the root element and contains the binding information. The attribute <include> element
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
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
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. <componentBinding> element
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
This element defines all the options for the class to be generated. <interface> element
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
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
XML SchemaThe 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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||