users@jaxb.java.net

Adding methods to generated classes

From: Petra Malik <petra_at_CS.WAIKATO.AC.NZ>
Date: Mon, 10 Mar 2003 01:32:45 -0700

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).

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>