mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 14:22:05 +01:00
Use XmlUtil for XML pretty printing in Protocol
This commit is contained in:
parent
4fb34a6952
commit
73168bff69
2 changed files with 8 additions and 36 deletions
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright 2017 Florian Schmaus.
|
* Copyright 2017-2018 Florian Schmaus.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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);
|
transformerFactory.setAttribute("indent-number", 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String prettyFormatXml(String xml) {
|
public static String prettyFormatXml(CharSequence xml) {
|
||||||
StreamSource source = new StreamSource(new StringReader(xml));
|
String xmlString = xml.toString();
|
||||||
|
StreamSource source = new StreamSource(new StringReader(xmlString));
|
||||||
StringWriter stringWriter = new StringWriter();
|
StringWriter stringWriter = new StringWriter();
|
||||||
StreamResult result = new StreamResult(stringWriter);
|
StreamResult result = new StreamResult(stringWriter);
|
||||||
|
|
||||||
|
@ -54,7 +55,7 @@ public class XmlUtil {
|
||||||
}
|
}
|
||||||
catch (TransformerException | IllegalArgumentException e) {
|
catch (TransformerException | IllegalArgumentException e) {
|
||||||
LOGGER.log(Level.SEVERE, "Transformer error", e);
|
LOGGER.log(Level.SEVERE, "Transformer error", e);
|
||||||
return xml;
|
return xmlString;
|
||||||
}
|
}
|
||||||
|
|
||||||
return stringWriter.toString();
|
return stringWriter.toString();
|
||||||
|
|
|
@ -18,21 +18,13 @@ package org.jivesoftware.util;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.io.StringReader;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Queue;
|
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.packet.Stanza;
|
||||||
|
import org.jivesoftware.smack.util.XmlUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class can be used in conjunction with a mocked XMPP connection (
|
* This class can be used in conjunction with a mocked XMPP connection (
|
||||||
|
@ -136,10 +128,10 @@ public class Protocol {
|
||||||
|
|
||||||
if (printProtocol) {
|
if (printProtocol) {
|
||||||
System.out.println("------------------- Request -------------\n");
|
System.out.println("------------------- Request -------------\n");
|
||||||
System.out.println(prettyFormat(request.toXML().toString()));
|
System.out.println(XmlUtil.prettyFormatXml(request.toXML()));
|
||||||
System.out.println("------------------- Response ------------\n");
|
System.out.println("------------------- Response ------------\n");
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
System.out.println(prettyFormat(response.toXML().toString()));
|
System.out.println(XmlUtil.prettyFormatXml(response.toXML()));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
System.out.println("No response");
|
System.out.println("No response");
|
||||||
|
@ -176,25 +168,4 @@ public class Protocol {
|
||||||
return requests;
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue