Java Reference
In-Depth Information
making to code generated from it. This could also be more or less of an issue depending on
the extent and scope of your customizations.
Example 2-22 shows a file, sometimes felicitously called a “schemalet,” that contains only
JAXB customizations. The default schema mapping of an xs:dateTime object is an
XMLGregorianCalendar . It is possible that for some reason you'd like your code to use a
java.util.Calendar or java.util.Date instead. Perhaps the API is a little trickier to use
than what you're used to, or you don't want to leak the XML origins of the class to callers if
it isn't necessary. You can specify this in a schemalet, an XML file that looks something like
the one shown in Example 2-22 .
Example2-22.External file for XML schema binding customization
<!-- Changes dates and dateTimes to java.util.Date -->
<jaxb:bindings version="2.0"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
jaxb:extensionBindingPrefixes="xjc">
<jaxb:globalBindings mapSimpleTypeDef="false"
choiceContentProperty="true">
<jaxb:javaType name="java.util.Date" xmlType="xs:date"
parseMethod="javax.xml.bind.DatatypeConverter.parseDate"
printMethod="javax.xml.bind.DatatypeConverter.printDate"/>
<jaxb:javaType name="java.util.Date" xmlType="xs:dateTime"
parseMethod="javax.xml.bind.DatatypeConverter.parseDate"
printMethod="javax.xml.bind.DatatypeConverter.printDate"/>
</jaxb:globalBindings>
</jaxb:bindings>
This file maps both xs:date and xs:dateTime to java.util.Date objects instead of
XMLGregorianCalendar , leaving xs:time (and everything else) to its default. Save it with an
.xjbextension to have the binding compiler find it automatically.
NOTE
The code to specify the binding customization in an external file is very similar to that specified in-
line. In fact, the code within the <jaxb:globalBindings> element is identical. But the root is
different because in the file its a <jaxb:bindings> and with a schema it's, well, <xsd:schema> .
Say you have a Library schema that defines a book with a title and a due date. You point
to the schemalet using the -b switch that allows you to specify either a directory in which
Search WWH ::




Custom Search