1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 03:52:06 +01:00

[core] Pass down the XML environment in IQChildElementXmlStringBuilder

This allows to avoid redundant XML namespaces within IQs, like for
example here:

<iq xmlns='jabber:client' id='EKP8I-1' type='set'>
    <jingle xmlns='urn:xmpp:jingle:1' action='content-accept' sid='MySession'>
        <content xmlns='urn:xmpp:jingle:1' creator='initiator' name='Hello world'>
        </content>
    </jingle>
</iq>

Fixes SMACK-917

Reported-by: Jonathan Lennox
This commit is contained in:
Florian Schmaus 2021-10-19 11:29:23 +02:00
parent 585bcb4dc8
commit b243a40e26
5 changed files with 19 additions and 18 deletions

View file

@ -202,7 +202,7 @@ public abstract class IQ extends Stanza implements IqView {
// Add the query section if there is one.
IQChildElementXmlStringBuilder iqChildElement = getIQChildElementBuilder(
new IQChildElementXmlStringBuilder(this));
new IQChildElementXmlStringBuilder(getChildElementName(), getChildElementNamespace(), null, xml.getXmlEnvironment()));
// TOOD: Document the cases where iqChildElement is null but childElementName not. And if there are none, change
// the logic.
if (iqChildElement == null) {
@ -399,17 +399,16 @@ public abstract class IQ extends Stanza implements IqView {
private boolean isEmptyElement;
private IQChildElementXmlStringBuilder(IQ iq) {
this(iq.getChildElementName(), iq.getChildElementNamespace());
public IQChildElementXmlStringBuilder(ExtensionElement extensionElement,
XmlEnvironment enclosingXmlEnvironment) {
this(extensionElement.getElementName(), extensionElement.getNamespace(), extensionElement.getLanguage(),
enclosingXmlEnvironment);
}
public IQChildElementXmlStringBuilder(ExtensionElement pe) {
this(pe.getElementName(), pe.getNamespace());
}
private IQChildElementXmlStringBuilder(String element, String namespace) {
prelude(element, namespace);
this.element = element;
private IQChildElementXmlStringBuilder(String elementName, String xmlNs, String xmlLang,
XmlEnvironment enclosingXmlEnvironment) {
super(elementName, xmlNs, xmlLang, enclosingXmlEnvironment);
this.element = elementName;
}
public void setEmptyElement() {

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2014-2020 Florian Schmaus
* Copyright 2014-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -53,11 +53,13 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
}
public XmlStringBuilder(FullyQualifiedElement element, XmlEnvironment enclosingXmlEnvironment) {
sb = new LazyStringBuilder();
halfOpenElement(element);
this(element.getElementName(), element.getNamespace(), element.getLanguage(), enclosingXmlEnvironment);
}
public XmlStringBuilder(String elementName, String xmlNs, String xmlLang, XmlEnvironment enclosingXmlEnvironment) {
sb = new LazyStringBuilder();
halfOpenElement(elementName);
String xmlNs = element.getNamespace();
String xmlLang = element.getLanguage();
if (enclosingXmlEnvironment == null) {
xmlnsAttribute(xmlNs);
xmllangAttribute(xmlLang);

View file

@ -153,7 +153,7 @@ public class DataPacketExtension implements ExtensionElement {
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this));
XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this, enclosingNamespace));
xml.closeElement(this);
return xml;
}

View file

@ -114,7 +114,7 @@ public class RoomInvitation implements ExtensionElement {
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this));
XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this, enclosingNamespace));
xml.closeElement(this);
return xml;
}

View file

@ -109,7 +109,7 @@ public class RoomTransfer implements ExtensionElement {
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this));
XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this, enclosingNamespace));
xml.closeElement(this);
return xml;
}