users@jaxb.java.net

JAXB problem recognizing enums?

From: AllenABQ <afbagwe_at_sandia.gov>
Date: Mon, 24 Mar 2008 08:05:41 -0700 (PDT)

This has been a perplexing problem. I am using JAXB to map my classes so
that I can unmarshal an XML document that is my initialization data/objects
for an application. I seem to be stumbling on creating Enum objects that
get assigned to a class field of type Object.


Here's the salient parts of XSD file that schemagen produces. Note that the
initialValue field is of type xs:anyType because in the class definition it
is the Object class. There are a variety of forms an initialValue should be
able to take including a string, a double, a boolean, and a Java Enum.
ServiceVar* types map to sub-classes of ServiceAttribute that only define
some abstract methods. No new fields are declared in them. The DimmerSwitch
class is just a test class I created to prove this process to myself (and my
team).


  &lt;xs:complexType name="ServiceAttribute"&gt;
    &lt;xs:sequence&gt;
      &lt;xs:element name="uniqueName" type="xs:string"/&gt;
      &lt;xs:element name="type" type="attributeType"/&gt;
      &lt;xs:element name="initialValue" type="xs:anyType"
nillable="true"/&gt;
    &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;

  &lt;xs:complexType name="DimmerSwitch"&gt;
    &lt;xs:complexContent&gt;
      &lt;xs:extension base="ServiceElement"&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="power" type="ServiceVarBoolean"/&gt;
          &lt;xs:element name="brightness" type="ServiceVarNumeric"/&gt;
          &lt;xs:element name="color" type="ServiceVarEnum"/&gt;
          &lt;xs:element name="state" type="ServiceVarEnum"/&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:extension&gt;
    &lt;/xs:complexContent&gt;
  &lt;/xs:complexType&gt;

  &lt;xs:simpleType name="attributeType"&gt;
    &lt;xs:restriction base="xs:string"&gt;
      &lt;xs:enumeration value="STRING"/&gt;
      &lt;xs:enumeration value="NUMERIC"/&gt;
      &lt;xs:enumeration value="BOOLEAN"/&gt;
      &lt;xs:enumeration value="ENUM"/&gt;
    &lt;/xs:restriction&gt;
  &lt;/xs:simpleType&gt;

  &lt;xs:simpleType name="DColor"&gt;
    &lt;xs:restriction base="xs:string"&gt;
      &lt;xs:enumeration value="VIOLET"/&gt;
      &lt;xs:enumeration value="BLUE"/&gt;
      &lt;xs:enumeration value="GREEN"/&gt;
      &lt;xs:enumeration value="YELLOW"/&gt;
      &lt;xs:enumeration value="ORANGE"/&gt;
      &lt;xs:enumeration value="RED"/&gt;
    &lt;/xs:restriction&gt;
  &lt;/xs:simpleType&gt;

  &lt;xs:simpleType name="DState"&gt;
    &lt;xs:restriction base="xs:string"&gt;
      &lt;xs:enumeration value="BLINKING"/&gt;
      &lt;xs:enumeration value="STEADY"/&gt;
      &lt;xs:enumeration value="NONE"/&gt;
    &lt;/xs:restriction&gt;
  &lt;/xs:simpleType&gt;



The problem occurs when I use JAXB to unmarshal my XML file. It validates
against my XSD. Here's the snippet:


&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;ServiceElementTestFile
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:noNamespaceSchemaLocation="file:/home/afbagwe/workspace/JKnowledgeBase/dist/config_schema.xsd"&gt;
    &lt;DimmerSwitch&gt;
        &lt;power&gt;
            &lt;uniqueName&gt;D1.power&lt;/uniqueName&gt;
            &lt;type&gt;BOOLEAN&lt;/type&gt;
            &lt;initialValue
xsi:type="xsd:boolean"&gt;false&lt;/initialValue&gt;
        &lt;/power&gt;
        &lt;brightness&gt;
            &lt;uniqueName&gt;D1.brightness&lt;/uniqueName&gt;
            &lt;type&gt;NUMERIC&lt;/type&gt;
            &lt;initialValue
xsi:type="xsd:double"&gt;0.0&lt;/initialValue&gt;
        &lt;/brightness&gt;
        &lt;color&gt;
            &lt;uniqueName&gt;D1.color&lt;/uniqueName&gt;
            &lt;type&gt;ENUM&lt;/type&gt;
            &lt;initialValue
xsi:type="DColor"&gt;VIOLET&lt;/initialValue&gt;
        &lt;/color&gt;
        &lt;state&gt;
            &lt;uniqueName&gt;D1.state&lt;/uniqueName&gt;
            &lt;type&gt;ENUM&lt;/type&gt;
            &lt;initialValue xsi:type="DState"&gt;NONE&lt;/initialValue&gt;
        &lt;/state&gt;
    &lt;/DimmerSwitch&gt;
    
&lt;/ServiceElementTestFile&gt;



The JAXB unmarshalling process doesn't throw any errors, but it doesn't
create the proper output on the DColor enum (and likewise on the state if I
wasn't causing it to fail at this point):


TRACE knowledgebase.ServiceAttribute.ServiceVarBoolean -- Class of Initial
value = Boolean
TRACE knowledgebase.ServiceAttribute.ServiceVarBoolean -- Initial value =
false
TRACE knowledgebase.ServiceAttribute.ServiceVarNumeric -- Class of Initial
value = Double
TRACE knowledgebase.ServiceAttribute.ServiceVarNumeric -- Initial value =
0.0
TRACE knowledgebase.ServiceAttribute.ServiceVarEnum -- Class of Initial
value = org.apache.xerces.dom.ElementNSImpl
TRACE knowledgebase.ServiceAttribute.ServiceVarEnum -- Initial value =
[initialValue: null]
FATAL knowledgebase.ServiceAttribute.ServiceVarEnum -- Initial value of
D1.color is not an enum value: [initialValue: null]

So instead of getting my DColor enum, JAXB (apparently at some point in the
xerces code) ignores "VIOLET" and sets the value to null.

Anyone have an idea what is going on here?

--Allen
-- 
View this message in context: http://www.nabble.com/JAXB-problem-recognizing-enums--tp16253892p16253892.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.