diff --git a/quickfixj-core/src/main/java/quickfix/Message.java b/quickfixj-core/src/main/java/quickfix/Message.java index b447ca4a0d..6384e1e01e 100644 --- a/quickfixj-core/src/main/java/quickfix/Message.java +++ b/quickfixj-core/src/main/java/quickfix/Message.java @@ -316,7 +316,24 @@ public boolean trailerHasGroup(Group group) { * @see #toXML(DataDictionary) */ public String toXML() { - return toXML(null); + return toXML(false); + } + + /** + * Converts the message into a simple XML format. This format is + * probably not sufficient for production use, but is more intended + * for diagnostics and debugging. THIS IS NOT FIXML. + * + * To get names instead of tag number, use toXML(DataDictionary, boolean) + * instead. + * + * @param indent specifies whether the Transformer may add additional + * whitespace when outputting the result tree + * @return an XML representation of the message. + * @see #toXML(DataDictionary, boolean) + */ + public String toXML(boolean indent) { + return toXML(null, indent); } /** @@ -328,6 +345,20 @@ public String toXML() { * @return the XML representation of the message */ public String toXML(DataDictionary dataDictionary) { + return toXML(dataDictionary, false); + } + + /** + * Converts the message into a simple XML format. This format is + * probably not sufficient for production use, but is more intended + * for diagnostics and debugging. THIS IS NOT FIXML. + * + * @param indent specifies whether the Transformer may add additional + * whitespace when outputting the result tree + * @param dataDictionary + * @return the XML representation of the message + */ + public String toXML(DataDictionary dataDictionary, boolean indent) { try { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final Document document = factory.newDocumentBuilder() @@ -343,7 +374,12 @@ public String toXML(DataDictionary dataDictionary) { final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); - serializer.setOutputProperty(OutputKeys.INDENT, "yes"); + if (indent) { + serializer.setOutputProperty(OutputKeys.INDENT, "yes"); + serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); + } else { + serializer.setOutputProperty(OutputKeys.INDENT, "no"); + } serializer.transform(domSource, streamResult); return out.toString(); } catch (final Exception e) { diff --git a/quickfixj-core/src/test/java/quickfix/MessageTest.java b/quickfixj-core/src/test/java/quickfix/MessageTest.java index 44ed9e33ec..0d0966f761 100644 --- a/quickfixj-core/src/test/java/quickfix/MessageTest.java +++ b/quickfixj-core/src/test/java/quickfix/MessageTest.java @@ -19,7 +19,6 @@ package quickfix; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -1934,7 +1933,6 @@ public void testIfMessageHeaderIsCreatedWithEveryConstructor() throws Exception } @Test - @Ignore public void shouldConvertToXmlWhenDataDictionaryLoadedWithExternalDTD() throws ConfigError { DataDictionary dataDictionary = new DataDictionary("FIX_External_DTD.xml", DocumentBuilderFactory::newInstance); @@ -1942,8 +1940,33 @@ public void shouldConvertToXmlWhenDataDictionaryLoadedWithExternalDTD() throws C message.setString(Account.FIELD, "test-account"); String xml = message.toXML(dataDictionary); - xml = xml.replace("\r", "").replace("\n", " "); - assertEquals("
", xml); + xml = xml.replace("\r", "").replace("\n", "").replaceAll(">\\s+<", "><"); + assertEquals("
", xml); + } + + @Test + public void shouldConvertToXMLWithoutIndent() { + Message message = new Message(); + message.setString(Account.FIELD, "test-account"); + + assertEquals("
", message.toXML()); + } + + @Test + public void shouldConvertToXMLWithIndent() { + Message message = new Message(); + message.setString(Account.FIELD, "test-account"); + + String xml = message.toXML(true); + xml = xml.replace("\r", ""); + // formatting CDATA elements can be different across JVM's so we have to strip whitespaces before and after for the test to pass + // https://bugs.openjdk.java.net/browse/JDK-8215543 + xml = xml.replaceAll("\\s+\\s+", ""); + + assertEquals("\n" + "\n" + + "
\n" + " \n" + + " \n" + " \n" + + " \n" + "\n", xml); } private void assertHeaderField(Message message, String expectedValue, int field)