1
0
mirror of https://github.com/google/nomulus synced 2026-02-09 14:30:33 +00:00

Make nomulus compatible with Java 11 (#649)

* Make nomulus compatible with Java 11

This fixes the double-spacing bug with logged EPP XML on App Engine that started
appearing after App Engine switching from using Java 8 to Java 11. Java 9 made
some changes to XML Transformer classes that needed a little bit of work to
accommodate.

This also fixes the unit tests that were failing in Java 11 (all of which were
related to said XML Transformer changes).

* Make code review changes
This commit is contained in:
Ben McIlwain
2020-06-25 13:17:22 -04:00
committed by GitHub
parent 2e5466f32f
commit 6e26dacdff
4 changed files with 55 additions and 29 deletions

View File

@@ -297,7 +297,7 @@ public class XmlTransformer {
return marshaller;
}
/** Pretty print xml. */
/** Pretty print XML. */
public static String prettyPrint(String xmlString) {
StringWriter prettyXml = new StringWriter();
try {
@@ -308,13 +308,18 @@ public class XmlTransformer {
transformer.transform(
new StreamSource(new StringReader(xmlString)),
new StreamResult(prettyXml));
return prettyXml.toString();
// Remove whitespace-only/blank lines (which the XMLTransformer in Java 9 and up sometimes
// adds depending on input format). Surprisingly, this is the least bad solution. See:
// https://stackoverflow.com/questions/58478632/how-to-avoid-extra-blank-lines-in-xml-generation-with-java
// Note that a simple regex replace is waaaaay more performant than using an XSLT.
return prettyXml.toString().replaceAll("\\n\\s*\\n", "\n");
} catch (TransformerException e) {
return xmlString; // We couldn't prettify it, but that's ok; fail gracefully.
}
}
/** Pretty print xml bytes. */
/** Pretty print XML bytes. */
public static String prettyPrint(byte[] xmlBytes) {
return prettyPrint(new String(xmlBytes, UTF_8));
}