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.util.Locale;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.jid.parts.Resourcepart;
@ -27,6 +28,12 @@ import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
public class ParserUtils {
/**
* The constant String "jid".
*/
public static final String JID = "jid";
public static void assertAtStartTag(XmlPullParser parser) throws XmlPullParserException {
assert(parser.getEventType() == XmlPullParser.START_TAG);
}
@ -44,7 +51,7 @@ public class ParserUtils {
}
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 {
@ -55,6 +62,18 @@ public class ParserUtils {
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 {
final String resourcepartString = parser.getAttributeValue("", name);
if (resourcepartString == null) {

View File

@ -655,15 +655,13 @@ public class MultiUserChat {
* @throws NotConnectedException
* @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();
iq.setTo(room);
iq.setType(IQ.Type.set);
// Create the reason for the room destruction
Destroy destroy = new Destroy();
destroy.setReason(reason);
destroy.setJid(alternateJID);
Destroy destroy = new Destroy(alternateJID, reason);
iq.setDestroy(destroy);
connection.createPacketCollectorAndSend(iq).nextResultOrThrow();

View File

@ -16,8 +16,12 @@
*/
package org.jivesoftware.smackx.muc.packet;
import java.io.Serializable;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.TypedCloneable;
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
@ -26,18 +30,32 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
*
* @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";
private String reason;
private String jid;
private final String reason;
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.
*
* @return the JID of an alternate location.
*/
public String getJid() {
public BareJid getJid() {
return jid;
}
@ -50,24 +68,6 @@ public class Destroy implements NamedElement {
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
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);
@ -83,4 +83,8 @@ public class Destroy implements NamedElement {
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.packet.Destroy;
import org.jivesoftware.smackx.muc.packet.MUCItem;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.parts.Resourcepart;
import org.xmlpull.v1.XmlPullParser;
@ -61,22 +62,27 @@ public class MUCParserUtils {
}
public static Destroy parseDestroy(XmlPullParser parser) throws XmlPullParserException, IOException {
boolean done = false;
Destroy destroy = new Destroy();
destroy.setJid(parser.getAttributeValue("", "jid"));
while (!done) {
final int initialDepth = parser.getDepth();
final BareJid jid = ParserUtils.getBareJidAttribute(parser);
String reason = null;
outerloop: while (true) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("reason")) {
destroy.setReason(parser.nextText());
switch (eventType) {
case XmlPullParser.START_TAG:
final String name = parser.getName();
switch (name) {
case "reason":
reason = parser.nextText();
break;
}
}
else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("destroy")) {
done = true;
break;
case XmlPullParser.END_TAG:
if (initialDepth == parser.getDepth()) {
break outerloop;
}
break;
}
}
return destroy;
return new Destroy(jid, reason);
}
}