background image

MyDatatypeConverter Class

<< Property Binding Declarations | Datatype Converter Example >>
<< Property Binding Declarations | Datatype Converter Example >>
54
U
SING
JAXB
MyDatatypeConverter Class
The
<INSTALL>/examples/jaxb/inline-customize
/MyDatatypeConverter
class, shown below, provides a way to customize the
translation of XML datatypes to and from Java datatypes by means of a
<javaType>
customization.
package primer;
import java.math.BigInteger;
import javax.xml.bind.DatatypeConverter;
public class MyDatatypeConverter {
public static short parseIntegerToShort(String value) {
BigInteger result =
DatatypeConverter.parseInteger(value);
return (short)(result.intValue());
}
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>