1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-24 03:27:59 +01: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

@ -1,6 +1,6 @@
/**
*
* Copyright 2017 Florian Schmaus.
* Copyright 2017-2018 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -38,8 +38,9 @@ public class XmlUtil {
transformerFactory.setAttribute("indent-number", 2);
}
public static String prettyFormatXml(String xml) {
StreamSource source = new StreamSource(new StringReader(xml));
public static String prettyFormatXml(CharSequence xml) {
String xmlString = xml.toString();
StreamSource source = new StreamSource(new StringReader(xmlString));
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
@ -54,7 +55,7 @@ public class XmlUtil {
}
catch (TransformerException | IllegalArgumentException e) {
LOGGER.log(Level.SEVERE, "Transformer error", e);
return xml;
return xmlString;
}
return stringWriter.toString();

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);
}
}