jstrachan 01/08/22 12:44:56 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 CustomerBean.java TestBeanWriter.java Added: betwixt/src/java/org/apache/commons/betwixt/expression IteratorExpression.java Removed: betwixt/src/java/org/apache/commons/betwixt/expression Loop.java Log: Almost got iterators working when navigating bean models Revision Changes Path 1.3 +33 -5 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.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- XMLIntrospector.java 2001/08/22 18:30:48 1.2 +++ XMLIntrospector.java 2001/08/22 19:44:55 1.3 @@ -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.2 2001/08/22 18:30:48 jstrachan Exp $ + * $Id: XMLIntrospector.java,v 1.3 2001/08/22 19:44:55 jstrachan Exp $ */ package org.apache.commons.betwixt; @@ -19,15 +19,21 @@ import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; +import java.util.Enumeration; +import java.util.Iterator; import java.util.List; +import java.util.Map; +import org.apache.commons.betwixt.expression.Expression; +import org.apache.commons.betwixt.expression.IteratorExpression; import org.apache.commons.betwixt.expression.MethodExpression; /** <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.2 $ + * @version $Revision: 1.3 $ */ public class XMLIntrospector { @@ -102,7 +108,15 @@ if ( readMethod == null ) { return; } - if ( isPrimitiveType( type ) ) { + Expression loop = createLoopExpression( propertyDescriptor, type ); + if ( loop != null ) { + ElementDescriptor elementDescriptor = new ElementDescriptor(); + elementDescriptor.setContextExpression( loop ); + + nodeDescriptor = elementDescriptor; + elements.add( nodeDescriptor ); + } + else if ( isPrimitiveType( type ) ) { nodeDescriptor = new AttributeDescriptor(); nodeDescriptor.setTextExpression( new MethodExpression( readMethod ) ); attributes.add( nodeDescriptor ); @@ -116,8 +130,7 @@ elementDescriptor.setAttributeDescriptors( childDescriptor.getAttributeDescriptors() ); elementDescriptor.setContextExpression( new MethodExpression( readMethod ) ); - nodeDescriptor = elementDescriptor; - + nodeDescriptor = elementDescriptor; elements.add( nodeDescriptor ); } nodeDescriptor.setLocalName( propertyDescriptor.getName() ); @@ -134,6 +147,21 @@ return answer; } + /** Creates a new Expression for loop based properties such as Arrays, + * Collections, Enumerations, Arrays etc. + */ + protected Expression createLoopExpression( PropertyDescriptor propertyDescriptor, Class type ) { + if ( type.isArray() + || type.isAssignableFrom( Map.class ) + || type.isAssignableFrom( Collection.class ) + || type.isAssignableFrom( Enumeration.class ) + || type.isAssignableFrom( Iterator.class ) + ) { + return new IteratorExpression( new MethodExpression( propertyDescriptor.getReadMethod() ) ); + } + return null; + } + /** Returns true for primitive types */ protected boolean isPrimitiveType(Class type) { return type.getName().startsWith( "java.lang." ) || type.isAssignableFrom( Date.class ); 1.1 jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/IteratorExpression.java Index: IteratorExpression.java =================================================================== package org.apache.commons.betwixt.expression; import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.Iterator; import java.util.Map; import org.apache.commons.collections.ArrayIterator; import org.apache.commons.collections.EnumerationIterator; import org.apache.commons.collections.SingletonIterator; /** <p><code>IteratorExpression</code> returns an iterator over the current context.</p> * * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a> * @version $Revision: 1.1 $ */ public class IteratorExpression implements Expression { private Expression expression; public IteratorExpression(Expression expression) { this.expression = expression; } /** Returns an interator over the current context */ public Object evaluate(Context context) { Object value = expression.evaluate( context ); //System.out.println( "IeratorExpression evaluating: " + value ); if ( value instanceof Iterator ) { return (Iterator) value; } else if ( value instanceof Collection ) { Collection collection = (Collection) value; return collection.iterator(); } else if ( value instanceof Map ) { Map map = (Map) value; return map.entrySet().iterator(); } else if ( value instanceof Enumeration ) { return new EnumerationIterator( (Enumeration) value ); } else if ( value != null ) { Class type = value.getClass(); if ( type.isArray() ) { /* Object[] array = (Object[]) value; return new ArrayIterator( array ); */ } } return Collections.EMPTY_LIST.iterator(); } } 1.3 +16 -5 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.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- BeanWriter.java 2001/08/22 18:30:48 1.2 +++ BeanWriter.java 2001/08/22 19:44:55 1.3 @@ -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.2 2001/08/22 18:30:48 jstrachan Exp $ + * $Id: BeanWriter.java,v 1.3 2001/08/22 19:44:55 jstrachan Exp $ */ package org.apache.commons.betwixt.io; @@ -15,6 +15,7 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; +import java.util.Iterator; import org.apache.commons.betwixt.AttributeDescriptor; import org.apache.commons.betwixt.ElementDescriptor; @@ -26,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.2 $ + * @version $Revision: 1.3 $ */ public class BeanWriter { @@ -66,6 +67,7 @@ write( elementDescriptor, context ); } } + if ( autoFlush ) { writer.flush(); } @@ -101,7 +103,7 @@ //------------------------------------------------------------------------- /** Writes the given element */ - protected void write( ElementDescriptor elementDescriptor, Context context ) throws IOException { + protected void write( ElementDescriptor elementDescriptor, Context context ) throws IOException, IntrospectionException { String qualifiedName = elementDescriptor.getQualifiedName(); writeIndent(); writer.write( "<" ); @@ -122,7 +124,7 @@ * * @return true if some content was written */ - protected boolean writeContent( ElementDescriptor elementDescriptor, Context context ) throws IOException { + protected boolean writeContent( ElementDescriptor elementDescriptor, Context context ) throws IOException, IntrospectionException { boolean answer = false; ElementDescriptor[] childDescriptors = elementDescriptor.getElementDescriptors(); if ( childDescriptors != null && childDescriptors.length > 0 ) { @@ -137,8 +139,17 @@ if ( childBean == null ) { // XXXX: should we handle nulls better continue; + } + if ( childBean instanceof Iterator ) { + for ( Iterator iter = (Iterator) childBean; iter.hasNext(); ) { + write( iter.next() ); + } + continue; + } + else { + childContext = context.newContext( childBean ); } - childContext = context.newContext( childBean ); + } writePrintln(); write( childDescriptor, childContext ); 1.3 +4 -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.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- CustomerBean.java 2001/08/22 18:30:48 1.2 +++ CustomerBean.java 2001/08/22 19:44:56 1.3 @@ -5,7 +5,7 @@ * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. * - * $Id: CustomerBean.java,v 1.2 2001/08/22 18:30:48 jstrachan Exp $ + * $Id: CustomerBean.java,v 1.3 2001/08/22 19:44:56 jstrachan Exp $ */ package org.apache.commons.betwixt; @@ -19,7 +19,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.2 $ + * @version $Revision: 1.3 $ */ public class CustomerBean implements Serializable { @@ -48,6 +48,7 @@ return address; } +/* public Map getProjectMap() { return projectMap; } @@ -65,6 +66,7 @@ } return new IteratorEnumeration( projectMap.values().iterator() ); } +*/ public void setID(String id) { this.id = id; 1.3 +5 -2 jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/TestBeanWriter.java Index: TestBeanWriter.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/TestBeanWriter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- TestBeanWriter.java 2001/08/22 18:30:48 1.2 +++ TestBeanWriter.java 2001/08/22 19:44:56 1.3 @@ -5,7 +5,7 @@ * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. * - * $Id: TestBeanWriter.java,v 1.2 2001/08/22 18:30:48 jstrachan Exp $ + * $Id: TestBeanWriter.java,v 1.3 2001/08/22 19:44:56 jstrachan Exp $ */ package org.apache.commons.betwixt; @@ -19,7 +19,7 @@ /** Test harness for the BeanWriter * * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a> - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ */ public class TestBeanWriter extends AbstractTestCase { @@ -37,11 +37,14 @@ Object bean = createBean(); +/* writer.write( bean ); String text = buffer.toString(); System.out.println( "Found: " + text ); +*/ + System.out.println( "Now trying pretty print" ); writer = new BeanWriter();