commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b p <trashspam2...@yahoo.com>
Subject [betwixt] Collections not set when using .betwixt file
Date Tue, 20 Jan 2004 18:08:21 GMT
Hi Robert,
I've run into a issue with collections that I can work around, but I am hoping can be solved
more cleanly with a fairly simple modification to the ElementRule class. 
 
Here's the situation.  I have a bean that includes a composite property as well as some properties
that I do not want written to XML or set when XML is read.   Therefore, I created a .betwixt
file that includes the composite property, but does not include the property I want ignored.
 When I create XML from the bean, it looks exactly right.  However, when I read it back in,
the bean does not have the composite property set.
 
The composite property is set if I have no .betwixt file or include the <addDefaults/>
tag in the .betwixt file.  However, I need a .betwixt file and I would rather not use the
<addDefaults/> tag because if I do, I'll have to explicitly "hide" all the properties
I want ignored in the .betwixt file.  For maintenance reasons, I'd rather be able to just
write the .betwixt file once and not have to change it if new properties are added to the
bean that do not pertain to the XML generation/reading.
 
I included below a test case and a patch that causes the test case to pass and doesn't break
any other unit tests (I tested against your refactoring branch).  
 
Is there a better way to solve this problem?  If not, would it be possible to commit the patch?
 
Thanks again for the continued help.
Brian
 

Test Case:

TestLoopType.java
package org.apache.commons.betwixt.dotbetwixt;

import junit.framework.Test;
import junit.framework.TestSuite;
import junit.framework.TestCase;
import junit.textui.TestRunner;

import java.util.ArrayList;
import java.util.List;
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 TestLoopType extends TestCase {

  public void testSimpleList() throws Exception {
    Father father = new Father();
    father.setSpouse("Julie");
    father.addKid("John");
    father.addKid("Jane");

    StringWriter outputWriter = new StringWriter();

    outputWriter.write("<?xml version='1.0' ?>\n");
    BeanWriter beanWriter = new BeanWriter(outputWriter);
    beanWriter.enablePrettyPrint();
    beanWriter.getBindingConfiguration().setMapIDs(true);
    beanWriter.write(father);

    BeanReader beanReader = new BeanReader();

    // Configure the reader
    beanReader.registerBeanClass(Father.class);
    StringReader xmlReader = new StringReader(outputWriter.toString());

    //Parse the xml
    Father result = (Father)beanReader.parse(xmlReader);

    assertNotNull("Unexpected null list of children!", result.getKids());
    assertEquals("got wrong number of children", father.getKids().size(), result.getKids().size());
    assertNull("Spouse should not get set because it is not in the .betwixt file", result.getSpouse());
  }

  public static Test suite() {
    return new TestSuite(TestLoopType.class);
  }

  public static void main(String[] args) {
    TestRunner.run(suite());
  }


}


Father.java
package org.apache.commons.betwixt.dotbetwixt;

import java.util.List;
import java.util.ArrayList;

/**
 * @author Brian Pugh
 */
public class Father {

  private List kids;
  private String spouse;

  public String getSpouse() {
    return spouse;
  }

  public void setSpouse(String spouse) {
    this.spouse = spouse;
  }

  public List getKids() {
    return kids;
  }

  public void addKid(String kid) {
    if (this.kids == null) {
      this.kids = new ArrayList();
    }
    this.kids.add(kid);
  }

}

Father.betwixt
<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes="attribute">
    <element name='father'>
        <element name='children' property='kids'/>
    </element>
</info>

Patch:

? betwixt/src/test/org/apache/commons/betwixt/dotbetwixt/Father.betwixt
? betwixt/src/test/org/apache/commons/betwixt/dotbetwixt/Father.java
? betwixt/src/test/org/apache/commons/betwixt/dotbetwixt/TestLoopType.java
Index: betwixt/src/java/org/apache/commons/betwixt/digester/AddDefaultsRule.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/AddDefaultsRule.java,v
retrieving revision 1.9.2.3
diff -u -r1.9.2.3 AddDefaultsRule.java
--- betwixt/src/java/org/apache/commons/betwixt/digester/AddDefaultsRule.java 18 Jan 2004
19:21:17 -0000 1.9.2.3
+++ betwixt/src/java/org/apache/commons/betwixt/digester/AddDefaultsRule.java 20 Jan 2004
17:42:09 -0000
@@ -128,10 +128,7 @@
             }
         }
         
-        // default any addProperty() methods
-        getXMLIntrospector().defaultAddMethods( 
-                                            getRootElementDescriptor(), 
-                                            beanClass );
+       
     }
 
 
Index: betwixt/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.4
diff -u -r1.13.2.4 ElementRule.java
--- betwixt/src/java/org/apache/commons/betwixt/digester/ElementRule.java 18 Jan 2004 22:25:22
-0000 1.13.2.4
+++ betwixt/src/java/org/apache/commons/betwixt/digester/ElementRule.java 20 Jan 2004 17:42:10
-0000
@@ -201,6 +201,14 @@
      */
     public void end(String name, String namespace) {
         Object top = digester.pop();
+        // default any addProperty() methods
+        Object parent = digester.peek();
+        if ( parent instanceof XMLBeanInfo ) {
+            XMLBeanInfo beanInfo = (XMLBeanInfo) parent;        
+            getXMLIntrospector().defaultAddMethods(
+                                            beanInfo.getElementDescriptor(),
+                                            beanClass );
+        }
     }


---------------------------------
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message