JAXB-Facets
The Java Java Architecture for XML Binding (JAXB, JSR222) is a framework which simplifies the mapping between Java objects and their XML representation. Although an often requested feature, the JAXB reference implementation (JAXB RI, http://jaxb.java.net/) currently does not support XSD facets for simple types (e.g., pattern, maxSize, fractionDigits etc.) in generated XML Schema files. We therefore provide a simple extension to JAXB RI to define facets for Java class attributes using an @Facets annotation, which are then considered in the the XML Schema generation procedure. Furthermore, our implementation supports @MinOccurs and @MaxOccurs annotations. These features are particularly useful when implementing Web services with JAX-WS (Java API for XML Web Services, JSR224, JAX-WS RI), as illustrated in the example code of an imaginative chart Web service below:@XmlRootElement public class ChartRequest { public static enum ChartType { line, bar, pie } @XmlElement(required=true) public ChartType type; @MinOccurs(1) @MaxOccurs(10) @Facets(minInclusive=-100, maxInclusive=100) public List<Integer> value; @Facets(pattern="[a-z][a-z0-9]{0,4}") public String name; } @WebService public class ChartService { public String generateChart(ChartRequest request) { // generate some chart image } }Upon deployment of the ChartService with JAX-WS, the generated WSDL file describing the service interface will contain an XSD declaration similar to the code snippet below:<complexType name="chartRequest"> <sequence> <element name="type"> <simpleType> <restriction base="string"> <enumeration value="line" /> <enumeration value="bar" /> <enumeration value="pie" /> </restriction> </simpleType> </element> <element name="value" minOccurs="1" maxOccurs="10"> <simpleType> <restriction base="int"> <minInclusive value="-100" /> <maxInclusive value="100" /> </restriction> </simpleType> </element> <element name="name"> <simpleType> <restriction base="string"> <pattern value="[a-z][a-z0-9]{0,4}" /> </restriction> </simpleType> </element> </sequence> </complexType>Download
The implementation (source code and compiled class files) can be downloaded here. Please refer to the README.txt for installation/usage instructions.
- 2012-04-15: version 0.2 (supports Facets for XML attributes; support for Java 1.7)
- 2011-12-04: version 0.1 (initial release)
Maven Users
JAXB-Facets can be easily integrated into your Maven project. Simply add the following entries to your pom.xml file.
Java 1.7
Because JRE 1.7 ships with built-in support for JAXB (in <jre>/lib/rt.jar), we need to tell the JVM how to integrate JAXB-Facets and override the default implementation. One way to achieve this is the Java endorsed standards override mechanism (basically, by setting the system property "java.endorsed.dirs"). In the code below, simply replace "at.ac.tuwien.infosys.jaxb.test.FacetsTestUtil" with the name of the class you want to execute in your project and then run "mvn exec:exec".<project> ... <dependencies> <dependency> <groupId>at.ac.tuwien.infosys</groupId> <artifactId>jaxb-facets</artifactId> <version>0.2</version> </dependency> ... </dependencies> </repositories> ... <repository> <id>infosys-repo</id> <name>infosys-repo</name> <url>http://www.infosys.tuwien.ac.at/mvn/</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.12</version> <configuration> <argLine>-Djava.endorsed.dirs="${settings.localRepository}/at/ac/tuwien/infosys/jaxb-facets/0.2/"</argLine> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <goals> <goal>exec</goal> </goals> </execution> </executions> <configuration> <executable>java</executable> <arguments> <argument>-Djava.endorsed.dirs=${settings.localRepository}/at/ac/tuwien/infosys/jaxb-facets/0.2/</argument> <argument>-classpath</argument> <classpath/> <argument>at.ac.tuwien.infosys.jaxb.test.FacetsTestUtil</argument> </arguments> </configuration> </plugin> ... </plugins> ... </build> </project>Java 1.6 and below
<project> ... <dependencies> <!-- note that jaxb-facets should be at the top of all dependencies (at least before any dependencies to JAXB libraries) --> <dependency> <groupId>at.ac.tuwien.infosys</groupId> <artifactId>jaxb-facets</artifactId> <version>0.1</version> </dependency> ... <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.4</version> </dependency> ... </dependencies> </repositories> ... <repository> <id>infosys-repo</id> <name>infosys-repo</name> <url>http://www.infosys.tuwien.ac.at/mvn/</url> </repository> </repositories> </project>