Thursday, February 12, 2015

A JAXB Nuance: String Versus Enum from Enumerated Restricted XSD String

Although Java Architecture for XML Binding (JAXB) is fairly easy to use in nominal cases (especially since Java SE 6), it also presents numerous nuances. Some of the common nuances are due to the inability to exactly match (bind) XML Schema Definition (XSD) types to Java types. This post looks at one specific example of this that also demonstrates how different XSD constructs that enforce the same XML structure can lead to different Java types when the JAXB compiler generates the Java classes.

The next code listing, for Food.xsd, defines a schema for food types. The XSD mandates that valid XML will have a root element called "Food" with three nested elements "Vegetable", "Fruit", and "Dessert". Although the approach used to specify the "Vegetable" and "Dessert" elements is different than the approach used to specify the "Fruit" element, both approaches result in similar "valid XML." The "Vegetable" and "Dessert" elements are declared directly as elements of the prescribed simpleTypes defined later in the XSD. The "Fruit" element is defined via reference (ref=) to another defined element that consists of a simpleType.

Food.xsd
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  
  3.            xmlns:dustin="http://marxsoftware.blogspot.com/foodxml"  
  4.            targetNamespace="http://marxsoftware.blogspot.com/foodxml"  
  5.            elementFormDefault="qualified"  
  6.            attributeFormDefault="unqualified">  
  7.   
  8.    <xs:element name="Food">  
  9.       <xs:complexType>  
  10.          <xs:sequence>  
  11.             <xs:element name="Vegetable" type="dustin:Vegetable" />  
  12.             <xs:element ref="dustin:Fruit" />  
  13.             <xs:element name="Dessert" type="dustin:Dessert" />  
  14.          </xs:sequence>  
  15.       </xs:complexType>  
  16.    </xs:element>  
  17.   
  18.    <!--  
  19.         Direct simple type that restricts xs:string will become enum in  
  20.         JAXB-generated Java class.  
  21.    -->  
  22.    <xs:simpleType name="Vegetable">  
  23.       <xs:restriction base="xs:string">  
  24.          <xs:enumeration value="Carrot"/>  
  25.          <xs:enumeration value="Squash"/>  
  26.          <xs:enumeration value="Spinach"/>  
  27.          <xs:enumeration value="Celery"/>  
  28.       </xs:restriction>  
  29.    </xs:simpleType>  
  30.   
  31.    <!--  
  32.         Simple type that restricts xs:string but is wrapped in xs:element  
  33.         (making it an Element rather than a SimpleType) will become Java  
  34.         String in JAXB-generated Java class for Elements that reference it.  
  35.    -->  
  36.    <xs:element name="Fruit">  
  37.       <xs:simpleType>  
  38.          <xs:restriction base="xs:string">  
  39.             <xs:enumeration value="Watermelon"/>  
  40.             <xs:enumeration value="Apple"/>  
  41.             <xs:enumeration value="Orange"/>  
  42.             <xs:enumeration value="Grape"/>  
  43.          </xs:restriction>  
  44.       </xs:simpleType>  
  45.    </xs:element>  
  46.   
  47.    <!--  
  48.         Direct simple type that restricts xs:string will become enum in  
  49.         JAXB-generated Java class.          
  50.    -->  
  51.    <xs:simpleType name="Dessert">  
  52.       <xs:restriction base="xs:string">  
  53.          <xs:enumeration value="Pie"/>  
  54.          <xs:enumeration value="Cake"/>  
  55.          <xs:enumeration value="Ice Cream"/>  
  56.       </xs:restriction>  
  57.    </xs:simpleType>  
  58.   
  59. </xs:schema>  

Although Vegetable and Dessert elements are defined in the schema differently than Fruit, the resulting valid XML is the same. A valid XML file is shown next in the code listing for food1.xml.

food1.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Food xmlns="http://marxsoftware.blogspot.com/foodxml"  
  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  4.    <Vegetable>Spinach</Vegetable>  
  5.    <Fruit>Watermelon</Fruit>  
  6.    <Dessert>Pie</Dessert>  
  7. </Food>  

At this point, I'll use a simple Groovy script to validate the above XML against the above XSD. The code for this Groovy XML validation script (validateXmlAgainstXsd.groovy) is shown next.

validateXmlAgainstXsd.groovy
  1. #!/usr/bin/env groovy  
  2.   
  3. // validateXmlAgainstXsd.groovy  
  4. //  
  5. // Accepts paths/names of two files. The first is the XML file to be validated  
  6. // and the second is the XSD against which to validate that XML.  
  7.   
  8. if (args.length < 2)  
  9. {  
  10.    println "USAGE: groovy validateXmlAgainstXsd.groovy <xmlFile> <xsdFile>"  
  11.    System.exit(-1)  
  12. }  
  13.   
  14. String xml = args[0]  
  15. String xsd = args[1]  
  16.   
  17. import javax.xml.validation.Schema  
  18. import javax.xml.validation.SchemaFactory  
  19. import javax.xml.validation.Validator  
  20.   
  21. try  
  22. {  
  23.    SchemaFactory schemaFactory =  
  24.       SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)  
  25.    Schema schema = schemaFactory.newSchema(new File(xsd))  
  26.    Validator validator = schema.newValidator()  
  27.    validator.validate(new javax.xml.transform.stream.StreamSource(xml))  
  28. }  
  29. catch (Exception exception)  
  30. {  
  31.    println "\nERROR: Unable to validate ${xml} against ${xsd} due to '${exception}'\n"  
  32.    System.exit(-1)  
  33. }  
  34. println "\nXML file ${xml} validated successfully against ${xsd}.\n"  

The next screen snapshot demonstrates running the above Groovy XML validation script against food1.xml and Food.xsd.

The objective of this post so far has been to show how different approaches in an XSD can lead to the same XML being valid. Although these different XSD approaches prescribe the same valid XML, they lead to different Java class behavior when JAXB is used to generate classes based on the XSD. The next screen snapshot demonstrates running the JDK-provided JAXB xjc compiler against the Food.xsd to generate the Java classes.

The output from the JAXB generation shown above indicates that Java classes were created for the "Vegetable" and "Dessert" elements but not for the "Fruit" element. This is because "Vegetable" and "Dessert" were defined differently than "Fruit" in the XSD. The next code listing is for the Food.java class generated by the xjc compiler. From this we can see that the generated Food.java class references specific generated Java types for Vegetable and Dessert, but references simply a generic Java String for Fruit.

Food.java (generated by JAXB jxc compiler)
  1. //  
  2. // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802   
  3. // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>   
  4. // Any modifications to this file will be lost upon recompilation of the source schema.   
  5. // Generated on: 2015.02.11 at 10:17:32 PM MST   
  6. //  
  7.   
  8.   
  9. package com.blogspot.marxsoftware.foodxml;  
  10.   
  11. import javax.xml.bind.annotation.XmlAccessType;  
  12. import javax.xml.bind.annotation.XmlAccessorType;  
  13. import javax.xml.bind.annotation.XmlElement;  
  14. import javax.xml.bind.annotation.XmlRootElement;  
  15. import javax.xml.bind.annotation.XmlSchemaType;  
  16. import javax.xml.bind.annotation.XmlType;  
  17.   
  18.   
  19. /** 
  20.  * <p>Java class for anonymous complex type. 
  21.  *  
  22.  * <p>The following schema fragment specifies the expected content contained within this class. 
  23.  *  
  24.  * <pre> 
  25.  * <complexType> 
  26.  *   <complexContent> 
  27.  *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
  28.  *       <sequence> 
  29.  *         <element name="Vegetable" type="{http://marxsoftware.blogspot.com/foodxml}Vegetable"/> 
  30.  *         <element ref="{http://marxsoftware.blogspot.com/foodxml}Fruit"/> 
  31.  *         <element name="Dessert" type="{http://marxsoftware.blogspot.com/foodxml}Dessert"/> 
  32.  *       </sequence> 
  33.  *     </restriction> 
  34.  *   </complexContent> 
  35.  * </complexType> 
  36.  * </pre> 
  37.  *  
  38.  *  
  39.  */  
  40. @XmlAccessorType(XmlAccessType.FIELD)  
  41. @XmlType(name = "", propOrder = {  
  42.     "vegetable",  
  43.     "fruit",  
  44.     "dessert"  
  45. })  
  46. @XmlRootElement(name = "Food")  
  47. public class Food {  
  48.   
  49.     @XmlElement(name = "Vegetable", required = true)  
  50.     @XmlSchemaType(name = "string")  
  51.     protected Vegetable vegetable;  
  52.     @XmlElement(name = "Fruit", required = true)  
  53.     protected String fruit;  
  54.     @XmlElement(name = "Dessert", required = true)  
  55.     @XmlSchemaType(name = "string")  
  56.     protected Dessert dessert;  
  57.   
  58.     /** 
  59.      * Gets the value of the vegetable property. 
  60.      *  
  61.      * @return 
  62.      *     possible object is 
  63.      *     {@link Vegetable } 
  64.      *      
  65.      */  
  66.     public Vegetable getVegetable() {  
  67.         return vegetable;  
  68.     }  
  69.   
  70.     /** 
  71.      * Sets the value of the vegetable property. 
  72.      *  
  73.      * @param value 
  74.      *     allowed object is 
  75.      *     {@link Vegetable } 
  76.      *      
  77.      */  
  78.     public void setVegetable(Vegetable value) {  
  79.         this.vegetable = value;  
  80.     }  
  81.   
  82.     /** 
  83.      * Gets the value of the fruit property. 
  84.      *  
  85.      * @return 
  86.      *     possible object is 
  87.      *     {@link String } 
  88.      *      
  89.      */  
  90.     public String getFruit() {  
  91.         return fruit;  
  92.     }  
  93.   
  94.     /** 
  95.      * Sets the value of the fruit property. 
  96.      *  
  97.      * @param value 
  98.      *     allowed object is 
  99.      *     {@link String } 
  100.      *      
  101.      */  
  102.     public void setFruit(String value) {  
  103.         this.fruit = value;  
  104.     }  
  105.   
  106.     /** 
  107.      * Gets the value of the dessert property. 
  108.      *  
  109.      * @return 
  110.      *     possible object is 
  111.      *     {@link Dessert } 
  112.      *      
  113.      */  
  114.     public Dessert getDessert() {  
  115.         return dessert;  
  116.     }  
  117.   
  118.     /** 
  119.      * Sets the value of the dessert property. 
  120.      *  
  121.      * @param value 
  122.      *     allowed object is 
  123.      *     {@link Dessert } 
  124.      *      
  125.      */  
  126.     public void setDessert(Dessert value) {  
  127.         this.dessert = value;  
  128.     }  
  129.   
  130. }  

The advantage of having specific Vegetable and Dessert classes is the additional type safety they bring as compared to a general Java String. Both Vegetable.java and Dessert.java are actually enums because they come from enumerated values in the XSD. The two generated enums are shown in the next two code listings.

Vegetable.java (generated with JAXB xjc compiler)
  1. //  
  2. // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802   
  3. // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>   
  4. // Any modifications to this file will be lost upon recompilation of the source schema.   
  5. // Generated on: 2015.02.11 at 10:17:32 PM MST   
  6. //  
  7.   
  8.   
  9. package com.blogspot.marxsoftware.foodxml;  
  10.   
  11. import javax.xml.bind.annotation.XmlEnum;  
  12. import javax.xml.bind.annotation.XmlEnumValue;  
  13. import javax.xml.bind.annotation.XmlType;  
  14.   
  15.   
  16. /** 
  17.  * <p>Java class for Vegetable. 
  18.  *  
  19.  * <p>The following schema fragment specifies the expected content contained within this class. 
  20.  * <p> 
  21.  * <pre> 
  22.  * <simpleType name="Vegetable"> 
  23.  *   <restriction base="{http://www.w3.org/2001/XMLSchema}string"> 
  24.  *     <enumeration value="Carrot"/> 
  25.  *     <enumeration value="Squash"/> 
  26.  *     <enumeration value="Spinach"/> 
  27.  *     <enumeration value="Celery"/> 
  28.  *   </restriction> 
  29.  * </simpleType> 
  30.  * </pre> 
  31.  *  
  32.  */  
  33. @XmlType(name = "Vegetable")  
  34. @XmlEnum  
  35. public enum Vegetable {  
  36.   
  37.     @XmlEnumValue("Carrot")  
  38.     CARROT("Carrot"),  
  39.     @XmlEnumValue("Squash")  
  40.     SQUASH("Squash"),  
  41.     @XmlEnumValue("Spinach")  
  42.     SPINACH("Spinach"),  
  43.     @XmlEnumValue("Celery")  
  44.     CELERY("Celery");  
  45.     private final String value;  
  46.   
  47.     Vegetable(String v) {  
  48.         value = v;  
  49.     }  
  50.   
  51.     public String value() {  
  52.         return value;  
  53.     }  
  54.   
  55.     public static Vegetable fromValue(String v) {  
  56.         for (Vegetable c: Vegetable.values()) {  
  57.             if (c.value.equals(v)) {  
  58.                 return c;  
  59.             }  
  60.         }  
  61.         throw new IllegalArgumentException(v);  
  62.     }  
  63.   
  64. }  
Dessert.java (generated with JAXB xjc compiler)
  1. //  
  2. // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802   
  3. // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>   
  4. // Any modifications to this file will be lost upon recompilation of the source schema.   
  5. // Generated on: 2015.02.11 at 10:17:32 PM MST   
  6. //  
  7.   
  8.   
  9. package com.blogspot.marxsoftware.foodxml;  
  10.   
  11. import javax.xml.bind.annotation.XmlEnum;  
  12. import javax.xml.bind.annotation.XmlEnumValue;  
  13. import javax.xml.bind.annotation.XmlType;  
  14.   
  15.   
  16. /** 
  17.  * <p>Java class for Dessert. 
  18.  *  
  19.  * <p>The following schema fragment specifies the expected content contained within this class. 
  20.  * <p> 
  21.  * <pre> 
  22.  * <simpleType name="Dessert"> 
  23.  *   <restriction base="{http://www.w3.org/2001/XMLSchema}string"> 
  24.  *     <enumeration value="Pie"/> 
  25.  *     <enumeration value="Cake"/> 
  26.  *     <enumeration value="Ice Cream"/> 
  27.  *   </restriction> 
  28.  * </simpleType> 
  29.  * </pre> 
  30.  *  
  31.  */  
  32. @XmlType(name = "Dessert")  
  33. @XmlEnum  
  34. public enum Dessert {  
  35.   
  36.     @XmlEnumValue("Pie")  
  37.     PIE("Pie"),  
  38.     @XmlEnumValue("Cake")  
  39.     CAKE("Cake"),  
  40.     @XmlEnumValue("Ice Cream")  
  41.     ICE_CREAM("Ice Cream");  
  42.     private final String value;  
  43.   
  44.     Dessert(String v) {  
  45.         value = v;  
  46.     }  
  47.   
  48.     public String value() {  
  49.         return value;  
  50.     }  
  51.   
  52.     public static Dessert fromValue(String v) {  
  53.         for (Dessert c: Dessert.values()) {  
  54.             if (c.value.equals(v)) {  
  55.                 return c;  
  56.             }  
  57.         }  
  58.         throw new IllegalArgumentException(v);  
  59.     }  
  60.   
  61. }  

Having enums generated for the XML elements ensures that only valid values for those elements can be represented in Java.

Conclusion

JAXB makes it relatively easy to map Java to XML, but because there is not a one-to-one mapping between Java and XML types, there can be some cases where the generated Java type for a particular XSD prescribed element is not obvious. This post has shown how two different approaches to building an XSD to enforce the same basic XML structure can lead to very different results in the Java classes generated with the JAXB xjc compiler. In the example shown in this post, declaring elements in the XSD directly on simpleTypes restricting XSD's string to a specific set of enumerated values is preferable to declaring elements as references to other elements wrapping a simpleType of restricted string enumerated values because of the type safety that is achieved when enums are generated rather than use of general Java Strings.

2 comments:

eegee said...

Could you please try your example with different JAVA versions (i.e. 6, 7, & *)? I am sure you will appreciate the further nuances.

Sivakumar said...

There is away to avoid the adding the @XmlSchemaType(name="String") for the enum dataType.