users@jaxb.java.net

Re: Getting JAXB to instantiate your subclasses

From: Ryan Shoemaker - JavaSoft East <Ryan.Shoemaker_at_sun.com>
Date: Mon, 07 Apr 2003 11:43:29 -0400

Allister Bertram wrote:
> In my example, "owned" elements can be positioned in an XML document within
> the context of an "owning" element, or can be positioned under the root of
> the document with a reference to the "owning" element.
>
> Here's a doctored schema for simplicity. "A" can own AA, BB, and CC
> elements. "AA" can own BB and CC elements, and needs an "A" reference if
> it isn't positioned under the owning "A".
>
> I'd like to subclass all of A, AA, BB, CC to add business logic to the
> content.
>
> When I inline binding customizations to specify an implementation class for
> all of A, AA, BB, CC, the generated classes give me subclass instances only
> when the elements are positioned directly under the document root. When
> the elements are positioned under owning elements, the implClass
> customization doesn't seem to apply.
>

Hi Allister,

When you add the <implClass> customizations to A, AA, BB, & CC (and you
follow the double-compile workaround), you should get the following impl
type hierarchy:

    // assume xjc generates into package org.acme.foo
    public class AImpl implements A {...}
    public class AAImpl implements AA {...}
    etc.

Your impl classes should have the following signatures:

    public class MyAImpl extends org.acme.foo.impl.AImpl {...}
    public class MyAAImpl extends org.acme.foo.impl.AAImpl {...}
    etc.

Are we in agreement so far?

I think what you are saying is that when you unmarshal a document like
this, you are able to cast to your impl classes:

    <DocumentRoot>
      <ElementA>
        <ElementAA>..</ElementAA>
        <ElementBB>..</ElementBB>
        <ElementCC>..</ElementCC>

But when you try to unmarshal a document like this, you aren't:

    <ElementA>
      <ElementAA>..</ElementAA>
      <ElementBB>..</ElementBB>
      <ElementCC>..</ElementCC>

I must be misunderstanding your problem, because you should be able to cast
to your impl classes in both cases. Can you clarify what you mean by "the
generated classes give me subclass instances only when the elements are
positioned directly under the document root. When the elements are positioned
under owning elements, the implClass customization doesn't seem to apply."

Is there an error that you are seeing when you unmarshal or are you having
trouble getting the generated code to compile?

Thanks,

--Ryan

> <xs:element name="DocumentRoot">
> <xs:complexType>
> <xs:choice minOccurs="0" maxOccurs="unbounded">
> <xs:element ref="ElementA" />
> <xs:element ref="ElementAA" />
> <xs:element ref="ElementBB" />
> <xs:element ref="ElementCC" />
> </xs:choice>
> </xs:complexType>
> </xs:element>
>
> <xs:element name="ElementA">
> <xs:complexType>
> <xs:sequence>
> <xs:element minOccurs="0" maxOccurs="unbounded" ref="ElementAA" />
> <xs:element minOccurs="0" maxOccurs="unbounded" ref="ElementBB" />
> <xs:element minOccurs="0" maxOccurs="unbounded" ref="ElementCC" />
> </xs:sequence>
> </xs:complexType>
> </xs:element>
>
> <xs:element name="ElementARef">
> <xs:complexType>
> <xs:attribute name="Key" type="xs:string" />
> </xs:complexType>
> </xs:element>
>
> <xs:element name="ElementAA">
> <xs:complexType>
> <xs:sequence>
> <xs:element minOccurs="0" ref="ElementARef" />
> <xs:element minOccurs="0" maxOccurs="unbounded" ref="ElementBB" />
> <xs:element minOccurs="0" maxOccurs="unbounded" ref="ElementCC" />
> </xs:sequence>
> <xs:attribute name="Type" type="xs:string" use="required" />
> <xs:attribute name="Description" type="xs:string" />
> </xs:complexType>
> </xs:element>
>
> etc.