@Test public void testEscapeXml() { assertEquals("&lt;some text&gt;", TextUtil.escapeXml("<some text>")); assertEquals("&lt;some&amp;text&quot;", TextUtil.escapeXml("<some&text\"")); assertEquals("&apos;tis the s&apos;son", TextUtil.escapeXml("'tis the s'son")); assertNull(TextUtil.escapeXml(null)); assertEquals("copyrighted&#169;", TextUtil.escapeXml("copyrighted©")); }
public static String escapeXml(String text) { if (text == null) return null; StringBuilder escapedXML = new StringBuilder(); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { case '<': escapedXML.append("&lt;"); break; case '>': escapedXML.append("&gt;"); break; case '\"': escapedXML.append("&quot;"); break; case '&': escapedXML.append("&amp;"); break; case '\'': escapedXML.append("&apos;"); break; default: if (c > 0x7e) { escapedXML.append("&#" + ((int) c) + ";"); } else escapedXML.append(c); } } return escapedXML.toString(); }
TextUtil { public static String escapeXml(String text) { if (text == null) return null; StringBuilder escapedXML = new StringBuilder(); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { case '<': escapedXML.append("&lt;"); break; case '>': escapedXML.append("&gt;"); break; case '\"': escapedXML.append("&quot;"); break; case '&': escapedXML.append("&amp;"); break; case '\'': escapedXML.append("&apos;"); break; default: if (c > 0x7e) { escapedXML.append("&#" + ((int) c) + ";"); } else escapedXML.append(c); } } return escapedXML.toString(); } }
TextUtil { public static String escapeXml(String text) { if (text == null) return null; StringBuilder escapedXML = new StringBuilder(); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { case '<': escapedXML.append("&lt;"); break; case '>': escapedXML.append("&gt;"); break; case '\"': escapedXML.append("&quot;"); break; case '&': escapedXML.append("&amp;"); break; case '\'': escapedXML.append("&apos;"); break; default: if (c > 0x7e) { escapedXML.append("&#" + ((int) c) + ";"); } else escapedXML.append(c); } } return escapedXML.toString(); }  }
TextUtil { public static String escapeXml(String text) { if (text == null) return null; StringBuilder escapedXML = new StringBuilder(); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { case '<': escapedXML.append("&lt;"); break; case '>': escapedXML.append("&gt;"); break; case '\"': escapedXML.append("&quot;"); break; case '&': escapedXML.append("&amp;"); break; case '\'': escapedXML.append("&apos;"); break; default: if (c > 0x7e) { escapedXML.append("&#" + ((int) c) + ";"); } else escapedXML.append(c); } } return escapedXML.toString(); }  static void println(PrintStream out, String pattern, Object... args); static void println(PrintStream out, int maxLineLength, String linePrefix, String pattern, Object... args); static void println(PrintStream out, int maxLineLength, String linePrefix, String content); static String format(String pattern, Object... args); static String repeat(String base, int repeat); static String padRight(String base, String repeatString, int totalLength); static String trimToNull(String source); static String nullToEmpty(String source); static String trimToEmpty(String source); static boolean toBoolean(String value); static List<String> wrap(String in, int length, String prefix, String wrappedLineIndent); static List<String> wrap(String in, int length); static int findFirstInstanceOf(String toBeSearched, int searchFrom, String... toBeFound); static int findLastInstanceOf(String toBeSearched, int searchFrom, String... toBeFound); static String escapeXml(String text); }
TextUtil { public static String escapeXml(String text) { if (text == null) return null; StringBuilder escapedXML = new StringBuilder(); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { case '<': escapedXML.append("&lt;"); break; case '>': escapedXML.append("&gt;"); break; case '\"': escapedXML.append("&quot;"); break; case '&': escapedXML.append("&amp;"); break; case '\'': escapedXML.append("&apos;"); break; default: if (c > 0x7e) { escapedXML.append("&#" + ((int) c) + ";"); } else escapedXML.append(c); } } return escapedXML.toString(); }  static void println(PrintStream out, String pattern, Object... args); static void println(PrintStream out, int maxLineLength, String linePrefix, String pattern, Object... args); static void println(PrintStream out, int maxLineLength, String linePrefix, String content); static String format(String pattern, Object... args); static String repeat(String base, int repeat); static String padRight(String base, String repeatString, int totalLength); static String trimToNull(String source); static String nullToEmpty(String source); static String trimToEmpty(String source); static boolean toBoolean(String value); static List<String> wrap(String in, int length, String prefix, String wrappedLineIndent); static List<String> wrap(String in, int length); static int findFirstInstanceOf(String toBeSearched, int searchFrom, String... toBeFound); static int findLastInstanceOf(String toBeSearched, int searchFrom, String... toBeFound); static String escapeXml(String text); static final String[] EMPTY_STRING_ARRAY; static final List<String> EMPTY_STRING_LIST; static final String NULL_PRINT; static final String SECOND_LINE_INDENT; }