Skip to main content

JAXB customization just got easier to use

Posted by kohsuke on September 1, 2006 at 10:22 AM PDT

Besides the spec improvements that are planned for 2.1, we are thinking about a bunch of implementation improvements. I've been working for the past 2 days coding intensively to implement one of them, and it's ready for you to try. So that's what I'm going to talk about today.

One of the things I don't like about the JAXB customization files is the use of XPath. For example, if you want to change the class name generated from a complex type, you'd write something like:

<br /><bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"><br />  <bindings schemaLocation="../path/to/my.xsd" node="/xs:schema/xs:complexType[@name='foo']"><br />    <class name="FooType"/><br />  </bindings><br /></bindings><br />

While it works, the problem with this is that this XPath+schemaLocation combo tends to be verbose and error prone. It's verbose, because often a trivial target schema component like this "global complex type foo" takes up a lot of characters. The xs namespace declaration also takes up some space, although in this case we managed to avoid declaring the tns namespace (that represents the namespace that the schema defines.)

It's also error prone, because it relies on the way schema documents are laid out, because the schemaLocation attribute needs to point to the right schema document file. When a schema is split into multiple files for modularity (happens especially often with large ones), then you'd have to find which schema file it is. Even though you can use relative paths, this hard-coding of path information makes it hard to pass around the binding file to other people.

What we need is a better way to specify a component in XML Schema. This is where the JAXB RI 2.1 does a better job.

The key technology to solve this problem is "schema component designator" (SCD.) This is a path language just like XPath, but whereas XPath is designed to refer to XML infoset items like elements and attributes, SCD is designed to refer to schema components like element declarations or complex types.

With the SCD support in the JAXB RI 2.1, the above binding can be written as follows:

<br /><bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.1" xmlns:tns="http://my.namespace/"><br />  <bindings scd="/type::tns:foo"><br />    <class name="FooType"/><br />  </bindings><br /></bindings><br />

"/type::tns:foo" can be written more concisely as "/~tns:foo", too. If you are interested in more about the syntax of SCD, read the example part of the spec, and maybe EBNF. If you know XPath, I think you'll find fairly easy to learn.

Another benefit of SCD is that tools will have easier time generating SCDs than XPath, as XPath is often vulnerable to a little change in the schema document, while SCDs are much more robust. Stay tuned for more improvements in this area in 2.1 RI and tools around it.

The RI that implements this is available for download. Just be aware that SCD is still a working draft and therefore the syntax may change in the future.

If you are just interested in SCD outside JAXB, the SCD support is implemented on XSOM in a reusable form, available under CDDL.

Related Topics >>