commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b p <>
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.

Test Case:
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;


 * @author Brian Pugh
public class TestLoopType extends TestCase {

  public void testSimpleList() throws Exception {
    Father father = new Father();

    StringWriter outputWriter = new StringWriter();

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

    BeanReader beanReader = new BeanReader();

    // Configure the reader
    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) {;

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 ( == null) { = new ArrayList();


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


? betwixt/src/test/org/apache/commons/betwixt/dotbetwixt/Father.betwixt
? betwixt/src/test/org/apache/commons/betwixt/dotbetwixt/
? betwixt/src/test/org/apache/commons/betwixt/dotbetwixt/
Index: betwixt/src/java/org/apache/commons/betwixt/digester/
RCS file: /home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/,v
retrieving revision
diff -u -r1.9.2.3
--- betwixt/src/java/org/apache/commons/betwixt/digester/ 18 Jan 2004
19:21:17 -0000
+++ betwixt/src/java/org/apache/commons/betwixt/digester/ 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/
RCS file: /home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/,v
retrieving revision
diff -u -r1.13.2.4
--- betwixt/src/java/org/apache/commons/betwixt/digester/ 18 Jan 2004 22:25:22
+++ betwixt/src/java/org/apache/commons/betwixt/digester/ 20 Jan 2004 17:42:10
@@ -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
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message