users@jaxb.java.net

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

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Mon, 15 Jun 2009 21:16:44 +0200

The schema snippet you posted does not compile with the schema compilers I
have installed, with the following error message:

Complex Type Definition Representation Error for type 'EnumValue'. When
<simpleContent> is used, the base type must be a complexType whose content
type is simple, or, only if restriction is specified, a complex type with
mixed content and emptiable particle, or, only if extension is specified, a
simple type. 'string' satisfies none of these conditions.

-W


On Mon, Jun 15, 2009 at 11:59 AM, Jose Correia <correij_at_gmail.com> wrote:

> 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
>