public final String escape ( String in ) { if ( in == null ) return "" ; if ( in . trim ( ) . equals ( "" ) ) return in ; if ( in . trim ( ) . length ( ) == 0 ) return in ; in = in . replaceAll ( "&" , "&amp;" ) ; in = in . replaceAll ( "<" , "&lt;" ) ; in = in . replaceAll ( ">" , "&gt;" ) ; StringBuffer result = new StringBuffer ( ) ; char chars [ ] = in . toCharArray ( ) ; for ( int i = 0 ; i < chars . length ; i ++ ) { if ( ( chars [ i ] >= 127 ) && ( chars [ i ] < 160 ) ) { chars [ i ] = 32 ; result . append ( chars [ i ] ) ; } else if ( chars [ i ] == 160 ) { result . append ( "&#160;" ) ; } else if ( chars [ i ] == 9 ) { result . append ( " " ) ; } else if ( ( chars [ i ] < 32 ) && ( chars [ i ] != 9 ) && ( chars [ i ] != 10 ) && ( chars [ i ] != 13 ) ) { chars [ i ] = 32 ; result . append ( chars [ i ] ) ; } else { result . append ( chars [ i ] ) ; } } in = result . toString ( ) ; return in ; }
Escape the given string to be a valid content for an xml text node content