1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02:00

Add XmlUtil.prettyFormatXml()

This commit is contained in:
Florian Schmaus 2017-09-10 16:46:26 +02:00
parent 8020fc1416
commit 255865f733
3 changed files with 109 additions and 45 deletions

View file

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

View file

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