I had a lot of problems with this, and had to hack up
my version of Castor to get it to work (so now I am
stuck on an older version). For version 0.9.9.1, I
added my own support following the pattern of the
existing support. I had to implement new MapItem
classes for arrays or collections to be values of hash
maps. I did not submit my changes as a patch because I
don't think what I did was the right way to support
this - it was a quick-and-dirty fix.
See CASTOR-1313 for other related Map weaknesses when
the value is itself multivalued, e.g. a Map, List, or
array. If somebody is revamping Map processing, it
would be nice to keep that in mind as well.
Bryan
--- Christopher Spoehr <[EMAIL PROTECTED]> wrote:
> Hello-
>
> I'm trying to marshal/unmarshal a HashMap of
> HashMaps (eventually it will be a HashMap of HMs,
> Vectors, and Arrays, but one step at a time.) The
> XML I'll eventually be working with will be given to
> me as one string - that's why I'm working with the
> string readers/writers.
>
> Using the Castor FAQ and the archives, I've gotten
> Castor to marshal and unmarshal the bean class
> holding the HM successfully for a single dimensional
> HashMap. For the nested HM, Castor marshals
> acceptably but gets hung up on the unmarshaling. Any
> tips would be appreciated. I'd seen in a previous
> thread that Stephen Bash has had some experience
> with this. Are you still out the Stephen?
>
> Holder Class:
>
> public class HashMapHolderBean {
>
> private HashMap heldHashMap;
>
> public HashMapHolderBean()
> {
> heldHashMap = new HashMap();
> }
>
> public HashMapHolderBean(HashMap inputHashMap)
> {
> heldHashMap = inputHashMap;
> }
>
> public HashMap getHeldHashMap() {
> return heldHashMap;
> }
>
> public void setHeldHashMap(HashMap map) {
> heldHashMap = map;
> }
> }
>
> HashMapHolderBeanMapping.xml:
> <?xml version="1.0" encoding="UTF-8"?>
> <mapping>
> <class
> name="com.sbc.cctp.easl.web.artt.HashMapHolderBean"
> auto-complete="false">
> <description>Default mapping for class
>
com.sbc.cctp.easl.web.artt.HashMapHolderBean</description>
> <map-to xml="hash-map-holder-bean" />
> <field name="heldHashMap" collection="map">
> <bind-xml name="held-hash-map">
> <class
> name="org.exolab.castor.mapping.MapItem">
> <field name="key" type="string">
> <bind-xml name="key"
> node="element" />
> </field>
> <field name="value"
> type="java.lang.Object">
> <bind-xml name="value"
> node="element">
> <class
> name="org.exolab.castor.mapping.MapItem" />
> </bind-xml>
> </field>
> </class>
> </bind-xml>
> </field>
> </class>
> </mapping>
>
> Marshalling/Unmarshalling code:
> private String testMarshalling()
> {
> HashMap mapHM = new HashMap();
> mapHM.put("test","Hello World.");
> mapHM.put("test2","Hello Universe");
>
> HashMap level2 = new HashMap();
> level2.put("level2_1","I'm level two");
>
> mapHM.put("level2", level2);
>
> HashMapHolderBean hashMapHolder = new
> HashMapHolderBean(mapHM);
> StringWriter stringWriter = new StringWriter();
>
> try
> {
>
> //InputSource mappingSource = new InputSource(
> new StringReader(generateMapString()));
> InputSource mappingSource = new
>
InputSource(ClassLoader.getSystemResource("HashMapHolderBeanMapping.xml").toString());
> Mapping mapping = new
>
Mapping(AutomatedRegressionTestTool.class.getClassLoader());
> mapping.loadMapping(mappingSource);
>
> Marshaller marshaller = new
> Marshaller(stringWriter);
> marshaller.setMapping(mapping);
> marshaller.marshal(hashMapHolder);
> }
> catch (Exception e)
> {
> log.error("Marshalling Error",e);
> }
>
> return stringWriter.getBuffer().toString();
> }
>
> private HashMapHolderBean testUnMarshalling(String
> marshalled)
> {
> String sourceString = marshalled;
> log.info("Marshalled length: " +
> marshalled.length());
> log.info("Source length: " +
> sourceString.length());
> log.info(sourceString);
>
> HashMapHolderBean hmhb = new
> HashMapHolderBean();
>
> try
> {
> InputSource mappingSource = new
>
InputSource(ClassLoader.getSystemResource("HashMapHolderBeanMapping.xml").toString());
> Mapping mapping = new
>
Mapping(AutomatedRegressionTestTool.class.getClassLoader());
> mapping.loadMapping(mappingSource);
>
> Unmarshaller unm = new Unmarshaller(mapping);
>
> hmhb = (HashMapHolderBean)unm.unmarshal(new
> StringReader(sourceString));
> }
> catch (IOException ioException)
> {
> log.error("IOException occured in
> testUnMarshalling() ", ioException);
> }
> catch (MappingException mappingException)
> {
> log.error("Mapping exception occured in
> testUnMarshalling() ", mappingException);
> }
> catch (Exception e)
> {
> log.error("Unmarshalling exception caught.",
> e);
> }
>
> return hmhb;
>
> }
>
> toString of the HM returned by testUnMarshalling
> (from inside of the holder bean, of course):
> {test2=Hello Universe,
> [EMAIL PROTECTED],
> test=Hello World.}
>
> Of course I'll be happy to answer any clarification
> questions one may need to ask.
>
> Chris Spoehr
>
>
---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email