TextUtil { @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("|"); } 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 + ";"; } else { entity = character; } } else { entity = character; } result.append(entity); } if (lastIndex < text.length()) { result.append(text.substring(lastIndex)); } return result.toString(); } private  TextUtil(); @Pure static boolean equalsIgnoreCase(String firstText, String secondText, boolean isNullEmptyEquivalence); @Pure @SuppressWarnings("checkstyle:magicnumber") static String encodeBase26(int number); @Pure @SuppressWarnings("checkstyle:npathcomplexity") static Map<String, Integer> getHtmlToJavaTranslationTable(); @Pure @SuppressWarnings("checkstyle:npathcomplexity") static Map<Character, String> getJavaToHTMLTranslationTable(); @Pure @SuppressWarnings("checkstyle:magicnumber") static String parseHTML(String html); @Pure static String toHTML(String text); @Pure static String cutString(String text, int column); @Pure static String[] cutStringAsArray(String text, int column); static void cutStringAsArray(String text, CutStringCritera critera, List<String> output); @Pure static char getMnemonicChar(String text); @Pure static String removeMnemonicChar(String text); @Pure @SuppressWarnings("checkstyle:npathcomplexity") static Map<Character, String> getAccentTranslationTable(); @Pure static String removeAccents(String text); @Pure static String removeAccents(String text, Map<Character, String> map); @Pure @Inline(value = "textUtil.split('{', '}', $1)", imported = {TextUtil.class}) static String[] splitBrackets(String str); @Pure static String[] split(char leftSeparator, char rightSeparator, String str); @Pure @Inline(value = "textUtil.splitAsList('{', '}', $1)", imported = {TextUtil.class}) static List<String> splitBracketsAsList(String str); @Pure static List<String> splitAsList(char leftSeparator, char rightSeparator, String str); @Pure @Inline(value = "textUtil.splitAsUUIDs('{', '}', $1)", imported = {TextUtil.class}) static List<UUID> splitBracketsAsUUIDs(String str); @Pure static List<UUID> splitAsUUIDs(char leftSeparator, char rightSeparator, String str); @Pure @Inline(value = "TextUtil.join('{', '}', $1)", imported = {TextUtil.class}) static String mergeBrackets(@SuppressWarnings("unchecked") T... strs); @Pure @Inline(value = "TextUtil.join('{', '}', $1)", imported = {TextUtil.class}) static String mergeBrackets(Iterable<?> strs); @Pure static String join(char leftSeparator, char rightSeparator, @SuppressWarnings("unchecked") T... strs); @Pure static String join(char leftSeparator, char rightSeparator, Iterable<?> strs); @Pure @Inline(value = "TextUtil.join($1, null, null, Arrays.asList($2))", imported = {TextUtil.class, Arrays.class}) static String join(String joinText, @SuppressWarnings("unchecked") T... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, boolean... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, byte... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, char... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, short... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, int... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, long... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, float... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, double... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, Iterable<?> elements); @Pure @Inline(value = "TextUtil.join($1, $2, $3, Arrays.asList($4))", imported = {TextUtil.class, Arrays.class}) static String join(String joinText, String prefix, String postfix, @SuppressWarnings("unchecked") T... elements); @Pure static String join(String joinText, String prefix, String postfix, Iterable<?> elements); @Pure static String join(String joinText, String prefix, String postfix, boolean... elements); @Pure static String join(String joinText, String prefix, String postfix, byte... elements); @Pure static String join(String joinText, String prefix, String postfix, char... elements); @Pure static String join(String joinText, String prefix, String postfix, short... elements); @Pure static String join(String joinText, String prefix, String postfix, int... elements); @Pure static String join(String joinText, String prefix, String postfix, long... elements); @Pure static String join(String joinText, String prefix, String postfix, float... elements); @Pure static String join(String joinText, String prefix, String postfix, double... elements); @Pure @Inline(value = "TextUtil.removeAccents($1, $3).equals(TextUtil.removeAccents($2, $3))", imported = {TextUtil.class}) static boolean equalsIgnoreAccents(String s1, String s2, Map<Character, String> map); @Pure @Inline(value = "TextUtil.removeAccents($1, $3).equalsIgnoreCase(TextUtil.removeAccents($2, $3))", imported = {TextUtil.class}) static boolean equalsIgnoreCaseAccents(String s1, String s2, Map<Character, String> map); @Pure static String toLowerCaseWithoutAccent(String text); @Pure static String toLowerCaseWithoutAccent(String text, Map<Character, String> map); @Pure static String toUpperCaseWithoutAccent(String text); @Pure static String toUpperCaseWithoutAccent(String text, Map<Character, String> map); @Pure @SuppressWarnings({"checkstyle:magicnumber", "checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity"}) static String formatTime(double amount, TimeUnit unit); @Pure @Inline(value = "$3.formatHex($1, $2)", imported = {StringEscaper.class}) static String formatHex(int amount, int digits); @Pure static String formatDouble(double amount, int decimalCount); @Pure static String formatFloat(float amount, int decimalCount); static int getLevenshteinDistance(String firstString, String secondString); @Pure static String toJavaString(String text); @Pure static String toJsonString(String text);  }
@Test public void toHTML() { String source, expected, actual; source = Locale.getString("JAVA_HTML_SOURCE"); expected = Locale.getString("JAVA_HTML_EXPECTED"); assertNull(TextUtil.toHTML(null)); actual = TextUtil.toHTML(source); assertEquals(expected, actual); }