@Test(dataProvider = "escaping") public void specialCharactersAreCorrectlyEscaped(String content, String escaped) { assertThat(XMLUtils.escapeSpecialCharacters(content)).isEqualTo(escaped); }
public static String escapeSpecialCharacters(String text) { text = removeInvalidXMLChars(text); if (text == null || text.isEmpty()) { return text; } final StringBuilder sb = new StringBuilder(text.length()); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { case '&': sb.append("&amp;"); break; case '<': sb.append("&lt;"); break; case '>': sb.append("&gt;"); break; case '\"': sb.append("&quot;"); break; case '\'': sb.append("&apos;"); break; case '\n': sb.append("&#xA;"); break; case '\r': sb.append("&#xD;"); break; default: sb.append(c); } } return sb.toString(); }
XMLUtils { public static String escapeSpecialCharacters(String text) { text = removeInvalidXMLChars(text); if (text == null || text.isEmpty()) { return text; } final StringBuilder sb = new StringBuilder(text.length()); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { case '&': sb.append("&amp;"); break; case '<': sb.append("&lt;"); break; case '>': sb.append("&gt;"); break; case '\"': sb.append("&quot;"); break; case '\'': sb.append("&apos;"); break; case '\n': sb.append("&#xA;"); break; case '\r': sb.append("&#xD;"); break; default: sb.append(c); } } return sb.toString(); } }
XMLUtils { public static String escapeSpecialCharacters(String text) { text = removeInvalidXMLChars(text); if (text == null || text.isEmpty()) { return text; } final StringBuilder sb = new StringBuilder(text.length()); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { case '&': sb.append("&amp;"); break; case '<': sb.append("&lt;"); break; case '>': sb.append("&gt;"); break; case '\"': sb.append("&quot;"); break; case '\'': sb.append("&apos;"); break; case '\n': sb.append("&#xA;"); break; case '\r': sb.append("&#xD;"); break; default: sb.append(c); } } return sb.toString(); }  }
XMLUtils { public static String escapeSpecialCharacters(String text) { text = removeInvalidXMLChars(text); if (text == null || text.isEmpty()) { return text; } final StringBuilder sb = new StringBuilder(text.length()); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { case '&': sb.append("&amp;"); break; case '<': sb.append("&lt;"); break; case '>': sb.append("&gt;"); break; case '\"': sb.append("&quot;"); break; case '\'': sb.append("&apos;"); break; case '\n': sb.append("&#xA;"); break; case '\r': sb.append("&#xD;"); break; default: sb.append(c); } } return sb.toString(); }  String getATTR_VALUE_PAIR_NODE(); static Document toDOMDocument(String xmlString, Debug debug); static Document toDOMDocument(InputStream is, Debug debug); static Set parseAttributesTag(Node n); static Map<String, Set<String>> parseAttributeValuePairTags(Node parentNode); static Document newDocument(); static Document getXMLDocument(InputStream in); static Node getRootNode(Document doc, String nodeName); static Node getChildNode(Node parentNode, String childName); static boolean hasElementChild(Node node); static Node getNamedChildNode(Node parentNode, String childNodeName,
            String attrName, String attrValue); static Set getChildNodes(Node parentNode, String childName); static String getElementValue(Element element); static String getChildrenValue(Element element); static String getElementString(Element element); static String getNodeAttributeValue(Node node, String attrName); static String getNodeAttributeValueNS(Node node,
            String namespaceURI, String attrName); static Set getAttributeValuePair(Node node); static Set getAttributeValuePair(Node node, boolean unescape); static String getValueOfValueNode(Node n); static String getValueOfValueNode(Node n, boolean unescape); static String getValueOfValueNodeNoTrim(Node n); static String getValueOfValueNodeNoTrim(Node n, boolean unescape); static List getElementsByTagNameNS1(
        Element element,
        String nsName,
        String tagName
    ); static String printAttributeValue(Element node, String prefix); static String print(Node node); static String print(Node node, String encoding); static String unescapeSpecialCharacters(String text); static String escapeSpecialCharacters(String text); static String removeInvalidXMLChars(String text); static Set encodeAttributeSet(Set set, Debug debug); static Set decodeAttributeSet(Set set); static String removeNullCharAtEnd(String st); static DocumentBuilder getSafeDocumentBuilder(boolean validating); static SAXParser getSafeSAXParser(boolean validating); static TransformerFactory getTransformerFactory(); static SAXSource createSAXSource(InputSource source); }
XMLUtils { public static String escapeSpecialCharacters(String text) { text = removeInvalidXMLChars(text); if (text == null || text.isEmpty()) { return text; } final StringBuilder sb = new StringBuilder(text.length()); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { case '&': sb.append("&amp;"); break; case '<': sb.append("&lt;"); break; case '>': sb.append("&gt;"); break; case '\"': sb.append("&quot;"); break; case '\'': sb.append("&apos;"); break; case '\n': sb.append("&#xA;"); break; case '\r': sb.append("&#xD;"); break; default: sb.append(c); } } return sb.toString(); }  String getATTR_VALUE_PAIR_NODE(); static Document toDOMDocument(String xmlString, Debug debug); static Document toDOMDocument(InputStream is, Debug debug); static Set parseAttributesTag(Node n); static Map<String, Set<String>> parseAttributeValuePairTags(Node parentNode); static Document newDocument(); static Document getXMLDocument(InputStream in); static Node getRootNode(Document doc, String nodeName); static Node getChildNode(Node parentNode, String childName); static boolean hasElementChild(Node node); static Node getNamedChildNode(Node parentNode, String childNodeName,
            String attrName, String attrValue); static Set getChildNodes(Node parentNode, String childName); static String getElementValue(Element element); static String getChildrenValue(Element element); static String getElementString(Element element); static String getNodeAttributeValue(Node node, String attrName); static String getNodeAttributeValueNS(Node node,
            String namespaceURI, String attrName); static Set getAttributeValuePair(Node node); static Set getAttributeValuePair(Node node, boolean unescape); static String getValueOfValueNode(Node n); static String getValueOfValueNode(Node n, boolean unescape); static String getValueOfValueNodeNoTrim(Node n); static String getValueOfValueNodeNoTrim(Node n, boolean unescape); static List getElementsByTagNameNS1(
        Element element,
        String nsName,
        String tagName
    ); static String printAttributeValue(Element node, String prefix); static String print(Node node); static String print(Node node, String encoding); static String unescapeSpecialCharacters(String text); static String escapeSpecialCharacters(String text); static String removeInvalidXMLChars(String text); static Set encodeAttributeSet(Set set, Debug debug); static Set decodeAttributeSet(Set set); static String removeNullCharAtEnd(String st); static DocumentBuilder getSafeDocumentBuilder(boolean validating); static SAXParser getSafeSAXParser(boolean validating); static TransformerFactory getTransformerFactory(); static SAXSource createSAXSource(InputSource source);  }