@Test public void testDirtyStrings() { assertEquals("&amp;", escapeTextChar("&")); assertEquals("&amp; ", escapeTextChar("& ")); assertEquals(" &amp;", escapeTextChar(" &")); assertEquals(" &amp; ", escapeTextChar(" & ")); assertEquals("&amp;&amp;", escapeTextChar("&&")); assertEquals("A&amp;B", escapeTextChar("A&B")); assertEquals("A&amp;B&amp;", escapeTextChar("A&B&")); assertEquals("&amp;A&amp;", escapeTextChar("&A&")); assertEquals("&amp;&lt;&gt;", escapeTextChar("&<>")); }
public static String escapeTextChar(String text) { int s = 0; StringBuilder sb = null; for (int i = 0; i < text.length(); ++i) { char c = text.charAt(i); final String repl = escapeTextChar(c); if (repl != null) { if (sb == null) { sb = new StringBuilder(text.substring(0, i)); } else if (s != i) { sb.append(text, s, i); } sb.append(repl); s = i + 1; } } if (sb == null) { return text; } else if (s < text.length()) { sb.append(text, s, text.length()); } return sb.toString(); }
XmlUtils { public static String escapeTextChar(String text) { int s = 0; StringBuilder sb = null; for (int i = 0; i < text.length(); ++i) { char c = text.charAt(i); final String repl = escapeTextChar(c); if (repl != null) { if (sb == null) { sb = new StringBuilder(text.substring(0, i)); } else if (s != i) { sb.append(text, s, i); } sb.append(repl); s = i + 1; } } if (sb == null) { return text; } else if (s < text.length()) { sb.append(text, s, text.length()); } return sb.toString(); } }
XmlUtils { public static String escapeTextChar(String text) { int s = 0; StringBuilder sb = null; for (int i = 0; i < text.length(); ++i) { char c = text.charAt(i); final String repl = escapeTextChar(c); if (repl != null) { if (sb == null) { sb = new StringBuilder(text.substring(0, i)); } else if (s != i) { sb.append(text, s, i); } sb.append(repl); s = i + 1; } } if (sb == null) { return text; } else if (s < text.length()) { sb.append(text, s, text.length()); } return sb.toString(); }  }
XmlUtils { public static String escapeTextChar(String text) { int s = 0; StringBuilder sb = null; for (int i = 0; i < text.length(); ++i) { char c = text.charAt(i); final String repl = escapeTextChar(c); if (repl != null) { if (sb == null) { sb = new StringBuilder(text.substring(0, i)); } else if (s != i) { sb.append(text, s, i); } sb.append(repl); s = i + 1; } } if (sb == null) { return text; } else if (s < text.length()) { sb.append(text, s, text.length()); } return sb.toString(); }  static String escapeTextChar(String text); static String escapeTextChar(char c); }
XmlUtils { public static String escapeTextChar(String text) { int s = 0; StringBuilder sb = null; for (int i = 0; i < text.length(); ++i) { char c = text.charAt(i); final String repl = escapeTextChar(c); if (repl != null) { if (sb == null) { sb = new StringBuilder(text.substring(0, i)); } else if (s != i) { sb.append(text, s, i); } sb.append(repl); s = i + 1; } } if (sb == null) { return text; } else if (s < text.length()) { sb.append(text, s, text.length()); } return sb.toString(); }  static String escapeTextChar(String text); static String escapeTextChar(char c);  }