XMLUtils { public static String escapeXMLComment(String content) { StringBuffer str = new StringBuffer(content.length()); char[] buff = content.toCharArray(); char lastChar = 0; for (char c : buff) { if (c == '\\') { str.append('\\'); } else if (c == '-' && lastChar == '-') { str.append('\\'); } str.append(c); lastChar = c; } if (lastChar == '-') { str.append('\\'); } return str.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 escapeXMLComment() { assertEquals("-\\- ", XMLUtils.escapeXMLComment("-- ")); assertEquals("\\\\", XMLUtils.escapeXMLComment("\\")); assertEquals("-\\", XMLUtils.escapeXMLComment("-")); assertEquals("-\\-\\-\\", XMLUtils.escapeXMLComment("---")); assertEquals("- ", XMLUtils.escapeXMLComment("- ")); }