dev@jaxb.java.net

JAXB Support for XSD Facets

From: <hummer_at_infosys.tuwien.ac.at>
Date: Sun, 2 Jan 2011 18:37:05 +0000 (GMT)

Hi,

apologies for my previous empty post.. ;)

We are currently developing a framework for API coverage of Web
services, and one of the core issues in this project is the exact
specification of service interfaces in terms of the input/output
messages using XSD facets. To the best of our knowledge, annotations
for XSD facets are still not supported in JAXB, yet this seems to be an
often requested feature [1][2][3].

We believe to have come up with a simple solution for annotating JAXB
classes with facets, and automatic generation of the resulting XSD
schema.

We defined an @Facets annotation, which can be used, e.g., as follows:

@XmlRootElement
public class Person {
   @Facets(pattern="[a-zA-Z0-9]+")
   public String username;
   @Facets(minInclusive=18, maxInclusive=120)
   public int age;
}

The following 2 new lines of code have been added to the class
com.sun.xml.bind.v2.schemagen.XmlSchemaGenerator:

private Tree handleElementProp(final ElementPropertyInfo<T,C> ep) {
   ...
   if(!XsdFacetsGenerator.hasFacets(t, e)) // new line of code #1
      writeTypeRef(e,t, "type");
   ...
   writeOccurs(e,isOptional,repeated);
   XsdFacetsGenerator.addFacets(t, e); // new line of code #2
   ...
}

The class XsdFacetsGenerator contains the implementation to generate
the XSD code for the facet, e.g.:
<xs:element name="username">
 <xs:simpleType>
  <xs:restriction base="xs:string">
   <xs:pattern value="[a-zA-Z0-9]+"/>
  </xs:restriction>
 </xs:simpleType>
</xs:element>

In sum, the following extensions to JAXB RI would be required:
* new annotation class @javax.xml.bind.annotation.Facets
* new class com.sun.xml.bind.v2.schemagen.XsdFacetsGenerator
* 2 new lines of code in
com.sun.xml.bind.v2.schemagen.XmlSchemaGenerator

We still need to test the approach thoroughly, but the results so far
look promising. Are you interested in merging our code into the JAXB
RI, and if so, how should we proceed?

Best regards,
Waldemar Hummer

[1] http://jaxb.java.net/guide/Generating_Schema_that_you_want.html
[2] http://java.net/jira/browse/JAXB-392
[3]
http://old.nabble.com/forum/Search.jtp?query=facet&local=y&forum=13500&
daterange=0&startdate=&enddate=