I figured out what I was doing wrong.
When you create a JAXB content object, it will walk the tree of annotated
classes, but unless you have a class loader in memory for each class you
expect to create objects of, JAXB will not be able to create the proper
element. With regard to my code, I didn't actually reference or create an
instance of the DColor or DState enums in any of the classes JAXB was
parsing . So in order to fix the problem, I had to specify the those
classes up front in addition to my root node class:
Class<?>[] testClasses =
{NapElementTestFile.class,DColor.class,DState.class};
JAXBContext context = JAXBContext.newInstance(testClasses);
This produced the proper creation of my enums.
AllenABQ wrote:
>
> 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).
>
>
> <xs:complexType name="ServiceAttribute">
> <xs:sequence>
> <xs:element name="uniqueName" type="xs:string"/>
> <xs:element name="type" type="attributeType"/>
> <xs:element name="initialValue" type="xs:anyType"
> nillable="true"/>
> </xs:sequence>
> </xs:complexType>
>
> <xs:complexType name="DimmerSwitch">
> <xs:complexContent>
> <xs:extension base="ServiceElement">
> <xs:sequence>
> <xs:element name="power" type="ServiceVarBoolean"/>
> <xs:element name="brightness" type="ServiceVarNumeric"/>
> <xs:element name="color" type="ServiceVarEnum"/>
> <xs:element name="state" type="ServiceVarEnum"/>
> </xs:sequence>
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
>
> <xs:simpleType name="attributeType">
> <xs:restriction base="xs:string">
> <xs:enumeration value="STRING"/>
> <xs:enumeration value="NUMERIC"/>
> <xs:enumeration value="BOOLEAN"/>
> <xs:enumeration value="ENUM"/>
> </xs:restriction>
> </xs:simpleType>
>
> <xs:simpleType name="DColor">
> <xs:restriction base="xs:string">
> <xs:enumeration value="VIOLET"/>
> <xs:enumeration value="BLUE"/>
> <xs:enumeration value="GREEN"/>
> <xs:enumeration value="YELLOW"/>
> <xs:enumeration value="ORANGE"/>
> <xs:enumeration value="RED"/>
> </xs:restriction>
> </xs:simpleType>
>
> <xs:simpleType name="DState">
> <xs:restriction base="xs:string">
> <xs:enumeration value="BLINKING"/>
> <xs:enumeration value="STEADY"/>
> <xs:enumeration value="NONE"/>
> </xs:restriction>
> </xs:simpleType>
>
>
>
> The problem occurs when I use JAXB to unmarshal my XML file. It validates
> against my XSD. Here's the snippet:
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <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">
> <DimmerSwitch>
> <power>
> <uniqueName>D1.power</uniqueName>
> <type>BOOLEAN</type>
> <initialValue
> xsi:type="xsd:boolean">false</initialValue>
> </power>
> <brightness>
> <uniqueName>D1.brightness</uniqueName>
> <type>NUMERIC</type>
> <initialValue
> xsi:type="xsd:double">0.0</initialValue>
> </brightness>
> <color>
> <uniqueName>D1.color</uniqueName>
> <type>ENUM</type>
> <initialValue
> xsi:type="DColor">VIOLET</initialValue>
> </color>
> <state>
> <uniqueName>D1.state</uniqueName>
> <type>ENUM</type>
> <initialValue
> xsi:type="DState">NONE</initialValue>
> </state>
> </DimmerSwitch>
>
> </ServiceElementTestFile>
>
>
>
> 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--tp16253892p16276700.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.