users@jaxb.java.net

jaxb and upcasting

From: <dev_at_samizdatdigital.org>
Date: Tue, 22 Jun 2004 19:05:32 -0700

this would seem to be a partial rehashing of issues raised in the
following threads:

        https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2645
        https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2476
        https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2626

and it seems that there are no easy solutions at the moment, other than
wait for an implementation of jaxb 2.0. since some of those threads may
be dated, can someone confirm or deny that there is still no current
solution (other than various hacks). a description of the problem is
below, though i don't think it's any different than what people
describe above.

i've got a bunch of schemae which i'm using to build a type library.
the pattern is that for every object there is a complexType which
describes that object, and the root of the schema in whic is define an
element of that type. for instance, i might have:

        <xsd:schema targetNamespace="http://somewhere.com/foo"
                  xmlns:foo="http://somewhere.com/foo"
                  ... >
        
                <xsd:complexType name="FooType">
                        ...
                </xsd:complexType>

                <xsd:element name="Foo" type="foo:FooType"/>
        
        </xsd:schema>

in foo.xsd, and

        <xsd:schema targetNamespace="http://somewhere.com/bar"
                  xmlns:bar="http://somewhere.com/bar"
                           xmlns:foo="http://somewhere.com/foo"
                  ... >
                
                <xsd:import namespace="http://somewhere.com/foo"
                      schemaLocation="..."/>
        
                <xsd:complexType name="BarType">
                        <xsd:sequence>
                                <xsd:element name="SomethingNice" type="foo:FooType"/>
                                ...
                        </xsd:sequence>
                </xsd:complexType>

                <xsd:element name="Bar" type="foo:BarType"/>
        
        </xsd:schema>

in bar.xsd.

the output from xjc will be com.somewhere.foo.FooType, extended by
com.somewhere.foo.Foo (the xsd:element extends the xsd:complexType).
similarly, i get a com.somewhere.bar.BarType extended by a
com.somewhere.bar.Bar.

the reason i'm doing all of this is that sometimes i want to marshal a
Foo and have it be the root of my document -- i want a <Foo>...</Foo>.
in this case i need the xsd:element -- the com.somewhere.foo.Foo.
othertimes i just want to have objects somewhere else of type FooType,
such as //BarType/SomethingNice.

this is all natural and makes sense, even outside of jaxb. what i find
intersting so far is that if i use the method Bar.setSomethingNice(
FooType value ) and pass in a com.somewhere.foo.Foo, which extends the
interface of FooType, i get exceptions at validation or unmarshalling
-- i can marshal the object, but can't do anything with it afterwords.
if i explicitly cast a Foo up to a FooType, and call
Bar.setSomethingNice( (FooType) someFooElementIveGottenFromSomewhere ),
it fails the same way. probably this isn't surprising, given the nature
of method invocation ( someFooElementIveGottenFromSomewhere.getClass()
will still be Foo, not FooType).

the difference in the marshalled xml is that, when marshalling the Foo,
you get a full namespace:

        <Foo xmlns:ns3="http://somewhere.com/foo">...</Foo>

but when marshalling a FooType, you get

        <Foo xmlns="">...</Foo>

is there some better way to get this to work? if not, i can always use
some reflection to take a Foo and return an equivalent FooType, or
otherwise try and enforce that Bar.setSomethingNice is really only
being called on an object of FooType (can't enforce this at
compile-time though), but i'd rather jaxb do it for me:)

jon


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net