1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 20:12:07 +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 { public class AffiliationsExtension extends NodeExtension {
protected List<Affiliation> items = Collections.emptyList(); protected List<Affiliation> items = Collections.emptyList();
private final String node;
public AffiliationsExtension() { public AffiliationsExtension() {
this(null); this(null);
@ -51,9 +50,8 @@ public class AffiliationsExtension extends NodeExtension {
} }
public AffiliationsExtension(AffiliationNamespace affiliationsNamespace, List<Affiliation> subList, String node) { public AffiliationsExtension(AffiliationNamespace affiliationsNamespace, List<Affiliation> subList, String node) {
super(affiliationsNamespace.type); super(affiliationsNamespace.type, node);
items = subList; items = subList;
this.node = node;
} }
public List<Affiliation> getAffiliations() { public List<Affiliation> getAffiliations() {
@ -61,19 +59,14 @@ public class AffiliationsExtension extends NodeExtension {
} }
@Override @Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { protected void addXml(XmlStringBuilder xml) {
if ((items == null) || (items.size() == 0)) { if ((items == null) || (items.size() == 0)) {
return super.toXML(enclosingNamespace); xml.closeEmptyElement();
return;
} }
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.rightAngleBracket();
xml.append(items); xml.append(items);
xml.closeElement(this); xml.closeElement(this);
return xml;
}
} }
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -51,13 +51,9 @@ public class UnsubscribeExtension extends NodeExtension {
} }
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { protected void addXml(XmlStringBuilder xml) {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(getElementName());
xml.attribute("jid", jid); xml.attribute("jid", jid);
xml.optAttribute("node", getNode());
xml.optAttribute("subid", id); xml.optAttribute("subid", id);
xml.closeEmptyElement(); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 java.util.List;
import org.jivesoftware.smackx.pubsub.Affiliation.Type; import org.jivesoftware.smackx.pubsub.Affiliation.Type;
import org.jivesoftware.smackx.pubsub.packet.PubSub;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.jxmpp.jid.BareJid; import org.jxmpp.jid.BareJid;
@ -40,10 +41,10 @@ public class AffiliationsExtensionTest {
AffiliationsExtension affiliationsExtension = new AffiliationsExtension(affiliationsList, "testNode"); 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>", assertXmlSimilar("<affiliations node='testNode'><affiliation xmlns='http://jabber.org/protocol/pubsub#owner' jid='one@exampleone.org' affiliation='member'/></affiliations>",
xml.toString()); xml);
} }
} }