@ Pure public static String toHTML ( String text ) { if ( text == null ) { return null ; } final Map < Character , String > transTbl = getJavaToHTMLTranslationTable ( ) ; assert transTbl != null ; if ( transTbl . isEmpty ( ) ) { return text ; } final StringBuilder patternStr = new StringBuilder ( ) ; for ( final Character c : transTbl . keySet ( ) ) { if ( patternStr . length ( ) > 0 ) { patternStr . append ( "|" ) ; //$NON-NLS-1$ } patternStr . append ( Pattern . quote ( c . toString ( ) ) ) ; } final Pattern pattern = Pattern . compile ( patternStr . toString ( ) ) ; final Matcher matcher = pattern . matcher ( text ) ; final StringBuilder result = new StringBuilder ( ) ; String character ; String entity ; int lastIndex = 0 ; while ( matcher . find ( ) ) { final int idx = matcher . start ( ) ; result . append ( text . substring ( lastIndex , idx ) ) ; lastIndex = matcher . end ( ) ; character = matcher . group ( ) ; if ( character . length ( ) == 1 ) { entity = transTbl . get ( Character . valueOf ( character . charAt ( 0 ) ) ) ; if ( entity != null ) { entity = "&" + entity + ";" ; //$NON-NLS-1$ //$NON-NLS-2$ } else { entity = character ; } } else { entity = character ; } result . append ( entity ) ; } if ( lastIndex < text . length ( ) ) { result . append ( text . substring ( lastIndex ) ) ; } return result . toString ( ) ; }
Translate all the special character from the given text to their equivalent HTML entities .