static public String entityEscape ( String s , String wrt ) { if ( wrt == null ) wrt = ENTITYESCAPES ; StringBuilder escaped = new StringBuilder ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char c = s . charAt ( i ) ; int index = wrt . indexOf ( c ) ; if ( index < 0 ) escaped . append ( c ) ; else switch ( c ) { case ' ' : escaped . append ( ' ' + ENTITY_AMP + ' ' ) ; break ; case ' ' : escaped . append ( ' ' + ENTITY_LT + ' ' ) ; break ; case ' ' : escaped . append ( ' ' + ENTITY_GT + ' ' ) ; break ; case ' ' : escaped . append ( ' ' + ENTITY_QUOT + ' ' ) ; break ; case ' ' : escaped . append ( ' ' + ENTITY_APOS + ' ' ) ; break ; case ' ' : case ' ' : case ' ' : escaped . append ( c ) ; // These are the only legal control chars break ; case ' ' : // What to do about nul? currrently we suppress it break ; default : if ( c >= ' ' ) escaped . append ( c ) ; break ; } } return escaped . toString ( ) ; }
Escape selected characters in a string using XML entities