Index: C:/workspace/castor1625/src/doc/project.xml =================================================================== --- C:/workspace/castor1625/src/doc/project.xml (revision 6308) +++ C:/workspace/castor1625/src/doc/project.xml (working copy) @@ -166,6 +166,10 @@ xmlschema.xml Schema Support + + srcgen-example.xml + Example +
Index: C:/workspace/castor1625/src/doc/srcgen-example.xml =================================================================== --- C:/workspace/castor1625/src/doc/srcgen-example.xml (revision 0) +++ C:/workspace/castor1625/src/doc/srcgen-example.xml (revision 0) @@ -0,0 +1,410 @@ + + + + + + The Castor XML Code Generator- Example + + Werner Guttmann + + + Describes a fully blown example that shows how to use the + XML code generator to create Java source code from an XML + schema. + + Final + + + + + Example using the Castor XML code generator +
+

+ API Reference: + + The XML code generator API + +

+
+ +
+ +

+ In this section we illustrate the use of the Source + Generator by explaining the generated classes from a + given XML schema. The XML code generator is going to be used + with the “java class mapping� property set to element + (default value). +

+ +
+ +
+ +

The input file is the schema file given with the XML code generator + example in the distribution of Castor + (under /src/examples/SourceGenerator/invoice.xsd).

+ + + + + + + This is a test XML Schema for Castor XML. + + + + + + + + A simple representation of an invoice + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Represents a U.S. Address + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +]]> + +

The structure of this schema is simple: it is composed of a + top-level element which is a complexType with references to + other elements inside. This schema represents a simple + invoice: an invoice is a customer (customer top-level + group), an article (item element), a shipping method + (shipping-method element) and a shipping date + (shipping-date element). Notice that the ship-to element + uses a reference to an address element. This address + element is a top-level element that contains a reference to + a non-top-level element (the zip-cod element). At the end + of the schema we have two simpleTypes for representing a + telephone number and a price. The Source Generator is used + with the element property set for class creation + so a class is going to be generated for all top-level elements. No classes + are going to be generated for complexTypes and simpleTypes since the + simpleType is not an enumeration.

+ +

To summarize, we can expect 7 classes : Invoice, Customer, + Address, Item, ShipTo, ShippingMethod + and ShippingDate and the 7 corresponding class descriptors. Note + that a class is generated for the top-level group customer

+ +
+ +
+ +

To run the source generator and create the source from the + invoice.xsd file in a package test, we just call + in the command line:

+ + +java -cp %CP% org.exolab.castor.builder.SourceGenerator -i invoice.xsd -package test + +
+ +
+ +

To simplify this example we now focus on the item element.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +]]> + +

To represent an Item object, we need to know its Id, the + Quantity ordered and the Price for one item. So we can + expect to find a least three private variables: a string for + the Id element, an int for the quantity element (see the + section on XML Schema support if you want to see the mapping + between a W3C XML Schema type and a java type), but what type + for the Price element?

+ +

While processing the Price + element, Castor is going to process the type of Price i.e. + the simpleType PriceType which base is decimal. Since + derived types are automatically mapped to parent types and + W3C XML Schema decimal type is mapped to a + java.math.BigDecimal, the price element will be a + java.math.BigDecimal. Another private variable is created + for quantity: quantity is mapped to a primitive java type, + so a boolean has_quantity is created for monitoring the + state of the quantity variable. The rest of the code is the + getter/setter methods and the Marshalling framework + specific methods. Please find below the complete Item class + (with Javadoc comments stripped off):

+ + +/** + * This class was automatically generated with + * Castor 1.0.4, + * using an XML Schema. + */ + +package test; + +public class Item implements java.io.Serializable { + + //--------------------------/ + //- Class/Member Variables -/ + //--------------------------/ + + private java.lang.String _id; + + private int _quantity; + + /** + * keeps track of state for field: _quantity + */ + private boolean _has_quantity; + + private java.math.BigDecimal _price; + + //----------------/ + //- Constructors -/ + //----------------/ + + public Item() { + super(); + } //-- test.Item() + + + //-----------/ + //- Methods -/ + //-----------/ + + public java.lang.String getId() { + return this._id; $ + } //-- java.lang.String getId() + + public java.math.BigDecimal getPrice() { + return this._price; + } //-- java.math.BigDecimal getPrice() + + public int getQuantity() { + return this._quantity; + } //-- int getQuantity() + + public boolean hasQuantity() { + return this._has_quantity; + } //-- boolean hasQuantity() + + public boolean isValid() { + try { + validate(); + } catch (org.exolab.castor.xml.ValidationException vex) { + return false; + } + return true; + } //-- boolean isValid() + + public void marshal(java.io.Writer out) + throws org.exolab.castor.xml.MarshalException,org.exolab.castor.xml.ValidationException { + Marshaller.marshal(this, out); + } //-- void marshal(java.io.Writer) + + public void marshal(org.xml.sax.DocumentHandler handler) + throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException { + Marshaller.marshal(this, handler); + } //-- void marshal(org.xml.sax.DocumentHandler) + + public void setId(java.lang.String _id) { + this._id = _id; + } //-- void setId(java.lang.String) + + public void setPrice(java.math.BigDecimal _price) { + this._price = _price; + } //-- void setPrice(java.math.BigDecimal) + + public void setQuantity(int _quantity) { + this._quantity = _quantity; + this._has_quantity = true; + } //-- void setQuantity(int) + + public static test.Item unmarshal(java.io.Reader reader) + throws org.exolab.castor.xml.MarshalException,org.exolab.castor.xml.ValidationException { + return (test.Item) Unmarshaller.unmarshal(test.Item.class, reader); + } //-- test.Item unmarshal(java.io.Reader) + + public void validate() + throws org.exolab.castor.xml.ValidationException { + org.exolab.castor.xml.Validator.validate(this, null); + } //-- void validate() + +} + + +

The ItemDescriptor class is a bit more complex. This class + is containing inner classes which are the XML field + descriptors for the different components of an ‘Item’ + element i.e. id, quantity and price.

+ +
+ + + +
+