Hi Dennis,
Your explanation clear my understanding very well. In the long term, I
would possible to have the XML schema changed. For now, I would like to
adjust the class structure so that it can be marshall/unmarshall with
this XML schema. As currently the schema is also used by other
application as well.
I agree with you that the easiest path would be to make the
EquipmentLineItem polymorphic. So I would go with this direction.
Now what I have is like this:
abstract class EquipmentLineItem {
private String name;
private int quantity;
}
class PhoneLineItem extends EquipmentLineItem {
}
class PdaLineItem extends EquipmentLineItem {
}
Now I can define the mapping so that different subclass of
EquipmentLineItem can have different tag. However, I still could not
make the field of EquipmentLineItem map to different tag depend on
subclasses. What is the syntax to define that?
Currently, my mapping looks like this:
<mapping class="EquipmentLineItem" abstract="true">
<value name="name" field="name" />
<value name="quantity" field="quantity" />
</mapping>
<mapping class="PhoneLineItem" extends="EquipmentLineItem">
<structure map-as="EquipmentLineItem" />
</mapping>
Which results in XML like this:
<equipment>
<phone>
<name>Siemens S45</name>
<quantity>2</quantity>
</phone>
<pda>
<name>O2 XDA</name>
<quantity>5</quantity>
</pda>
</equipment>
When I try to provide a <value> mapping inside mapping for Phone or Pda
so that both can have different tags, the following error message is
show when compile the binding file:
Nonstatic field name not found in class com.waveman.poc.Phone for value
element at....
Nonstatic field quantity not found in class com.waveman.poc.Phone for
value element at....
Regards,
Vairoj
--__--__--
Message: 4
Date: Thu, 10 Nov 2005 15:16:53 -0800
From: Dennis Sosnoski <[EMAIL PROTECTED]>
To: [email protected]
Subject: Re: [jibx-users] Abstract, structure mapping with multiple elements
Reply-To: [email protected]
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
-------------------------------------------------------
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