background image

Datatype Converter Example

<< Property Binding Declarations | Building and Running the Example >>
<< Property Binding Declarations | Building and Running the Example >>

Datatype Converter Example

public static String printShortToInteger(short value) {
BigInteger result = BigInteger.valueOf(value);
return DatatypeConverter.printInteger(result);
}
public static int parseIntegerToInt(String value) {
BigInteger result = DatatypeConverter.parseInteger(value);
return result.intValue();
}
public static String printIntToInteger(int value) {
BigInteger result = BigInteger.valueOf(value);
return DatatypeConverter.printInteger(result);
}
};
The following code shows how the MyDatatypeConverter class is referenced in a <javaType>
declaration in po.xsd:
<xsd:simpleType name=
"ZipCodeType">
<xsd:annotation>
<xsd:appinfo>
<jxb:javaType name="int"
parseMethod="primer.MyDatatypeConverter.parseIntegerToInt"
printMethod="primer.MyDatatypeConverter.printIntTo Integer" />
</xsd:appinfo>
</xsd:annotation>
<xsd:restriction base=
"xsd:integer">
<xsd:minInclusive value=
"10000"/>
<xsd:maxInclusive value=
"99999"/>
</xsd:restriction>
</xsd:simpleType>
In this example, the jxb:javaType binding declaration overrides the default JAXB binding of
this type to java.math.BigInteger. For the purposes of the Customize Inline example, the
restrictions on ZipCodeType (specifically, that legal United States ZIP codes are limited to five
digits) make it so all valid values can easily fit within the Java primitive data type int. Note also
that, because <jxb:javaType name="int"/> is declared within ZipCodeType, the customization
applies to all JAXB properties that reference this simpleType definition, including the getZip
and setZip methods.
Datatype Converter Example
The Datatype Converter example is very similar to the Customize Inline example. As with the
Customize Inline example, the customizations in the Datatype Converter example are made by
using inline binding declarations in the XML schema for the application, po.xsd.
Customizing JAXB Bindings
Chapter 17 · Binding between XML Schema and Java Classes
531