background image

Datatype Converter Example

<< MyDatatypeConverter Class | External Customize Example >>
<< MyDatatypeConverter Class | External Customize Example >>
D
ATATYPE
C
ONVERTER
E
XAMPLE
55
<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 US ZIP codes are limited to five digits--make it so all valid values can eas-
ily fit within the Java primitive datatype
int
. Note also that, because
<jxb:jav-
aType
name="int"/>
is declared within
ZipCodeType
, the customization
applies to all JAXB properties that reference this
simpleType
definition, includ-
ing the
getZip
and
setZip
methods.
Datatype Converter Example
The Datatype Converter example is very similar to the Customize Inline exam-
ple. 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
.
The global, schema, and package, and most of the class customizations for the
Customize Inline and Datatype Converter examples are identical. Where the
Datatype Converter example differs from the Customize Inline example is in the
parseMethod
and
printMethod
used for converting XML data to the Java
int
datatype.
Specifically, rather than using methods in the custom
MyDataTypeConverter
class to perform these datatype conversions, the Datatype Converter example
uses the built-in methods provided by
javax.xml.bind.DatatypeConverter
:
<xsd:simpleType name="ZipCodeType">
<xsd:annotation>
<xsd:appinfo>
<jxb:javaType name="int"
parseMethod="javax.xml.bind.DatatypeConverter.parseInt"
printMethod="javax.xml.bind.DatatypeConverter.printInt"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:restriction base="xsd:integer">