Customizing CodeGen code generationYou 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 customizationsHere'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 ( 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 codeRun 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 |