Extension customization elements are used to extend or modify the actual code generation. CodeGen allows you to apply extensions either to a particular schema or to a set of schemas, so they fit into the customization document structure as child elements of a schema or schema-set element. Since they always apply to all the schemas associated with the parent schema or schema-set element, they must precede any other child elements. Here's a sample to show how this works, with the extension elements shown in bold:
The name-converter (modifying the way names are generated) and class-decorator (adding collection helper methods to generated classes, in this case) elements are both children of the root schema-set element, so they apply to all code generation. The schema-type element (changing the Java type generated for a schema datatype) is a child of the nested schema-set, so it only applies to the code generation for schemas included in that set.
Some extension elements are designed to support selecting a particular implementation for an interface used during code generation. These extension elements normally have one required attribute, the 'class' attribute (though in the case of the name-converter extension, this attribute has a default value matching a built-in class). This attribute is used to supply the fully-qualified class name of your implementation class. Besides this required attribute, you can add other attributes which correspond to properties of your implementation class. These added attributes will be used to configure an instance of the class before it is used in code generation, so that you can customize general-purpose extension classes for each use.
CodeGen uses reflection to match any additional attributes to property or field names in your class (or in a superclass). First any hyphens in the attribute name are removed, with the character following any hyphen converted to uppercase. This modified name is then used to look for a method or field name, in three different forms.
Due to some legacy code handling, the preferred form of match is somewhat unusual -
CodeGen looks for a method name starting with
If the preferred form of match described above is not found, CodeGen next looks for
You can see the source code for the classes mentioned in the remainder of this section for examples of this correspondence between attribute names and method or field names.
The name-converter extension element is used to control how XML names are converted
to Java names. It can be used in two ways: To change the behavior of the default name converter
class used by CodeGen (
When used to change the behavior of the default name converter class used by CodeGen, the following attributes apply:
Default name converter customization attributes
When the name-converter element is used to replace the default name converter
class with your own implementation, there's only one fixed attribute: The 'class'
attribute. This attribute gives the fully-qualified name of your name converter
implementation class, which must implement the
If you do take the approach of writing your own name converter implementation you'll
probably want to refer to the default
You can extend Java class generation to add special handling or features to the generated code, using class-decorator elements. Multiple class-decorator elements can be used, and by default the decorators are inherited by child schema-set and schema-set elements within the customizations. You can change this by using the inherit-decorators customization attribute.
The required 'class' attribute must give the fully-qualified class name of a class
Several decorators are provided with CodeGen, along with an abstract base class useful for writing your own decorators. These build-in decorators are described in the remainder of this section.
Base class for matching names
Adding collection methods
Fully-qualified class and method name of a static method used to check if a
text string represents a valid instance of the type. The referenced method must take
Fully-qualified class and method name of a static method used to convert a text
string to an instance of the Java type. This attribute is optional, and is not needed if
the Java type defines a constructor taking a parameter of type
Name of a built-in JiBX format to be used for conversions between a text value of the schema type and an instance of the Java type. This optional attribute is really only useful when you want to use the Joda Date/time conversions.
Java fully-qualified class name. This required attribute gives the name of the Java type to be used for the schema type.
Fully-qualified class and method name of a static method used to convert an instance
of the Java type to a text string. This attribute is optional, and is not needed if
the Java type defines a
Schema built-in type name. This required attribute gives the simple (unqualified) name of the schema type being handled.