1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-10 18:59:41 +02:00

Use XmlUtil for XML pretty printing in Protocol

This commit is contained in:
Florian Schmaus 2018-04-18 13:16:57 +02:00
parent 4fb34a6952
commit 73168bff69
2 changed files with 8 additions and 36 deletions

View file

@ -18,21 +18,13 @@ package org.jivesoftware.util;
import static org.junit.Assert.assertEquals;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.util.XmlUtil;
/**
* This class can be used in conjunction with a mocked XMPP connection (
@ -136,10 +128,10 @@ public class Protocol {
if (printProtocol) {
System.out.println("------------------- Request -------------\n");
System.out.println(prettyFormat(request.toXML().toString()));
System.out.println(XmlUtil.prettyFormatXml(request.toXML()));
System.out.println("------------------- Response ------------\n");
if (response != null) {
System.out.println(prettyFormat(response.toXML().toString()));
System.out.println(XmlUtil.prettyFormatXml(response.toXML()));
}
else {
System.out.println("No response");
@ -176,25 +168,4 @@ public class Protocol {
return requests;
}
private static String prettyFormat(String input, int indent) {
try {
Source xmlInput = new StreamSource(new StringReader(input));
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
String.valueOf(indent));
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
}
catch (Exception e) {
return "error while formatting the XML: " + e.getMessage();
}
}
private static String prettyFormat(String input) {
return prettyFormat(input, 2);
}
}