commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sweetland, Jesse" <JSweetl...@talk.com>
Subject RE: [PATCH] Betwixt 0.7 - Suppress empty collection tags
Date Wed, 20 Sep 2006 15:07:35 GMT
The attachment didn't seem to come through correctly.  Here is it is inline:

*** AbstractBeanWriter.java
--- AbstractBeanWriter.java.old
***************
*** 1075,1138 ****
              return false;
          }
          
!         // Collective descriptor types should be considered empty if they
!         // they have no elements.  This will suppress marshalling of
!         // wrapper elements that enclose empty collections.  Consider the
!         // following .betwixt mapping:
!         // 
!         // <?xml version="1.0"?>
!         // <info>
!         //  <element name="foo">
!         //   <element name="bars">
!         //    <element name="bar" property="bars"/>
!         //   </element>
!         //   <addDefaults/>
!         //  </element>
!         // </info>
!         //
!         // A descriptor is ignored if it is empty.  A descriptor is
!         // considered empty if it is a simple type with a null or empty
!         // string value, or if it is a complex type whose components are
!         // are empty.  In order to suppress the rendering of the <bars>
!         // wrapper element, the nested collection must be considered to be
!         // empty.  To do so, we need to cast the expression value to an
!         // Iterator and see if the Iterator has a next value.  If we
consider
!         // all collections to be non-empty, then we will always end up
with
!         // an empty <bars/> tag, which may not be desirable.
!         if(descriptor.isCollective()) {
!             Expression expression = descriptor.getContextExpression();
!             Object value = (expression == null) ? null :
expression.evaluate(context);
!             if(value == null) {
!                 log.trace("Loop type is empty because its value is null");

!                 return true;
!             } else if(value instanceof Iterator) {
!                 if(((Iterator)value).hasNext()) {
!                     log.trace("Loop type is not empty because it has at
least one element");
!                     return false;
!                 } else {
!                     log.trace("Loop type is empty because it has no
elements");
!                     return true;
!                 }
!             }
!             // If the element is collective then the value should be null
or an
!             // Iterator.  I'm not sure what to do if it's not an iterator.
!             // I guess we can default to the original behavior, which is
to
!             // treat a collective descriptor as non-empty, although
perhaps a
!             // warning should be issue here.
!             return false;
!         } else {
              Expression expression = descriptor.getTextExpression();
!             Object value = (expression == null) ? null :
expression.evaluate(context);
              String text = convertToString( value, descriptor, context );
!             if((text != null) && (text.length() > 0)) {
                  log.trace( "Element has body text which isn't empty." );
                  return false;
              }
          }
          
          // now test child elements
          // an element is empty if it has no non-empty child elements
--- 1075,1097 ----
              return false;
          }
          
!         // an element is not empty if it has a non-empty body
          Expression expression = descriptor.getTextExpression();
!         if ( expression != null ) {
!             Object value = expression.evaluate( context );
              String text = convertToString( value, descriptor, context );
!             if ( text != null && text.length() > 0 ) {
                  log.trace( "Element has body text which isn't empty." );
                  return false;
              }
          }
          
+         // always write out loops - even when they have no elements
+         if ( descriptor.isCollective() ) {
+             log.trace("Loop type so not empty.");
+             return false;
+         }
+         
          // now test child elements
          // an element is empty if it has no non-empty child elements
          if ( descriptor.hasChildren() ) {





-----Original Message-----
From: Sweetland, Jesse [mailto:JSweetland@talk.com] 
Sent: Wednesday, September 20, 2006 8:53 AM
To: 'commons-dev@jakarta.apache.org'
Subject: [PATCH] Betwixt 0.7 - Suppress empty collection tags 

Attached is a patch that modifies the isEmptyElement method of
AbstractBeanWriter to consider collective element descriptors as empty if
the collection properties they reference have no elements.  This suppresses
the output of empty wrapper elements.  For example, consider the following
.betwixt file for class Foo:
<info> 
  <element name="foo"> 
    <element name="bars"> 
      <element name="bar" property="bars"/> 
    </element> 
    <addDefaults/> 
  </element> 
</info> 
Under Betwixt 0.7, an empty <bars/> element is written even if Foo.getBars()
returns a null or empty collection.  This patch determines that the
collection descriptor for the <bar> element is empty, so all of the children
elements of the <bars> wrapper element descriptor are empty, so the <bars>
element descriptor itself is empty, and is therefore ignored.
  

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message