users@jaxb.java.net

ClassCast when defining a complex type of simpleContent with restriction on xs:string

From: Jose Correia <correij_at_gmail.com>
Date: Mon, 15 Jun 2009 11:59:34 +0200

Hi All

I'm flabbergasted at this one and I'm trusting someone can shed some light
on this. First details:

JDK: 1.6.0_14
JAXB: 2.1.8 (have tried 2.1.11 too to check if there was a fix)

I define this in xsd:

<xs:complexType name="EnumValue">
       <xs:simpleContent>
         <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="255"/>
            <xs:attribute name="restricted" type="xs:boolean" />
        </xs:restriction>
      </xs:simpleContent>
    </xs:complexType>

<xs:complexType name="Enumeration">
        <xs:sequence>
            <xs:element name="value" type="EnumValue"
maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>



<xs:complexType name="PropertyDescriptor">
  <xs:sequence>
            .... (ommited)
          <xs:element name="choices" type="Enumeration" minOccurs="0"/>
            .... (ommited)
    </xs:sequence>
   <xs:attribute name="format" type="xs:string"/>
</xs:complexType>

My EnumValue class gets created when I generate the JAXB. The Enumeration
class has the following:

public List<EnumValue> getValue() {
        if (value == null) {
            value = new ArrayList<EnumValue>();
        }
        return this.value;
    }

So it clearly expects a List of EnumValue s.

When I parse my xml, I do it in the following manner:

com.traderoot.tools.artifact.jaxb.Enumeration enum2 = pd.getChoices();
              List<EnumValue> values = enum2.getValue();
for (Iterator<EnumValue> j = values.iterator(); j.hasNext();) {

        EnumValue en = j.next(); //CLASSCAST EXCEPTION HERE
                  if (en.getValue().equals(value)) {
                    found = true;
                    break;
                  }

}

Now here is the issue:

If I use the following XML:

<propertyDescriptor>
                .... (omitted)
                <choices>
                    <value restricted='false'>inactive</value>
                    <value restricted='false'>master</value>
                    <value restricted='false'>slave</value>
                </choices>
...(omitted)

Then everything works as expected, the EnumValue objects get created for
each value and great.

If I use this XML however, where I take away the optional attribute of
value:

<propertyDescriptor>
                <name>postilion_key_exchange_policy</name>
                <description>The role that this gateway takes in Key
Exchanges with the Remote PostBridge Node. </description>
                <type>CHOICE</type>
                <defaultValue>inactive</defaultValue>
                <choices>
                    <value >inactive</value>
                    <value >master</value>
                    <value >slave</value>
                </choices>

It no longer builds a List of EnumValue's but of String hence why it throws
a ClassCast Exception... (I put it on debug through Eclipse to make sure).

So is this not a bug on the JAXB parser, should it not, irrespective of
attributes existing, match the XML to the EnumValue class??

All help is appreciated.

Regards
Jose Correia