Skip to content

Commit

Permalink
Disable indenting XML output by default
Browse files Browse the repository at this point in the history
Turns out, that the behavior of the XML indenter differs greatly between
Java versions, some of which result in significant whitespace changes
with mixed content.

Therefore, rather output unformatted XML files than broken ones, with an
option to re-enable it.

Also remove the workaround in USFX tests I added yesterday, and add Java
21 to GitHub CI.
  • Loading branch information
schierlm committed Aug 18, 2024
1 parent 7628db9 commit 84e6b5a
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 52 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,14 @@ jobs:
java-version: 17
- name: Build with Maven
run: mvn -B package

build21:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 21
uses: actions/setup-java@v1
with:
java-version: 21
- name: Build with Maven
run: mvn -B package
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ public void doExport(Bible bible, String... exportArgs) throws Exception {
m.setSchema(getSchema());
m.marshal(xmlbible, doc);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
if (Boolean.getBoolean("biblemulticonverter.indentxml")) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
transformer.transform(new DOMSource(doc), new StreamResult(file));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,10 @@ public void doExport(Bible bible, String... exportArgs) throws Exception {
doc.getDocumentElement().setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
doc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation", "haggai_20130620.xsd");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
if (Boolean.getBoolean("biblemulticonverter.indentxml")) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
transformer.transform(new DOMSource(doc), new StreamResult(file));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,10 @@ public void doExport(Bible bible, String... exportArgs) throws Exception {
m.setSchema(getSchema());
m.marshal(xmlbible, doc);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
if (Boolean.getBoolean("biblemulticonverter.indentxml")) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
transformer.transform(new DOMSource(doc), new StreamResult(file));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ public void doExport(Bible bible, String... exportArgs) throws Exception {
doc.normalize();
maskWhitespaceNodes(doc.getDocumentElement());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
if (Boolean.getBoolean("biblemulticonverter.indentxml")) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
transformer.transform(new DOMSource(doc), new StreamResult(file));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,10 @@ public void doExport(Bible bible, String... exportArgs) throws Exception {
doc.getDocumentElement().setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
doc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation", "zef2005.xsd");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
if (Boolean.getBoolean("biblemulticonverter.indentxml")) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
transformer.transform(new DOMSource(doc), new StreamResult(file));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,10 @@ public Visitor<IOException> visitExtraAttribute(ExtraAttributePriority prio, Str
maskWhitespaceNodes(docc.getDocumentElement());
try (FileOutputStream fos = new FileOutputStream(exportArgs[0])) {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
if (Boolean.getBoolean("biblemulticonverter.indentxml")) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
transformer.transform(new DOMSource(docc), new StreamResult(fos));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,10 @@ public void doExport(Bible bible, String... exportArgs) throws Exception {
doc.getDocumentElement().setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
doc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation", "zef2005.xsd");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
if (Boolean.getBoolean("biblemulticonverter.indentxml")) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
transformer.transform(new DOMSource(doc), new StreamResult(file));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,10 @@ public void doExportBooks(List<ParatextBook> books, String... exportArgs) throws
doc.getDocumentElement().setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
doc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation", "https://eBible.org/usfx.xsd");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
if (Boolean.getBoolean("biblemulticonverter.indentxml")) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
transformer.transform(new DOMSource(doc), new StreamResult(file));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,10 @@ public void doExport(File outputFile, List<Versification> versifications, List<V
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
m.marshal(refsys, doc);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
if (Boolean.getBoolean("biblemulticonverter.indentxml")) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
transformer.transform(new DOMSource(doc), new StreamResult(outputFile));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ protected void doExportBook(ParatextBook book, File outFile) throws Exception {
}

private void testSingleFormat(AbstractParatextFormat format, String extraArg, String expectedContent) throws Exception {
testSingleFormat(format, extraArg, expectedContent, false);
}

private void testSingleFormat(AbstractParatextFormat format, String extraArg, String expectedContent, boolean normalizeOutputWhitespace) throws Exception {
File roundtripFile = File.createTempFile("~roundtrip", ".tmp");
if (!extraArg.isEmpty()) {
roundtripFile.delete();
Expand All @@ -119,11 +115,8 @@ private void testSingleFormat(AbstractParatextFormat format, String extraArg, St
File resultFile = USX3Test.createTempFile("export", ".usfm");
usfm.doExportBook(books.get(0), resultFile);
String actualContent = new String(Files.readAllBytes(resultFile.toPath()), StandardCharsets.UTF_8);
if (normalizeOutputWhitespace) {
actualContent = actualContent.replaceAll(" +", " ").replaceAll(" +\n", "\n");
}
assertEquals(expectedContent == null ? testBookContent : expectedContent, actualContent);
if (expectedContent != null && !normalizeOutputWhitespace) {
if (expectedContent != null) {
usfm.doExportBook(usfm.doImportBook(resultFile), resultFile);
assertEquals(expectedContent, new String(Files.readAllBytes(resultFile.toPath()), StandardCharsets.UTF_8));
}
Expand Down Expand Up @@ -153,37 +146,13 @@ public void testUSX3() throws Exception {

@Test
public void testUSFX() throws Exception {
// Some newer Java versions with current JAXB version are unable to marshal mixed content.
// skip this test for them.
ObjectFactory of = new ObjectFactory();
Usfx usfx = of.createUsfx();
Usfx.Book book = of.createUsfxBook();
usfx.getContent().add(of.createUsfxBook(book));
PType p = of.createPType();
book.getContent().add(of.createUsfxBookP(p));
p.getContent().add("X");
p.getContent().add(of.createPTypeBk(of.createPType()));
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName());
Marshaller m = ctx.createMarshaller();
m.marshal(usfx, doc);
doc.getDocumentElement().setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
doc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation", "https://eBible.org/usfx.xsd");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StringWriter sw = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(sw));
assumeTrue(sw.toString().contains("<p>X<bk/>"));

// now we are fine
String expectedContent = testBookContent.replace("\\ca ", " \n\\p \\ca ").replace("\\cat Etymology\\cat*", "")
.replace("\\tc1-2", "\\tc1 \\tc2").replace("\\th2-3", "\\th2 \\th3").replace("~", " ").replace("|link-href=\"GEN 9:8\"", "")
.replace("|gloss=\"Roo:bee\"", "").replace("|link-href=\"https://schierlm.github.io\" x-why=\"That's me\"", "")
.replace("\\ndx*\\fig", "\\ndx* \\fig").replace("|link-id=\"a-loop\"", "").replace("|link-href=\"#a-loop\" link-title=\"Loop\"", "")
.replace("|link-id=\"a-loop\"", "").replace("|link-href=\"#a-loop\" link-title=\"Loop\"", "")
.replace("|id=\"measures\"", "").replace("|id=\"x-custom\"", "")
.replaceFirst("\\\\esb(.|\n)*?\\\\esbe\n", "").replaceAll(" +", " ").replaceAll(" +\n", "\n");
testSingleFormat(new USFX(), "", expectedContent, true);
testSingleFormat(new USFX(), "", expectedContent);
}

@Test
Expand Down

0 comments on commit 84e6b5a

Please sign in to comment.