users@jaxb.java.net

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

From: Jose Correia <correij_at_gmail.com>
Date: Wed, 17 Jun 2009 09:42:38 +0200

Hi

Can u try with 2.1.8 xjc and see if it still gives u that exception, maybe
2.1.8 implements a better version?

Thanks
Jose

On Wed, Jun 17, 2009 at 8:42 AM, Wolfgang Laun <wolfgang.laun_at_gmail.com>wrote:

> Doesn't compile with xjc from:
> 2.1.3 from JDK
> hudson 2.1-792 (2.1.10)
> hudson 2.1-793 (2.1.11)
> hudson 2.2-34 (2.2 EA)
>
> -W
> On 6/16/09, Jose Correia <correij_at_gmail.com> wrote:
>>
>> what schema compilers u using, I'm using maven with latest version of jaxb
>> xjc and don't get any exceptions?
>>
>>
>> On Mon, Jun 15, 2009 at 9:16 PM, Wolfgang Laun <wolfgang.laun_at_gmail.com>wrote:
>>
>>> 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
>>>>
>>>
>>>
>>>
>>
>>
>