From 73168bff6979930ff166557d2f69ec8982dedf35 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 18 Apr 2018 13:16:57 +0200 Subject: [PATCH] Use XmlUtil for XML pretty printing in Protocol --- .../org/jivesoftware/smack/util/XmlUtil.java | 9 ++--- .../java/org/jivesoftware/util/Protocol.java | 35 ++----------------- 2 files changed, 8 insertions(+), 36 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlUtil.java b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlUtil.java index 46b1d8088..7fc081b43 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlUtil.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlUtil.java @@ -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(); diff --git a/smack-extensions/src/test/java/org/jivesoftware/util/Protocol.java b/smack-extensions/src/test/java/org/jivesoftware/util/Protocol.java index 906c57594..7d6eda39d 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/util/Protocol.java +++ b/smack-extensions/src/test/java/org/jivesoftware/util/Protocol.java @@ -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); - } - }