jstrachan    02/05/28 07:17:23

  Modified:    betwixt/src/java/org/apache/commons/betwixt/digester
                        XMLIntrospectorHelper.java
               betwixt/src/java/org/apache/commons/betwixt
                        XMLIntrospector.java
               betwixt/src/test/org/apache/commons/betwixt
                        TestMavenProject.java
  Log:
  Patched code so that betwixt can now parse Maven project files, provided the 
XMLIntrospector is configured with the Maven/Turbine configuration.
  It'd be nice to provide this configuration in some static helper method to do this 
automatically; just need to think of a good name. e.g. 
createTurbineStyleIntrospector() ?
  
  The only thing missing right now is to be able to plugin a type name to element name 
plugin strategy, so that we can default all type-based elements in Maven/Turbine to 
lower case. 
  Right now the output of the Maven project appears as <Project> similarly its 
<Developer> and <Dependency> etc.
  
  Revision  Changes    Path
  1.6       +36 -5     
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/digester/XMLIntrospectorHelper.java
  
  Index: XMLIntrospectorHelper.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/digester/XMLIntrospectorHelper.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- XMLIntrospectorHelper.java        28 May 2002 13:38:26 -0000      1.5
  +++ XMLIntrospectorHelper.java        28 May 2002 14:17:22 -0000      1.6
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/digester/XMLIntrospectorHelper.java,v
 1.5 2002/05/28 13:38:26 jstrachan Exp $
  - * $Revision: 1.5 $
  - * $Date: 2002/05/28 13:38:26 $
  + * $Header: 
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/digester/XMLIntrospectorHelper.java,v
 1.6 2002/05/28 14:17:22 jstrachan Exp $
  + * $Revision: 1.6 $
  + * $Date: 2002/05/28 14:17:22 $
    *
    * ====================================================================
    *
  @@ -57,7 +57,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    * 
  - * $Id: XMLIntrospectorHelper.java,v 1.5 2002/05/28 13:38:26 jstrachan Exp $
  + * $Id: XMLIntrospectorHelper.java,v 1.6 2002/05/28 14:17:22 jstrachan Exp $
    */
   package org.apache.commons.betwixt.digester;
   
  @@ -99,7 +99,7 @@
     * common code shared between the digestor and introspector.</p>
     *
     * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
  -  * @version $Revision: 1.5 $
  +  * @version $Revision: 1.6 $
     */
   public class XMLIntrospectorHelper {
   
  @@ -344,6 +344,19 @@
                                   descriptor.setUpdater( new MethodUpdater( method ) 
);
                                   descriptor.setSingularPropertyType( types[0] );
                               }
  +                            
  +                            if ( introspector.isWrapCollectionsInElement() ) {
  +                                // lets wrap the descriptor in a dummy element
  +                                ElementDescriptor parent = new ElementDescriptor();
  +                                parent.setQualifiedName( 
descriptor.getPropertyName() );
  +                                ElementDescriptor[] children = new 
ElementDescriptor[] {
  +                                    descriptor
  +                                };
  +                                parent.setElementDescriptors( children );
  +                                descriptor.setQualifiedName( propertyName );
  +                                // now lets try swap in the new element descriptor 
above the current one
  +                                swapDescriptor( rootDescriptor, descriptor, parent 
);
  +                            }
                           }
                           else {
                               if ( log.isDebugEnabled() ) {
  @@ -411,5 +424,23 @@
               }
           }
           return null;
  +    }
  +
  +    /**
  +     * Traverse the tree of element descriptors and find the oldValue and swap it 
with the newValue.
  +     * This would be much easier to do if ElementDescriptor supported a parent 
relationship.
  +     */     
  +    protected static void swapDescriptor( ElementDescriptor rootDescriptor, 
ElementDescriptor oldValue, ElementDescriptor newValue ) {
  +        ElementDescriptor[] children = rootDescriptor.getElementDescriptors();
  +        if ( children != null ) {
  +            for ( int i = 0, size = children.length; i < size; i++ ) {
  +                ElementDescriptor child = children[i];
  +                if ( child == oldValue ) {
  +                    children[i] = newValue;
  +                    break;
  +                }
  +                swapDescriptor( child, oldValue, newValue );
  +            }
  +        }
       }
   }
  
  
  
  1.24      +23 -8     
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.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- XMLIntrospector.java      28 May 2002 13:38:26 -0000      1.23
  +++ XMLIntrospector.java      28 May 2002 14:17:22 -0000      1.24
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java,v
 1.23 2002/05/28 13:38:26 jstrachan Exp $
  - * $Revision: 1.23 $
  - * $Date: 2002/05/28 13:38:26 $
  + * $Header: 
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java,v
 1.24 2002/05/28 14:17:22 jstrachan Exp $
  + * $Revision: 1.24 $
  + * $Date: 2002/05/28 14:17:22 $
    *
    * ====================================================================
    *
  @@ -57,7 +57,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    * 
  - * $Id: XMLIntrospector.java,v 1.23 2002/05/28 13:38:26 jstrachan Exp $
  + * $Id: XMLIntrospector.java,v 1.24 2002/05/28 14:17:22 jstrachan Exp $
    */
   package org.apache.commons.betwixt;
   
  @@ -102,7 +102,7 @@
     * Later requests for the same class will return the cached value.</p>
     *
     * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
  -  * @version $Revision: 1.23 $
  +  * @version $Revision: 1.24 $
     */
   public class XMLIntrospector {
   
  @@ -112,15 +112,20 @@
       /** should attributes or elements be used for primitive types */
       private boolean attributesForPrimitives = false;
       
  -    /** Maps classes to <code>XMLBeanInfo</code>'s */
  -    protected HashMap cacheXMLBeanInfos = new HashMap();
  +    /** should we wrap collections in an extra element? */
  +    private boolean wrapCollectionsInElement = false;
       
       /** Is <code>XMLBeanInfo</code> caching enabled? */
       boolean cachingEnabled = true;
       
  +    /** Maps classes to <code>XMLBeanInfo</code>'s */
  +    protected HashMap cacheXMLBeanInfos = new HashMap();
  +    
       /** Digester used to parse the XML descriptor files */
       private XMLBeanInfoDigester digester;
  -    
  +
  +    // pluggable strategies
  +        
       /** The strategy used to detect matching singular and plural properties */
       private PluralStemmer pluralStemmer;
       
  @@ -289,6 +294,16 @@
       /** Set whether attributes (or elements) should be used for primitive types. */
       public void setAttributesForPrimitives(boolean attributesForPrimitives) {
           this.attributesForPrimitives = attributesForPrimitives;
  +    }
  +
  +    /** @return whether we should we wrap collections in an extra element? */
  +    public boolean isWrapCollectionsInElement() {
  +        return wrapCollectionsInElement;
  +    }
  +
  +    /** Sets whether we should we wrap collections in an extra element? */
  +    public void setWrapCollectionsInElement(boolean wrapCollectionsInElement) {
  +        this.wrapCollectionsInElement = wrapCollectionsInElement;
       }
   
       /** 
  
  
  
  1.3       +49 -51    
jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/TestMavenProject.java
  
  Index: TestMavenProject.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/TestMavenProject.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestMavenProject.java     28 May 2002 13:38:26 -0000      1.2
  +++ TestMavenProject.java     28 May 2002 14:17:23 -0000      1.3
  @@ -112,18 +112,62 @@
        * Tests we can parse a project.xml
        */
       public void testParse() throws Exception {
  -        // create a BeanReader
  +        BeanReader reader = createBeanReader();
  +        
  +        Project project = (Project) reader.parse( new FileInputStream( 
"project.xml" ) );
  +
  +        testProject( project );
  +    }
  +    
  +
  +    /**
  +     * Tests we can round trip from the XML -> bean -> XML -> bean.
  +     * Ideally this method should test both Project objects are identical
  +     */
  +    public void testRoundTrip() throws Exception {
  +        BeanReader reader = createBeanReader();
  +        
  +        Project project = (Project) reader.parse( new FileInputStream( 
"project.xml" ) );
  +
  +        // now lets output it to a buffer
  +        StringWriter buffer = new StringWriter();
  +        write( project, buffer );
  +        
  +
  +        // create a new BeanReader
  +        reader = createBeanReader();
  +
  +        // now lets try parse the output sing the BeanReader 
  +        String text = buffer.toString();        
  +        
  +        System.out.println( text );
  +
  +        Project newProject = (Project) reader.parse( new StringReader(text ) );
  +        
  +        // managed to parse it again!
  +        testProject( newProject );
  +        
  +        // #### should now test the old and new Project instances for equality.
  +    }
  +
  +
  +    // Implementation methods
  +    //-------------------------------------------------------------------------    
  +    
  +    protected BeanReader createBeanReader() throws Exception {
           BeanReader reader = new BeanReader();
           
           // set elements for attributes to true
           reader.getXMLIntrospector().setAttributesForPrimitives(false);
  +        // wrap collections in an XML element
  +        reader.getXMLIntrospector().setWrapCollectionsInElement(true);
           
           // #### remove the need to name the element, via a lower-case-naming 
strategy
           reader.registerBeanClass( "project", Project.class );
  -
  -        // now lets try parse the output sing the BeanReader 
  -        Project project = (Project) reader.parse( new FileInputStream( 
"project.xml" ) );
  -
  +        return reader;
  +    }
  +        
  +    protected void testProject(Project project) throws Exception {    
           assertTrue( "Returned null project instance", project != null );        
           assertEquals( "commons-betwixt", project.getName() );
           assertEquals( "commons-betwixt", project.getId() );
  @@ -146,52 +190,6 @@
           
       }        
           
  -/*        
  -    public void testRoundTrip() throws Exception {
  -        // create a BeanReader
  -        BeanReader reader = new BeanReader();
  -        reader.registerBeanClass( Project.class );
  -
  -        // now lets try parse the output sing the BeanReader 
  -        String text = buffer.toString();        
  -        Project project = (Project) reader.parse( new FileReader( "project.xml" ) );
  -
  -
  -        // lets parse the example using the RSSDigester
  -        RSSDigester digester = new RSSDigester();
  -        
  -        InputStream in = new FileInputStream( 
"src/test/org/apache/commons/betwixt/rss-example.xml" );
  -        Object bean = digester.parse( in ); 
  -        in.close();
  -        
  -        // now lets output it to a buffer
  -        StringWriter buffer = new StringWriter();
  -        write( bean, buffer );
  -        
  -
  -        // create a BeanReader
  -        BeanReader reader = new BeanReader();
  -        reader.registerBeanClass( Project.class );
  -
  -        // now lets try parse the output sing the BeanReader 
  -        String text = buffer.toString();        
  -        Project project = (Project) reader.parse( new StringReader(text ) );
  -        
  -        // managed to parse it again!
  -        
  -        // now lets write it to another buffer
  -        buffer = new StringWriter();
  -        write( bean, buffer );
  -        
  -        String text2 = buffer.toString();
  -
  -        // if the two strings are equal then we've done a full round trip
  -        // with the XML staying the same. Though the original source XML
  -        // could well be different
  -        assertEquals( "Round trip value should remain unchanged", text, text2 );
  -    }
  -*/
  -    
       protected void write(Object bean, Writer out) throws Exception {
           BeanWriter writer = new BeanWriter(out);
           writer.getXMLIntrospector().setAttributesForPrimitives(false);
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to