1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-29 23:42:06 +01:00

Improve MUC's Destroy element class

- Made jid of type BareJid
- Made it implement TypedCloneable
- Made it implement Serializable
- Made it immutable

Also update its parsing code. And add some convenience methods to
ParserUtils.
This commit is contained in:
Florian Schmaus 2015-03-11 21:31:37 +01:00
parent 369878b6d9
commit 516e397679
4 changed files with 66 additions and 39 deletions

View file

@ -19,6 +19,7 @@ package org.jivesoftware.smack.util;
import java.io.IOException; import java.io.IOException;
import java.util.Locale; import java.util.Locale;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.Jid; import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.jid.parts.Resourcepart; import org.jxmpp.jid.parts.Resourcepart;
@ -27,6 +28,12 @@ import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
public class ParserUtils { public class ParserUtils {
/**
* The constant String "jid".
*/
public static final String JID = "jid";
public static void assertAtStartTag(XmlPullParser parser) throws XmlPullParserException { public static void assertAtStartTag(XmlPullParser parser) throws XmlPullParserException {
assert(parser.getEventType() == XmlPullParser.START_TAG); assert(parser.getEventType() == XmlPullParser.START_TAG);
} }
@ -44,7 +51,7 @@ public class ParserUtils {
} }
public static Jid getJidAttribute(XmlPullParser parser) throws XmppStringprepException { public static Jid getJidAttribute(XmlPullParser parser) throws XmppStringprepException {
return getJidAttribute(parser, "jid"); return getJidAttribute(parser, JID);
} }
public static Jid getJidAttribute(XmlPullParser parser, String name) throws XmppStringprepException { public static Jid getJidAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
@ -55,6 +62,18 @@ public class ParserUtils {
return JidCreate.from(jidString); return JidCreate.from(jidString);
} }
public static BareJid getBareJidAttribute(XmlPullParser parser) throws XmppStringprepException {
return getBareJidAttribute(parser, JID);
}
public static BareJid getBareJidAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
final String jidString = parser.getAttributeValue("", name);
if (jidString == null) {
return null;
}
return JidCreate.bareFrom(jidString);
}
public static Resourcepart getResourcepartAttribute(XmlPullParser parser, String name) throws XmppStringprepException { public static Resourcepart getResourcepartAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
final String resourcepartString = parser.getAttributeValue("", name); final String resourcepartString = parser.getAttributeValue("", name);
if (resourcepartString == null) { if (resourcepartString == null) {

View file

@ -655,15 +655,13 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
* @throws InterruptedException * @throws InterruptedException
*/ */
public void destroy(String reason, String alternateJID) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { public void destroy(String reason, BareJid alternateJID) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
MUCOwner iq = new MUCOwner(); MUCOwner iq = new MUCOwner();
iq.setTo(room); iq.setTo(room);
iq.setType(IQ.Type.set); iq.setType(IQ.Type.set);
// Create the reason for the room destruction // Create the reason for the room destruction
Destroy destroy = new Destroy(); Destroy destroy = new Destroy(alternateJID, reason);
destroy.setReason(reason);
destroy.setJid(alternateJID);
iq.setDestroy(destroy); iq.setDestroy(destroy);
connection.createPacketCollectorAndSend(iq).nextResultOrThrow(); connection.createPacketCollectorAndSend(iq).nextResultOrThrow();

View file

@ -16,8 +16,12 @@
*/ */
package org.jivesoftware.smackx.muc.packet; package org.jivesoftware.smackx.muc.packet;
import java.io.Serializable;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.TypedCloneable;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jxmpp.jid.BareJid;
/** /**
* Represents a request to the server to destroy a room. The sender of the request should be the * Represents a request to the server to destroy a room. The sender of the request should be the
@ -26,18 +30,32 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class Destroy implements NamedElement { public class Destroy implements NamedElement, TypedCloneable<Destroy>, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final String ELEMENT = "destroy"; public static final String ELEMENT = "destroy";
private String reason; private final String reason;
private String jid; private final BareJid jid;
public Destroy(Destroy other) {
this(other.jid, other.reason);
}
public Destroy(BareJid alternativeJid, String reason) {
this.jid = alternativeJid;
this.reason = reason;
}
/** /**
* Returns the JID of an alternate location since the current room is being destroyed. * Returns the JID of an alternate location since the current room is being destroyed.
* *
* @return the JID of an alternate location. * @return the JID of an alternate location.
*/ */
public String getJid() { public BareJid getJid() {
return jid; return jid;
} }
@ -50,24 +68,6 @@ public class Destroy implements NamedElement {
return reason; return reason;
} }
/**
* Sets the JID of an alternate location since the current room is being destroyed.
*
* @param jid the JID of an alternate location.
*/
public void setJid(String jid) {
this.jid = jid;
}
/**
* Sets the reason for the room destruction.
*
* @param reason the reason for the room destruction.
*/
public void setReason(String reason) {
this.reason = reason;
}
@Override @Override
public XmlStringBuilder toXML() { public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this);
@ -83,4 +83,8 @@ public class Destroy implements NamedElement {
return ELEMENT; return ELEMENT;
} }
@Override
public Destroy clone() {
return new Destroy(this);
}
} }

View file

@ -23,6 +23,7 @@ import org.jivesoftware.smackx.muc.MUCAffiliation;
import org.jivesoftware.smackx.muc.MUCRole; import org.jivesoftware.smackx.muc.MUCRole;
import org.jivesoftware.smackx.muc.packet.Destroy; import org.jivesoftware.smackx.muc.packet.Destroy;
import org.jivesoftware.smackx.muc.packet.MUCItem; import org.jivesoftware.smackx.muc.packet.MUCItem;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.Jid; import org.jxmpp.jid.Jid;
import org.jxmpp.jid.parts.Resourcepart; import org.jxmpp.jid.parts.Resourcepart;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -61,22 +62,27 @@ public class MUCParserUtils {
} }
public static Destroy parseDestroy(XmlPullParser parser) throws XmlPullParserException, IOException { public static Destroy parseDestroy(XmlPullParser parser) throws XmlPullParserException, IOException {
boolean done = false; final int initialDepth = parser.getDepth();
Destroy destroy = new Destroy(); final BareJid jid = ParserUtils.getBareJidAttribute(parser);
destroy.setJid(parser.getAttributeValue("", "jid")); String reason = null;
while (!done) { outerloop: while (true) {
int eventType = parser.next(); int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) { switch (eventType) {
if (parser.getName().equals("reason")) { case XmlPullParser.START_TAG:
destroy.setReason(parser.nextText()); final String name = parser.getName();
switch (name) {
case "reason":
reason = parser.nextText();
break;
}
break;
case XmlPullParser.END_TAG:
if (initialDepth == parser.getDepth()) {
break outerloop;
}
break;
} }
} }
else if (eventType == XmlPullParser.END_TAG) { return new Destroy(jid, reason);
if (parser.getName().equals("destroy")) {
done = true;
}
}
}
return destroy;
} }
} }