diff --git a/std-bits/base/src/main/java/org/enso/base/XML_Utils.java b/std-bits/base/src/main/java/org/enso/base/XML_Utils.java index 71eb80b7919b..0236342c48ce 100644 --- a/std-bits/base/src/main/java/org/enso/base/XML_Utils.java +++ b/std-bits/base/src/main/java/org/enso/base/XML_Utils.java @@ -2,16 +2,14 @@ import java.io.ByteArrayOutputStream; import javax.xml.parsers.DocumentBuilder; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Result; -import javax.xml.transform.Source; -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 org.w3c.dom.DOMConfiguration; import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import org.w3c.dom.bootstrap.DOMImplementationRegistry; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSOutput; +import org.w3c.dom.ls.LSSerializer; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; @@ -24,14 +22,15 @@ public class XML_Utils { * @return the string representation of the element * @throws TransformerException */ - public static String outerXML(Element element) throws TransformerException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Transformer transformer = TransformerFactory.newInstance().newTransformer(); - transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); - Source source = new DOMSource(element); - Result target = new StreamResult(out); - transformer.transform(source, target); - return out.toString(); + public static String outerXML(Element element) + throws ClassNotFoundException, IllegalAccessException, InstantiationException { + DOMImplementationLS dom = + (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS"); + LSSerializer serializer = dom.createLSSerializer(); + DOMConfiguration config = serializer.getDomConfig(); + config.setParameter("xml-declaration", false); + serializer.setNewLine("\n"); + return serializer.writeToString(element); } /** @@ -41,15 +40,20 @@ public static String outerXML(Element element) throws TransformerException { * @return the string representation of the element's contents * @throws TransformerException */ - public static String innerXML(Element element) throws TransformerException { + public static String innerXML(Element element) + throws ClassNotFoundException, IllegalAccessException, InstantiationException { ByteArrayOutputStream out = new ByteArrayOutputStream(); - Transformer transformer = TransformerFactory.newInstance().newTransformer(); - transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); - Result target = new StreamResult(out); + DOMImplementationLS dom = + (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS"); + LSSerializer serializer = dom.createLSSerializer(); + DOMConfiguration config = serializer.getDomConfig(); + config.setParameter("xml-declaration", false); + serializer.setNewLine("\n"); NodeList childNodes = element.getChildNodes(); + LSOutput output = dom.createLSOutput(); + output.setByteStream(out); for (int i = 0; i < childNodes.getLength(); ++i) { - Source source = new DOMSource(childNodes.item(i)); - transformer.transform(source, target); + serializer.write(childNodes.item(i), output); } return out.toString(); } diff --git a/test/Tests/src/Data/XML/XML_Spec.enso b/test/Tests/src/Data/XML/XML_Spec.enso index d74c4bca824b..636d8cbea43c 100644 --- a/test/Tests/src/Data/XML/XML_Spec.enso +++ b/test/Tests/src/Data/XML/XML_Spec.enso @@ -10,8 +10,6 @@ spec = document = XML_Document.from_file test_file root = document . root_element - fix_windows_newlines s = s.replace '\r\n' '\n' - Test.group "Read XML" <| Test.specify "Can read from a file" <| root.name . should_equal "class" @@ -145,14 +143,14 @@ spec = Test.group "inner / outer xml" <| Test.specify "Can get the inner xml" <| - fix_windows_newlines (root.at "/class/teacher[1]" . at 0 . inner_xml) . should_equal '\n Mary\n Smith\n \n Blah blah\n \n ' - fix_windows_newlines (root.at "/class/teacher[1]/bio" . at 0 . inner_xml) . should_equal '\n Blah blah\n ' - fix_windows_newlines (root.at "/class/teacher[2]/bio" . at 0 . inner_xml) . should_equal '\n This that\n ' - fix_windows_newlines (root.at "/class/teacher[2]" . at 0 . inner_xml) . should_equal '\n Bob\n Jones\n \n This that\n \n ' + (root.at "/class/teacher[1]" . at 0 . inner_xml) . should_equal '\n Mary\n Smith\n \n Blah blah\n \n ' + (root.at "/class/teacher[1]/bio" . at 0 . inner_xml) . should_equal '\n Blah blah\n ' + (root.at "/class/teacher[2]/bio" . at 0 . inner_xml) . should_equal '\n This that\n ' + (root.at "/class/teacher[2]" . at 0 . inner_xml) . should_equal '\n Bob\n Jones\n \n This that\n \n ' Test.specify "Can get the outer xml" <| - fix_windows_newlines (root.at "/class/teacher[1]/bio" . at 0 . outer_xml) . should_equal '\n Blah blah\n ' - fix_windows_newlines (root.at "/class/teacher[2]/bio" . at 0 . outer_xml) . should_equal '\n This that\n ' + (root.at "/class/teacher[1]/bio" . at 0 . outer_xml) . should_equal '\n Blah blah\n ' + (root.at "/class/teacher[2]/bio" . at 0 . outer_xml) . should_equal '\n This that\n ' Test.group "get_elements_by_tag_name" <| Test.specify "Can get elements by tag name" <|