Hi Archie,
You can't really represent this structure without using concrete
mappings for the Vehicle classes corresponding to the actual elements
used in the document. I understand that you prefer to use abstract
mappings, and in general you can do that for all classes except those
used as the root element of a document. But the implementation of a
substitution group, which is what you're describing, is based on
concrete mappings. So for the case you're describing you'd need the
mappings:
<mapping class="com.example.Vehicle" abstract="true">...
<mapping class="com.example.Car" name="Car"
extends="com.example.Vehicle">...
<mapping class="com.example.Truck" name="Truck"
extends="com.example.Vehicle">...
This allows your reference <structure field="vehicle" map-as="vehicle"/>
to work as expected, allowing either a <Car> or a <Truck> (or any other
element name mapped as an extension of com.example.Vehicle - but not
other, unrelated, elements) to appear in a document being unmarshalled,
and marshalling whichever subclass is actually present with the
appropriate element name.
>From your response I'm not sure if you're just saying that you don't
like having to use concrete mappings in this case, or if there's an
actual problem with using the concrete mappings. If it's the former, can
you suggest a cleaner way of representing substitution groups? I don't
think it's likely to change in the current codebase, but perhaps I can
implement a different approach for JiBX 2.
- Dennis
Dennis M. Sosnoski
Java SOA and Web Services Consulting <http://www.sosnoski.com/consult.html>
Axis2/CXF/Metro SOA and Web Services Training
<http://www.sosnoski.com/training.html>
Web Services Jump-Start <http://www.sosnoski.com/jumpstart.html>
On 03/13/2011 03:45 AM, Archie Cobbs wrote:
> Thanks Dennis. That all makes sense... however I still am confused
> about the right approach is for doing what I want to do.
>
> In my particular application I have a Java class hierarchy and am
> modeling it in XML pretty much directly using JiBX. Classes correspond
> pretty much to XML elements. For each class, for example FooBar.java I
> create an abstract mapping
>
> <include path="janFoo.xml"/>
> <mapping abstract="true" type-name="fooBar" class="com.example.FooBar">
> <value name="color" field="color"/>
>
>
> <structure name="JanFoo" field="janFoo" map-as="janFoo"/>
>
>
> ...
> </mapping>
>
> in a separate file fooBar.xml.
>
> The problem comes in a situation where FooBar has a Vehicle property
> where Vehicle is abstract .. e.g. I have these classes:
>
> abstract class Vehicle
> class Car extends Vehicle
> class Truck extends Vehicle
>
> So the vehicle property is always a Car or a Truck and in XML should
> appear as either a <Car> or <Truck> element.
>
> I want to do something like this but can't figure out exactly how:
>
> <include path="vehicle.xml"/>
> <mapping abstract="true" type-name="fooBar" class="com.example.FooBar">
> <value name="color" field="color"/>
>
>
> <structure field="vehicle" map-as="vehicle"/> <!-- name="Car" or
> name="Truck" ?? -->
>
>
> ...
> </mapping>
>
> How would I model this property like the example above in without
> using extends and yet still use type-names to reference all my
> mappings instead of class names?
>
> What I end up having to do is making special cases for all these cases
> where
>
> * Must define concrete mappings, whereas all other structures
> (except the topmost one) can have abstract mappings
> * Can't use type-name, have to use class name and extends
>
> This messes up the nice modularity of the
> abstract-mapping-per-Java-class approach.
>
> Thanks,
> -Archie
>
>
> On Fri, Mar 11, 2011 at 8:49 PM, Dennis Sosnoski <d...@sosnoski.com
> <mailto:d...@sosnoski.com>> wrote:
>
> Hi Archie,
>
> The "extends" attribute is mostly just a left-over from the
> earlier days of JiBX. The only time it's important is when you're
> using substitution groups in your XML documents (which allow you
> to substitute an extension element for a base element in a
> document - such as <vehicle> for the base element, <automobile>
> for the extension) - and since substitution groups are made of
> elements it makes sense to require a class with a concrete mapping
> as the base.
>
> I personally think substitution groups are the best way of
> handling polymorphism in XML documents, since the element name
> tells you the structure. The alternative of type substitution,
> where you keep the same element name and just add an xsi:type
> attribute to say what you're really doing, seems kludgy to me.
>
> In terms of the mappings, just using <structure map-as="..."/>
> gives you the equivalent of type extension. Though it's not a
> direct equivalent of schema type extension, since it allows you to
> reference the base type anywhere within the structure of the
> extension (so really a combination of schema type extension, group
> references, and attributeGroup references).
>
> - Dennis
>
> Dennis M. Sosnoski
> Java SOA and Web Services Consulting
> <http://www.sosnoski.com/consult.html>
> Axis2/CXF/Metro SOA and Web Services Training
> <http://www.sosnoski.com/training.html>
> Web Services Jump-Start <http://www.sosnoski.com/jumpstart.html>
>
>
> On 03/11/2011 07:56 AM, Archie Cobbs wrote:
>> I always get confused when trying to use the "extends" attribute...
>>
>> Here's a simple question: whereas "map-as" allows either a class
>> name or a type name, "extends" only allows a class name. Why?
>>
>> As it stands now, there's no way to extend a mapping that has a
>> type-name.
>>
>> Thanks,
>> -Archie
>>
>> --
>> Archie L. Cobbs
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Colocation vs. Managed Hosting
>> A question and answer guide to determining the best fit
>> for your organization - today and in the future.
>> http://p.sf.net/sfu/internap-sfd2d
>>
>>
>> _______________________________________________
>> jibx-users mailing list
>> jibx-users@lists.sourceforge.net
>> <mailto:jibx-users@lists.sourceforge.net>
>> https://lists.sourceforge.net/lists/listinfo/jibx-users
>>
>
>
>
>
> --
> Archie L. Cobbs
>
------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users