commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robert burrell donkin <robertburrelldon...@blueyonder.co.uk>
Subject Re: [betwixt] Problem with Maps and Arrays
Date Sun, 18 Apr 2004 20:39:45 GMT
hi brian

you're right: this is quite an interesting problem :)

the tricky part is that i think that an intermediary list is going to  
be needed (which will be converted into the array when the element  
ends).

i've been look at some refactoring and design improvements today which  
should help. i've pushed more functionality from the actions into the  
context and simplified the read context API. i hope that i'll be able  
to add an updater stack to the context and allow actions to push and  
pop updaters. this will allow intermediary objects to be transparently  
supported. once this is done, i should be able to use a special action  
implementation to set everything up and make the call to the method at  
the end. (anyway, this is my plan and it's probably subject to change  
if anyone comes up with a better one. as usual, if anyone has any ideas  
for better ways to do this, please jump in.)

but unfortunately, i don't think that i'll have the time to complete  
this tonight...

- robert

On 16 Apr 2004, at 22:00, b p wrote:

> Hi Robert,
>
> I've hit a problem with Maps and arrays that I haven't been able to  
> resolve.  I would like to write/read a Map that has an array (either  
> java array or collection) as the values of the map.  When I write,  
> everything looks ok.  When I read back in, I'm getting a map with no  
> values.  If I wrap the array or collection in another bean (so the  
> value of the map is a bean, not an array or collection), it will work.  
> I can live with that if I need to,  but I would prefer to have the map  
> values be arrays or collections directly.  I haven't been able to  
> track down a good way to do that-- I'm hoping you'll see a relatively  
> easy solution I'm missing :).
>
> Below is a test case that shows the problem.  Any suggestions?
>
> Thanks,
> Brian
>
>
> AddressBook.java
> ---------------------------------
> package org.apache.commons.betwixt.io.read;
> import java.util.HashMap;
> /**
>  * @author Brian Pugh
>  */
> public class AddressBook {
>   private HashMap addressesMap = new HashMap();
>
>   public HashMap getAddressBookItems() {
>     return addressesMap;
>   }
>   public void setAddressBookItems(HashMap map) {
>     this.addressesMap = map;
>   }
>   public void addAddressBookItem(String name, AddressBean[] addresses)  
> {
>     addressesMap.put(name, addresses);
>   }
>
> }
>
>
> Test case (patch to TestMaps.java)
> ----------------------------------------------------
>
> Index: src/test/org/apache/commons/betwixt/io/read/TestMaps.java
> ===================================================================
> RCS file:  
> /home/cvspublic/jakarta-commons/betwixt/src/test/org/apache/commons/ 
> betwixt/io/read/Attic/TestMaps.java,v
> retrieving revision 1.1.2.1
> diff -u -r1.1.2.1 TestMaps.java
> --- src/test/org/apache/commons/betwixt/io/read/TestMaps.java 22 Feb  
> 2004 17:09:29 -0000 1.1.2.1
> +++ src/test/org/apache/commons/betwixt/io/read/TestMaps.java 16 Apr  
> 2004 20:46:55 -0000
> @@ -132,4 +132,60 @@
>
>      }
>
> +    public void testMapWithArray() throws Exception {
> +
> +      AddressBook addressBook = new AddressBook();
> +      AddressBean[] johnsAddresses = new AddressBean[2];
> +      johnsAddresses[0] = new AddressBean("12 here", "Chicago",  
> "USA", "1234");
> +      johnsAddresses[1] = new AddressBean("333 there", "Los Angeles",  
> "USA", "99999");
> +      String name = "John";
> +      addressBook.addAddressBookItem(name, johnsAddresses);
> +      StringWriter outputWriter = new StringWriter();
> +      outputWriter.write("<?xml version='1.0' ?>\n");
> +      BeanWriter beanWriter = new BeanWriter(outputWriter);
> +      beanWriter.enablePrettyPrint();
> +      beanWriter.write(addressBook);
> +      System.out.println(outputWriter.toString());
> +      String xml = "<?xml version='1.0' ?>\n" +
> +        "  <AddressBook id=\"1\">\n" +
> +        "    <addressBookItems>\n" +
> +        "      <entry id=\"2\">\n" +
> +        "        <key>John</key>\n" +
> +        "        <value id=\"3\">\n" +
> +        "          <AddressBean id=\"4\">\n" +
> +        "            <city>Chicago</city>\n" +
> +        "            <code>1234</code>\n" +
> +        "            <country>USA</country>\n" +
> +        "            <street>12 here</street>\n" +
> +        "          </AddressBean>\n" +
> +        "          <AddressBean id=\"5\">\n" +
> +        "            <city>Los Angeles</city>\n" +
> +        "            <code>99999</code>\n" +
> +        "            <country>USA</country>\n" +
> +        "            <street>333 there</street>\n" +
> +        "          </AddressBean>\n" +
> +        "        </value>\n" +
> +        "      </entry>\n" +
> +        "    </addressBookItems>\n" +
> +        "  </AddressBook>\n";
> +
> +      assertEquals(xml, outputWriter.toString());
> +      BeanReader reader = new BeanReader();
> +      reader.registerBeanClass(AddressBook.class);
> +      StringReader xmlReader = new  
> StringReader(outputWriter.toString());
> +      AddressBook result = (AddressBook) reader.parse(xmlReader);
> +      assertNotNull("Expected to get an AddressBook!", result);
> +      assertNotNull("Expected AddressBook to have some address  
> entryitems!", result.getAddressBookItems());
> +      AddressBean []resultAddresses = (AddressBean[])  
> result.getAddressBookItems().get(name);
> +      assertNotNull("Expected to have some addresses for " + name,  
> resultAddresses);
> +      assertEquals("Got wrong city in first address for " + name,
> +                   johnsAddresses[0].getCity(),
> +                   resultAddresses[0].getCity());
> +      assertEquals("Got wrong city in second address for " + name,
> +                   johnsAddresses[1].getCity(),
> +                   resultAddresses[1].getCity());
> +
> +
> +  }
> +
>  }
>
>
>
>
> 		
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Tax Center - File online by April 15th


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message