1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-23 19:18:00 +01:00

[pubsub] Rework NodeExtension.toXML()

This commit is contained in:
Florian Schmaus 2020-06-14 16:52:13 +02:00
parent c689bef7ec
commit 9d6665735f
14 changed files with 76 additions and 145 deletions

View file

@ -32,7 +32,6 @@ import org.jivesoftware.smackx.pubsub.Affiliation.AffiliationNamespace;
*/
public class AffiliationsExtension extends NodeExtension {
protected List<Affiliation> items = Collections.emptyList();
private final String node;
public AffiliationsExtension() {
this(null);
@ -51,9 +50,8 @@ public class AffiliationsExtension extends NodeExtension {
}
public AffiliationsExtension(AffiliationNamespace affiliationsNamespace, List<Affiliation> subList, String node) {
super(affiliationsNamespace.type);
super(affiliationsNamespace.type, node);
items = subList;
this.node = node;
}
public List<Affiliation> getAffiliations() {
@ -61,19 +59,14 @@ public class AffiliationsExtension extends NodeExtension {
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
protected void addXml(XmlStringBuilder xml) {
if ((items == null) || (items.size() == 0)) {
return super.toXML(enclosingNamespace);
}
else {
// Can't use XmlStringBuilder(this), because we don't want the namespace to be included
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(getElementName());
xml.optAttribute("node", node);
xml.rightAngleBracket();
xml.append(items);
xml.closeElement(this);
return xml;
xml.closeEmptyElement();
return;
}
xml.rightAngleBracket();
xml.append(items);
xml.closeElement(this);
}
}

View file

@ -16,6 +16,8 @@
*/
package org.jivesoftware.smackx.pubsub;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.xdata.packet.DataForm;
/**
@ -69,26 +71,14 @@ public class FormNode extends NodeExtension {
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
protected void addXml(XmlStringBuilder xml) {
if (configForm == null) {
return super.toXML(enclosingNamespace);
xml.closeEmptyElement();
return;
}
else {
StringBuilder builder = new StringBuilder("<");
builder.append(getElementName());
if (getNode() != null) {
builder.append(" node='");
builder.append(getNode());
builder.append("'>");
}
else
builder.append('>');
builder.append(configForm.toXML());
builder.append("</");
builder.append(getElementName() + '>');
return builder.toString();
}
xml.append(configForm);
xml.closeElement(this);
}
}

View file

@ -54,13 +54,9 @@ public class GetItemsRequest extends NodeExtension {
}
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(getElementName());
xml.attribute("node", getNode());
protected void addXml(XmlStringBuilder xml) {
xml.optAttribute("subid", getSubscriptionId());
xml.optIntAttribute("max_items", getMaxItems());
xml.closeEmptyElement();
return xml;
}
}

View file

@ -150,21 +150,9 @@ public class Item extends NodeExtension {
}
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = getCommonXml();
xml.closeEmptyElement();
return xml;
}
protected final XmlStringBuilder getCommonXml() {
XmlStringBuilder xml = new XmlStringBuilder(this);
protected void addXml(XmlStringBuilder xml) {
xml.optAttribute("id", getId());
xml.optAttribute("node", getNode());
return xml;
xml.closeEmptyElement();
}
@Override

View file

@ -20,6 +20,7 @@ import java.util.List;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* This class is used for multiple purposes.
@ -150,35 +151,21 @@ public class ItemsExtension extends NodeExtension implements EmbeddedPacketExten
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
protected void addXml(XmlStringBuilder xml) {
if ((items == null) || (items.size() == 0)) {
return super.toXML(enclosingNamespace);
xml.closeEmptyElement();
return;
}
else {
StringBuilder builder = new StringBuilder("<");
builder.append(getElementName());
builder.append(" node='");
builder.append(getNode());
if (notify != null) {
builder.append("' ");
builder.append(type.getElementAttribute());
builder.append("='");
builder.append(notify.equals(Boolean.TRUE) ? 1 : 0);
builder.append("'>");
}
else {
builder.append("'>");
for (NamedElement item : items) {
builder.append(item.toXML());
}
}
builder.append("</");
builder.append(getElementName());
builder.append('>');
return builder.toString();
if (notify != null) {
xml.attribute(type.getElementAttribute(), notify);
xml.rightAngleBracket();
} else {
xml.rightAngleBracket();
xml.append(items);
}
xml.closeElement(this);
}
@Override

View file

@ -17,6 +17,8 @@
package org.jivesoftware.smackx.pubsub;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
@ -78,8 +80,17 @@ public class NodeExtension implements ExtensionElement {
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
return '<' + getElementName() + (node == null ? "" : " node='" + node + '\'') + "/>";
public final XmlStringBuilder toXML(XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.optAttribute("node", node);
addXml(xml);
return xml;
}
protected void addXml(XmlStringBuilder xml) {
xml.closeEmptyElement();
}
@Override

View file

@ -50,13 +50,15 @@ public class OptionsExtension extends NodeExtension {
}
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder();
protected void addXml(XmlStringBuilder xml) {
xml.rightAngleBracket();
xml.halfOpenElement(getElementName());
xml.attribute("jid", jid);
xml.optAttribute("node", getNode());
xml.optAttribute("subid", id);
xml.closeEmptyElement();
return xml;
xml.closeElement(this);
}
}

View file

@ -132,14 +132,11 @@ public class PayloadItem<E extends ExtensionElement> extends Item {
}
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = getCommonXml();
protected void addXml(XmlStringBuilder xml) {
xml.optAttribute("id", getId());
xml.rightAngleBracket();
xml.append(payload.toXML());
xml.append(payload);
xml.closeElement(this);
return xml;
}
@Override

View file

@ -19,6 +19,8 @@ package org.jivesoftware.smackx.pubsub;
import java.util.ArrayList;
import java.util.Collection;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* Represents a request to publish an item(s) to a specific node.
*
@ -51,18 +53,9 @@ public class PublishItem<T extends Item> extends NodeExtension {
}
@Override
public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
StringBuilder builder = new StringBuilder("<");
builder.append(getElementName());
builder.append(" node='");
builder.append(getNode());
builder.append("'>");
for (Item item : items) {
builder.append(item.toXML());
}
builder.append("</publish>");
return builder.toString();
protected void addXml(XmlStringBuilder xml) {
xml.rightAngleBracket();
xml.append(items);
xml.closeElement(this);
}
}

View file

@ -16,7 +16,6 @@
*/
package org.jivesoftware.smackx.pubsub;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jxmpp.jid.Jid;
@ -44,11 +43,8 @@ public class SubscribeExtension extends NodeExtension {
}
@Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment);
xml.optAttribute("node", getNode());
protected void addXml(XmlStringBuilder xml) {
xml.attribute("jid", getJid());
xml.closeEmptyElement();
return xml;
}
}

View file

@ -138,16 +138,11 @@ public class Subscription extends NodeExtension {
}
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder builder = new XmlStringBuilder(this);
builder.attribute("jid", jid);
builder.optAttribute("node", getNode());
builder.optAttribute("subid", id);
builder.optAttribute("subscription", state.toString());
builder.closeEmptyElement();
return builder;
protected void addXml(XmlStringBuilder xml) {
xml.attribute("jid", jid);
xml.optAttribute("subid", id);
xml.optAttribute("subscription", state);
xml.closeEmptyElement();
}
}

View file

@ -19,6 +19,8 @@ package org.jivesoftware.smackx.pubsub;
import java.util.Collections;
import java.util.List;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* Represents the element holding the list of subscription elements.
*
@ -91,29 +93,13 @@ public class SubscriptionsExtension extends NodeExtension {
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
protected void addXml(XmlStringBuilder xml) {
if ((items == null) || (items.size() == 0)) {
return super.toXML(enclosingNamespace);
}
else {
StringBuilder builder = new StringBuilder("<");
builder.append(getElementName());
if (getNode() != null) {
builder.append(" node='");
builder.append(getNode());
builder.append('\'');
}
builder.append('>');
for (Subscription item : items) {
builder.append(item.toXML());
}
builder.append("</");
builder.append(getElementName());
builder.append('>');
return builder.toString();
xml.closeEmptyElement();
return;
}
xml.rightAngleBracket();
xml.append(items);
xml.closeElement(this);
}
}

View file

@ -51,13 +51,9 @@ public class UnsubscribeExtension extends NodeExtension {
}
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(getElementName());
protected void addXml(XmlStringBuilder xml) {
xml.attribute("jid", jid);
xml.optAttribute("node", getNode());
xml.optAttribute("subid", id);
xml.closeEmptyElement();
return xml;
}
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2017 Florian Schmaus
* Copyright 2017-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.
@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smackx.pubsub.Affiliation.Type;
import org.jivesoftware.smackx.pubsub.packet.PubSub;
import org.junit.jupiter.api.Test;
import org.jxmpp.jid.BareJid;
@ -40,10 +41,10 @@ public class AffiliationsExtensionTest {
AffiliationsExtension affiliationsExtension = new AffiliationsExtension(affiliationsList, "testNode");
CharSequence xml = affiliationsExtension.toXML();
CharSequence xml = affiliationsExtension.toXML(PubSub.NAMESPACE);
assertXmlSimilar("<affiliations node='testNode'><affiliation xmlns='http://jabber.org/protocol/pubsub#owner' jid='one@exampleone.org' affiliation='member'/></affiliations>",
xml.toString());
xml);
}
}