CodeGen default code generationYou can use the Ant 'codegen' target to generate code from the schema using the CodeGen default settings. Here's the actual 'codegen' target from the build.xml: <!-- generate using default settings --> <target name="codegen" depends="check-runtime,clean"> <echo message="Running code generation from schema"/> <java classname="org.jibx.schema.codegen.CodeGen" fork="yes" classpathref="classpath" failonerror="true"> <arg value="-t"/> <arg value="gen/src"/> <arg value="otasubset/OTA_AirLowFareSearch*.xsd"/> </java> </target> This passes the '-t' parameter to CodeGen with the value 'gen/src' to tell it the target generation directory. The third argument value tells CodeGen to use schemas from the otasubset directory with names matching the pattern "OTA_AirLowFareSearch*.xsd" as input for code generation. You can specify as many schema names or schema name patterns as you want when running CodeGen, but must have at least one - otherwise there's nothing for CodeGen to generate! Generated codeRun the 'codegen' target and see the gen/src directory. (You can also use the 'full' target to generate and compile the code, run the binding compiler, and finally run a supplied test program which roundtrips the sample documents from the samples directory.) CodeGen's default behavior is to derive the package used for generated code from the
schema namespace URI. In this case, the schema namespace is
The generated classes in this case directly model the schema (with the exception of
xs:union simpleTypes - xs:union is currently not supported directly, instead using a
simple If you look at the generated Java class files, you'll see that each class definition
starts with a JavaDoc comment giving first the documentation from the schema component
matching the Java class, and then the schema fragment matching the class definition.
This schema fragment is from a normalized version of the schema generated after type
substitutions and inlining (the latter not a factor with the default generation used in
this example), stripping annotations, and simplifications from eliminating schema
components not handled by CodeGen (such as xs:unions - currently handled just as
Here's a sample of the generated code (reformated for a reasonable page width), showing portions of the two main classes used for the response message: /** * The Low Fare Search Response message contains a number of 'Priced Itinerary' options. Each includes: - A set of available flights matching the client's request. - Pricing information including taxes and full fare breakdown for each passenger type - Ticketing information - Fare Basis Codes and the information necessary to make a rules entry. This message contains similar information to a standard airline CRS or GDS Low Fare Search Response message. * * Schema fragment(s) for this class: * <pre> * <xs:element xmlns:ns="http://www.opentravel.org/OTA/2003/05" xmlns:xs="http://www.w3.org/2001/XMLSchema" name="OTA_AirLowFareSearchRS"> * <xs:complexType> * <xs:sequence> * <xs:choice> * <xs:sequence> * <xs:element type="ns:SuccessType" name="Success"/> * <xs:element type="ns:WarningsType" name="Warnings" minOccurs="0"/> * <xs:element type="ns:PricedItinerariesType" name="PricedItineraries"/> * </xs:sequence> * <xs:element type="ns:ErrorsType" name="Errors"/> * </xs:choice> * </xs:sequence> * <xs:attributeGroup ref="ns:OTA_PayloadStdAttributes"/> * </xs:complexType> * </xs:element> * </pre> */ public class OTAAirLowFareSearchRS { private int choiceSelect = -1; private static final int SUCCESS_CHOICE = 0; private static final int ERRORS_CHOICE = 1; private SuccessType success; private WarningsType warnings; private PricedItinerariesType pricedItineraries; private ErrorsType errors; private OTAPayloadStdAttributes OTAPayloadStdAttributes; private void setChoiceSelect(int choice) { if (choiceSelect == -1) { choiceSelect = choice; } else if (choiceSelect != choice) { throw new IllegalStateException( "Need to call clearChoiceSelect() before changing existing choice"); } } /** * Clear the choice selection. */ public void clearChoiceSelect() { choiceSelect = -1; } /** * Check if Success is current selection for choice. * * @return <code>true</code> if selection, <code>false</code> if not */ public boolean ifSuccess() { return choiceSelect == SUCCESS_CHOICE; } /** * Get the 'Success' element value. Success Standard way to indicate successful processing of an OTA message. Returning an empty element of this type indicates success. * * @return value */ public SuccessType getSuccess() { return success; } /** * Set the 'Success' element value. Success Standard way to indicate successful processing of an OTA message. Returning an empty element of this type indicates success. * * @param success */ public void setSuccess(SuccessType success) { setChoiceSelect(SUCCESS_CHOICE); this.success = success; } /** * Get the 'Warnings' element value. Standard way to indicate successful processing of an OTA message, but one in which warnings are generated. * * @return value */ public WarningsType getWarnings() { return warnings; } /** * Set the 'Warnings' element value. Standard way to indicate successful processing of an OTA message, but one in which warnings are generated. * * @param warnings */ public void setWarnings(WarningsType warnings) { setChoiceSelect(SUCCESS_CHOICE); this.warnings = warnings; } /** * Get the 'PricedItineraries' element value. Successfull Low Fare priced itineraries in response to a Low Fare Search request. * * @return value */ public PricedItinerariesType getPricedItineraries() { return pricedItineraries; } /** * Set the 'PricedItineraries' element value. Successfull Low Fare priced itineraries in response to a Low Fare Search request. * * @param pricedItineraries */ public void setPricedItineraries(PricedItinerariesType pricedItineraries) { setChoiceSelect(SUCCESS_CHOICE); this.pricedItineraries = pricedItineraries; } ... } /** * Container for priced itineraries. * * Schema fragment(s) for this class: * <pre> * <xs:complexType xmlns:ns="http://www.opentravel.org/OTA/2003/05" xmlns:xs="http://www.w3.org/2001/XMLSchema" name="PricedItinerariesType"> * <xs:sequence> * <xs:element name="PricedItinerary" maxOccurs="unbounded"> * <!-- Reference to inner class PricedItinerary --> * </xs:element> * </xs:sequence> * </xs:complexType> * </pre> */ public class PricedItinerariesType { private List<PricedItinerary> pricedItineraryList = new ArrayList<PricedItinerary>(); /** * Get the list of 'PricedItinerary' element items. * * @return list */ public List<PricedItinerary> getPricedItineraryList() { return pricedItineraryList; } /** * Set the list of 'PricedItinerary' element items. * * @param list */ public void setPricedItineraryList(List<PricedItinerary> list) { pricedItineraryList = list; } /** * Itinerary with pricing information. * * Schema fragment(s) for this class: * <pre> * <xs:element xmlns:ns="http://www.opentravel.org/OTA/2003/05" xmlns:xs="http://www.w3.org/2001/XMLSchema" name="PricedItinerary" maxOccurs="unbounded"> * <xs:complexType> * <xs:complexContent> * <xs:extension base="ns:PricedItineraryType"> * <xs:attribute type="xs:integer" use="optional" name="OriginDestinationRefNumber"/> * </xs:extension> * </xs:complexContent> * </xs:complexType> * </xs:element> * </pre> */ public static class PricedItinerary extends PricedItineraryType { private BigInteger originDestinationRefNumber; /** * Get the 'OriginDestinationRefNumber' attribute value. This attribute refers back to origin destination information in the OTA_AirLowFareSearchRQ message. * * @return value */ public BigInteger getOriginDestinationRefNumber() { return originDestinationRefNumber; } /** * Set the 'OriginDestinationRefNumber' attribute value. This attribute refers back to origin destination information in the OTA_AirLowFareSearchRQ message. * * @param originDestinationRefNumber */ public void setOriginDestinationRefNumber( BigInteger originDestinationRefNumber) { this.originDestinationRefNumber = originDestinationRefNumber; } } } The above code sample illustrates several aspects of the default code generation.
The The choice between the sequence starting with the <Success> element and the
<Errors> element is represented in the Java code with an internal state variable.
The public methods The You can also see from the code listing how CodeGen uses schema documentation components in creating class and method JavaDocs. The class JavaDoc begins with any documentation for the corresponding schema component (see import-docs), followed by the actual schema fragment matching the class (see show-schema and delete-annotations). The get/set access methods for each value in a class describe the schema equivalent of the value (as an element or attribute name) in the first sentence, followed by any schema documentation provided for that element or attribute (again controlled by import-docs). |