CodeGen Examples

# CodeGen Examples |

JiBX Basics Overview Getting Started Binding Compiler Binding on Load Runtime Extras Building JiBX Contributing License News and Status Support FAQ Mailing Lists Bugs User Comments Sponsors Start from Code Start from Code BindGen Example Code Example 1 Example 2 Example 3 Example 4 Customizations Reference SchemaGen Jibx2Wsdl Examples Customizations Start from Schema Start from Schema CodeGen Example Code Example 1 Example 2 Example 3 Customizations Reference Binding Definition Binding Tutorial A basic binding Binding extras Structure mapping Collections and arrays Using mappings Advanced binding Method hooks Custom code Precompiled Bindings Definition details Definition contexts Conversions XML summary <binding> element <include> element <format> element <namespace> element <mapping> element <value> element <structure> element <collection> element Attribute groups Usage API Runtime JavaDocs Subprojects Eclipse Plug-in Introduction Install Usage Axis2 Usage | ### CodeGen example schema The /examples/codegen directory of the JiBX distribution contains some samples for demonstrating the use of CodeGen. The samples use a subset of schema definitions from the OpenTravel Alliance, an organization developing open standards for use in electronic message exchange. The OTA definitions consist of hundreds of schemas in total, but the subset used for these samples includes just seven schemas, representing a request-response pair of messages: OTA_AirLowFareSearchRQ and OTA_AirLowFareSearchRS. Here's a listing of the main schemas:
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xs="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified" id="OTA2007A"
        targetNamespace="http://www.opentravel.org/OTA/2003/05" version="2.007">
      <xsd:include schemaLocation="OTA_AirCommonTypes.xsd"/>
      <xsd:include schemaLocation="OTA_AirPreferences.xsd"/>
      <xsd:element name="OTA_AirLowFareSearchRQ">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element type="POS_Type" name="POS"/>
            <xsd:element name="ProcessingInfo" minOccurs="0">
              <xsd:complexType>
                <xsd:attributeGroup ref="AirProcessingInfoGroup"/>
              </xsd:complexType>
            </xsd:element>
            <xsd:element name="OriginDestinationInformation" maxOccurs="10">
              <xsd:complexType>
                <xsd:complexContent>
                  <xsd:extension base="OriginDestinationInformationType">
                    <xsd:sequence>
                      <xsd:element name="AlternateLocationInfo" minOccurs="0">
                        <xsd:complexType>
                          <xsd:attribute type="ListOfStringLength1to8" use="optional"
                              name="OriginLocation"/>
                          <xsd:attribute type="ListOfStringLength1to8" use="optional"
                              name="DestinationLocation"/>
                        </xsd:complexType>
                      </xsd:element>
                      <xsd:element ref="TPA_Extensions" minOccurs="0"/>
                    </xsd:sequence>
                    <xsd:attribute type="xs:string" use="optional" name="RPH"/>
                    <xsd:attribute type="xs:integer" use="optional" name="RefNumber"/>
                  </xsd:extension>
                </xsd:complexContent>
              </xsd:complexType>
            </xsd:element>
            <xsd:element type="SpecificFlightInfoType" name="SpecificFlightInfo" minOccurs="0"/>
            <xsd:element name="TravelPreferences" minOccurs="0" maxOccurs="10">
              <xsd:complexType>
                <xsd:complexContent>
                  <xsd:extension base="AirSearchPrefsType">
                    <xsd:attribute use="optional" name="FlexDatePref">
                      <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                          <xsd:enumeration value="Outbound"/>
                          <xsd:enumeration value="Return"/>
                          <xsd:enumeration value="Both"/>
                        </xsd:restriction>
                      </xsd:simpleType>
                    </xsd:attribute>
                    <xsd:attribute type="xs:boolean" use="optional" name="FlexWeekendIndicator"/>
                    <xsd:attribute type="xs:boolean" use="optional" name="FlexLevelIndicator"/>
                    <xsd:attribute type="xs:boolean" use="optional" name="NoFareBreakIndicator"/>
                    <xsd:attribute type="ListOfRPH" use="optional" name="OriginDestinationRPHs"/>
                  </xsd:extension>
                </xsd:complexContent>
              </xsd:complexType>
            </xsd:element>
            <xsd:element name="TravelerInfoSummary">
              <xsd:complexType>
                <xsd:complexContent>
                  <xsd:extension base="TravelerInfoSummaryType">
                    <xsd:attribute type="xs:string" use="optional" name="TicketingCountryCode"/>
                    <xsd:attribute type="xs:boolean" use="optional" name="SpecificPTC_Indicator"/>
                  </xsd:extension>
                </xsd:complexContent>
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
          <xsd:attributeGroup ref="OTA_PayloadStdAttributes"/>
          <xsd:attributeGroup ref="MaxResponsesGroup"/>
          <xsd:attribute type="xs:boolean" use="optional" default="false" name="DirectFlightsOnly"/>
          <xsd:attribute type="xs:boolean" use="optional" name="AvailableFlightsOnly"/>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.opentravel.org/OTA/2003/05"
        elementFormDefault="qualified" id="OTA2007A"
        targetNamespace="http://www.opentravel.org/OTA/2003/05" version="5.000">
      <xsd:include schemaLocation="OTA_AirCommonTypes.xsd"/>
      <xsd:element name="OTA_AirLowFareSearchRS">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:choice>
              <xsd:sequence>
                <xsd:element type="SuccessType" name="Success"/>
                <xsd:element type="WarningsType" name="Warnings" minOccurs="0"/>
                <xsd:element type="PricedItinerariesType" name="PricedItineraries"/>
              </xsd:sequence>
              <xsd:element type="ErrorsType" name="Errors"/>
            </xsd:choice>
          </xsd:sequence>
          <xsd:attributeGroup ref="OTA_PayloadStdAttributes"/>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
### Sample documents The /examples/codegen/samples directory contains the OTA_AirLowFareSearchRQ and OTA_AirLowFareSearchRS sample documents from the OTA distribution (two documents of each type). Here's one of the sample requests, and an edited version of the sample response:
    <OTA_AirLowFareSearchRQ EchoToken="50987" TimeStamp="2003-11-19T19:44:10-05:00"
        Target="Production" SequenceNmbr="1" xmlns="http://www.opentravel.org/OTA/2003/05"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="2.001">
      <POS>
        <Source>
          <RequestorID Type="13" ID="01235"/>
        </Source>
      </POS>
      <OriginDestinationInformation>
        <DepartureDateTime>2004-06-05T10:00:00-05:00</DepartureDateTime>
        <OriginLocation LocationCode="IAD"/>
        <DestinationLocation LocationCode="FLG"/>
      </OriginDestinationInformation>
      <OriginDestinationInformation>
        <DepartureDateTime>2004-06-27T10:00:00-05:00</DepartureDateTime>
        <OriginLocation LocationCode="FLG"/>
        <DestinationLocation LocationCode="IAD"/>
      </OriginDestinationInformation>
      <TravelPreferences>
        <FareRestrictPref>
          <AdvResTicketing AdvTicketingInd="false">
            <AdvTicketing/>
          </AdvResTicketing>
          <StayRestrictions StayRestrictionsInd="true">
                 <MinimumStay />
            <MaximumStay MaxStay="14" StayUnit="Days"/>
          </StayRestrictions>
        </FareRestrictPref>
      </TravelPreferences>
      <TravelerInfoSummary>
        <AirTravelerAvail>
            <PassengerTypeQuantity Code="ADT" Quantity="1"/>
        </AirTravelerAvail>
      </TravelerInfoSummary>
    </OTA_AirLowFareSearchRQ>

    <OTA_AirLowFareSearchRS EchoToken="53468" TimeStamp="2003-11-19T19:44:17-05:00"
        Target="Production" SequenceNmbr="1" xmlns="http://www.opentravel.org/OTA/2003/05"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="2.001">
      <Success/>
      <PricedItineraries>
        <PricedItinerary SequenceNumber="1">
          <AirItineraryPricingInfo>
            <ItinTotalFare>
              <BaseFare Amount="256.75" CurrencyCode="USD"/>
              <TotalFare Amount="285.00" CurrencyCode="USD"/>
            </ItinTotalFare>
            <FareInfos>
              <FareInfo>
                <FareReference> "K"</FareReference>
                <RuleInfo>
                  <ResTicketingRules>
                    <AdvResTicketing AdvTicketingInd="true">
                      <AdvTicketing FromDepartPeriod="21" FromDepartUnit="Days"/>
                    </AdvResTicketing>
                  </ResTicketingRules>
                  <LengthOfStayRules>
                    <MinimumStay MinStay="14" StayUnit="Days"/>
                    <MaximumStay ReturnType="C" MaxStay="30" StayUnit="Days"/>
                  </LengthOfStayRules>
                  <ChargesRules>
                    <VoluntaryChanges VolChangeInd="true">
                      <Penalty Percent="100"/>
                    </VoluntaryChanges>
                  </ChargesRules>
                </RuleInfo>
                <FilingAirline>HP</FilingAirline>
                <DepartureAirport LocationCode="IAD"/>
                <ArrivalAirport LocationCode="FLG"/>
              </FareInfo>
              <FareInfo>
                <FareReference> "H"</FareReference>
                <RuleInfo>
                  <ResTicketingRules>
                    <AdvResTicketing AdvTicketingInd="true">
                      <AdvTicketing FromDepartPeriod="21" FromDepartUnit="Days"/>
                    </AdvResTicketing>
                  </ResTicketingRules>
                  <LengthOfStayRules>
                    <MinimumStay MinStay="14" StayUnit="Days"/>
                    <MaximumStay ReturnType="C" MaxStay="30" StayUnit="Days"/>
                  </LengthOfStayRules>
                  <ChargesRules>
                    <VoluntaryChanges VolChangeInd="true">
                      <Penalty Percent="100"/>
                    </VoluntaryChanges>
                  </ChargesRules>
                </RuleInfo>
                <FilingAirline>HP</FilingAirline>
                <DepartureAirport LocationCode="FLG"/>
                <ArrivalAirport LocationCode="IAD"/>
              </FareInfo>
            </FareInfos>
          </AirItineraryPricingInfo>
        </PricedItinerary>
        ...
        <PricedItinerary SequenceNumber="3">
          <AirItineraryPricingInfo>
            <ItinTotalFare>
              <BaseFare Amount="681.75" CurrencyCode="USD" />
              <TotalFare Amount="757.00" CurrencyCode="USD" />
            </ItinTotalFare>
            <FareInfos>
              ...
            </FareInfos>
          </AirItineraryPricingInfo>
        </PricedItinerary>
      </PricedItineraries>
    </OTA_AirLowFareSearchRS>
### Examples The examples using these schemas and sample documents demonstrate using CodeGen with increasingly sophisticated levels of customization. One of the common complaints about code generation from schema is that it results in extremely complex data models with a large number of classes, so simplifying the data model using customizations is a main focus of the examples (which go from 173 classes in the data model, including inner classes, down to 50 classes). 'codegen' target - default generation, including all schema components 'custgen1' target - generating only selected global definitions to eliminate unneeded classes 'custgen2' target - customizing names and types, eliminating unused parts of schema definitions