Hi Folks,

When I try to marshal a collection (specifically a Map) that contains
another collection (specifically a List), it marshals things in a sensible
way, so for something like a Map<Date>, List<String>>, it'll generate the
following XML;

<my-collection>
    <strings>
        <key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:type="date">2008-05-05T21:23:19.906+01:00</key>
        <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:type="java:java.lang.String">Hello</value>
        <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:type="java:java.lang.String">World!</value>
    </strings>
</my-collection>

but is unable to unmarshall them complaining that the "value" is repeated.
As in,

*org.exolab.castor.xml.MarshalException: element "value" occurs more than
once. (parent class: org.exolab.castor.mapping.MapItem)
 location: /strings/strings/value{File: [not available]; line: 6; column:
117}*

This seems to be very similar to bugs 1313, 1551 etc, but although they say
its been resolved I get still get the problem on 1.2 of Castor. I can't
quiet figure out what those bugs are telling me, or how to apply the patches
or run the test case in 1313.

I've attached a test case that demonstrates the problem.

Any tips? Thanks in advance,
Toby
package com.racediary.model;

import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class MyCollection {
	private Map<Date, List<String>> strings;
	
	public MyCollection() {
		strings = new Hashtable<Date, List<String>>();
	}

	public Map<Date, List<String>> getStrings() {
		return strings;
	}
	
	public void setStrings(Map<Date, List<String>> strings) {
		this.strings = strings;
	}
}
package com.racediary.model;

import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import junit.framework.TestCase;

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.xml.sax.InputSource;

/**
 * @see https://jira.codehaus.org/browse/CASTOR-1551
 */
public class MyCollectionTest extends TestCase {

	// grrrr
	public void testCastorUnmarshallsCollections() throws Exception {
		Date date = new Date();
		MyCollection collection = setupData(date);

		Mapping mapping = new Mapping();
		mapping.loadMapping(getMapping());

		StringWriter writer = new StringWriter();
		Marshaller marshaller = new Marshaller(writer);
		marshaller.setMapping(mapping);
		marshaller.marshal(collection);

		String xml = writer.toString();
		assertNotNull(xml);
		assertTrue(xml != "");
		System.out.println(xml);

		Unmarshaller unmarshaller = new Unmarshaller();
		unmarshaller.setMapping(mapping);
		MyCollection deseralised = (MyCollection) unmarshaller.unmarshal(new StringReader(xml));
		List<String> val = (List<String>) deseralised.getStrings().get(date);
		assertTrue(val != null);
		assertEquals(2, val.size());
	}

	private MyCollection setupData(Date date) {
		MyCollection col = new MyCollection();
		List<String> strings = new ArrayList<String>();
		strings.add("Hello");
		strings.add("World!");
		col.getStrings().put(date, strings);
		return col;
	}

	private InputSource getMapping() throws MappingException {
		InputStream resourceAsStream = this.getClass().getResourceAsStream("mycollection.xml");
		if (resourceAsStream == null)
			throw new MappingException("Unable to find mapping.xml");
		return new InputSource(resourceAsStream);
	}
}
<mapping>
	
	<class name="com.racediary.model.MyCollection">
		<field name="strings" collection="map">
			<bind-to name="strings">
				<class name="org.exolab.castor.mapping.MapItem">
					<field name="key" type="java.util.Date">
						<bind-xml name="date" node="attribute" />
					</field>
					<field name="value" type="java.util.List">
						<bind-xml name="string" />
					</field>
				</class>
			</bind-to>
		</field>
	</class>
</mapping>
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Reply via email to