Bug 9495

Summary: Not all element characters being picked up by characters()
Product: Crimson Reporter: Gillian Roper <garoper>
Component: otherAssignee: Edwin Goei <edwingo>
Status: NEW ---    
Severity: normal    
Priority: P3    
Version: 1.1.3   
Target Milestone: ---   
Hardware: PC   
OS: All   

Description Gillian Roper 2002-05-29 15:35:03 UTC
The error is that not all of the characters in an element are being stored in 
the mContent stringbuffer in the characters method e.g. a method coded like 
this:

public void characters(char[] chars, int start, int len)  
                                       throws SAXException {
	    mContent.append( chars, start, len );
            if ( mContent==null ) {
                // Validation takes care of that except for spaces. Discard.
                return;
            } 
}

The error occurs inconsistently but predominately in large XML files. 

To avoid this error for a description element e.g. <description>What a great 
description</description>: 

1. declare a member variable to collect all of the description's characters and 
one to store the current element's name at the beginning of the DefaultHandler
	protected String mCurrentDescription = null;
	protected String mCurrentElementName = null;

2. add the following code to startElement()
	   if ( localName != null && localName.length() > 0 ) {
		    mCurrentElementName = localName;
	    } else if( qName != null && qName.length() > 0 ) {
		    mCurrentElementName = qName;
	    }
	    if( mCurrentElementName == null ) {
		     trace.error( "startElement() no element to handle" );
		     return;
	    }
	    if ( "description".equals( mCurrentElementName ) ){
		    mCurrentDescription = null;
	    }

3. change the characters() to:
public void characters(char[] chars,
                               int start, int len)  throws SAXException
        {
            String charsToAdd = new String( chars, start, len );
	    
	    if ( mCurrentElementName.equals( "description" ) ) {
		    if ( mCurrentDescription == null ){
			    mCurrentDescription = charsToAdd;
		    } else {
			    mCurrentDescription += charsToAdd;
		    }
	    } 
        }

4. reinitialize mCurrentDescription to null in the endElement method.