Hi,
I have a problem with castors xml mapping.
Starting point is a class with two private members: a list and a map.
The setter for the list constructs the map from data
from the list. The mapping file describes that only the list is
mapped to xml.
I construct an instance and use the setter for setting a
list with three strings. This works fine.
After serialization and deserialization the list with 3 strings
is full available, but for the map only 1 instead of 3
entries is built. The generated xml looks fine.
Logging the setter I get that it is called with a list with
one element !
So: why is the setter only called with a list with one instance ?
But: why is the list field ok after deserialisation ?
I put the code and logging below this mail. I hope this is not
to lengthy, I tried to reduce it as far as possible.
Regards, Uwe
+++ stdout with comments ++++++++++++++++++++++++++++++++++++++++++++++++
when constructing:
default cons called
setLi called with list of length 3
objects state after constructing. the left column
comes from the list, the right column from the map
zero: 0
one: 1
two: 2
generated xml is:
<?xml version="1.0" encoding="UTF-8"?>
<reproduce-strange-castor-behaviour>
<li>zero</li>
<li>one</li>
<li>two</li>
</reproduce-strange-castor-behaviour>
during deserialisation:
default cons called
setLi called with list of length 1
objects state after deserialisation. the left column
comes from the list, the right column from the map:
zero: 0
one: null
two: null
+++ code +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package sandbox;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.XMLContext;
public class ReproduceStrangeCastorBehaviour {
private ArrayList<String> li;
private Map<String, Integer> ma;
public ReproduceStrangeCastorBehaviour() {
System.out.println("default cons called");
}
public List<String> getLi() {
return li;
}
public void setLi(ArrayList<String> li) {
System.out.println("setLi called with list of length
"+li.size());
this.li = li;
// construct map
this.ma = new HashMap<String, Integer>();
for (int i = 0; i < li.size(); ++i)
this.ma.put(li.get(i), i);
}
public void print() {
for (String l : this.li) {
System.out.println(l + ": " + this.ma.get(l));
}
}
private static String toXML(Object o, XMLContext context) throws
Exception {
Marshaller ms = context.createMarshaller();
StringWriter sw = new StringWriter();
ms.setWriter(sw);
ms.marshal(o);
return sw.toString();
}
private static Object fromXML(String xml, XMLContext context) throws
Exception {
return context.createUnmarshaller().unmarshal(new
StringReader(xml));
}
public static void main(String[] args) throws Exception {
// construct object rp
ReproduceStrangeCastorBehaviour rp = new
ReproduceStrangeCastorBehaviour();
ArrayList<String> li = new ArrayList<String>();
li.add("zero");
li.add("one");
li.add("two");
rp.setLi(li);
// state of rp
rp.print();
// serialisation + deserialisation
Mapping m = new Mapping();
m.loadMapping("castor_mappings/reproduce_strange_behavior.xml");
XMLContext context = new XMLContext();
context.addMapping(m);
String xml = toXML(rp, context);
System.out.println(xml);
rp = (ReproduceStrangeCastorBehaviour) fromXML(xml, context);
// state of rp
rp.print();
}
}
+++ mapping +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<mapping>
<class name="sandbox.ReproduceStrangeCastorBehaviour">
<field name="li" type="string" collection="arraylist" />
</class>
</mapping>
--
Dr. rer. nat. Uwe Schmitt
Forschung & Entwicklung Mathematik
mineway GmbH
Gebäude 4
Im Helmerswald 2
66121 Saarbrücken
Telefon: +49 (0)681 8390 5334
Telefax: +49 (0)681 830 4376
[email protected]
www.mineway.de
Geschäftsführung: Dr.-Ing. Mathias Bauer
Amtsgericht Saarbrücken HRB 12339
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email