Can I specify the adapter that JAXB uses to marshal / unmount objects in my XML schema?
For example, if I want to parse the following as an integer:
<SpecialValue>0x1234</SpecialValue>
I can use the following in my circuit:
<xs:simpleType name="HexInt"> <xs:annotation> <xs:appinfo> <jaxb:javaType name="int" parseMethod="Integer.decode" /> </xs:appinfo> </xs:annotation> <xs:restriction base="xs:string"> <xs:pattern value="0x[0-9a-fA-F]+" /> </xs:restriction> </xs:simpleType> <xs:element name="SpecialValue" type="HexInt" />
When I run the circuit using the XJC tool, the string "0x1234" should be decoded using Integer.decode () as an integer with a value of 0x1234 or 4660 in decimal form. The adapter class created reflects this correctly:
public Integer unmarshal(String value) { return (Integer.decode(value)); }
However, when I want to bind a value to an XML element (which is a string literal), the Adapter class does the following:
public String marshal(Integer value) { if (value == null) { return null; } return value.toString(); }
In this case, the String value of the integer value 0x1234 (4660 in decimal) is โ4660โ, which does not match my pattern (because it does not have the โ0xโ prefix).
How can I tell the adapter that I want the integer 0x1234 to be ordered as the string literal "0x1234"? I would like to be able to do this within a framework so that I can simply generate new Java classes without the need to change the output. Is it possible?
Edit: Based on the accepted answer, here is what I did for this:
I wrote a method in the com.example.Parse class called toHexString ():
public static String toHexString(int value) { return ("0x" + Integer.toHexString(value)); }
Then I pointed out my circuit for this printing method:
<xs:simpleType name="HexInt"> <xs:annotation> <xs:appinfo> <jaxb:javaType name="int" parseMethod="Integer.decode" printMethod="com.example.Parse.toHexString" /> </xs:appinfo> </xs:annotation> <xs:restriction base="xs:string"> <xs:pattern value="0x[0-9a-fA-F]+" /> </xs:restriction> </xs:simpleType>