Eliminate line feeds from XML.outer_xml on Windows (#8013)

- Closes #7999

# Important Notes
None
This commit is contained in:
somebody1234 2023-10-11 09:21:34 +10:00 committed by GitHub
parent 6f78570115
commit 826127d8ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 29 deletions

View File

@ -2,16 +2,14 @@ package org.enso.base;
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 class XML_Utils {
* @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();
}

View File

@ -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 <firstname>Mary</firstname>\n <lastname>Smith</lastname>\n <bio>\n Blah blah\n </bio>\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 <firstname>Bob</firstname>\n <lastname>Jones</lastname>\n <bio>\n This that\n </bio>\n '
(root.at "/class/teacher[1]" . at 0 . inner_xml) . should_equal '\n <firstname>Mary</firstname>\n <lastname>Smith</lastname>\n <bio>\n Blah blah\n </bio>\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 <firstname>Bob</firstname>\n <lastname>Jones</lastname>\n <bio>\n This that\n </bio>\n '
Test.specify "Can get the outer xml" <|
fix_windows_newlines (root.at "/class/teacher[1]/bio" . at 0 . outer_xml) . should_equal '<bio>\n Blah blah\n </bio>'
fix_windows_newlines (root.at "/class/teacher[2]/bio" . at 0 . outer_xml) . should_equal '<bio>\n This that\n </bio>'
(root.at "/class/teacher[1]/bio" . at 0 . outer_xml) . should_equal '<bio>\n Blah blah\n </bio>'
(root.at "/class/teacher[2]/bio" . at 0 . outer_xml) . should_equal '<bio>\n This that\n </bio>'
Test.group "get_elements_by_tag_name" <|
Test.specify "Can get elements by tag name" <|