jstrachan    01/08/23 08:03:07

  Modified:    betwixt/src/java/org/apache/commons/betwixt
                        XMLIntrospector.java
               betwixt/src/java/org/apache/commons/betwixt/io
                        BeanWriter.java
               betwixt/src/test/org/apache/commons/betwixt
                        AbstractTestCase.java CustomerBean.java
  Log:
  Tidied up the default mapping
  
  Revision  Changes    Path
  1.6       +36 -11    
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java
  
  Index: XMLIntrospector.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- XMLIntrospector.java      2001/08/23 14:25:57     1.5
  +++ XMLIntrospector.java      2001/08/23 15:03:07     1.6
  @@ -5,7 +5,7 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: XMLIntrospector.java,v 1.5 2001/08/23 14:25:57 jstrachan Exp $
  + * $Id: XMLIntrospector.java,v 1.6 2001/08/23 15:03:07 jstrachan Exp $
    */
   package org.apache.commons.betwixt;
   
  @@ -35,12 +35,12 @@
   /** <p><code>XMLIntrospector</code> an introspector of beans to create a 
XMLBeanInfo instance.</p>
     *
     * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
  -  * @version $Revision: 1.5 $
  +  * @version $Revision: 1.6 $
     */
   public class XMLIntrospector {
   
  -    /** Should methods inherited from {@link Object} be shown? */
  -    private boolean filterObject = true;
  +    /** should attributes or elements be used for primitive types */
  +    private boolean attributesForPrimitives;
   
       public XMLIntrospector() {
       }
  @@ -72,8 +72,16 @@
               loopDescriptor.setContextExpression(
                   new IteratorExpression( EmptyExpression.getInstance() )
               );
  -            
  +            if ( Map.class.isAssignableFrom( beanType ) ) {
  +                loopDescriptor.setQualifiedName( "entry" );
  +            }
               elementDescriptor.setElementDescriptors( new ElementDescriptor[] { 
loopDescriptor } );
  +            
  +/*            
  +            elementDescriptor.setContextExpression(
  +                new IteratorExpression( EmptyExpression.getInstance() )
  +            );
  +*/
           }
           else {
               ArrayList elements = new ArrayList();
  @@ -106,7 +114,17 @@
           answer.setElementDescriptor( elementDescriptor );        
           return answer;
       }
  +    
  +    /** Should attributes be used for primitive types or elements.
  +     */
  +    public boolean isAttributesForPrimitives() {
  +        return attributesForPrimitives;
  +    }
   
  +    public void setAttributesForPrimitives(boolean attributesForPrimitives) {
  +        this.attributesForPrimitives = attributesForPrimitives;
  +    }
  +
       
       // Implementation methods
       //-------------------------------------------------------------------------    
  @@ -127,27 +145,34 @@
               return;
           }
           if ( isPrimitiveType( type ) ) {
  -            nodeDescriptor = new AttributeDescriptor();
  +            if ( isAttributesForPrimitives() ) {
  +                nodeDescriptor = new AttributeDescriptor();
  +                attributes.add( nodeDescriptor );
  +            }
  +            else {
  +                nodeDescriptor = new ElementDescriptor();
  +                elements.add( nodeDescriptor );
  +            }
               nodeDescriptor.setTextExpression( new MethodExpression( readMethod ) );
  -            attributes.add( nodeDescriptor );
           }
           else if ( isLoopType( type ) ) {
               ElementDescriptor loopDescriptor = new ElementDescriptor();
               loopDescriptor.setContextExpression(
                   new IteratorExpression( new MethodExpression( readMethod ) )
               );
  +            if ( Map.class.isAssignableFrom( type ) ) {
  +                loopDescriptor.setQualifiedName( "entry" );
  +            }
               
               ElementDescriptor elementDescriptor = new ElementDescriptor();
               elementDescriptor.setElementDescriptors( new ElementDescriptor[] { 
loopDescriptor } );
  +            
               nodeDescriptor = elementDescriptor;            
               elements.add( nodeDescriptor );
           }
           else {
  -            ElementDescriptor childDescriptor = new ElementDescriptor();
  -            childDescriptor.setContextExpression( new MethodExpression( readMethod 
) );
  -            
               ElementDescriptor elementDescriptor = new ElementDescriptor();
  -            elementDescriptor.setElementDescriptors( new ElementDescriptor[] { 
childDescriptor } );
  +            elementDescriptor.setContextExpression( new MethodExpression( 
readMethod ) );
               
               nodeDescriptor = elementDescriptor;            
               elements.add( nodeDescriptor );
  
  
  
  1.6       +21 -8     
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java
  
  Index: BeanWriter.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- BeanWriter.java   2001/08/23 14:25:57     1.5
  +++ BeanWriter.java   2001/08/23 15:03:07     1.6
  @@ -5,7 +5,7 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: BeanWriter.java,v 1.5 2001/08/23 14:25:57 jstrachan Exp $
  + * $Id: BeanWriter.java,v 1.6 2001/08/23 15:03:07 jstrachan Exp $
    */
   package org.apache.commons.betwixt.io;
   
  @@ -27,7 +27,7 @@
   /** <p><code>BeanWriter</code> outputs a bean as XML.</p>
     *
     * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
  -  * @version $Revision: 1.5 $
  +  * @version $Revision: 1.6 $
     */
   public class BeanWriter {
   
  @@ -64,7 +64,7 @@
               ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor();
               if ( elementDescriptor != null ) {
                   Context context = new Context( bean );
  -                write( elementDescriptor, context );
  +                write( elementDescriptor.getQualifiedName(), elementDescriptor, 
context );
               }
           }
           
  @@ -102,9 +102,21 @@
       // Implementation methods
       //-------------------------------------------------------------------------    
       
  +    public void write(String qualifiedName, Object bean) throws IOException, 
IntrospectionException  {
  +        XMLBeanInfo beanInfo = introspector.introspect( bean );
  +        if ( beanInfo != null ) {
  +            ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor();
  +            if ( elementDescriptor != null ) {
  +                Context context = new Context( bean );
  +                if ( qualifiedName == null ) {
  +                    qualifiedName = elementDescriptor.getQualifiedName();
  +                }
  +                write( qualifiedName, elementDescriptor, context );
  +            }
  +        }
  +    }
       /** Writes the given element */
  -    protected void write( ElementDescriptor elementDescriptor, Context context ) 
throws IOException, IntrospectionException {
  -        String qualifiedName = elementDescriptor.getQualifiedName();
  +    protected void write( String qualifiedName, ElementDescriptor 
elementDescriptor, Context context ) throws IOException, IntrospectionException {
           writePrintln();
           writeIndent();
           writer.write( "<" );
  @@ -138,19 +150,20 @@
                   if ( childExpression != null ) {
                       Object childBean = childExpression.evaluate( context );
                       if ( childBean != null ) {
  +                        String qualifiedName = childDescriptor.getQualifiedName();
                           // XXXX: should we handle nulls better
                           if ( childBean instanceof Iterator ) {
                               for ( Iterator iter = (Iterator) childBean; 
iter.hasNext(); ) {
  -                                write( iter.next() );
  +                                write( qualifiedName, iter.next() );
                               }
                           }
                           else {
  -                            write( childBean );
  +                            write( qualifiedName, childBean );
                           }
                       }                    
                   }
                   else {
  -                    write( childDescriptor, childContext );
  +                    write( childDescriptor.getQualifiedName(), childDescriptor, 
childContext );
                   }
               }
               --indentLevel;
  
  
  
  1.3       +5 -2      
jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/AbstractTestCase.java
  
  Index: AbstractTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/AbstractTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractTestCase.java     2001/08/23 14:25:58     1.2
  +++ AbstractTestCase.java     2001/08/23 15:03:07     1.3
  @@ -5,7 +5,7 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: AbstractTestCase.java,v 1.2 2001/08/23 14:25:58 jstrachan Exp $
  + * $Id: AbstractTestCase.java,v 1.3 2001/08/23 15:03:07 jstrachan Exp $
    */
   package org.apache.commons.betwixt;
   
  @@ -21,7 +21,7 @@
   /** Abstract base class for test cases.
     *
     * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
  -  * @version $Revision: 1.2 $
  +  * @version $Revision: 1.3 $
     */
   public abstract class AbstractTestCase extends TestCase {
       
  @@ -35,6 +35,9 @@
           bean.setName( "James" );
           bean.setEmails( new String[] { "[EMAIL PROTECTED]", 
"[EMAIL PROTECTED]" } );
           bean.setNumbers( new int[] { 3, 4, 5 } );
  +        bean.setLocation(0, "Highbury Barn" );
  +        bean.setLocation(1, "Monument" );
  +        bean.setLocation(2, "Leeds" );
           
           Map projects = new HashMap();
           projects.put( "dom4j", "http://dom4j.org"; );
  
  
  
  1.6       +21 -2     
jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/CustomerBean.java
  
  Index: CustomerBean.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/CustomerBean.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- CustomerBean.java 2001/08/23 14:25:58     1.5
  +++ CustomerBean.java 2001/08/23 15:03:07     1.6
  @@ -5,13 +5,15 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: CustomerBean.java,v 1.5 2001/08/23 14:25:58 jstrachan Exp $
  + * $Id: CustomerBean.java,v 1.6 2001/08/23 15:03:07 jstrachan Exp $
    */
   package org.apache.commons.betwixt;
   
   import java.io.Serializable;
  +import java.util.ArrayList;
   import java.util.Enumeration;
   import java.util.Iterator;
  +import java.util.List;
   import java.util.Map;
   
   import org.apache.commons.collections.IteratorEnumeration;
  @@ -19,7 +21,7 @@
   /** <p><code>CustomerBean</code> is a sample bean for use by the test cases.</p>
     *
     * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
  -  * @version $Revision: 1.5 $
  +  * @version $Revision: 1.6 $
     */
   public class CustomerBean implements Serializable {
   
  @@ -29,6 +31,7 @@
       private int[] numbers;
       private AddressBean address;
       private Map projectMap;
  +    private List locations = new ArrayList();
       
       public CustomerBean() {
       }
  @@ -71,6 +74,11 @@
           return new IteratorEnumeration( projectMap.values().iterator() );
       }
       
  +    /** An indexed property */
  +    public String getLocation(int index) {
  +        return (String) locations.get(index);
  +    }
  +    
       public void setID(String id) {
           this.id = id;
       }
  @@ -94,4 +102,15 @@
       public void setProjectMap(Map projectMap) {
           this.projectMap = projectMap;
       }
  +    
  +    /** An indexed property */
  +    public void setLocation(int index, String location) {
  +        if ( index == locations.size() ) {
  +            locations.add( location );
  +        }
  +        else {
  +            locations.set(index, location);
  +        }
  +    }
  +    
   }
  
  
  

Reply via email to