users@jaxb.java.net

Re: JAXB generating binding involving substitutionGroup head

From: Farrukh S. Najmi <farrukh_at_wellfleetsoftware.com>
Date: Fri, 22 Jun 2007 13:09:32 -0400

Aleksei Valikov wrote:
> Hi.
>
> I am migrating my app from JAXB 1.0 to JAXB 2.0. My schema uses
> substitutionGroup feature of XSD.
>
> A part of the schema is generating a binding that I am not
> understanding:
>
> <complexType name="RegistryObjectListType">
> <sequence>
> <element maxOccurs="unbounded" minOccurs="0"
> ref="tns:Identifiable"/>
> </sequence>
> </complexType>
>
> In above, Identifiable is a complexType that is a head of a
> subtitutionGroup.
>
> Based on JAXB1.0 experience with same schema I was expecting the
> binding
> class RegistryObjectType to generate the following methods:
>
> public List<IdentifiableType> getIdentifiable();
>
> public void setIdentifiable(List<IdentifiableType> identifiable);
>
>
> ...
>
> I would appreciate some help in understanding why the generated
> methods
> are not the simpler ones I expected and also if there is a
> customization
> that will yield the simpler methods.
>
>
> In your case, the field "Identifiable" may contain any element that
> substitutes Identifiable. They will all be of the IdentifiableType (or
> its subclasses), BUT they will have different names. Therefore, to
> track names of elements, JAXB need to use JAXBElements.
Hi Aleksei,

Thanks for your helpful explanation.

I am having a little trouble figuring out how to use the JAXB Binding
class involving the substitutionGroup inside a sequence in example I
gave above.

I am attaching the JAXB Binding class in question. All I want is to add
a new item in the List of items but it seems that I will need to create
a JAXBElement to do that. Could I get an example code fragment on how to
add an IdentifiableType instance to the RegistryObjectListType?

Note that I was able to go the other direction and get a List of
IdentifiableTypes of a particular sub-type using following method.

    public static Set<RegistryObjectType>
getRegistryObjects(RegistryObjectListType rolist) {
        Set<RegistryObjectType> ros = new HashSet<RegistryObjectType>();
        List<JAXBElement<? extends IdentifiableType>> elems =
rolist.getIdentifiable();
        for (Iterator<JAXBElement<? extends IdentifiableType>> i =
elems.iterator(); i.hasNext(); ) {
            JAXBElement<? extends IdentifiableType> elem = i.next();
            IdentifiableType identifiable = elem.getValue();
           
            if (identifiable instanceof RegistryObjectType) {
                RegistryObjectType ro = (RegistryObjectType)identifiable;
                ros.add(ro);
            }
        }
       
        return ros;
    }

Now I am having trouble implementing the reverse of above convenience
method to create a new RegistryObjectListType from a List of
IdentifiableTypes (really RegistryObjectTypes) to a RegistryObjectList.
The signature of desired method is:

    public static RegistryObjectListType
getRegistryObjectListType(List<RegistryObjectType> ros) {
    ...
    }

Thanks for any help.

-- 
Regards,
Farrukh
Web: http://www.wellfleetsoftware.com