diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java index ff828f648..23f4e79b3 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java @@ -66,6 +66,13 @@ public class XmlStringBuilder implements Appendable, CharSequence { return this; } + public XmlStringBuilder optElement(Element element) { + if (element != null) { + append(element.toXML()); + } + return this; + } + public XmlStringBuilder optElement(String name, Enum content) { if (content != null) { element(name, content); @@ -139,6 +146,20 @@ public class XmlStringBuilder implements Appendable, CharSequence { return this; } + /** + * Add the given attribute if value => 0 + * + * @param name + * @param value + * @return a reference to this object + */ + public XmlStringBuilder optIntAttribute(String name, int value) { + if (value >= 0) { + attribute(name, Integer.toString(value)); + } + return this; + } + public XmlStringBuilder xmlnsAttribute(String value) { optAttribute("xmlns", value); return this; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/Affiliate.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/Affiliate.java index aad0b9717..72d842f17 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/Affiliate.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/Affiliate.java @@ -17,8 +17,7 @@ package org.jivesoftware.smackx.muc; -import org.jivesoftware.smackx.muc.packet.MUCAdmin; -import org.jivesoftware.smackx.muc.packet.MUCOwner; +import org.jivesoftware.smackx.muc.packet.MUCItem; /** * Represents an affiliation of a user to a given room. The affiliate's information will always have @@ -36,15 +35,7 @@ public class Affiliate { private String role; private String nick; - Affiliate(MUCOwner.Item item) { - super(); - this.jid = item.getJid(); - this.affiliation = item.getAffiliation(); - this.role = item.getRole(); - this.nick = item.getNick(); - } - - Affiliate(MUCAdmin.Item item) { + Affiliate(MUCItem item) { super(); this.jid = item.getJid(); this.affiliation = item.getAffiliation(); 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 c9ee1cf55..6ef8a6884 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 @@ -64,8 +64,10 @@ import org.jivesoftware.smackx.disco.NodeInformationProvider; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.packet.DiscoverInfo; import org.jivesoftware.smackx.disco.packet.DiscoverItems; +import org.jivesoftware.smackx.muc.packet.Destroy; import org.jivesoftware.smackx.muc.packet.MUCAdmin; import org.jivesoftware.smackx.muc.packet.MUCInitialPresence; +import org.jivesoftware.smackx.muc.packet.MUCItem; import org.jivesoftware.smackx.muc.packet.MUCOwner; import org.jivesoftware.smackx.muc.packet.MUCUser; import org.jivesoftware.smackx.xdata.Form; @@ -73,7 +75,7 @@ import org.jivesoftware.smackx.xdata.Form; /** * A MultiUserChat is a conversation that takes place among many users in a virtual * room. A room could have many occupants with different affiliation and roles. - * Possible affiliatons are "owner", "admin", "member", and "outcast". Possible roles + * Possible affiliations are "owner", "admin", "member", and "outcast". Possible roles * are "moderator", "participant", and "visitor". Each role and affiliation guarantees * different privileges (e.g. Send messages to all occupants, Kick participants and visitors, * Grant voice, Edit member list, etc.). @@ -83,8 +85,7 @@ import org.jivesoftware.smackx.xdata.Form; public class MultiUserChat { private static final Logger LOGGER = Logger.getLogger(MultiUserChat.class.getName()); - private final static String discoNamespace = "http://jabber.org/protocol/muc"; - private final static String discoNode = "http://jabber.org/protocol/muc#rooms"; + private final static String DISCO_NODE = MUCInitialPresence.NAMESPACE + "#rooms"; private static Map> joinedRooms = new WeakHashMap>(); @@ -118,13 +119,13 @@ public class MultiUserChat { // Set on every established connection that this client supports the Multi-User // Chat protocol. This information will be used when another client tries to // discover whether this client supports MUC or not. - ServiceDiscoveryManager.getInstanceFor(connection).addFeature(discoNamespace); + ServiceDiscoveryManager.getInstanceFor(connection).addFeature(MUCInitialPresence.NAMESPACE); // Set the NodeInformationProvider that will provide information about the // joined rooms whenever a disco request is received final WeakReference weakRefConnection = new WeakReference(connection); ServiceDiscoveryManager.getInstanceFor(connection).setNodeInformationProvider( - discoNode, + DISCO_NODE, new NodeInformationProvider() { public List getNodeItems() { XMPPConnection connection = weakRefConnection.get(); @@ -187,7 +188,7 @@ public class MultiUserChat { public static boolean isServiceEnabled(XMPPConnection connection, String user) throws NoResponseException, XMPPErrorException, NotConnectedException { return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(user, - discoNamespace); + MUCInitialPresence.NAMESPACE); } /** @@ -223,7 +224,7 @@ public class MultiUserChat { ArrayList answer = new ArrayList(); // Send the disco packet to the user DiscoverItems result = ServiceDiscoveryManager.getInstanceFor(connection).discoverItems( - user, discoNode); + user, DISCO_NODE); // Collect the entityID for each returned item for (DiscoverItems.Item item : result.getItems()) { answer.add(item.getEntityID()); @@ -264,7 +265,7 @@ public class MultiUserChat { DiscoverItems items = discoManager.discoverItems(connection.getServiceName()); for (DiscoverItems.Item item : items.getItems()) { DiscoverInfo info = discoManager.discoverInfo(item.getEntityID()); - if (info.containsFeature(discoNamespace)) { + if (info.containsFeature(MUCInitialPresence.NAMESPACE)) { answer.add(item.getEntityID()); } } @@ -420,7 +421,7 @@ public class MultiUserChat { Presence presence = enter(nickname, null, null, connection.getPacketReplyTimeout()); // Look for confirmation of room creation from the server - MUCUser mucUser = getMUCUserExtension(presence); + MUCUser mucUser = MUCUser.getFrom(presence); if (mucUser != null && mucUser.getStatus() != null) { if ("201".equals(mucUser.getStatus().getCode())) { // Room was created and the user has joined the room @@ -661,7 +662,7 @@ public class MultiUserChat { iq.setType(IQ.Type.set); // Create the reason for the room destruction - MUCOwner.Destroy destroy = new MUCOwner.Destroy(); + Destroy destroy = new Destroy(); destroy.setReason(reason); destroy.setJid(alternateJID); iq.setDestroy(destroy); @@ -1371,7 +1372,7 @@ public class MultiUserChat { iq.setTo(room); iq.setType(IQ.Type.set); // Set the new affiliation. - MUCOwner.Item item = new MUCOwner.Item(affiliation); + MUCItem item = new MUCItem(affiliation); item.setJid(jid); iq.addItem(item); @@ -1385,7 +1386,7 @@ public class MultiUserChat { iq.setType(IQ.Type.set); for (String jid : jids) { // Set the new affiliation. - MUCOwner.Item item = new MUCOwner.Item(affiliation); + MUCItem item = new MUCItem(affiliation); item.setJid(jid); iq.addItem(item); } @@ -1409,7 +1410,7 @@ public class MultiUserChat { iq.setTo(room); iq.setType(IQ.Type.set); // Set the new affiliation. - MUCAdmin.Item item = new MUCAdmin.Item(affiliation, null); + MUCItem item = new MUCItem(affiliation, null); item.setJid(jid); item.setReason(reason); iq.addItem(item); @@ -1424,7 +1425,7 @@ public class MultiUserChat { iq.setType(IQ.Type.set); for (String jid : jids) { // Set the new affiliation. - MUCAdmin.Item item = new MUCAdmin.Item(affiliation, null); + MUCItem item = new MUCItem(affiliation, null); item.setJid(jid); iq.addItem(item); } @@ -1437,7 +1438,7 @@ public class MultiUserChat { iq.setTo(room); iq.setType(IQ.Type.set); // Set the new role. - MUCAdmin.Item item = new MUCAdmin.Item(null, role); + MUCItem item = new MUCItem(null, role); item.setNick(nickname); item.setReason(reason); iq.addItem(item); @@ -1451,7 +1452,7 @@ public class MultiUserChat { iq.setType(IQ.Type.set); for (String nickname : nicknames) { // Set the new role. - MUCAdmin.Item item = new MUCAdmin.Item(null, role); + MUCItem item = new MUCItem(null, role); item.setNick(nickname); iq.addItem(item); } @@ -1478,7 +1479,7 @@ public class MultiUserChat { * in the group chat. For example, "conference@chat.jivesoftware.com/SomeUser". * Typically, a client would only display the nickname of the occupant. To * get the nickname from the fully qualified name, use the - * {@link org.jivesoftware.smack.util.StringUtils#parseResource(String)} method. + * {@link org.jxmpp.util.XmppStringUtils#parseResource(String)} method. * Note: this value will only be accurate after joining the group chat, and may * fluctuate over time. * @@ -1606,14 +1607,14 @@ public class MultiUserChat { iq.setTo(room); iq.setType(IQ.Type.get); // Set the specified affiliation. This may request the list of owners/admins/members/outcasts. - MUCAdmin.Item item = new MUCAdmin.Item(affiliation, null); + MUCItem item = new MUCItem(affiliation, null); iq.addItem(item); MUCAdmin answer = (MUCAdmin) connection.createPacketCollectorAndSend(iq).nextResultOrThrow(); // Get the list of affiliates from the server's answer List affiliates = new ArrayList(); - for (MUCAdmin.Item mucadminItem : answer.getItems()) { + for (MUCItem mucadminItem : answer.getItems()) { affiliates.add(new Affiliate(mucadminItem)); } return affiliates; @@ -1658,13 +1659,13 @@ public class MultiUserChat { iq.setTo(room); iq.setType(IQ.Type.get); // Set the specified role. This may request the list of moderators/participants. - MUCAdmin.Item item = new MUCAdmin.Item(null, role); + MUCItem item = new MUCItem(null, role); iq.addItem(item); MUCAdmin answer = (MUCAdmin) connection.createPacketCollectorAndSend(iq).nextResultOrThrow(); // Get the list of participants from the server's answer List participants = new ArrayList(); - for (MUCAdmin.Item mucadminItem : answer.getItems()) { + for (MUCItem mucadminItem : answer.getItems()) { participants.add(new Occupant(mucadminItem)); } return participants; @@ -1828,20 +1829,6 @@ public class MultiUserChat { cleanup(); } - /** - * Returns the MUCUser packet extension included in the packet or null if none. - * - * @param packet the packet that may include the MUCUser extension. - * @return the MUCUser found in the packet. - */ - private MUCUser getMUCUserExtension(Packet packet) { - if (packet != null) { - // Get the MUC User extension - return (MUCUser) packet.getExtension("x", "http://jabber.org/protocol/muc#user"); - } - return null; - } - /** * Adds a listener that will be notified of changes in your status in the room * such as the user being kicked, banned, or granted admin permissions. @@ -1978,11 +1965,11 @@ public class MultiUserChat { Presence oldPresence = occupantsMap.put(from, presence); if (oldPresence != null) { // Get the previous occupant's affiliation & role - MUCUser mucExtension = getMUCUserExtension(oldPresence); + MUCUser mucExtension = MUCUser.getFrom(packet); String oldAffiliation = mucExtension.getItem().getAffiliation(); String oldRole = mucExtension.getItem().getRole(); // Get the new occupant's affiliation & role - mucExtension = getMUCUserExtension(presence); + mucExtension = MUCUser.getFrom(packet); String newAffiliation = mucExtension.getItem().getAffiliation(); String newRole = mucExtension.getItem().getRole(); // Fire role modification events @@ -2005,7 +1992,7 @@ public class MultiUserChat { } else if (presence.getType() == Presence.Type.unavailable) { occupantsMap.remove(from); - MUCUser mucUser = getMUCUserExtension(presence); + MUCUser mucUser = MUCUser.getFrom(packet); if (mucUser != null && mucUser.getStatus() != null) { // Fire events according to the received presence code checkPresenceCode( @@ -2030,7 +2017,7 @@ public class MultiUserChat { PacketListener declinesListener = new PacketListener() { public void processPacket(Packet packet) { // Get the MUC User extension - MUCUser mucUser = getMUCUserExtension(packet); + MUCUser mucUser = MUCUser.getFrom(packet); // Check if the MUCUser informs that the invitee has declined the invitation if (mucUser.getDecline() != null && ((Message) packet).getType() != Message.Type.error) { @@ -2513,8 +2500,7 @@ public class MultiUserChat { invitationPacketListener = new PacketListener() { public void processPacket(Packet packet) { // Get the MUCUser extension - MUCUser mucUser = - (MUCUser) packet.getExtension("x", "http://jabber.org/protocol/muc#user"); + MUCUser mucUser = MUCUser.getFrom(packet); // Check if the MUCUser extension includes an invitation if (mucUser.getInvite() != null && ((Message) packet).getType() != Message.Type.error) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/Occupant.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/Occupant.java index 64199ee5f..07bcc3027 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/Occupant.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/Occupant.java @@ -17,7 +17,7 @@ package org.jivesoftware.smackx.muc; -import org.jivesoftware.smackx.muc.packet.MUCAdmin; +import org.jivesoftware.smackx.muc.packet.MUCItem; import org.jivesoftware.smackx.muc.packet.MUCUser; import org.jivesoftware.smack.packet.Presence; import org.jxmpp.util.XmppStringUtils; @@ -36,8 +36,7 @@ public class Occupant { private String jid; private String nick; - Occupant(MUCAdmin.Item item) { - super(); + Occupant(MUCItem item) { this.jid = item.getJid(); this.affiliation = item.getAffiliation(); this.role = item.getRole(); @@ -45,10 +44,9 @@ public class Occupant { } Occupant(Presence presence) { - super(); MUCUser mucUser = (MUCUser) presence.getExtension("x", "http://jabber.org/protocol/muc#user"); - MUCUser.Item item = mucUser.getItem(); + MUCItem item = mucUser.getItem(); this.jid = item.getJid(); this.affiliation = item.getAffiliation(); this.role = item.getRole(); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/PacketMultiplexListener.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/PacketMultiplexListener.java index ce615301c..cc208cedb 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/PacketMultiplexListener.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/PacketMultiplexListener.java @@ -26,6 +26,7 @@ import org.jivesoftware.smack.filter.PacketTypeFilter; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Presence; +import org.jivesoftware.smackx.muc.packet.MUCUser; /** * The single PacketListener used by each {@link MultiUserChat} @@ -42,9 +43,8 @@ class PacketMultiplexListener implements PacketListener { return msg.getSubject() != null; } }; - private static final PacketFilter DECLINES_FILTER = - new PacketExtensionFilter("x", - "http://jabber.org/protocol/muc#user"); + private static final PacketFilter DECLINES_FILTER = new PacketExtensionFilter(MUCUser.ELEMENT, + MUCUser.NAMESPACE); private ConnectionDetachedPacketCollector messageCollector; private PacketListener presenceListener; 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 new file mode 100644 index 000000000..b14931f08 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/Destroy.java @@ -0,0 +1,86 @@ +/** + * + * Copyright 2003-2007 Jive Software, 2014 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muc.packet; + +import org.jivesoftware.smack.packet.Element; +import org.jivesoftware.smack.util.XmlStringBuilder; + +/** + * Represents a request to the server to destroy a room. The sender of the request should be the + * room's owner. If the sender of the destroy request is not the room's owner then the server will + * answer a "Forbidden" error. + * + * @author Gaston Dombiak + */ +public class Destroy implements Element { + public static final String ELEMENT = "destroy"; + + private String reason; + private String jid; + + /** + * Returns the JID of an alternate location since the current room is being destroyed. + * + * @return the JID of an alternate location. + */ + public String getJid() { + return jid; + } + + /** + * Returns the reason for the room destruction. + * + * @return the reason for the room destruction. + */ + public String getReason() { + 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); + xml.optAttribute("jid", getJid()); + xml.rightAngelBracket(); + xml.optElement("reason", getReason()); + xml.closeElement(this); + return xml; + } + + @Override + public String getElementName() { + return ELEMENT; + } + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/GroupChatInvitation.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/GroupChatInvitation.java index 2b8422f29..156507c67 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/GroupChatInvitation.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/GroupChatInvitation.java @@ -17,8 +17,10 @@ package org.jivesoftware.smackx.muc.packet; +import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.provider.PacketExtensionProvider; +import org.jivesoftware.smack.util.XmlStringBuilder; import org.xmlpull.v1.XmlPullParser; /** @@ -55,14 +57,14 @@ public class GroupChatInvitation implements PacketExtension { /** * Element name of the packet extension. */ - public static final String ELEMENT_NAME = "x"; + public static final String ELEMENT = "x"; /** * Namespace of the packet extension. */ public static final String NAMESPACE = "jabber:x:conference"; - private String roomAddress; + private final String roomAddress; /** * Creates a new group chat invitation to the specified room address. @@ -88,17 +90,23 @@ public class GroupChatInvitation implements PacketExtension { } public String getElementName() { - return ELEMENT_NAME; + return ELEMENT; } public String getNamespace() { return NAMESPACE; } - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append(""); - return buf.toString(); + @Override + public XmlStringBuilder toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.attribute("jid", getRoomAddress()); + xml.closeEmptyElement(); + return xml; + } + + public static GroupChatInvitation getFrom(Packet packet) { + return packet.getExtension(ELEMENT, NAMESPACE); } public static class Provider implements PacketExtensionProvider { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCAdmin.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCAdmin.java index 774cebf5c..37c957233 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCAdmin.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCAdmin.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.List; import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.util.XmlStringBuilder; /** * IQ packet that serves for kicking users, granting and revoking voice, banning users, @@ -32,7 +33,9 @@ import org.jivesoftware.smack.packet.IQ; */ public class MUCAdmin extends IQ { - private List items = new ArrayList(); + public static final String NAMESPACE = MUCInitialPresence.NAMESPACE + "#admin"; + + private final List items = new ArrayList(); /** * Returns a List of item childs that holds information about roles, affiliation, @@ -41,9 +44,9 @@ public class MUCAdmin extends IQ { * @return a List of item childs that holds information about roles, affiliation, * jids and nicks. */ - public List getItems() { + public List getItems() { synchronized (items) { - return Collections.unmodifiableList(new ArrayList(items)); + return Collections.unmodifiableList(new ArrayList(items)); } } @@ -52,182 +55,26 @@ public class MUCAdmin extends IQ { * * @param item the item child that holds information about roles, affiliation, jids and nicks. */ - public void addItem(Item item) { + public void addItem(MUCItem item) { synchronized (items) { items.add(item); } } - public String getChildElementXML() { - StringBuilder buf = new StringBuilder(); - buf.append(""); + @Override + public XmlStringBuilder getChildElementXML() { + XmlStringBuilder xml = new XmlStringBuilder(); + xml.halfOpenElement(IQ.QUERY_ELEMENT); + xml.xmlnsAttribute(NAMESPACE); + xml.rightAngelBracket(); synchronized (items) { - for (int i = 0; i < items.size(); i++) { - Item item = items.get(i); - buf.append(item.toXML()); + for (MUCItem item : items) { + xml.append(item.toXML()); } } // Add packet extensions, if any are defined. - buf.append(getExtensionsXML()); - buf.append(""); - return buf.toString(); + xml.append(getExtensionsXML()); + xml.closeElement(IQ.QUERY_ELEMENT); + return xml; } - - /** - * Item child that holds information about roles, affiliation, jids and nicks. - * - * @author Gaston Dombiak - */ - public static class Item { - private String actor; - private String reason; - private String affiliation; - private String jid; - private String nick; - private String role; - - /** - * Creates a new item child. - * - * @param affiliation the actor's affiliation to the room - * @param role the privilege level of an occupant within a room. - */ - public Item(String affiliation, String role) { - this.affiliation = affiliation; - this.role = role; - } - - /** - * Returns the actor (JID of an occupant in the room) that was kicked or banned. - * - * @return the JID of an occupant in the room that was kicked or banned. - */ - public String getActor() { - return actor; - } - - /** - * Returns the reason for the item child. The reason is optional and could be used to - * explain the reason why a user (occupant) was kicked or banned. - * - * @return the reason for the item child. - */ - public String getReason() { - return reason; - } - - /** - * Returns the occupant's affiliation to the room. The affiliation is a semi-permanent - * association or connection with a room. The possible affiliations are "owner", "admin", - * "member", and "outcast" (naturally it is also possible to have no affiliation). An - * affiliation lasts across a user's visits to a room. - * - * @return the actor's affiliation to the room - */ - public String getAffiliation() { - return affiliation; - } - - /** - * Returns the by which an occupant is identified within the context - * of a room. If the room is non-anonymous, the JID will be included in the item. - * - * @return the room JID by which an occupant is identified within the room. - */ - public String getJid() { - return jid; - } - - /** - * Returns the new nickname of an occupant that is changing his/her nickname. The new - * nickname is sent as part of the unavailable presence. - * - * @return the new nickname of an occupant that is changing his/her nickname. - */ - public String getNick() { - return nick; - } - - /** - * Returns the temporary position or privilege level of an occupant within a room. The - * possible roles are "moderator", "participant", and "visitor" (it is also possible to - * have no defined role). A role lasts only for the duration of an occupant's visit to - * a room. - * - * @return the privilege level of an occupant within a room. - */ - public String getRole() { - return role; - } - - /** - * Sets the actor (JID of an occupant in the room) that was kicked or banned. - * - * @param actor the actor (JID of an occupant in the room) that was kicked or banned. - */ - public void setActor(String actor) { - this.actor = actor; - } - - /** - * Sets the reason for the item child. The reason is optional and could be used to - * explain the reason why a user (occupant) was kicked or banned. - * - * @param reason the reason why a user (occupant) was kicked or banned. - */ - public void setReason(String reason) { - this.reason = reason; - } - - /** - * Sets the by which an occupant is identified within the context - * of a room. If the room is non-anonymous, the JID will be included in the item. - * - * @param jid the JID by which an occupant is identified within a room. - */ - public void setJid(String jid) { - this.jid = jid; - } - - /** - * Sets the new nickname of an occupant that is changing his/her nickname. The new - * nickname is sent as part of the unavailable presence. - * - * @param nick the new nickname of an occupant that is changing his/her nickname. - */ - public void setNick(String nick) { - this.nick = nick; - } - - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append(""); - } - else { - buf.append(">"); - if (getReason() != null) { - buf.append("").append(getReason()).append(""); - } - if (getActor() != null) { - buf.append(""); - } - buf.append(""); - } - return buf.toString(); - } - }; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCInitialPresence.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCInitialPresence.java index a88e4783c..64ae7363d 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCInitialPresence.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCInitialPresence.java @@ -17,11 +17,13 @@ package org.jivesoftware.smackx.muc.packet; +import org.jivesoftware.smack.packet.Element; +import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.PacketExtension; +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jxmpp.util.XmppDateTime; -import java.text.SimpleDateFormat; import java.util.Date; -import java.util.TimeZone; /** * Represents extended presence information whose sole purpose is to signal the ability of @@ -37,29 +39,28 @@ import java.util.TimeZone; */ public class MUCInitialPresence implements PacketExtension { + public static final String ELEMENT = "x"; + public static final String NAMESPACE = "http://jabber.org/protocol/muc"; + private String password; private History history; public String getElementName() { - return "x"; + return ELEMENT; } public String getNamespace() { - return "http://jabber.org/protocol/muc"; + return NAMESPACE; } - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append( - "\">"); - if (getPassword() != null) { - buf.append("").append(getPassword()).append(""); - } - if (getHistory() != null) { - buf.append(getHistory().toXML()); - } - buf.append(""); - return buf.toString(); + @Override + public XmlStringBuilder toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.rightAngelBracket(); + xml.optElement("password", getPassword()); + xml.optElement(getHistory()); + xml.closeElement(this); + return xml; } /** @@ -102,13 +103,25 @@ public class MUCInitialPresence implements PacketExtension { this.password = password; } + /** + * Retrieve the MUCInitialPresence PacketExtension from packet, if any. + * + * @param packet + * @return the MUCInitialPresence PacketExtension or {@code null} + */ + public static MUCInitialPresence getFrom(Packet packet) { + return packet.getExtension(ELEMENT, NAMESPACE); + } + /** * The History class controls the number of characters or messages to receive * when entering a room. * * @author Gaston Dombiak */ - public static class History { + public static class History implements Element { + + public static final String ELEMENT = "history"; private int maxChars = -1; private int maxStanzas = -1; @@ -196,25 +209,21 @@ public class MUCInitialPresence implements PacketExtension { this.since = since; } - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append(""); - return buf.toString(); + xml.closeEmptyElement(); + return xml; + } + + @Override + public String getElementName() { + return ELEMENT; } } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCItem.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCItem.java new file mode 100644 index 000000000..e5d888668 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCItem.java @@ -0,0 +1,189 @@ +/** + * + * Copyright 2003-2007 Jive Software, 2014 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muc.packet; + +import org.jivesoftware.smack.packet.Element; +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.util.XmlStringBuilder; + +/** + * Item child that holds information about roles, affiliation, jids and nicks. + * + * @author Gaston Dombiak + */ +public class MUCItem implements Element { + public static final String ELEMENT = IQ.ITEM; + + private final String affiliation; + private String role; + private String actor; + private String reason; + private String jid; + private String nick; + + /** + * Creates a new item child. + * + * @param affiliation the actor's affiliation to the room + */ + public MUCItem(String affiliation) { + this.affiliation = affiliation; + } + + /** + * Creates a new item child. + * + * @param affiliation the actor's affiliation to the room + * @param role the privilege level of an occupant within a room. + */ + public MUCItem(String affiliation, String role) { + this(affiliation); + this.role = role; + } + + /** + * Returns the actor (JID of an occupant in the room) that was kicked or banned. + * + * @return the JID of an occupant in the room that was kicked or banned. + */ + public String getActor() { + return actor; + } + + /** + * Returns the reason for the item child. The reason is optional and could be used to explain + * the reason why a user (occupant) was kicked or banned. + * + * @return the reason for the item child. + */ + public String getReason() { + return reason; + } + + /** + * Returns the occupant's affiliation to the room. The affiliation is a semi-permanent + * association or connection with a room. The possible affiliations are "owner", "admin", + * "member", and "outcast" (naturally it is also possible to have no affiliation). An + * affiliation lasts across a user's visits to a room. + * + * @return the actor's affiliation to the room + */ + public String getAffiliation() { + return affiliation; + } + + /** + * Returns the by which an occupant is identified within the context of a + * room. If the room is non-anonymous, the JID will be included in the item. + * + * @return the room JID by which an occupant is identified within the room. + */ + public String getJid() { + return jid; + } + + /** + * Returns the new nickname of an occupant that is changing his/her nickname. The new nickname + * is sent as part of the unavailable presence. + * + * @return the new nickname of an occupant that is changing his/her nickname. + */ + public String getNick() { + return nick; + } + + /** + * Returns the temporary position or privilege level of an occupant within a room. The possible + * roles are "moderator", "participant", and "visitor" (it is also possible to have no defined + * role). A role lasts only for the duration of an occupant's visit to a room. + * + * @return the privilege level of an occupant within a room. + */ + public String getRole() { + return role; + } + + /** + * Sets the actor (JID of an occupant in the room) that was kicked or banned. + * + * @param actor the actor (JID of an occupant in the room) that was kicked or banned. + */ + public void setActor(String actor) { + this.actor = actor; + } + + /** + * Sets the reason for the item child. The reason is optional and could be used to explain the + * reason why a user (occupant) was kicked or banned. + * + * @param reason the reason why a user (occupant) was kicked or banned. + */ + public void setReason(String reason) { + this.reason = reason; + } + + /** + * Sets the by which an occupant is identified within the context of a room. + * If the room is non-anonymous, the JID will be included in the item. + * + * @param jid the JID by which an occupant is identified within a room. + */ + public void setJid(String jid) { + this.jid = jid; + } + + /** + * Sets the new nickname of an occupant that is changing his/her nickname. The new nickname is + * sent as part of the unavailable presence. + * + * @param nick the new nickname of an occupant that is changing his/her nickname. + */ + public void setNick(String nick) { + this.nick = nick; + } + + /** + * Sets the temporary position or privilege level of an occupant within a room. The possible + * roles are "moderator", "participant", and "visitor" (it is also possible to have no defined + * role). A role lasts only for the duration of an occupant's visit to a room. + * + * @param role the new privilege level of an occupant within a room. + */ + public void setRole(String role) { + this.role = role; + } + + public XmlStringBuilder toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.optAttribute("affiliation", getAffiliation()); + xml.optAttribute("jid", getJid()); + xml.optAttribute("nick", getNick()); + xml.optAttribute("role", getRole()); + xml.rightAngelBracket(); + xml.optElement("reason", getReason()); + if (getActor() != null) { + xml.halfOpenElement("actor").attribute("jid", getActor()).closeEmptyElement(); + } + xml.closeElement(IQ.ITEM); + return xml; + } + + @Override + public String getElementName() { + return ELEMENT; + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCOwner.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCOwner.java index 1b135021a..1767adb96 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCOwner.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCOwner.java @@ -17,6 +17,7 @@ package org.jivesoftware.smackx.muc.packet; import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.util.XmlStringBuilder; import java.util.ArrayList; import java.util.Collections; @@ -31,7 +32,9 @@ import java.util.List; */ public class MUCOwner extends IQ { - private List items = new ArrayList(); + public static final String NAMESPACE = MUCInitialPresence.NAMESPACE + "#owner"; + + private final List items = new ArrayList(); private Destroy destroy; /** @@ -41,9 +44,9 @@ public class MUCOwner extends IQ { * @return a List of item childs that holds information about affiliation, * jids and nicks. */ - public List getItems() { + public List getItems() { synchronized (items) { - return Collections.unmodifiableList(new ArrayList(items)); + return Collections.unmodifiableList(new ArrayList(items)); } } @@ -74,265 +77,28 @@ public class MUCOwner extends IQ { * * @param item the item child that holds information about affiliation, jids and nicks. */ - public void addItem(Item item) { + public void addItem(MUCItem item) { synchronized (items) { items.add(item); } } - public String getChildElementXML() { - StringBuilder buf = new StringBuilder(); - buf.append(""); + @Override + public XmlStringBuilder getChildElementXML() { + XmlStringBuilder xml = new XmlStringBuilder(); + xml.halfOpenElement(IQ.QUERY_ELEMENT); + xml.xmlnsAttribute(NAMESPACE); + xml.rightAngelBracket(); synchronized (items) { - for (int i = 0; i < items.size(); i++) { - Item item = (Item) items.get(i); - buf.append(item.toXML()); + for (MUCItem item : items) { + xml.append(item.toXML()); } } - if (getDestroy() != null) { - buf.append(getDestroy().toXML()); - } + xml.optElement(getDestroy()); // Add packet extensions, if any are defined. - buf.append(getExtensionsXML()); - buf.append(""); - return buf.toString(); + xml.append(getExtensionsXML()); + xml.closeElement(IQ.QUERY_ELEMENT); + return xml; } - /** - * Item child that holds information about affiliation, jids and nicks. - * - * @author Gaston Dombiak - */ - public static class Item { - - private String actor; - private String reason; - private String affiliation; - private String jid; - private String nick; - private String role; - - /** - * Creates a new item child. - * - * @param affiliation the actor's affiliation to the room - */ - public Item(String affiliation) { - this.affiliation = affiliation; - } - - /** - * Returns the actor (JID of an occupant in the room) that was kicked or banned. - * - * @return the JID of an occupant in the room that was kicked or banned. - */ - public String getActor() { - return actor; - } - - /** - * Returns the reason for the item child. The reason is optional and could be used to - * explain the reason why a user (occupant) was kicked or banned. - * - * @return the reason for the item child. - */ - public String getReason() { - return reason; - } - - /** - * Returns the occupant's affiliation to the room. The affiliation is a semi-permanent - * association or connection with a room. The possible affiliations are "owner", "admin", - * "member", and "outcast" (naturally it is also possible to have no affiliation). An - * affiliation lasts across a user's visits to a room. - * - * @return the actor's affiliation to the room - */ - public String getAffiliation() { - return affiliation; - } - - /** - * Returns the by which an occupant is identified within the context - * of a room. If the room is non-anonymous, the JID will be included in the item. - * - * @return the room JID by which an occupant is identified within the room. - */ - public String getJid() { - return jid; - } - - /** - * Returns the new nickname of an occupant that is changing his/her nickname. The new - * nickname is sent as part of the unavailable presence. - * - * @return the new nickname of an occupant that is changing his/her nickname. - */ - public String getNick() { - return nick; - } - - /** - * Returns the temporary position or privilege level of an occupant within a room. The - * possible roles are "moderator", "participant", and "visitor" (it is also possible to - * have no defined role). A role lasts only for the duration of an occupant's visit to - * a room. - * - * @return the privilege level of an occupant within a room. - */ - public String getRole() { - return role; - } - - /** - * Sets the actor (JID of an occupant in the room) that was kicked or banned. - * - * @param actor the actor (JID of an occupant in the room) that was kicked or banned. - */ - public void setActor(String actor) { - this.actor = actor; - } - - /** - * Sets the reason for the item child. The reason is optional and could be used to - * explain the reason why a user (occupant) was kicked or banned. - * - * @param reason the reason why a user (occupant) was kicked or banned. - */ - public void setReason(String reason) { - this.reason = reason; - } - - /** - * Sets the by which an occupant is identified within the context - * of a room. If the room is non-anonymous, the JID will be included in the item. - * - * @param jid the JID by which an occupant is identified within a room. - */ - public void setJid(String jid) { - this.jid = jid; - } - - /** - * Sets the new nickname of an occupant that is changing his/her nickname. The new - * nickname is sent as part of the unavailable presence. - * - * @param nick the new nickname of an occupant that is changing his/her nickname. - */ - public void setNick(String nick) { - this.nick = nick; - } - - /** - * Sets the temporary position or privilege level of an occupant within a room. The - * possible roles are "moderator", "participant", and "visitor" (it is also possible to - * have no defined role). A role lasts only for the duration of an occupant's visit to - * a room. - * - * @param role the new privilege level of an occupant within a room. - */ - public void setRole(String role) { - this.role = role; - } - - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append(""); - } - else { - buf.append(">"); - if (getReason() != null) { - buf.append("").append(getReason()).append(""); - } - if (getActor() != null) { - buf.append(""); - } - buf.append(""); - } - return buf.toString(); - } - }; - - /** - * Represents a request to the server to destroy a room. The sender of the request - * should be the room's owner. If the sender of the destroy request is not the room's owner - * then the server will answer a "Forbidden" error. - * - * @author Gaston Dombiak - */ - public static class Destroy { - private String reason; - private String jid; - - - /** - * Returns the JID of an alternate location since the current room is being destroyed. - * - * @return the JID of an alternate location. - */ - public String getJid() { - return jid; - } - - /** - * Returns the reason for the room destruction. - * - * @return the reason for the room destruction. - */ - public String getReason() { - 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; - } - - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append(""); - } - else { - buf.append(">"); - if (getReason() != null) { - buf.append("").append(getReason()).append(""); - } - buf.append(""); - } - return buf.toString(); - } - - } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCUser.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCUser.java index 00e91bc79..cbf7d5ade 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCUser.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCUser.java @@ -17,7 +17,10 @@ package org.jivesoftware.smackx.muc.packet; +import org.jivesoftware.smack.packet.Element; +import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.PacketExtension; +import org.jivesoftware.smack.util.XmlStringBuilder; /** * Represents extended presence information about roles, affiliations, full JIDs, @@ -27,45 +30,36 @@ import org.jivesoftware.smack.packet.PacketExtension; */ public class MUCUser implements PacketExtension { + public static final String ELEMENT = "x"; + public static final String NAMESPACE = MUCInitialPresence.NAMESPACE + "#user"; + private Invite invite; private Decline decline; - private Item item; + private MUCItem item; private String password; private Status status; private Destroy destroy; public String getElementName() { - return "x"; + return ELEMENT; } public String getNamespace() { - return "http://jabber.org/protocol/muc#user"; + return NAMESPACE; } - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append( - "\">"); - if (getInvite() != null) { - buf.append(getInvite().toXML()); - } - if (getDecline() != null) { - buf.append(getDecline().toXML()); - } - if (getItem() != null) { - buf.append(getItem().toXML()); - } - if (getPassword() != null) { - buf.append("").append(getPassword()).append(""); - } - if (getStatus() != null) { - buf.append(getStatus().toXML()); - } - if (getDestroy() != null) { - buf.append(getDestroy().toXML()); - } - buf.append(""); - return buf.toString(); + @Override + public XmlStringBuilder toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.rightAngelBracket(); + xml.optElement(getInvite()); + xml.optElement(getDecline()); + xml.optElement(getItem()); + xml.optElement("password", getPassword()); + xml.optElement(getStatus()); + xml.optElement(getDestroy()); + xml.closeElement(this); + return xml; } /** @@ -94,7 +88,7 @@ public class MUCUser implements PacketExtension { * * @return an item child that holds information about roles, affiliation, jids and nicks. */ - public Item getItem() { + public MUCItem getItem() { return item; } @@ -154,7 +148,7 @@ public class MUCUser implements PacketExtension { * * @param item the item child that holds information about roles, affiliation, jids and nicks. */ - public void setItem(Item item) { + public void setItem(MUCItem item) { this.item = item; } @@ -189,6 +183,16 @@ public class MUCUser implements PacketExtension { this.destroy = destroy; } + /** + * Retrieve the MUCUser PacketExtension from packet, if any. + * + * @param packet + * @return the MUCUser PacketExtension or {@code null} + */ + public static MUCUser getFrom(Packet packet) { + return packet.getExtension(ELEMENT, NAMESPACE); + } + /** * Represents an invitation for another user to a room. The sender of the invitation * must be an occupant of the room. The invitation will be sent to the room which in turn @@ -196,7 +200,9 @@ public class MUCUser implements PacketExtension { * * @author Gaston Dombiak */ - public static class Invite { + public static class Invite implements Element { + public static final String ELEMENT ="invite"; + private String reason; private String from; private String to; @@ -257,21 +263,20 @@ public class MUCUser implements PacketExtension { this.to = to; } - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append(""); - if (getReason() != null) { - buf.append("").append(getReason()).append(""); - } - buf.append(""); - return buf.toString(); + @Override + public XmlStringBuilder toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.optAttribute("to", getTo()); + xml.optAttribute("from", getFrom()); + xml.rightAngelBracket(); + xml.optElement("reason", getReason()); + xml.closeElement(this); + return xml; + } + + @Override + public String getElementName() { + return ELEMENT; } } @@ -281,7 +286,9 @@ public class MUCUser implements PacketExtension { * * @author Gaston Dombiak */ - public static class Decline { + public static class Decline implements Element { + public static final String ELEMENT = "decline"; + private String reason; private String from; private String to; @@ -342,179 +349,20 @@ public class MUCUser implements PacketExtension { this.to = to; } - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append(""); - if (getReason() != null) { - buf.append("").append(getReason()).append(""); - } - buf.append(""); - return buf.toString(); - } - } - - /** - * Item child that holds information about roles, affiliation, jids and nicks. - * - * @author Gaston Dombiak - */ - public static class Item { - private String actor; - private String reason; - private String affiliation; - private String jid; - private String nick; - private String role; - - /** - * Creates a new item child. - * - * @param affiliation the actor's affiliation to the room - * @param role the privilege level of an occupant within a room. - */ - public Item(String affiliation, String role) { - this.affiliation = affiliation; - this.role = role; + @Override + public XmlStringBuilder toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.optAttribute("to", getTo()); + xml.optAttribute("from", getFrom()); + xml.rightAngelBracket(); + xml.optElement("reason", getReason()); + xml.closeElement(this); + return xml; } - /** - * Returns the actor (JID of an occupant in the room) that was kicked or banned. - * - * @return the JID of an occupant in the room that was kicked or banned. - */ - public String getActor() { - return actor == null ? "" : actor; - } - - /** - * Returns the reason for the item child. The reason is optional and could be used to - * explain the reason why a user (occupant) was kicked or banned. - * - * @return the reason for the item child. - */ - public String getReason() { - return reason == null ? "" : reason; - } - - /** - * Returns the occupant's affiliation to the room. The affiliation is a semi-permanent - * association or connection with a room. The possible affiliations are "owner", "admin", - * "member", and "outcast" (naturally it is also possible to have no affiliation). An - * affiliation lasts across a user's visits to a room. - * - * @return the actor's affiliation to the room - */ - public String getAffiliation() { - return affiliation; - } - - /** - * Returns the by which an occupant is identified within the context - * of a room. If the room is non-anonymous, the JID will be included in the item. - * - * @return the room JID by which an occupant is identified within the room. - */ - public String getJid() { - return jid; - } - - /** - * Returns the new nickname of an occupant that is changing his/her nickname. The new - * nickname is sent as part of the unavailable presence. - * - * @return the new nickname of an occupant that is changing his/her nickname. - */ - public String getNick() { - return nick; - } - - /** - * Returns the temporary position or privilege level of an occupant within a room. The - * possible roles are "moderator", "participant", and "visitor" (it is also possible to - * have no defined role). A role lasts only for the duration of an occupant's visit to - * a room. - * - * @return the privilege level of an occupant within a room. - */ - public String getRole() { - return role; - } - - /** - * Sets the actor (JID of an occupant in the room) that was kicked or banned. - * - * @param actor the actor (JID of an occupant in the room) that was kicked or banned. - */ - public void setActor(String actor) { - this.actor = actor; - } - - /** - * Sets the reason for the item child. The reason is optional and could be used to - * explain the reason why a user (occupant) was kicked or banned. - * - * @param reason the reason why a user (occupant) was kicked or banned. - */ - public void setReason(String reason) { - this.reason = reason; - } - - /** - * Sets the by which an occupant is identified within the context - * of a room. If the room is non-anonymous, the JID will be included in the item. - * - * @param jid the JID by which an occupant is identified within a room. - */ - public void setJid(String jid) { - this.jid = jid; - } - - /** - * Sets the new nickname of an occupant that is changing his/her nickname. The new - * nickname is sent as part of the unavailable presence. - * - * @param nick the new nickname of an occupant that is changing his/her nickname. - */ - public void setNick(String nick) { - this.nick = nick; - } - - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append(""); - } - else { - buf.append(">"); - if (getReason() != null) { - buf.append("").append(getReason()).append(""); - } - if (getActor() != null) { - buf.append(""); - } - buf.append(""); - } - return buf.toString(); + @Override + public String getElementName() { + return ELEMENT; } } @@ -524,7 +372,9 @@ public class MUCUser implements PacketExtension { * * @author Gaston Dombiak */ - public static class Status { + public static class Status implements Element { + public static final String ELEMENT = "status"; + private String code; /** @@ -546,79 +396,17 @@ public class MUCUser implements PacketExtension { return code; } - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append(""); - return buf.toString(); - } - } - - /** - * Represents a notification that the room has been destroyed. After a room has been destroyed, - * the room occupants will receive a Presence packet of type 'unavailable' with the reason for - * the room destruction if provided by the room owner. - * - * @author Gaston Dombiak - */ - public static class Destroy { - private String reason; - private String jid; - - - /** - * Returns the JID of an alternate location since the current room is being destroyed. - * - * @return the JID of an alternate location. - */ - public String getJid() { - return jid; + @Override + public XmlStringBuilder toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.attribute("code", getCode()); + xml.closeEmptyElement(); + return xml; } - /** - * Returns the reason for the room destruction. - * - * @return the reason for the room destruction. - */ - public String getReason() { - return reason; + @Override + public String getElementName() { + return ELEMENT; } - - /** - * 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; - } - - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append(""); - } - else { - buf.append(">"); - if (getReason() != null) { - buf.append("").append(getReason()).append(""); - } - buf.append(""); - } - return buf.toString(); - } - } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCAdminProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCAdminProvider.java index 11fe77207..9f37b2956 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCAdminProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCAdminProvider.java @@ -36,7 +36,7 @@ public class MUCAdminProvider implements IQProvider { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG) { if (parser.getName().equals("item")) { - mucAdmin.addItem(parseItem(parser)); + mucAdmin.addItem(MUCParserUtils.parseItem(parser)); } } else if (eventType == XmlPullParser.END_TAG) { @@ -48,31 +48,4 @@ public class MUCAdminProvider implements IQProvider { return mucAdmin; } - - private MUCAdmin.Item parseItem(XmlPullParser parser) throws Exception { - boolean done = false; - MUCAdmin.Item item = - new MUCAdmin.Item( - parser.getAttributeValue("", "affiliation"), - parser.getAttributeValue("", "role")); - item.setNick(parser.getAttributeValue("", "nick")); - item.setJid(parser.getAttributeValue("", "jid")); - while (!done) { - int eventType = parser.next(); - if (eventType == XmlPullParser.START_TAG) { - if (parser.getName().equals("actor")) { - item.setActor(parser.getAttributeValue("", "jid")); - } - if (parser.getName().equals("reason")) { - item.setReason(parser.nextText()); - } - } - else if (eventType == XmlPullParser.END_TAG) { - if (parser.getName().equals("item")) { - done = true; - } - } - } - return item; - } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCOwnerProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCOwnerProvider.java index 5854860b2..53f233a72 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCOwnerProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCOwnerProvider.java @@ -37,10 +37,10 @@ public class MUCOwnerProvider implements IQProvider { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG) { if (parser.getName().equals("item")) { - mucOwner.addItem(parseItem(parser)); + mucOwner.addItem(MUCParserUtils.parseItem(parser)); } else if (parser.getName().equals("destroy")) { - mucOwner.setDestroy(parseDestroy(parser)); + mucOwner.setDestroy(MUCParserUtils.parseDestroy(parser)); } // Otherwise, it must be a packet extension. else { @@ -57,49 +57,4 @@ public class MUCOwnerProvider implements IQProvider { return mucOwner; } - - private MUCOwner.Item parseItem(XmlPullParser parser) throws Exception { - boolean done = false; - MUCOwner.Item item = new MUCOwner.Item(parser.getAttributeValue("", "affiliation")); - item.setNick(parser.getAttributeValue("", "nick")); - item.setRole(parser.getAttributeValue("", "role")); - item.setJid(parser.getAttributeValue("", "jid")); - while (!done) { - int eventType = parser.next(); - if (eventType == XmlPullParser.START_TAG) { - if (parser.getName().equals("actor")) { - item.setActor(parser.getAttributeValue("", "jid")); - } - if (parser.getName().equals("reason")) { - item.setReason(parser.nextText()); - } - } - else if (eventType == XmlPullParser.END_TAG) { - if (parser.getName().equals("item")) { - done = true; - } - } - } - return item; - } - - private MUCOwner.Destroy parseDestroy(XmlPullParser parser) throws Exception { - boolean done = false; - MUCOwner.Destroy destroy = new MUCOwner.Destroy(); - destroy.setJid(parser.getAttributeValue("", "jid")); - while (!done) { - int eventType = parser.next(); - if (eventType == XmlPullParser.START_TAG) { - if (parser.getName().equals("reason")) { - destroy.setReason(parser.nextText()); - } - } - else if (eventType == XmlPullParser.END_TAG) { - if (parser.getName().equals("destroy")) { - done = true; - } - } - } - return destroy; - } } 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 new file mode 100644 index 000000000..39a3ab27b --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCParserUtils.java @@ -0,0 +1,68 @@ +/** + * + * Copyright 2003-2007 Jive Software. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muc.provider; + +import org.jivesoftware.smackx.muc.packet.Destroy; +import org.jivesoftware.smackx.muc.packet.MUCItem; +import org.xmlpull.v1.XmlPullParser; + +public class MUCParserUtils { + public static MUCItem parseItem(XmlPullParser parser) throws Exception { + boolean done = false; + MUCItem item = new MUCItem(parser.getAttributeValue("", "affiliation")); + item.setNick(parser.getAttributeValue("", "nick")); + item.setRole(parser.getAttributeValue("", "role")); + item.setJid(parser.getAttributeValue("", "jid")); + while (!done) { + int eventType = parser.next(); + if (eventType == XmlPullParser.START_TAG) { + if (parser.getName().equals("actor")) { + item.setActor(parser.getAttributeValue("", "jid")); + } + if (parser.getName().equals("reason")) { + item.setReason(parser.nextText()); + } + } + else if (eventType == XmlPullParser.END_TAG) { + if (parser.getName().equals("item")) { + done = true; + } + } + } + return item; + } + + public static Destroy parseDestroy(XmlPullParser parser) throws Exception { + boolean done = false; + Destroy destroy = new Destroy(); + destroy.setJid(parser.getAttributeValue("", "jid")); + while (!done) { + int eventType = parser.next(); + if (eventType == XmlPullParser.START_TAG) { + if (parser.getName().equals("reason")) { + destroy.setReason(parser.nextText()); + } + } + else if (eventType == XmlPullParser.END_TAG) { + if (parser.getName().equals("destroy")) { + done = true; + } + } + } + return destroy; + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCUserProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCUserProvider.java index 437a1c5a5..516c1f790 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCUserProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCUserProvider.java @@ -31,14 +31,6 @@ import org.xmlpull.v1.XmlPullParser; */ public class MUCUserProvider implements PacketExtensionProvider { - /** - * Creates a new MUCUserProvider. - * ProviderManager requires that every PacketExtensionProvider has a public, no-argument - * constructor - */ - public MUCUserProvider() { - } - /** * Parses a MUCUser packet (extension sub-packet). * @@ -56,7 +48,7 @@ public class MUCUserProvider implements PacketExtensionProvider { mucUser.setInvite(parseInvite(parser)); } if (parser.getName().equals("item")) { - mucUser.setItem(parseItem(parser)); + mucUser.setItem(MUCParserUtils.parseItem(parser)); } if (parser.getName().equals("password")) { mucUser.setPassword(parser.nextText()); @@ -68,7 +60,7 @@ public class MUCUserProvider implements PacketExtensionProvider { mucUser.setDecline(parseDecline(parser)); } if (parser.getName().equals("destroy")) { - mucUser.setDestroy(parseDestroy(parser)); + mucUser.setDestroy(MUCParserUtils.parseDestroy(parser)); } } else if (eventType == XmlPullParser.END_TAG) { @@ -81,34 +73,7 @@ public class MUCUserProvider implements PacketExtensionProvider { return mucUser; } - private MUCUser.Item parseItem(XmlPullParser parser) throws Exception { - boolean done = false; - MUCUser.Item item = - new MUCUser.Item( - parser.getAttributeValue("", "affiliation"), - parser.getAttributeValue("", "role")); - item.setNick(parser.getAttributeValue("", "nick")); - item.setJid(parser.getAttributeValue("", "jid")); - while (!done) { - int eventType = parser.next(); - if (eventType == XmlPullParser.START_TAG) { - if (parser.getName().equals("actor")) { - item.setActor(parser.getAttributeValue("", "jid")); - } - if (parser.getName().equals("reason")) { - item.setReason(parser.nextText()); - } - } - else if (eventType == XmlPullParser.END_TAG) { - if (parser.getName().equals("item")) { - done = true; - } - } - } - return item; - } - - private MUCUser.Invite parseInvite(XmlPullParser parser) throws Exception { + private static MUCUser.Invite parseInvite(XmlPullParser parser) throws Exception { boolean done = false; MUCUser.Invite invite = new MUCUser.Invite(); invite.setFrom(parser.getAttributeValue("", "from")); @@ -129,7 +94,7 @@ public class MUCUserProvider implements PacketExtensionProvider { return invite; } - private MUCUser.Decline parseDecline(XmlPullParser parser) throws Exception { + private static MUCUser.Decline parseDecline(XmlPullParser parser) throws Exception { boolean done = false; MUCUser.Decline decline = new MUCUser.Decline(); decline.setFrom(parser.getAttributeValue("", "from")); @@ -149,24 +114,4 @@ public class MUCUserProvider implements PacketExtensionProvider { } return decline; } - - private MUCUser.Destroy parseDestroy(XmlPullParser parser) throws Exception { - boolean done = false; - MUCUser.Destroy destroy = new MUCUser.Destroy(); - destroy.setJid(parser.getAttributeValue("", "jid")); - while (!done) { - int eventType = parser.next(); - if (eventType == XmlPullParser.START_TAG) { - if (parser.getName().equals("reason")) { - destroy.setReason(parser.nextText()); - } - } - else if (eventType == XmlPullParser.END_TAG) { - if (parser.getName().equals("destroy")) { - done = true; - } - } - } - return destroy; - } }