Hi Vairoj,

This structure does present a problem, because the element you want to use to represent an EquipmentLineItem is determined by the actual type of the Equipment reference. I don't see any clean way to handle this directly in the binding for marshalling. If you were only binding for unmarshalling I think you could do this (at least with the CVS code) using a collection of the form:

<collection name="equipment" ordered="false" name="equipment" ordered="false" ...
   <structure name="phone" usage="optional">
     <structure field="equipment">
       <value name="phone-name" field="name"/>
     </structure>
     <value name="phone-quantity" field="quantity"/>
   </structure>
   <structure name="pda" usage="optional">
     <structure field="equipment">
       <value name="pda-name" field="name"/>
     </structure>
     <value name="pda-quantity" field="quantity"/>
   </structure>
 </collection>

since for unmarshalling only the element names of the child components of a <collection> need to be unique. If you also need marshalling you could probably add test-methods to the EquipmentLineItem to distinguish between the different variations, then in theory use a binding like:

<collection name="equipment" ordered="false" item-type="EquipmentLineItem" ...
   <structure name="phone" test-method="isPhone" usage="optional">
     <structure field="equipment">
       <value name="phone-name" field="name"/>
     </structure>
     <value name="phone-quantity" field="quantity"/>
   </structure>
   <structure name="pda" test-method="isPda" usage="optional">
     <structure field="equipment">
       <value name="pda-name" field="name"/>
     </structure>
     <value name="pda-quantity" field="quantity"/>
   </structure>
 </collection>

I say "in theory" because I don't know that this code generation path (structures distinguished by test-methods as items in a collection) has ever been tested. <mapping> definitions (abstract or not) really wouldn't gain you much here, since you're using distinct names even for the shared values.

I don't really understand the logic behind the current representation, so I'm not sure what to suggest in the way of changes to make it map more cleanly. The easiest way to handle things would be to distinguish at the line item level between the different types of devices, so that you'd have PhoneLineItem and PdaLineItem classes. These could derive from EquipmentLineItem, which would have quantity and name fields. Aside from that, the best I can suggest is a custom marshaller/unmarshaller for handling EquipmentLineItem. For the data you've shown this custom mar/umar could just get the class name of the Equipment instance and use in the XML representation as both the element name and the prefix on the child element names.

The other alternative is to change the XML representation, assuming you have control over that. I'd suggest something along the lines of:

 <equipment>
   <item>
     <phone>Siemens S45</phone>
     <quantity>5</quantity>
   </item>

this could be done using <mapping> definitions for the different subtypes of equipment.

 - Dennis

Vairoj A. wrote:

Hi,

This question combines structure mapping with abstract mapping, so it makes me confuse. However, I still not sure on how to separate the issue so maybe you guys could help me here.

This is a sample XML data:

<person>
  <name>Jack</name>
  <equipment>
     <phone>
        <phone-name>Siemens S45</name>
        <phone-quantity>5</quantity>
     </phone>
     <pda>
        <pda-name>O2 XDA mini</name>
        <pda-quantity>2</quantity>
     </pda>
  </equipment>
</person>

A person has a set of equipment line items, equipments can be categorized into 2 categories - phone and pda. Each line item is composed of name of the equipment and quantity. Please note that the tag that describe each line item is different by the type of equipment it contains.

The following are the classes which shows only parts that are relevant to the mapping:

class Person {
  private Set<EquipmentLineItem> lineItems;
}

class EquipmentLineItem {
  private Equipment equipment;
  private int quantity;
}

abstract class Equipment {
  private String name;
}

class Phone extends Equipment {
}

class Pda extends Equipment {
}

I have also attached an image file which is the class diagram for the preceding classes.

The question is how to map this class structure to the above XML format. There is several issue (for me).

- the structure is not symmetric. While in java class it is a collection of equipments, in XML the tag is up to the specific equipment type.

- In java class quantity attribute resides in EquipmentLineItem, whereas in XML they are all in the same level.

- sub element in each equipment has different element name depends on equipment type

Is it possible to do this type of mapping? Please suggest me. If it is not possible, I could adjust the class structure to which can be map better.


Regards,

Vairoj



------------------------------------------------------------------------



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to