Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions quickfixj-core/src/main/java/quickfix/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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()
Expand All @@ -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) {
Expand Down
31 changes: 27 additions & 4 deletions quickfixj-core/src/test/java/quickfix/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package quickfix;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -1934,16 +1933,40 @@ public void testIfMessageHeaderIsCreatedWithEveryConstructor() throws Exception
}

@Test
@Ignore
public void shouldConvertToXmlWhenDataDictionaryLoadedWithExternalDTD() throws ConfigError {
DataDictionary dataDictionary = new DataDictionary("FIX_External_DTD.xml", DocumentBuilderFactory::newInstance);

Message message = new Message();
message.setString(Account.FIELD, "test-account");

String xml = message.toXML(dataDictionary);
xml = xml.replace("\r", "").replace("\n", " ");
assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?> <message> <header/> <body> <field name=\"Account\" tag=\"1\"><![CDATA[test-account]]></field> </body> <trailer/> </message> ", xml);
xml = xml.replace("\r", "").replace("\n", "").replaceAll(">\\s+<", "><");
assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?><message><header/><body><field name=\"Account\" tag=\"1\"><![CDATA[test-account]]></field></body><trailer/></message>", xml);
}

@Test
public void shouldConvertToXMLWithoutIndent() {
Message message = new Message();
message.setString(Account.FIELD, "test-account");

assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?><message><header/><body><field tag=\"1\"><![CDATA[test-account]]></field></body><trailer/></message>", 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+<!\\[CDATA\\[test-account\\]\\]>\\s+", "<![CDATA[test-account]]>");

assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?>\n" + "<message>\n" +
" <header/>\n" + " <body>\n" +
" <field tag=\"1\"><![CDATA[test-account]]></field>\n" + " </body>\n" +
" <trailer/>\n" + "</message>\n", xml);
}

private void assertHeaderField(Message message, String expectedValue, int field)
Expand Down