question: public static String escapeCharacterToDisplay ( String text ) { Pattern pattern = PLAIN_TEXT_TO_ESCAPE ; Matcher match = pattern . matcher ( text ) ; if ( match . find ( ) ) { StringBuilder out = new StringBuilder ( ) ; int end = 0 ; do { int start = match . start ( ) ; out . append ( text . substring ( end , start ) ) ; end = match . end ( ) ; int c = text . codePointAt ( start ) ; if ( c == ' ' ) { for ( int i = 1 , n = end - start ; i < n ; ++ i ) { out . append ( "&nbsp;" ) ; } out . append ( ' ' ) ; } else if ( c == '\r' || c == '\n' ) { out . append ( "<br>" ) ; } else if ( c == '<' ) { out . append ( "&lt;" ) ; } else if ( c == '>' ) { out . append ( "&gt;" ) ; } else if ( c == '&' ) { out . append ( "&amp;" ) ; } } while ( match . find ( ) ) ; out . append ( text . substring ( end ) ) ; text = out . toString ( ) ; } return text ; }
target:  Escape some special character as HTML escape sequence.