package net.sargue.mailgun.content;

import net.sargue.mailgun.MailgunException;
import org.w3c.dom.Document;
import org.w3c.dom.Text;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;

class Util {
    private Util() {}

    static String escapeXml(String target) {
        try {
            Document document = DocumentBuilderFactory.newInstance()
                                                      .newDocumentBuilder()
                                                      .newDocument();
            Text text = document.createTextNode(target);
            Transformer transformer = TransformerFactory.newInstance()
                                                        .newTransformer();
            DOMSource source = new DOMSource(text);
            StringWriter writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                                          "yes");
            transformer.transform(source, result);
            return writer.toString();
        } catch (ParserConfigurationException | TransformerException e) {
            throw new MailgunException("Problem escaping XML", e);
        }
    }
}

@@ -237,47 +237,8 @@
 on;%0A
-import javax.xml.transform.OutputKeys;%0A
 impo
@@ -273,16 +273,16 @@
 former;%0A
+
 import j
@@ -489,16 +489,148 @@
 riter;%0A%0A
+import static javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING;%0Aimport static javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION;%0A%0A
 class Ut
@@ -1011,28 +1011,31 @@
 nsformer
- transformer
+Factory factory
  = Trans
@@ -1053,32 +1053,33 @@
 ry.newInstance()
+;
 %0A               
@@ -1079,52 +1079,106 @@
     
-                                            
+factory.setFeature(FEATURE_SECURE_PROCESSING, true);%0A            Transformer transformer = factory
 .new
@@ -1400,27 +1400,16 @@
 roperty(
-OutputKeys.
 OMIT_XML
@@ -1425,50 +1425,8 @@
 ION,
-%0A                                         
  %22ye

fix sonar: XML transformers should be secured