Menu

#82 JAXB datatype converters for xs:date xs:time and xs:dateTime

closed
nobody
Joda-Time (26)
5
2014-08-31
2012-08-20
David Boden
No

When building beans from a schema, JAXB defaults date and time types to Java's XMLGregorianCalendar.

If a project is using Joda Time, the desired default behaviour will almost certainly be to map:

xs:date to org.joda.time.LocalDate
xs:time to org.joda.time.LocalTime
xs:dateTime to org.joda.time.DateTime

The behaviour can be changed by first including the attached DatatypeConverter.java file into the joda-time build so that it's available to all users of Joda Time.

Then, the user can activate the conversion by adding the following binding (xjb) file:

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation=" &lt;a href=" http:="" java.sun.com="" xml="" ns="" jaxb"="">http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd ">
<jaxb:globalbindings>
<jaxb:javatype xmlns="http://java.sun.com/xml/ns/jaxb" name="org.joda.time.LocalDate" xmltype="xs:date" parsemethod="org.joda.time.jaxb.DatatypeConverter.parseDate" printmethod="org.joda.time.jaxb.DatatypeConverter.printDate">
<jaxb:javatype xmlns="http://java.sun.com/xml/ns/jaxb" name="org.joda.time.LocalTime" xmltype="xs:time" parsemethod="org.joda.time.jaxb.DatatypeConverter.parseTime" printmethod="org.joda.time.jaxb.DatatypeConverter.printTime">
<jaxb:javatype xmlns="http://java.sun.com/xml/ns/jaxb" name="org.joda.time.DateTime" xmltype="xs:dateTime" parsemethod="org.joda.time.jaxb.DatatypeConverter.parseTimestamp" printmethod="org.joda.time.jaxb.DatatypeConverter.printTimestamp">
</jaxb:javatype></jaxb:javatype></jaxb:javatype></jaxb:globalbindings>
</jaxb:bindings>

Discussion

  • David Boden

    David Boden - 2012-08-20
     
  • Stephen Colebourne

    Joda-Time v2 has static parse methods on the classes themselves, so I assume this would work:
    parseMethod= "org.joda.time.LocalTime.parse"

    For the print, can it not call toString() directly?
    printMethod= "org.joda.time.LocalTime.toString"

     
  • David Boden

    David Boden - 2012-08-21

    @scolebourne - good suggestion, thanks! One can't use toString() but there's a convenience method String.valueOf(Object) that just calls toString() on the object that you pass in which works nicely:

    <jaxb:javaType xmlns="http://java.sun.com/xml/ns/jaxb" name="org.joda.time.LocalDate"
              xmlType="xs:date"
              parseMethod="org.joda.time.LocalDate.parse"
              printMethod="java.lang.String.valueOf"
    />
    <jaxb:javaType xmlns="http://java.sun.com/xml/ns/jaxb" name="org.joda.time.LocalTime"
              xmlType="xs:time"
              parseMethod="org.joda.time.LocalTime.parse"
              printMethod="java.lang.String.valueOf"
    />
    <jaxb:javaType xmlns="http://java.sun.com/xml/ns/jaxb" name="org.joda.time.DateTime"
              xmlType="xs:dateTime"
              parseMethod="org.joda.time.DateTime.parse"
              printMethod="java.lang.String.valueOf"
    />
    

    My only remaining concern is that the parse methods of the Joda Time classes don't handle all allowed strings as specified by the lexical datatype definition of xs:dateTime, xs:time and xs:date. However, my unit tests pass which mean that Joda Time is correctly parsing all the example XMLs that I use, so that's good enough for me at the moment. Also, the XSD looks pretty strict:
    http://www.w3.org/TR/xmlschema-2/#dateTime
    3.2.7.1 Lexical representation

    The ·lexical space· of dateTime consists of finite-length sequences of characters of the form: '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?, where

    So, I doubt I'll run into problems.

     
  • Stephen Colebourne

    Closing as no action, and thanks for documenting this for future googling.

     
  • David Boden

    David Boden - 2012-08-21

    This request is invalid.

     
  • David Boden

    David Boden - 2012-08-21

    Closing as invalid seen as Joda Time already supports my use case well.

     

Log in to post a comment.

MongoDB Logo MongoDB