I am using SourceGen to generate Java classes from an XML schema. I
have two special
cases where I have not been able to work out whether Castor provides
explicit support or
not. I admit to being a programming dinosaur, so there may be some
Java pattern
that I have overlooked for these cases. I'd be grateful for any
advice or pointers.
=== CASE 1 ===
Given the following XSD snippet:
<xs:complexType name="base-type">
</xs:complexType>
<xs:complexType name="derived-type-1">
<xs:complexContent>
<xs:extension base="tns:base-type"/>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="derived-type-2">
<xs:complexContent>
<xs:extension base="tns:base-type"/>
</xs:complexContent>
</xs:complexType>
Castor generates the Java classes BaseType, DerivedType1 and
DerivedType2. If I have a
BaseType object in my Java program, is there a more elegant way of
finding out whether it
is a DerivedType1 or a DerivedType2 than using the instanceof
operator? Is there any way
of getting Castor to generate code to support something like the
following:
BaseType base = ...;
switch ( base.getExtendedType() ) {
case DERIVED_TYPE_1: DerivedType1 derived1 =
(DerivedType1) base;
// etc.
case DERIVED_TYPE_2: DerivedType2 derived2 =
(DerivedType2) base;
// etc
}
It occurs to me that I could add an element to "base-type" which
could be assigned a
different value depending on the individual derived types, but how
could I make the unmarshaller
assign the appropriate values?
=== CASE 2 ===
This is similar to case 1, not the same. Given the following XSD
snippet:
<xs:element name="container-object">
<xs:sequence>
<xs:choice>
<xs:element ref="tns:content-1"/>
<xs:element ref="tns:content-2"/>
<xs:element ref="tns:content-3"/>
</xs:choice>
</xs:sequence>
</xs:element>
Castor generates amongst other things a Java class
ContainerObjectChoiceItem which provides
the following methods:
getContent1()
getContent2()
getContent3()
Is there a simple way of finding out which of the variants is
currently present? I'd like to be able to
do the following:
ContainerObjectChoiceItem item = ...;
switch ( item.getCurrentChoice() ) {
case CONTENT_1:
case CONTENT_2:
case CONTENT_3:
}
I don't particularly want to check each variant for null, but I can't
see an alternative approach apart
from wrapping the Castor-generated class.
Also: is there anything to stop me doing the following?
setContent1(abc);
setContent2(def);
setContent3(ghi);
(i.e. assigning to all of the choice variants simultaneously).
Regards
Steve
-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:
[EMAIL PROTECTED]
-------------------------------------------------