Example 2: Generating only the required classes

# Example 2: Generating only the required classes |

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 | ### Customizing CodeGen code generation You can supply a customization file to CodeGen to control many aspects of the code and binding generation. Two customization examples are included in the samples/codegen directory. This page discusses the first customization example (custom1.xml), which demonstrates how you can easily eliminate unnecessary classes from the generated code. To pass the customization file to CodeGen you need to use a '-c' command line parameter, followed by the actual path to the customization file. The supplied Ant build.xml file has a 'custgen1' target for this purpose:
      <!-- generate using first customizations -->
      <target name="custgen1" 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="-c"/>
          <arg value="custom1.xml"/>
          <arg value="otasubset/OTA_AirLowFareSearch*.xsd"/>
        </java>

      </target>
Aside from the '-c' command line parameter and the actual customization file path, this is identical to the 'codegen' target used for the first example. ### First customizations Here's the actual content of the custom1.xml customizations file used in this example:
    <schema-set prefer-inline="true" generate-all="false" package="org.ota.air">
      <schema name="OTA_AirLowFareSearchRQ.xsd" includes="OTA_AirLowFareSearchRQ"/>
      <schema name="OTA_AirLowFareSearchRS.xsd" includes="OTA_AirLowFareSearchRS"/>
    </schema-set>
The attributes on the <schema-set> root element tell CodeGen to inline definitions where possible in the generated code (`prefer-inline="true"`), to only generate the required schema definitions, rather than all the schema definitions (`generate-all="false"`), and to use the Java package `org.ota.air`). The nested <schema> elements each identify a particular schema, and tell CodeGen which global definitions from that schema should be included in the code generation. ### Generated code Run the 'custgen1' target and see the gen/src directory. You can also use the 'custom1' 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. The number of generated classes is considerably smaller than in the first example, because many of the global definitions from included schemas are not actually used by the "OTA_AirLowFareSearchRQ" and "OTA_AirLowFareSearchRS" elements, and due to the customizations passed to CodeGen it drops these unused definitions from the code generation process. TODO: add discussion