From 516e39767994e8cf4a1b21caef6a35ca5bcc6d16 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 11 Mar 2015 21:31:37 +0100 Subject: [PATCH] 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. --- .../jivesoftware/smack/util/ParserUtils.java | 21 +++++++- .../smackx/muc/MultiUserChat.java | 6 +-- .../smackx/muc/packet/Destroy.java | 48 ++++++++++--------- .../smackx/muc/provider/MUCParserUtils.java | 30 +++++++----- 4 files changed, 66 insertions(+), 39 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java index 9091b4688..6da85350c 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java @@ -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) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java index 1d023ef95..0be33da50 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java @@ -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(); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/Destroy.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/Destroy.java index a24391bcf..fb43af243 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/Destroy.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/Destroy.java @@ -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, 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); + } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCParserUtils.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCParserUtils.java index a143d7418..8a14c8677 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCParserUtils.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCParserUtils.java @@ -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); } }