commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b p <trashspam2...@yahoo.com>
Subject [betwixt] another Map issue
Date Wed, 07 Apr 2004 04:26:58 GMT
Hi Robert,
 
Thanks for commiting that last patch for Maps.  
 
I ran into another problem as I tried to use a .betwixt file with a Map.  Comparing how descriptors
are created with and without a .betwixt file it seems that when a .betwixt file is used with
a map, the code to create a descriptor for the "entry" element is missing.  Below is a test
case that shows the problem and a patch to the ElementRule class that fixes the problem for
this test case.  Could you take a look and put the patch in if the changes to ElementRule
look ok to you?
 
Thanks,
Brian
 
---test case---
MapBean.java
 
package org.apache.commons.betwixt.dotbetwixt;
import java.util.Map;
import java.util.HashMap;
/**
 * @author Brian Pugh
 */
public class MapBean {
    private Map ids = new HashMap();
    public Map getValues() {
        return ids;
    }
    public void addValue(String key, Integer value) {
        this.ids.put(key, value);
    }
}
 
MapBean.betwixt
<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes="attribute">
    <element name='map-bean'>
        <element name='map-values' property='values'/>
        <addDefaults/>
    </element>
</info>
 
TestMap.java
package org.apache.commons.betwixt.dotbetwixt;
import junit.framework.TestCase;
import java.io.StringWriter;
import java.io.StringReader;
import org.apache.commons.betwixt.io.BeanWriter;
import org.apache.commons.betwixt.io.BeanReader;
/**
 * @author Brian Pugh
 */
public class TestMap extends TestCase {
    public void testMapWithDotBetwixtFile() throws Exception {
        MapBean map = new MapBean();
        String key = "one";
        map.addValue(key, new Integer(1));
        StringWriter outputWriter = new StringWriter();
        outputWriter.write("<?xml version='1.0' ?>\n");
        BeanWriter beanWriter = new BeanWriter(outputWriter);
        beanWriter.enablePrettyPrint();
        beanWriter.getBindingConfiguration().setMapIDs(true);
        beanWriter.write(map);
        BeanReader beanReader = new BeanReader();
        // Configure the reader
        beanReader.registerBeanClass(MapBean.class);
        StringReader xmlReader = new StringReader(outputWriter.toString());
        //Parse the xml
        MapBean result = (MapBean) beanReader.parse(xmlReader);
        assertNotNull("Should have deserialized a MapBean but got null.", result);
        assertEquals("Should have gotten the same value back from the Map after deserializing
that was put in.",
                map.getValues().get(key),
                result.getValues().get(key));

    }
}

 
--fix to element rule--
? betwixt-refactor-branch-orig/src/test/org/apache/commons/betwixt/dotbetwixt/MapBean.betwixt
? betwixt-refactor-branch-orig/src/test/org/apache/commons/betwixt/dotbetwixt/MapBean.java
? betwixt-refactor-branch-orig/src/test/org/apache/commons/betwixt/dotbetwixt/TestMap.java
Index: betwixt-refactor-branch-orig/src/java/org/apache/commons/betwixt/digester/ElementRule.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/ElementRule.java,v
retrieving revision 1.13.2.6
diff -u -r1.13.2.6 ElementRule.java
--- betwixt-refactor-branch-orig/src/java/org/apache/commons/betwixt/digester/ElementRule.java
8 Feb 2004 12:11:17 -0000 1.13.2.6
+++ betwixt-refactor-branch-orig/src/java/org/apache/commons/betwixt/digester/ElementRule.java
7 Apr 2004 04:12:57 -0000
@@ -61,6 +61,7 @@
  */ 
 import java.beans.PropertyDescriptor;
 import java.lang.reflect.Method;
+import java.util.Map;
 
 import org.apache.commons.betwixt.ElementDescriptor;
 import org.apache.commons.betwixt.XMLBeanInfo;
@@ -310,6 +311,19 @@
             elementDescriptor.setHollow(true);
 
             writeMethod = null;
+
+            if (Map.class.isAssignableFrom(type)) {
+              elementDescriptor.setLocalName( "entry" );
+              // add elements for reading
+              ElementDescriptor keyDescriptor = new ElementDescriptor( "key" );
+              keyDescriptor.setHollow( true );
+              elementDescriptor.addElementDescriptor( keyDescriptor );
+
+              ElementDescriptor valueDescriptor = new ElementDescriptor( "value" );
+              valueDescriptor.setHollow( true );
+              elementDescriptor.addElementDescriptor( valueDescriptor );
+            }
+
         } else {
             log.trace( "Standard property" );
             elementDescriptor.setHollow(true);
 
 


---------------------------------
Do you Yahoo!?
Yahoo! Small Business $15K Web Design Giveaway - Enter today
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message