public static String htmlentities(final String str){
  if (str == null) {
    return "";
  }
  if (htmlentities_map.isEmpty()) {
    initializeEntitiesTables();
  }
  final StringBuilder buf=new StringBuilder();
  for (int i=0; i < str.length(); ++i) {
    final char ch=str.charAt(i);
    final String entity=htmlentities_map.get(new Integer(ch));
    if (entity == null) {
      if (ch > 128) {
        buf.append("&#" + (int)ch + ";");
      }
 else {
        buf.append(ch);
      }
    }
 else {
      buf.append(entity);
    }
  }
  return buf.toString();
}

public static String htmlentities ( final String str ) { if ( str == null ) { return "" ; } if ( htmlentities_map . isEmpty ( ) ) { initializeEntitiesTables ( ) ; } final StringBuilder buf = new StringBuilder ( ) ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { final char ch = str . charAt ( i ) ; final String entity = htmlentities_map . get ( new Integer ( ch ) ) ; if ( entity == null ) { if ( ch > 128 ) { buf . append ( "&#" + ( int ) ch + ";" ) ; } else { buf . append ( ch ) ; } } else { buf . append ( entity ) ; } } return buf . toString ( ) ; }
convert special and extended characters into html entitities .
