question: public String escapeAttributeEntities ( String str ) { StringBuffer buffer ; char ch ; String entity ; buffer = null ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { ch = str . charAt ( i ) ; switch ( ch ) { case '<' : entity = "&lt;" ; break ; case '>' : entity = "&gt;" ; break ; case '\"' : entity = "&quot;" ; break ; case '&' : entity = "&amp;" ; break ; case '\r' : entity = "&#xD;" ; break ; case '\t' : entity = "&#x9;" ; break ; case '\n' : entity = "&#xA;" ; break ; default : entity = null ; break ; } if ( buffer == null ) { if ( entity != null ) { buffer = new StringBuffer ( str . length ( ) + 20 ) ; buffer . append ( str . substring ( 0 , i ) ) ; buffer . append ( entity ) ; } } else { if ( entity == null ) { buffer . append ( ch ) ; } else { buffer . append ( entity ) ; } } } return ( buffer == null ) ? str : buffer . toString ( ) ; }
target:  This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.  It does not convert the single quote (') because it's not necessary as the outputter writes attributes surrounded by double-quotes.