private void readAmpersand ( ) throws IOException { // Read the whole word, up to and including the ; final StringBuilder reference = new StringBuilder ( ) ; int ch ; // Should end in a ';' for ( ch = in . read ( ) ; isAlphaNumeric ( ch ) ; ch = in . read ( ) ) { reference . append ( ( char ) ch ) ; } // Did we tidily finish? if ( ch == ' ' ) { // Yes! Translate it into a &#nnn; code. final String code = XmlEntity . fromNamedReference ( reference ) ; if ( code != null ) { // Keep it. pushBack . append ( code ) ; } else { // invalid entity. Encode the & and append the sequence of chars. pushBack . append ( "&#38;" ) . append ( reference ) . append ( ( char ) ch ) ; } } else { // Did not terminate properly! // Perhaps an & on its own or a malformed reference. // Either way, escape the & pushBack . append ( "&#38;" ) . append ( reference ) . append ( ( char ) ch ) ; } }
Deal with an ampersand in the stream .