mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 06:12:05 +01:00
Add XmlUtil.prettyFormatXml()
This commit is contained in:
parent
8020fc1416
commit
255865f733
3 changed files with 109 additions and 45 deletions
|
@ -0,0 +1,62 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright 2017 Florian Schmaus.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smack.util;
|
||||||
|
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.xml.transform.OutputKeys;
|
||||||
|
import javax.xml.transform.Transformer;
|
||||||
|
import javax.xml.transform.TransformerException;
|
||||||
|
import javax.xml.transform.TransformerFactory;
|
||||||
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
import javax.xml.transform.stream.StreamSource;
|
||||||
|
|
||||||
|
public class XmlUtil {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(XmlUtil.class.getName());
|
||||||
|
|
||||||
|
private static final TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||||
|
|
||||||
|
static {
|
||||||
|
transformerFactory.setAttribute("indent-number", 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String prettyFormatXml(String xml) {
|
||||||
|
StreamSource source = new StreamSource(new StringReader(xml));
|
||||||
|
StringWriter stringWriter = new StringWriter();
|
||||||
|
StreamResult result = new StreamResult(stringWriter);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Transformer transformer = transformerFactory.newTransformer();
|
||||||
|
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||||
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||||
|
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
||||||
|
|
||||||
|
// Transform the requested string into a nice formatted XML string
|
||||||
|
transformer.transform(source, result);
|
||||||
|
}
|
||||||
|
catch (TransformerException | IllegalArgumentException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, "Transformer error", e);
|
||||||
|
return xml;
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringWriter.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright 2017 Florian Schmaus.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smack.util;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class XmlUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void prettyFormatXmlTest() {
|
||||||
|
final String uglyXml = "<foo attr1='value1' attr2='value2'><inner-element attr1='value1'>Test</inner-element></foo>";
|
||||||
|
|
||||||
|
final String prettyXml = XmlUtil.prettyFormatXml(uglyXml);
|
||||||
|
|
||||||
|
assertEquals("<foo attr1=\"value1\" attr2=\"value2\">\n <inner-element attr1=\"value1\">Test</inner-element>\n</foo>\n",
|
||||||
|
prettyXml);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void prettyFormatIncompleteXmlTest() {
|
||||||
|
final String uglyXml = "<foo attr1='value1' attr2='value2'><inner-element attr1='value1'>Test</inner-element>";
|
||||||
|
|
||||||
|
final String prettyXml = XmlUtil.prettyFormatXml(uglyXml);
|
||||||
|
|
||||||
|
assertEquals("<foo attr1='value1' attr2='value2'><inner-element attr1='value1'>Test</inner-element>",
|
||||||
|
prettyXml);
|
||||||
|
}
|
||||||
|
}
|
|
@ -31,8 +31,6 @@ import java.awt.event.ActionListener;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.StringReader;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
@ -61,13 +59,6 @@ import javax.swing.event.ListSelectionEvent;
|
||||||
import javax.swing.event.ListSelectionListener;
|
import javax.swing.event.ListSelectionListener;
|
||||||
import javax.swing.table.DefaultTableModel;
|
import javax.swing.table.DefaultTableModel;
|
||||||
import javax.swing.text.BadLocationException;
|
import javax.swing.text.BadLocationException;
|
||||||
import javax.xml.transform.OutputKeys;
|
|
||||||
import javax.xml.transform.Transformer;
|
|
||||||
import javax.xml.transform.TransformerConfigurationException;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
import javax.xml.transform.TransformerFactory;
|
|
||||||
import javax.xml.transform.stream.StreamResult;
|
|
||||||
import javax.xml.transform.stream.StreamSource;
|
|
||||||
|
|
||||||
import org.jivesoftware.smack.AbstractConnectionListener;
|
import org.jivesoftware.smack.AbstractConnectionListener;
|
||||||
import org.jivesoftware.smack.ConnectionListener;
|
import org.jivesoftware.smack.ConnectionListener;
|
||||||
|
@ -85,6 +76,7 @@ import org.jivesoftware.smack.util.ObservableWriter;
|
||||||
import org.jivesoftware.smack.util.ReaderListener;
|
import org.jivesoftware.smack.util.ReaderListener;
|
||||||
import org.jivesoftware.smack.util.StringUtils;
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
import org.jivesoftware.smack.util.WriterListener;
|
import org.jivesoftware.smack.util.WriterListener;
|
||||||
|
import org.jivesoftware.smack.util.XmlUtil;
|
||||||
|
|
||||||
import org.jxmpp.jid.EntityFullJid;
|
import org.jxmpp.jid.EntityFullJid;
|
||||||
import org.jxmpp.jid.Jid;
|
import org.jxmpp.jid.Jid;
|
||||||
|
@ -824,7 +816,7 @@ public class EnhancedDebugger extends SmackDebugger {
|
||||||
|
|
||||||
messagesTable.addRow(
|
messagesTable.addRow(
|
||||||
new Object[] {
|
new Object[] {
|
||||||
formatXML(packet.toXML().toString()),
|
XmlUtil.prettyFormatXml(packet.toXML().toString()),
|
||||||
dateFormatter.format(new Date()),
|
dateFormatter.format(new Date()),
|
||||||
packetReceivedIcon,
|
packetReceivedIcon,
|
||||||
packetTypeIcon,
|
packetTypeIcon,
|
||||||
|
@ -895,7 +887,7 @@ public class EnhancedDebugger extends SmackDebugger {
|
||||||
|
|
||||||
messagesTable.addRow(
|
messagesTable.addRow(
|
||||||
new Object[] {
|
new Object[] {
|
||||||
formatXML(packet.toXML().toString()),
|
XmlUtil.prettyFormatXml(packet.toXML().toString()),
|
||||||
dateFormatter.format(new Date()),
|
dateFormatter.format(new Date()),
|
||||||
packetSentIcon,
|
packetSentIcon,
|
||||||
packetTypeIcon,
|
packetTypeIcon,
|
||||||
|
@ -911,40 +903,6 @@ public class EnhancedDebugger extends SmackDebugger {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private String formatXML(String str) {
|
|
||||||
try {
|
|
||||||
// Use a Transformer for output
|
|
||||||
TransformerFactory tFactory = TransformerFactory.newInstance();
|
|
||||||
// Surround this setting in a try/catch for compatibility with Java 1.4. This setting is required
|
|
||||||
// for Java 1.5
|
|
||||||
try {
|
|
||||||
tFactory.setAttribute("indent-number", 2);
|
|
||||||
}
|
|
||||||
catch (IllegalArgumentException e) {
|
|
||||||
// Ignore
|
|
||||||
}
|
|
||||||
Transformer transformer = tFactory.newTransformer();
|
|
||||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
|
||||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
||||||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
|
||||||
|
|
||||||
// Transform the requested string into a nice formatted XML string
|
|
||||||
StreamSource source = new StreamSource(new StringReader(str));
|
|
||||||
StringWriter sw = new StringWriter();
|
|
||||||
StreamResult result = new StreamResult(sw);
|
|
||||||
transformer.transform(source, result);
|
|
||||||
return sw.toString();
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (TransformerConfigurationException tce) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Transformer Factory error", tce);
|
|
||||||
}
|
|
||||||
catch (TransformerException te) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Transformation error", te);
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the debugger's connection with the server is up and running.
|
* Returns true if the debugger's connection with the server is up and running.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue