XMLUtils { @Deprecated public static String escapeElementContent(Object content) { if (content == null) { return null; } String str = String.valueOf(content); StringBuilder result = new StringBuilder((int) (str.length() * 1.1)); int length = str.length(); char c; for (int i = 0; i < length; ++i) { c = str.charAt(i); switch (c) { case '&': result.append(AMP); break; case '<': result.append(LT); break; case '>': result.append(GT); break; default: result.append(c); } } return result.toString(); } private  XMLUtils(); static String extractXML(Node node, int start, int length); static String escapeXMLComment(String content); static String unescapeXMLComment(String content); @Deprecated static String escape(Object content); @Unstable static String escape(String content); @Deprecated static String escapeAttributeValue(Object content); @Unstable static String escapeAttributeValue(String content); @Unstable static String escapeElementText(String content); @Deprecated static String escapeElementContent(Object content); @Deprecated static String unescape(Object content); static Document createDOMDocument(); static Document parse(LSInput source); static String serialize(Node node); static String serialize(Node node, boolean withXmlDeclaration); static String transform(Source xml, Source xslt); static String formatXMLContent(String content);  }
@Test void escapeElementContent() { String escapedText = XMLUtils.escapeElementContent("a < a' && a' < a\" => a < a\""); assertFalse(escapedText.contains("<"), "Failed to escape <"); assertFalse(escapedText.contains(">"), "Failed to escape >"); assertTrue(escapedText.contains("'"), "Wrongfully escaped '"); assertTrue(escapedText.contains("\""), "Wrongfully escaped \""); assertFalse(escapedText.contains("&&"), "Failed to escape &"); }
@Test void escapeElementContentObjectContent() { String expected = XMLUtils.escapeElementContent("a < a' && a' < a\" => a < a\""); StringBuilder content = new StringBuilder(); content.append("a < a' && a' < a\" => a < a\""); String actual = XMLUtils.escapeElementContent(content); assertEquals(expected, actual); }
@Test void escapeElementContentContentNull() { StringBuilder content = null; assertNull(XMLUtils.escapeElementContent(content)); }
@Test void escapeElementContentEmptyString() { assertEquals("", XMLUtils.escapeElementContent(""), "\"\" should be \"\""); }
@Test void escapeElementContentWithNull() { assertNull(XMLUtils.escapeElementContent(null), "null should be null"); }
@Test void escapeElementContentNonAscii() { assertEquals("\u0123", XMLUtils.escapeElementContent("\u0123"), "Non-ASCII characters were escaped"); }