users@jaxb.java.net

How JAXB Generates Choices

From: Marc Dumontier <dumontier_at_mshri.on.ca>
Date: Thu, 09 Jan 2003 12:33:19 -0500

Hi All,

I'd like to see how other people are using objects like the following.
Here is a sample from my schema which describes a choice.

<xs:complexType name="BIND-dmo1">
  <xs:choice>
    <xs:element ref="BIND-dmo_general" />
    <xs:element name="BIND-dmo_outer-membrane"
type="BIND-dmo_outer-membrane1" />
    <xs:element name="BIND-dmo_inner-membrane"
type="BIND-dmo_inner-membrane1" />
    <xs:element name="BIND-dmo_general-membrane"
type="BIND-dmo_general-membrane1" />
  </xs:choice>
</xs:complexType>

the generated code creates get and set methods for each of the sub
elements. I think that what's missing, and very important is a way to
find out which sub-object is the choice.

here is what JAXB generates now.
----------
public interface BINDDmo1 {

    ca.bind.objects.BINDDmo_GeneralMembrane1 getBINDDmo_GeneralMembrane();
    void
setBINDDmo_GeneralMembrane(ca.bind.objects.BINDDmo_GeneralMembrane1 value);

    ca.bind.objects.BINDDmo_InnerMembrane1 getBINDDmo_InnerMembrane();
    void setBINDDmo_InnerMembrane(ca.bind.objects.BINDDmo_InnerMembrane1
value);

    ca.bind.objects.BINDDmo_OuterMembrane1 getBINDDmo_OuterMembrane();
    void setBINDDmo_OuterMembrane(ca.bind.objects.BINDDmo_OuterMembrane1
value);

    ca.bind.objects.BINDDmo_General getBINDDmo_General();
    void setBINDDmo_General(ca.bind.objects.BINDDmo_General value);

}


I think it would be a better approach if it generated something like
the following:
-----
public interface BINDDmo1 {

    static final int BINDDmo_GeneralMembrane = 0;
    static final int BINDDmo_InnerMembrane = 1;
    static final int BINDDmo_OuterMembrane = 2;
    static final int BINDDmo_General = 3;

    int getChoice();
    boolean setChoice(int);

    ca.bind.objects.BINDDmo_GeneralMembrane1 getBINDDmo_GeneralMembrane();
    void
setBINDDmo_GeneralMembrane(ca.bind.objects.BINDDmo_GeneralMembrane1 value);

    ca.bind.objects.BINDDmo_InnerMembrane1 getBINDDmo_InnerMembrane();
    void setBINDDmo_InnerMembrane(ca.bind.objects.BINDDmo_InnerMembrane1
value);

    ca.bind.objects.BINDDmo_OuterMembrane1 getBINDDmo_OuterMembrane();
    void setBINDDmo_OuterMembrane(ca.bind.objects.BINDDmo_OuterMembrane1
value);

    ca.bind.objects.BINDDmo_General getBINDDmo_General();
    void setBINDDmo_General(ca.bind.objects.BINDDmo_General value);

}


This would allow us to not have to check each sub class to find out
which is actually the choice. I've come across this because some of the
choices in my schema have alot of subelements.

Thanks,

Marc Dumontier
Intermediate Developer
Blueprint Initiative
Mount Sinai Hospital
Toronto