Am 18.05.2011, 17:44 Uhr, schrieb Uwe <[email protected]>:
Hi Uwe,
by default Castor calls entity.getList().add() for every element of the
list at unmarshaling. Does this match your observation.
According to my observation entity.getList().setList() is called once
and after that the remaing elements are added as you described above.
This is not very uniform.
But I will play with get-/set-methods and report my observations here.
Regards, Uwe
You can change this default behaviour by specifing get-/set-methods at
field element. This is described at:
http://castor.codehaus.org/reference/html/XML%20data%20binding.html#xml.mapping.mappingfile.field
I added a method
public void addLi(String s)
{
System.out.println("addToLi called with "+s);
this.li.add(s);
}
and the corresponding <field> in the mapping file is
<field name="li" type="string" collection="arraylist"
set-method="addLi" />
During unmarshalling this function is never called, the list is not
unmarshalled
at all and I there is no thrown exception at all.
If I misspell addLi in the mapping file I get an extension that the
corresponding
method with the expected signature ist not found.
Regards, Uwe
Regards
Ralf
Am 18.05.2011 14:56, schrieb Uwe:
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