From b7824f008d27d1f19ce8aa09cc1d6b4ebf0f3c2c Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 17 Sep 2020 12:31:35 +0200 Subject: [PATCH] Introduce and use XmlStringBuilder.text() Smack currently does unnecessary escaping of XML text, where it escapes e.g. '"' to '"'. This bloats the stanza size, especially if JSON payloads are involved. Fixes SMACK-892 (although there are probably still places where XmlStringBuilder.escape() is used when XmlStringBuild.text() could have been used). --- .../java/org/jivesoftware/smack/packet/Message.java | 2 +- .../smack/packet/StandardExtensionElement.java | 6 ++++-- .../jivesoftware/smack/util/XmlStringBuilder.java | 9 ++++++++- .../org/jivesoftware/smack/packet/MessageTest.java | 13 +++++++++++++ .../json/packet/AbstractJsonPacketExtension.java | 4 ++-- .../smackx/chat_markers/MarkableExtensionTest.java | 2 +- 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java index a85f7c5b6..61e43552b 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java @@ -590,7 +590,7 @@ public final class Message extends MessageOrPresence public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) { XmlStringBuilder xml = new XmlStringBuilder(this, enclosingXmlEnvironment); xml.rightAngleBracket(); - xml.escape(message); + xml.text(message); xml.closeElement(getElementName()); return xml; } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/StandardExtensionElement.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/StandardExtensionElement.java index fd7229b84..d78ce1845 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/StandardExtensionElement.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/StandardExtensionElement.java @@ -1,6 +1,6 @@ /** * - * Copyright 2015-2019 Florian Schmaus. + * Copyright 2015-2020 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -142,7 +142,9 @@ public final class StandardExtensionElement implements ExtensionElement { } xml.rightAngleBracket(); - xml.optEscape(text); + if (text != null) { + xml.text(text); + } if (elements != null) { for (Map.Entry entry : elements.entrySet()) { diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java index c483db913..f32e8dc7c 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java @@ -1,6 +1,6 @@ /** * - * Copyright 2014-2019 Florian Schmaus + * Copyright 2014-2020 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -457,6 +457,13 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element { return this; } + public XmlStringBuilder text(CharSequence text) { + assert text != null; + CharSequence escapedText = StringUtils.escapeForXmlText(text); + sb.append(escapedText); + return this; + } + public XmlStringBuilder escape(String text) { assert text != null; sb.append(StringUtils.escapeForXml(text)); diff --git a/smack-core/src/test/java/org/jivesoftware/smack/packet/MessageTest.java b/smack-core/src/test/java/org/jivesoftware/smack/packet/MessageTest.java index 4c45be4fe..cba14e74f 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/packet/MessageTest.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/packet/MessageTest.java @@ -206,4 +206,17 @@ public class MessageTest { assertXmlSimilar(control, message.toXML(StreamOpen.CLIENT_NAMESPACE).toString()); } + + /** + * Tests that only required characters are XML escaped in body. + * + * @see SMACK-892 + */ + @Test + public void escapeInBodyTest() { + String theFive = "\"'<>&"; + Message.Body body = new Message.Body(null, theFive); + + assertEquals("\"'<>&", body.toXML().toString()); + } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/json/packet/AbstractJsonPacketExtension.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/json/packet/AbstractJsonPacketExtension.java index 4d395debf..bcdc3f858 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/json/packet/AbstractJsonPacketExtension.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/json/packet/AbstractJsonPacketExtension.java @@ -1,6 +1,6 @@ /** * - * Copyright © 2014 Florian Schmaus + * Copyright © 2014-2020 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,7 +35,7 @@ public abstract class AbstractJsonPacketExtension implements ExtensionElement { public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { XmlStringBuilder xml = new XmlStringBuilder(this); xml.rightAngleBracket(); - xml.append(json); + xml.text(json); xml.closeElement(this); return xml; } diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/chat_markers/MarkableExtensionTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/chat_markers/MarkableExtensionTest.java index 2b08e1c1b..ff4198006 100644 --- a/smack-experimental/src/test/java/org/jivesoftware/smackx/chat_markers/MarkableExtensionTest.java +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/chat_markers/MarkableExtensionTest.java @@ -33,7 +33,7 @@ import org.jxmpp.jid.impl.JidCreate; public class MarkableExtensionTest { String markableMessageStanza = "" - + "My lord, dispatch; read o'er these articles." + + "My lord, dispatch; read o'er these articles." + "" + ""; String markableExtension = "";