Skip to content

Commit

Permalink
Eliminate line feeds from XML.outer_xml on Windows (#8013)
Browse files Browse the repository at this point in the history
- Closes #7999

# Important Notes
None
  • Loading branch information
somebody1234 authored Oct 10, 2023
1 parent 6f78570 commit 826127d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
46 changes: 25 additions & 21 deletions std-bits/base/src/main/java/org/enso/base/XML_Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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();
}
Expand Down
14 changes: 6 additions & 8 deletions test/Tests/src/Data/XML/XML_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" <|
Expand Down

0 comments on commit 826127d

Please sign in to comment.