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:
<schema-set xmlns:xs="http://www.w3.org/2001/XMLSchema"
type-substitutions="xs:integer xs:int xs:decimal xs:float">
<name-converter strip-prefixes="OTA_"
strip-suffixes="Type AttributeGroup Group Attributes"/>
<class-decorator class="org.jibx.schema.codegen.extend.CollectionMethodsDecorator"/>
<schema-set package="org.ota.air" names="OTA_Air*.xsd">
<schema-type type-name="dateTime" java-class="org.joda.time.DateTime"
format-name="DateTime.zoned"/>
<schema-set generate-all="false" prefer-inline="true"
names="OTA_AirCommonTypes.xsd OTA_AirPreferences.xsd"/>
<schema name="OTA_AirAvailRS.xsd">
<element path="element[@name=OTA_AirAvailRS]/**/element[@name=OriginDestinationOption]"
ignore="true"/>
</schema>
</schema-set>
<schema-set package="org.ota.hotel" names="OTA_Hotel*.xsd">
<schema-set generate-all="false" prefer-inline="true"
names="OTA_HotelCommonTypes.xsd OTA_HotelContentDescription.xsd
OTA_HotelEvent.xsd OTA_HotelPreferences.xsd OTA_HotelReservation.xsd
OTA_HotelRFP.xsd"/>
</schema-set>
...
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 set
followed by the attribute
name (with the first letter of the name converted to uppercase)
and ending with Text
. The method may have either one or two parameters, with
the first parameter a java.lang.String
and the second (if defined) a
org.jibx.runtime.IUnmarshallingContext
. This form of match is intended for
when the supplied attribute value needs to use some special conversion. If a match of
this type is found, CodeGen calls the method to set the value, passing the attribute
value text as the first parameter (and the unmarshalling context as the second parameter,
if used).
If the preferred form of match described above is not found, CodeGen next looks for
either a set
method matching the attribute name (with the first letter of
the attribute name converted to uppercase) with a single parameter, or a field name
matching the attribute name with m_
prefix. The method parameter or field
may be of any of the following types: boolean
/java.lang.Boolean
;
double
/java.lang.Double
; float
/java.lang.Float
;
int
/java.lang.Integer
; long
/java.lang.Long
;
java.lang.String
; or java.lang.String[]
. CodeGen converts the
supplied attribute value to the appropriate type (in the case of an array of
String
, by splitting at each embedded whitespace sequence and trimming all
leading and trailing whitespace from each resulting value) and calls the method or sets
the field value directly.
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 (org.jibx.schema.codegen.extend.DefaultNameConverter
), or
to completely replace that default name converter class with your own implementation.
When used to change the behavior of the default name converter class used by CodeGen, the following attributes apply:
Prefix string to be added at the beginning of generated normal (non-static) field names. By default, the prefix string is empty. |
|
Suffix string to be added at the end of generated normal (non-static) field names. By default, the suffix string is empty. |
|
Prefix string to be added at the beginning of generated static field names. By default, the prefix string is empty. |
|
Suffix string to be added at the end of generated static field names. By default, the suffix string is empty. |
|
Prefix strings to be stripped from schema names before converting to Java names. The value is a list of prefix strings, separated by whitespace characters. |
|
Suffix strings to be stripped from schema names before converting to Java names. The value is a list of suffix strings, separated by whitespace characters. |
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
org.jibx.schema.codegen.extend.NameConverter
interface. You can use other
attributes on the element to configure your name converter instance, as described
above.
If you do take the approach of writing your own name converter implementation you'll
probably want to refer to the default
org.jibx.schema.codegen.extend.DefaultNameConverter
implementation and either
use it as the base for your own implementation or just subclass and override methods
selectively.
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
implementing the org.jibx.schema.codegen.extend.ClassDecorator
interface.
This interface defines methods called by CodeGen during the process of generating a Java
class. The method parameters give your implementation code ways to hook into the code
generation, which uses the Eclipse Abstract Syntax Tree (AST) model. You can then modify
the AST in various ways to meet your needs.
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.
org.jibx.schema.codegen.extend.NameMatchDecoratorBase
is a convenient
base class to use if you want your decorator to apply only to some of the data model
classes. Decorators based on this class support a 'match-name' attribute on the
class-decorator customization. The value of this optional attribute is a name
pattern, which can include one or more '*' wildcard match characters. Subclasses can
call the base class matchName()
method to check if a class name matches
the pattern.
org.jibx.schema.codegen.extend.CollectionMethodsDecorator
adds
helper methods for collection values represented by java.util.List
instances.
The helper methods are int sizeXXX()
to get the number of items in the list,
void addXXX(type)
to add an item to the list, type getXXX(int)
to get an item by position, and void clearXXX()
to remove all items from
the list (where 'XXX' is the value name). No attributes are used with this decorator.
List
implementation classorg.jibx.schema.codegen.extend.ListImplementationDecorator
lets you
set the implementation class to be used for instances of java.util.List
in
the data model. The required 'list-class' attribute must give the fully-qualified class
name of the desired implementation class.
org.jibx.schema.codegen.extend.ExtensionDecorator
allows you to set
a base class to be extended by your data model classes. This decorator class itself
extends the abstract org.jibx.schema.codegen.extend.NameMatchDecoratorBase
class described above, so you can use a 'match-name' attribute to control the data
model classes which are modified by this decorator (matching on the simple class name,
without package name). The 'base-class' attribute must give the desired base class
name.
You can also use this decorator to customize the JiBX binding for data model classes to call user extension methods implemented by the base class. Use a 'pre-get-name' attribute to set a method to be called before marshalling the data class, a 'pre-set-name' attribute to set a method to be called before unmarshalling to the data class, and a 'post-set-name' attribute to set a method to be called after unmarshalling the data class.
org.jibx.schema.codegen.extend.SerializableDecorator
is used to add
the java.io.Serializable
interface to generated data model classes. If a
'serial-version' attribute is used with this decorator it will also add a
serialVersionUID
value to each generated class, with the specified version
(which must be a long integer value).
You can customize the code generation for schema built-in datatypes using the schema-type element. Besides determining the Java type to be used for a schema datatype, this also allows you to define the actual format used to convert instances of one into the other (see and <format> element for dicsussions of these conversions). The attributes used with the schema-type element are listed in the following table:
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
a |
|
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. |