1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 10:37:59 +01:00

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).
This commit is contained in:
Florian Schmaus 2020-09-17 12:31:35 +02:00
parent 9e9d30074c
commit b7824f008d
6 changed files with 29 additions and 7 deletions

View file

@ -590,7 +590,7 @@ public final class Message extends MessageOrPresence<MessageBuilder>
public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingXmlEnvironment);
xml.rightAngleBracket();
xml.escape(message);
xml.text(message);
xml.closeElement(getElementName());
return xml;
}

View file

@ -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<QName, StandardExtensionElement> entry : elements.entrySet()) {

View file

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

View file

@ -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 <a href="https://issues.igniterealtime.org/browse/SMACK-892">SMACK-892</a>
*/
@Test
public void escapeInBodyTest() {
String theFive = "\"'<>&";
Message.Body body = new Message.Body(null, theFive);
assertEquals("<body xmlns='jabber:client'>\"'&lt;>&amp;</body>", body.toXML().toString());
}
}

View file

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

View file

@ -33,7 +33,7 @@ import org.jxmpp.jid.impl.JidCreate;
public class MarkableExtensionTest {
String markableMessageStanza = "<message xmlns='jabber:client' to='ingrichard@royalty.england.lit/throne' id='message-1'>"
+ "<body>My lord, dispatch; read o&apos;er these articles.</body>"
+ "<body>My lord, dispatch; read o'er these articles.</body>"
+ "<markable xmlns='urn:xmpp:chat-markers:0'/>" + "</message>";
String markableExtension = "<markable xmlns='urn:xmpp:chat-markers:0'/>";