rdonkin     2004/08/23 12:35:15

  Modified:    betwixt/src/java/org/apache/commons/betwixt/io
                        AbstractBeanWriter.java
  Log:
  Added strategy for attribute suppression. This should (belatedly) give a solution 
for issue #24659.
  
  Revision  Changes    Path
  1.30      +38 -13    
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java
  
  Index: AbstractBeanWriter.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- AbstractBeanWriter.java   22 Aug 2004 16:46:40 -0000      1.29
  +++ AbstractBeanWriter.java   23 Aug 2004 19:35:14 -0000      1.30
  @@ -1015,7 +1015,10 @@
           private AttributeDescriptor[] attributes;
           /** Context to be evaluated when finding values */
           private Context context;
  -
  +        /** Cached attribute values */
  +        private String[] values;
  +        /** The number of unsuppressed attributes */
  +        private int length;
           
           
           /** 
  @@ -1025,8 +1028,36 @@
            * @param context evaluate against this context
            */
           ElementAttributes( ElementDescriptor descriptor, Context context ) {
  -            attributes = descriptor.getAttributeDescriptors();
               this.context = context;
  +            init(descriptor.getAttributeDescriptors());
  +        }
  +        
  +        private void init(AttributeDescriptor[] baseAttributes) {
  +            attributes = new AttributeDescriptor[baseAttributes.length];
  +            values = new String[baseAttributes.length];
  +            int index = 0;
  +            for (int i=0, size=baseAttributes.length; i<size; i++) {
  +                AttributeDescriptor baseAttribute = baseAttributes[i];
  +                String attributeValue = valueAttribute(baseAttribute);
  +                if (attributeValue != null 
  +                        && !context.getValueSuppressionStrategy()
  +                                     .suppressAttribute(baseAttribute, 
attributeValue)) {
  +                    values[index] = attributeValue;
  +                    attributes[index] = baseAttribute;
  +                    index++;
  +                }
  +            }
  +            length = index;
  +        }
  +        
  +        private String valueAttribute(AttributeDescriptor attribute) {
  +            Expression expression = attribute.getTextExpression();
  +            if ( expression != null ) {
  +                Object value = expression.evaluate( context );
  +                return convertToString( value, attribute, context );
  +            }
  +            
  +            return "";
           }
           
           /**
  @@ -1072,7 +1103,7 @@
            * @return the number of attributes in this list
            */
           public int getLength() {
  -            return attributes.length;
  +            return length;
           }
           
           /** 
  @@ -1159,14 +1190,8 @@
            * @todo add value caching
            */
           public String getValue( int index ) {
  -            if ( indexInRange( index )) {
  -                Expression expression = attributes[index].getTextExpression();
  -                if ( expression != null ) {
  -                    Object value = expression.evaluate( context );
  -                    return convertToString( value, attributes[index], context );
  -                }
  -                
  -                return "";
  +            if ( indexInRange( index ) ) {
  +                return values[index];
               }
               return null;
           }
  @@ -1203,7 +1228,7 @@
            * @return true if the index with within the range of the attribute list
            */
           private boolean indexInRange( int index ) {
  -            return ( index >= 0 && index < attributes.length );
  +            return ( index >= 0 && index < getLength() );
           }
       }
       
  
  
  

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

Reply via email to