Hi Jules,

TypedArrayMapper isn't supposed to require you to use the name, and if used without a name as in your original binding it should be able to handle things properly. I think it's broken, though, as your experience would indicate. :-( If you look at the code the TypedArrayMapper unmarshal method assumes it can take everything up to the end of the enclosing element:
// process all items present in document
while (!ctx.isEnd()) {
Object item = ctx.unmarshalElement();
m_holder.add(item);
}


What this really needs to do is a lot more complex - basically get the unmarshaller for the class of objects that go into the array, then loop only as long as the isPresent() method of the unmarshaller returns true. There are some problems with doing this in the general case, though - suppose the array is of java.lang.Object, or of an interface type that's implemented by multiple classes in the binding (which are not otherwise related)? Yuck.

I'm hoping to handle this directly in JiBX with beta 4, pretty much as is done with ArrayList now. I'll need to think through what I'm doing to make sure I've got the above concerns covered. In the meantime, your best approach may be to do it "by hand", with separate collection get/set methods in your class A that work with each type, along the lines of:

 <mapping name="A" class="A">
   <collection item-type="B" add-method="addB" load-method="getB"/>
   <collection item-type="C" add-method="addC" load-method="getC"/>

This means you'd need to actually implement the arrays yourself, though, behind the scenes in these add/load methods.

 - Dennis

Jules Gosnell wrote:

you always think of the answer as soon as you have posted :-)

Perhaps this is my problem...

TypedArrayMapper expects to be used with an intermediate array object i.e.

<A>
<Bs>
 <B.../>
 <B .../>
 ...
</Bs>
</A>

So I took this a while ago and realized that I could collapse the 'A' and 'Bs' element into the same class, which suited the arrangement of the 3rd party library to which I am mapping.

Then, having forgotten that I had done this, I tried to extend my working code to accomodate the second relationship - Bang !

I'm going to have another look at the problem. In the meantime, if anyone has any ideas I'd be grateful to hear them....

Jules


Jules Gosnell wrote:


Guys,

I've just started using jibx and have come across a problem with [my
use of] org.jibx.extras.TypedArrayMapper....

Imagine input that looks like this :

<A>
 <B.../>
 <B.../>
 <C.../>
 <C.../>
</A>

i.e. an 'A' owns a number of 'B's and a number of 'C's...

'A' might look like :

public class A
{
 public void setBs(B[] bs){...}
 public B[] getBs(){...}

 public void setCs(C[] bs){...}
 public C[] getCs(){...}
}

my binding.xml might look like this :

<binding>

 <mapping class="B[]"
   marshaller="org.jibx.extras.TypedArrayMapper"
   unmarshaller="org.jibx.extras.TypedArrayMapper"
   />

 <mapping name="B" class="B">
 </mapping>

 <mapping class="C[]"
   marshaller="org.jibx.extras.TypedArrayMapper"
   unmarshaller="org.jibx.extras.TypedArrayMapper"
   />

 <mapping name="C" class="C">
 </mapping>

 <mapping name="A" class="A">
  <structure set-method="setBs" get-method="getBs"/>
  <structure set-method="setCs" get-method="getCs"/>
 </mapping>

</binding>

I put together a testcase for the A's relationship with B - which
worked fine. However when I added the A-C relationship I found that as
the 'C's were parsed they where dumped into the same array as the
'B's. Jibx then attempts to pass a heterogeneous array of 'B's and
'C's (I assume into A.setCs()), but blows up with a java.lang.ArrayStoreException
in A.JiBX_binding2_unmarshal_2_0() because the array is of mixed type.


I have tried extending TypedArrayMapper into BMapper and CMapper and
using those in its place, but this does not seem to effect the problem
- in fact only one Mapper instance seems to get used, the other one
does not even get constructed before I encounter the error.

Perhaps I have misunderstood the intended use of these Mappers ?

Any help would be much appreciated - I can put together a testcase if
it would be of help.

Thanks for your time,


Jules

P.S.

I'm running beta3c on Sun 1.4.2_05



-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
_______________________________________________
jibx-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to