private void doUnescape ( StringBuilder sb , String str , int firstAmp ) { sb . append ( str , 0 , firstAmp ) ; int len = str . length ( ) ; for ( int i = firstAmp ; i < len ; i ++ ) { char c = str . charAt ( i ) ; if ( c == ' ' ) { int nextIdx = i + 1 ; int semiColonIdx = str . indexOf ( ' ' , nextIdx ) ; if ( semiColonIdx == - 1 ) { sb . append ( c ) ; continue ; } int amphersandIdx = str . indexOf ( ' ' , i + 1 ) ; if ( amphersandIdx != - 1 && amphersandIdx < semiColonIdx ) { // Then the text looks like &...&...; sb . append ( c ) ; continue ; } String entityContent = str . substring ( nextIdx , semiColonIdx ) ; int entityValue = - 1 ; int entityContentLen = entityContent . length ( ) ; if ( entityContentLen > 0 ) { if ( entityContent . charAt ( 0 ) == ' ' ) { // escaped value content is an integer (decimal or // hexidecimal) if ( entityContentLen > 1 ) { char isHexChar = entityContent . charAt ( 1 ) ; try { switch ( isHexChar ) { case ' ' : case ' ' : { entityValue = Integer . parseInt ( entityContent . substring ( 2 ) , 16 ) ; break ; } default : { entityValue = Integer . parseInt ( entityContent . substring ( 1 ) , 10 ) ; } } if ( entityValue > 0xFFFF ) { entityValue = - 1 ; } } catch ( NumberFormatException e ) { entityValue = - 1 ; } } } else { // escaped value content is an entity name entityValue = this . entityValue ( entityContent ) ; } } if ( entityValue == - 1 ) { sb . append ( ' ' ) ; sb . append ( entityContent ) ; sb . append ( ' ' ) ; } else { sb . append ( Character . toChars ( entityValue ) ) ; } i = semiColonIdx ; // move index up to the semi-colon } else { sb . append ( c ) ; } } }
Underlying unescape method that allows the optimisation of not starting from the 0 index again .