commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Wu, Chien-Hsing" <...@QuadraMed.com>
Subject [PATCH] betwixt support for derived interfaces
Date Tue, 27 Apr 2004 16:23:18 GMT
Hi,
 
Here's my second attempt to send the patch. Attached files were not received
correctly. I included Patch.diff and JUnit test package
org.commons.betwixt.interfaces in the email below.
 
I sent out the attached email below to a betwixt developer a while ago. That
email details the problem I faced when using betwixt. 
 
Patch.diff contains the changes I made. org.commons.betwixt.interfaces
contains the JUnit test code.
 
Can someone tell me how would I know if this patch is rejected or accepted?
 
Thanks,
 
 
 
Chien-Hsing Wu
QuadraMed Corporation
(703)709-2431
cwu@quadramed.com
 
=====Original Message======
From: Wu, Chien-Hsing 
Sent: Wednesday, April 14, 2004 12:57 PM
To: 'tobrien@apache.org'
Subject: betwixt question
 
Hi,
 
Pardon my intrusion but I am trying to use betwixt and came across a problem
that I could not find any answers on the web. I saw that you are listed in
the team web page of the betwixt site so I'd thought I'll ask you directly. 
 
I have a plural property in the top level bean whose type is an interface
with multiple base interfaces, like the following code:
 
package com.mystuff;
 
public class RootBean implements IContainer {
            public IContainer[] getChildren() {
       }
            
       public void addChild(IContainer child) {
       }
}
 
public interface IContainer entends IEnv, IService {
            public IContainer[] getChildren();
            public void addChild(IContainer child);
}
 
I set RootBean.betwixt like:
 
<element name='Root'>
            <element name='pot' property='children' type='com.mystuff.Pot'>
            <element name='belly' property='children'
type='com.mystuff.Belly'>
</element>
 
after went through the source code and found that I can set the property
type using the type attribute in <element>. I was hoping that in doing so
ElementDescriptors for the element 'pot' is derived from com.mysutff.Pot not
com.mystuff.IContainer when reading from an XML file. The reason for that is
IContainer is an interface and Java Introspector does not return all
properties of an interface, including its base interfaces. I could not use
ClassNormalizer since it's only used when writing beans out and it can only
map from one class to another.
 
And it did not work as I hoped. After dig in the code more I found that the
type of the property in my betwixt file is overwritten by the call to
ElementRule.configureDescriptor -> XMLIntrospectorHelper.configureProperty
and XMLIntrospectorHelper.defaultAddMethods. Moreover,
XMLIntrospectorHelper.defaultAddMethods also overwrites updater.
 
Can you tell me if this over writing behavior is intended or not? If not,
what's the expected behavior?
 
 
Thank you for your valuable input.
 
 
Chien-Hsing Wu
QuadraMed Corporation
(703)709-2431
cwu@quadramed.com
 
=====Patch.diff=====
cvs diff -u 
? ant-1.5.jar
? ant-optional-1.5.jar
? bin
? commons-beanutils-1.6.1.jar
? commons-collections-2.1.jar
? commons-digester-1.5.jar
? commons-logging-1.0.2.jar
? commons-logging-1.0.3.jar
? junit-3.8.1.jar
? log4j-1.2.8.jar
? xerces-2.2.1.jar
? xml-apis-1.0.b2.jar
? src/test/org/apache/commons/betwixt/interfaces
cvs server: Diffing .
cvs server: Diffing src
cvs server: Diffing src/conf
cvs server: Diffing src/java
cvs server: Diffing src/java/org
cvs server: Diffing src/java/org/apache
cvs server: Diffing src/java/org/apache/commons
cvs server: Diffing src/java/org/apache/commons/betwixt
Index: src/java/org/apache/commons/betwixt/Descriptor.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/
Descriptor.java,v
retrieving revision 1.5
diff -u -r1.5 Descriptor.java
--- src/java/org/apache/commons/betwixt/Descriptor.java    7 Apr 2004
21:16:27 -0000    1.5
+++ src/java/org/apache/commons/betwixt/Descriptor.java  26 Apr 2004
18:51:47 -0000
@@ -138,7 +138,11 @@
         this.singularPropertyType = singularPropertyType;
     }
     
-    
+    // Derived Interface Support
+    public boolean isSingularPropertyType() {
+        return (singularPropertyType != null);
+    }
+              
     /**
      * Gets the options for this descriptor.
      * Options are used to communicate non-declarative
cvs server: Diffing src/java/org/apache/commons/betwixt/digester
Index:
src/java/org/apache/commons/betwixt/digester/XMLIntrospectorHelper.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/
digester/XMLIntrospectorHelper.java,v
retrieving revision 1.29
diff -u -r1.29 XMLIntrospectorHelper.java
---
src/java/org/apache/commons/betwixt/digester/XMLIntrospectorHelper.java    
31 Mar 2004 21:11:52 -0000  1.29
+++
src/java/org/apache/commons/betwixt/digester/XMLIntrospectorHelper.java   26
Apr 2004 18:51:49 -0000
@@ -242,6 +242,11 @@
         Method writeMethod = propertyDescriptor.getWriteMethod();
         
         elementDescriptor.setLocalName( propertyDescriptor.getName() );
+        
+        // Derived Interface Support
+        if (elementDescriptor.getPropertyType() != type) {
+           
elementDescriptor.setSingularPropertyType(elementDescriptor.getPropertyType(
));
+        }
         elementDescriptor.setPropertyType( type );        
         
         // XXX: associate more bean information with the descriptor?
@@ -324,7 +329,11 @@
             } else {
     
                 elementDescriptor.setUpdater( new MethodUpdater(
updateMethod ) );
-                elementDescriptor.setSingularPropertyType(
updateMethod.getParameterTypes()[0] );
+                
+                // Derived Interface Support
+                if (!elementDescriptor.isSingularPropertyType()) {
+                    elementDescriptor.setSingularPropertyType(
updateMethod.getParameterTypes()[0] );
+                }
                 if ( log.isTraceEnabled() ) {
                     log.trace( "Set custom updater on " +
elementDescriptor);
                 }
@@ -450,8 +459,16 @@
                                     // this may match
a standard collection
or iteration
                                     log.trace("Matching
collection or
iteration");
                                     
-                                    descriptor.setUpdater(
new
MethodUpdater( method ) );
-                                    descriptor.setSingularPropertyType(
types[0] );
+                                    //       Derived
Interface Support
+                                    //descriptor.setUpdater(
new
MethodUpdater( method ) );
+                                    //descriptor.setSingularPropertyType(
types[0] );
+                                    if (descriptor.getUpdater()
== null) {
+                                        descriptor.setUpdater(
new
MethodUpdater( method ) );
+                                    }
+                                    
+                                    if
(!descriptor.isSingularPropertyType()) {
+                                        descriptor.setSingularPropertyType(
types[0] );
+                                    }
                                     
                                     if ( log.isDebugEnabled()
) {
                                         log.debug(
"!! " + method);
cvs server: Diffing src/java/org/apache/commons/betwixt/expression
cvs server: Diffing src/java/org/apache/commons/betwixt/io
Index: src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/
io/AbstractBeanWriter.java,v
retrieving revision 1.26
diff -u -r1.26 AbstractBeanWriter.java
--- src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java        
31 Mar 2004 21:11:52 -0000           1.26
+++ src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java       26
Apr 2004 18:51:54 -0000
@@ -797,6 +797,20 @@
                                     if (object == null)
{
                                         continue;
                                     }
+
+                                    // Derived Interface
Support
+                                    Class beanClass =
object.getClass();
+                                    Class type =
childDescriptor.getSingularPropertyType();
+                                    if (type != null)
{
+                                        if
(!type.isAssignableFrom(beanClass)) {
+                                            if
( log.isDebugEnabled() ) {
+                                               
log.debug("Skipping due to
incompatible singular property type " + object);
+                                               
log.debug(type);
+                                           
}                                                                           
                                                        

+                                            continue;
+                                        }
+                                    }
+                    
                                     writeBean( 
                                             namespaceUri,

                                             localName,

@@ -805,6 +819,19 @@
                                             context
);
                                 }
                             } else {
+                                // Derived Interface Support
+                                Class beanClass = childBean.getClass();
+                                Class type =
childDescriptor.getPropertyType();
+                                if (type != null) {
+                                    if (!type.isAssignableFrom(beanClass))
{
+                                        if ( log.isDebugEnabled()
) {
+                                            log.debug("Skipping
due to
incompatible property type " + childBean);
+                                            log.debug(type);
+                                       
}                                                                           
                                                

+                                                                           
                                           continue;
+                                    }
+                                }
+
                                 writeBean( 
                                             namespaceUri,

                                             localName,

cvs server: Diffing src/java/org/apache/commons/betwixt/io/id
cvs server: Diffing src/java/org/apache/commons/betwixt/io/read
cvs server: Diffing src/java/org/apache/commons/betwixt/registry
cvs server: Diffing src/java/org/apache/commons/betwixt/strategy
cvs server: Diffing src/media
cvs server: Diffing src/resources
cvs server: Diffing src/test
cvs server: Diffing src/test/org
cvs server: Diffing src/test/org/apache
cvs server: Diffing src/test/org/apache/commons
cvs server: Diffing src/test/org/apache/commons/betwixt
cvs server: Diffing src/test/org/apache/commons/betwixt/derived
cvs server: Diffing src/test/org/apache/commons/betwixt/digester
cvs server: Diffing src/test/org/apache/commons/betwixt/dotbetwixt
cvs server: Diffing src/test/org/apache/commons/betwixt/examples
cvs server: Diffing src/test/org/apache/commons/betwixt/examples/rss
cvs server: Diffing src/test/org/apache/commons/betwixt/expression
cvs server: Diffing src/test/org/apache/commons/betwixt/introspection
cvs server: Diffing src/test/org/apache/commons/betwixt/io
cvs server: Diffing src/test/org/apache/commons/betwixt/io/read
cvs server: Diffing src/test/org/apache/commons/betwixt/nowrap
cvs server: Diffing src/test/org/apache/commons/betwixt/recursion
cvs server: Diffing src/test/org/apache/commons/betwixt/registry
cvs server: Diffing src/test/org/apache/commons/betwixt/scarab
cvs server: Diffing src/test/org/apache/commons/betwixt/schema
cvs server: Diffing src/test/org/apache/commons/betwixt/strategy
cvs server: Diffing src/test/org/apache/commons/betwixt/strategy/alt
cvs server: Diffing src/test/org/apache/commons/betwixt/xmlunit
cvs server: Diffing src/test/org/apache/commons/digester
cvs server: Diffing src/test/org/apache/commons/digester/rss
cvs server: Diffing xdocs
cvs server: Diffing xdocs/guide
cvs server: Diffing xdocs/images
cvs server: Diffing xdocs/style
 
***** CVS exited normally with code 1 *****
 
=======org.commons.betwixt.interfaces========
--------------- Belly.betwixt ------------------
<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes='attribute'>
    <hide property='children'/>
    <hide property='parent'/>
    <element name='belly'>
        <addDefaults/>
    </element>
</info>
------------------ Belly.java ------------------
package org.apache.commons.betwixt.interfaces;
 
public class Belly extends Container {
    public Belly() {
        super();
        // TODO Auto-generated constructor stub
    }
}
------------------ Container.java ------------------
package org.apache.commons.betwixt.interfaces;
 
import java.util.HashMap;
 
public class Container implements IContainer {
 
    private String name;
    private String location;
    private String version;
 
    private IContainer parent;
 
    private HashMap children = new HashMap();
 
    /**
     * 
     */
    public Container() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /* (non-Javadoc)
     * @see org.apache.commons.betwixt.interfaces.IContainer#getName()
     */
    public String getName() {
        return name;
    }
 
    /* (non-Javadoc)
     * @see
org.apache.commons.betwixt.interfaces.IContainer#setName(java.lang.String)
     */
    public void setName(String name) {
        this.name = name;
    }
 
    /* (non-Javadoc)
     * @see org.apache.commons.betwixt.interfaces.IContainer#getParent()
     */
    public IContainer getParent() {
        return parent;
    }
 
    /* (non-Javadoc)
     * @see
org.apache.commons.betwixt.interfaces.IContainer#setParent(org.apache.common
s.betwixt.interfaces.IContainer)
     */
    public void setParent(IContainer parent) {
        this.parent = parent;
    }
 
    /* (non-Javadoc)
     * @see org.apache.commons.betwixt.interfaces.IContainer#getChildren()
     */
    public IContainer[] getChildren() {
        IContainer results[] = new IContainer[children.size()];
        return ((IContainer[]) children.values().toArray(results));
    }
 
    /* (non-Javadoc)
     * @see
org.apache.commons.betwixt.interfaces.IContainer#addChild(org.apache.commons
.betwixt.interfaces.IContainer)
     */
    public void addChild(IContainer child) {
        children.put(child.getName(), child);
    }
 
    /* (non-Javadoc)
     * @see org.apache.commons.betwixt.interfaces.IEnv#getVersion()
     */
    public String getVersion() {
        return version;
    }
 
    /* (non-Javadoc)
     * @see
org.apache.commons.betwixt.interfaces.IEnv#setVersion(java.lang.String)
     */
    public void setVersion(String version) {
        this.version = version;
    }
 
    /* (non-Javadoc)
     * @see org.apache.commons.betwixt.interfaces.IService#getLocation()
     */
    public String getLocation() {
        return location;
    }
 
    /* (non-Javadoc)
     * @see
org.apache.commons.betwixt.interfaces.IService#setLocation(java.lang.String)
     */
    public void setLocation(String location) {
        this.location = location;
    }
 
}
------------------ Fridge.betwixt ------------------
<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes='attribute'>
    <hide property='children'/>
    <hide property='parent'/>
    <element name='fridge'>
        <element name='pot' type='org.apache.commons.betwixt.interfaces.Pot'
property='children' updater='addChild'
class='org.apache.commons.betwixt.interfaces.Pot'/>
        <element name='belly'
type='org.apache.commons.betwixt.interfaces.Belly' property='children'
updater='addChild' class='org.apache.commons.betwixt.interfaces.Belly'/>
        <addDefaults/>
    </element>
</info>
------------------ Fridge.java ------------------
package org.apache.commons.betwixt.interfaces;
 
public class Fridge extends Container {
 
            /**
             * 
             */
            public Fridge() {
                        super();
                        // TODO Auto-generated constructor stub
            }
 
}
------------------ IContainer.java ------------------
package org.apache.commons.betwixt.interfaces;
 
public interface IContainer extends IEnv, IService {
    public String getName();
    public void setName(String name);
 
    public IContainer getParent();
    public void setParent(IContainer parent);
 
    public IContainer[] getChildren();
    public void addChild(IContainer child);
}
------------------ IEnv.java ------------------
package org.apache.commons.betwixt.interfaces;
 
public interface IEnv {
    public String getVersion();
    public void setVersion(String version);
}
------------------ IService.java ------------------
package org.apache.commons.betwixt.interfaces;
 
public interface IService {
    public String getLocation();
    public void setLocation(String location);
}
------------------ Pot.betwixt ------------------
<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes='attribute'>
    <hide property='children'/>
    <hide property='parent'/>
    <element name='pot'>
        <addDefaults/>
    </element>
</info>
------------------ Pot.java ------------------
package org.apache.commons.betwixt.interfaces;
 
public class Pot extends Container {
 
    /**
     * 
     */
    public Pot() {
        super();
    }
 
}
------------------ TestContainer.java ------------------
package org.apache.commons.betwixt.interfaces;
 
import java.io.File;
 
import junit.framework.Test;
import junit.framework.TestSuite;
 
import org.apache.commons.betwixt.AbstractTestCase;
import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.io.BeanWriter;
 
 
public class TestContainer extends AbstractTestCase {
    public TestContainer(String testName) {
    super(testName);
    }
 
    public static void main(String[] args) {
        //TestRunner.run(suite());
        try {
            new TestContainer("Junk").testContainer();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
    public static Test suite() {
        return new TestSuite(TestContainer.class);
    }
 
    public void testContainer() throws Exception {
        File config = new File(basedir,
"bin/org/apache/commons/betwixt/interfaces/fridge.xml");
 
        BeanReader reader = new BeanReader();
        reader.registerBeanClass( Fridge.class );
        Fridge fridge = (Fridge) reader.parse(config);
 
        BeanWriter writer = new BeanWriter();
        writer.getXMLIntrospector().setAttributesForPrimitives(true);
        writer.enablePrettyPrint();
        writer.writeXmlDeclaration("<?xml version=\"1.0\"?>");
        writer.write(fridge);
 
    }
}
------------------ fridge.xml ------------------
<?xml version="1.0"?>
<fridge name="north pole" location="earth" version="old">
    <pot name="big guy" location="kitchen" version="4/1/2004"/>
    <belly name="porcupine" location="zoo" version="new born"/>
</fridge>
 


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


Mime
View raw message