users@jaxb.java.net

Re: Adding methods to generated classes

From: Canning, Chuck <chuck.canning_at_zilliant.com>
Date: Wed, 12 Mar 2003 15:09:45 -0600

Hey,

Joe the information you provided was very helpful. I was having the same
problem as Petra. So, currently, I have the model group binding to a single
getXXX() method and it returns an object ogf the correct base type Base. I
can't figure out how to override the defualt naming method of
getChoiceAorChoiceBorChoiceC... Is there anyway that I can override it to be
Base getBase() instead of Base getBaseAOrBaseB... Thanks in advance for any
enlightening information.

Chuck

-----Original Message-----
From: Joe Fialli [mailto:joseph.fialli_at_sun.com]
Sent: Monday, March 10, 2003 4:54 PM
To: JAXB-INTEREST_at_JAVA.SUN.COM
Subject: Re: Adding methods to generated classes


Petra Malik wrote:
> Hi,
>
> I've got a quite large XML schema using extensions of types and
> substitutionGroups a lot. Since substitutionGroups are not supported
> by JAXB I decided to use choices instead. For example, consider an
> abstract element Term, and extensions AndTerm, OrTerm, and so on, as
> defined in the XML schema given below. This is used by type UseTerm,
> which contains an element that may be any term.
>
> I replaced all occurrences of term in the XML schema by a choice of
> all possible terms (andTerm, orTerm, ...). Now the generated class for
> UseTerm contains getAndTerm(), getOrTerm(), ... methods. It would be
> nice to add a getTerm() method to this class which finds out which
> choice was taken and returns the corresponding term (which is
> fortunately of type TermType).

See Section 5.9.8-5.9.11 in the JAXB Specification to read about model
group binding style that semantically preserves the choice model group in
the
Java bindings. Several customization options exist that get you closer to
the functionality that you are requesting.

-Joe Fialli, Sun Microsystems

>
> Here are my questions:
> 1) Is there a better way of dealing with substitutionGroups?
> 2) How can I add such a method as mentioned above to the generated
> code of the class UseTerm?
> Note that I still want to use the generated class hierarchy, i.e.
> it would be nice to get an instance useTerm of UseTerm and then
> be able to call useTerm.getTerm() which returns a TermType.
> Since my XML schema is quite large, it would be very tedious to
> use composition, since all the classes and methods already generated
> must be generated again using delegation.
>
> Thanks in advance,
> Petra
>
>
> --------------------------------------------------
> XML schema using substitutionGroups:
>
> <xs:element name="Term" type="TermType" abstract="true"/>
> <xs:element name="OrTerm" type ="OrTermType" substitutionGroup="Term"/>
> <xs:element name="AndTerm" type ="AndTermType" substitutionGroup="Term"/>
> ...
> <xs:complexType name="TermType">
> ...
> </xs:complexType>
> <xs:complexType name="OrTermType">
> <xs:complexContent>
> <xs:extension base="TermType">
> ...
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
> <xs:complexType name="AndTermType">
> <xs:complexContent>
> <xs:extension base="TermType">
> ...
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
>
> <xs:complexType name="UseTerm">
> <xs:sequence>
> <xs:element ref="Term">
> ...
> </xs:sequence>
> </xs:complexType>
>
> --------------------------------------------------
> XML schema without substitutionGroups
> generated from the schema above:
>
> <xs:element name="OrTerm" type ="OrTermType"/>
> <xs:element name="AndTerm" type ="AndTermType"/>
> ...
> <xs:complexType name="TermType">
> ...
> </xs:complexType>
> <xs:complexType name="OrTermType">
> <xs:complexContent>
> <xs:extension base="TermType">
> ...
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
> <xs:complexType name="AndTermType">
> <xs:complexContent>
> <xs:extension base="TermType">
> ...
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
>
> <xs:complexType name="UseTerm">
> <xs:sequence>
> <xs:choice>
> <xs:element ref="OrTerm"/>
> <xs:element ref="AndTerm"/>
> ...
> </xs:choice>
> ...
> </xs:sequence>
> </xs:complexType>


--