rdonkin 2004/10/31 02:15:22 Modified: betwixt/src/java/org/apache/commons/betwixt XMLIntrospector.java betwixt/src/java/org/apache/commons/betwixt/io BeanRuleSet.java betwixt/src/java/org/apache/commons/betwixt/strategy PropertySuppressionStrategy.java betwixt/src/test/org/apache/commons/betwixt TestCollectives.java betwixt/xdocs tasks.xml Log: Improved support for collections subclasses by adding ability for extra properties to be recognized. Revision Changes Path 1.39 +37 -13 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java Index: XMLIntrospector.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java,v retrieving revision 1.38 retrieving revision 1.39 diff -u -r1.38 -r1.39 --- XMLIntrospector.java 6 Oct 2004 21:38:05 -0000 1.38 +++ XMLIntrospector.java 31 Oct 2004 10:15:21 -0000 1.39 @@ -623,25 +623,35 @@ getLog().trace("Bean is primitive"); elementDescriptor.setTextExpression( StringExpression.getInstance() ); - } else if ( bean.isLoopType() ) { - getLog().trace("Bean is loop"); - ElementDescriptor loopDescriptor = new ElementDescriptor(); - loopDescriptor.setContextExpression( - new IteratorExpression( EmptyExpression.getInstance() ) - ); - if ( bean.isMapType() ) { - loopDescriptor.setQualifiedName( "entry" ); - } - elementDescriptor.setElementDescriptors( new ElementDescriptor[] { loopDescriptor } ); - } else { + getLog().trace("Bean is standard type"); + + boolean isLoopType = bean.isLoopType(); + List elements = new ArrayList(); List attributes = new ArrayList(); List contents = new ArrayList(); - addProperties( bean.getProperties(), elements, attributes, contents ); - + // add bean properties for all collection which are not basic + if ( !( isLoopType && isBasicCollection( bean.getClass() ) ) ) + { + addProperties( bean.getProperties(), elements, attributes, contents ); + } + + // add iterator for collections + if ( isLoopType ) { + getLog().trace("Bean is loop"); + ElementDescriptor loopDescriptor = new ElementDescriptor(); + loopDescriptor.setContextExpression( + new IteratorExpression( EmptyExpression.getInstance() ) + ); + if ( bean.isMapType() ) { + loopDescriptor.setQualifiedName( "entry" ); + } + elements.add( loopDescriptor ); + } + int size = elements.size(); if ( size > 0 ) { ElementDescriptor[] descriptors = new ElementDescriptor[size]; @@ -673,6 +683,20 @@ getLog().trace("Populated descriptor:"); getLog().trace(elementDescriptor); } + } + + /** + * <p>Is the given type a basic collection? + * </p><p> + * This is used to determine whether a collective type + * should be introspected as a bean (in addition to a collection). + * </p> + * @param type <code>Class</code>, not null + * @return + */ + private boolean isBasicCollection( Class type ) + { + return type.getName().startsWith( "java.util" ); } /** 1.22 +2 -2 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanRuleSet.java Index: BeanRuleSet.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanRuleSet.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -r1.21 -r1.22 --- BeanRuleSet.java 4 Oct 2004 22:27:12 -0000 1.21 +++ BeanRuleSet.java 31 Oct 2004 10:15:21 -0000 1.22 @@ -297,7 +297,7 @@ public void body(String namespace, String name, String text) throws Exception { - log.trace("[BRS] Body with text " + text); + if (log.isTraceEnabled()) log.trace("[BRS] Body with text " + text); if (digester.getCount() > 0) { MappingAction action = context.currentMappingAction(); action.body(text, context); 1.3 +9 -1 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/PropertySuppressionStrategy.java Index: PropertySuppressionStrategy.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/PropertySuppressionStrategy.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- PropertySuppressionStrategy.java 6 Oct 2004 21:38:05 -0000 1.2 +++ PropertySuppressionStrategy.java 31 Oct 2004 10:15:21 -0000 1.3 @@ -15,6 +15,8 @@ */ package org.apache.commons.betwixt.strategy; +import java.util.Collection; + /** * Pluggable strategy specifying whether property's should be surpressed. * Implementations can be used to give rules about which properties @@ -25,7 +27,8 @@ /** * Default implementation supresses the class property - * found on every object. + * found on every object. Also, the <code>isEmpty</code> + * property is supressed for implementations of <code>Collection</code>. */ public static final PropertySuppressionStrategy DEFAULT = new PropertySuppressionStrategy() { public boolean suppressProperty(Class clazz, Class propertyType, String propertyName) { @@ -34,6 +37,11 @@ if ( Class.class.equals( propertyType) && "class".equals( propertyName ) ) { result = true; } + // ignore isEmpty for collection subclasses + if ( "empty".equals( propertyName ) && Collection.class.isAssignableFrom( clazz )) { + result = true; + } + return result; } }; 1.3 +49 -1 jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/TestCollectives.java Index: TestCollectives.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/TestCollectives.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- TestCollectives.java 13 Jun 2004 21:32:46 -0000 1.2 +++ TestCollectives.java 31 Oct 2004 10:15:22 -0000 1.3 @@ -21,6 +21,7 @@ import java.io.StringWriter; import java.util.Iterator; +import org.apache.commons.betwixt.expression.IteratorExpression; import org.apache.commons.betwixt.io.BeanReader; import org.apache.commons.betwixt.io.BeanWriter; import org.apache.commons.betwixt.strategy.CapitalizeNameMapper; @@ -98,5 +99,52 @@ } + public void testIntrospectListExtension() throws Exception + { + XMLIntrospector xmlIntrospector = new XMLIntrospector(); + XMLBeanInfo beanInfo = xmlIntrospector.introspect(ArrayListExtender.class); + + ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor(); + ElementDescriptor[] childDescriptors = elementDescriptor.getElementDescriptors(); + assertEquals(2, childDescriptors.length); + assertEquals("another", childDescriptors[0].getPropertyName()); + assertTrue(childDescriptors[1].getContextExpression() instanceof IteratorExpression); + } + public void testWriteListExtension() throws Exception + { + ArrayListExtender bean = new ArrayListExtender("Whatever"); + bean.add(new Long(11)); + bean.add(new Long(12)); + bean.add(new Long(13)); + + StringWriter out = new StringWriter(); + out.write("<?xml version='1.0'?>"); + + BeanWriter writer = new BeanWriter(out); + writer.getBindingConfiguration().setMapIDs( false ); + writer.write(bean); + + String expected = "<?xml version='1.0'?><ArrayListExtender><another>Whatever</another>" + + "<Long>11</Long><Long>12</Long><Long>13</Long></ArrayListExtender>"; + + xmlAssertIsomorphicContent(parseString( expected ), parseString( out )); + } + + + public void testReadListExtension() throws Exception + { + String xml = "<?xml version='1.0'?><ArrayListExtender><another>Whatever</another>" + + "<Long>11</Long><Long>12</Long><Long>13</Long></ArrayListExtender>"; + + StringReader in = new StringReader( xml ); + + BeanReader reader = new BeanReader(); + reader.getBindingConfiguration().setMapIDs( false ); + + reader.registerBeanClass( ArrayListExtender.class ); + ArrayListExtender bean = (ArrayListExtender) reader.parse( in ); + + assertEquals("Whatever", bean.getAnother()); + } } 1.39 +7 -0 jakarta-commons/betwixt/xdocs/tasks.xml Index: tasks.xml =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/xdocs/tasks.xml,v retrieving revision 1.38 retrieving revision 1.39 diff -u -r1.38 -r1.39 --- tasks.xml 4 Oct 2004 22:27:13 -0000 1.38 +++ tasks.xml 31 Oct 2004 10:15:22 -0000 1.39 @@ -199,6 +199,8 @@ <li>All exceptions are now complex</li> <li><strong>PropertySuppressionStrategy</strong> added which allows course grained control of those properties which should be ignored by Betwixt.</li> + <li>Improved support for <strong>java.util collections API implementations</strong>. + Betwixt now recognizes additional properties on custom collection implementations.</li> </ul> </subsection> <subsection name='0.6'> @@ -455,6 +457,11 @@ <li> All exceptions are now complex types. This is now more consistent but the default binding for exceptions in java.lang package have been changed from simple to complex. + </li> + <li> +Properties on collection implementations are now recognized (rather than ignored) +by Betwixt. Please use an appropriate <code>ClassNormalizer</code> for implementations +that need to hide their extra properties. </li> </ul> </subsection>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]