1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-10 18:59:41 +02:00

changed "local entry" solution to "wrapper class" solution

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2065 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Gaston Dombiak 2003-08-24 16:05:45 +00:00 committed by gdombiak
parent 779f6ab82f
commit ca37ceb2f1
9 changed files with 1018 additions and 247 deletions

View file

@ -0,0 +1,179 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
* ====================================================================
* The Jive Software License (based on Apache Software License, Version 1.1)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* Jive Software (http://www.jivesoftware.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Smack" and "Jive Software" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact webmaster@jivesoftware.com.
*
* 5. Products derived from this software may not be called "Smack",
* nor may "Smack" appear in their name, without prior written
* permission of Jive Software.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*/
package org.jivesoftware.smackx.packet;
import java.util.*;
/**
* Represents a roster item, which consists of a JID and , their name and
* the groups the roster item belongs to. This roster item does not belong
* to the local roster. Therefore, it does not persist in the server.<p>
*
* The idea of a RemoteRosterEntry is to be used as part of a roster exchange.
*
* @author Gaston Dombiak
*/
public class RemoteRosterEntry {
private String user;
private String name;
private List groupNames;
/**
* Creates a new remote roster entry.
*
* @param user the user.
* @param name the user's name.
*/
public RemoteRosterEntry(String user, String name) {
this.user = user;
this.name = name;
groupNames = new ArrayList();
}
/**
* Returns the user.
*
* @return the user.
*/
public String getUser() {
return user;
}
/**
* Returns the user's name.
*
* @return the user's name.
*/
public String getName() {
return name;
}
/**
* Sets the user's name.
*
* @param name the user's name.
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns an Iterator for the group names (as Strings) that the roster entry
* belongs to.
*
* @return an Iterator for the group names.
*/
public Iterator getGroupNames() {
synchronized (groupNames) {
return Collections.unmodifiableList(groupNames).iterator();
}
}
/**
* Returns a String array for the group names that the roster entry
* belongs to.
*
* @return a String[] for the group names.
*/
public String[] getGroupArrayNames() {
synchronized (groupNames) {
return (String[])
(Collections
.unmodifiableList(groupNames)
.toArray(new String[groupNames.size()]));
}
}
/**
* Adds a group name.
*
* @param groupName the group name.
*/
public void addGroupName(String groupName) {
synchronized (groupNames) {
if (!groupNames.contains(groupName)) {
groupNames.add(groupName);
}
}
}
/**
* Removes a group name.
*
* @param groupName the group name.
*/
public void removeGroupName(String groupName) {
synchronized (groupNames) {
groupNames.remove(groupName);
}
}
public String toXML() {
StringBuffer buf = new StringBuffer();
buf.append("<item jid=\"").append(user).append("\"");
if (name != null) {
buf.append(" name=\"").append(name).append("\"");
}
buf.append(">");
synchronized (groupNames) {
for (int i = 0; i < groupNames.size(); i++) {
String groupName = (String) groupNames.get(i);
buf.append("<group>").append(groupName).append("</group>");
}
}
buf.append("</item>");
return buf.toString();
}
}

View file

@ -1,6 +1,55 @@
/*
* Created on 27/07/2003
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
* ====================================================================
* The Jive Software License (based on Apache Software License, Version 1.1)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* Jive Software (http://www.jivesoftware.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Smack" and "Jive Software" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact webmaster@jivesoftware.com.
*
* 5. Products derived from this software may not be called "Smack",
* nor may "Smack" appear in their name, without prior written
* permission of Jive Software.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*/
package org.jivesoftware.smackx.packet;
import java.util.*;
@ -9,80 +58,76 @@ import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.PacketExtension;
/**
* Represents XMPP Roster Item Exchange packets.
* The 'jabber:x:roster' namespace (which is not to be confused with the 'jabber:iq:roster' namespace) is used to
* send roster items from one client to another. A roster item is sent by adding to the <message/> element an <x/> child
* scoped by the 'jabber:x:roster' namespace. This <x/> element may contain one or more <item/> children (one for each roster item to be sent).
* Represents XMPP Roster Item Exchange packets.<p>
*
* Each <item/> element may possess the following attributes:
* The 'jabber:x:roster' namespace (which is not to be confused with the 'jabber:iq:roster'
* namespace) is used to send roster items from one client to another. A roster item is sent by
* adding to the &lt;message/&gt; element an &lt;x/&gt; child scoped by the 'jabber:x:roster' namespace. This
* &lt;x/&gt; element may contain one or more &lt;item/&gt; children (one for each roster item to be sent).<p>
*
* <jid/> -- The id of the contact being sent. This attribute is required.
* <name/> -- A natural-language nickname for the contact. This attribute is optional.
* Each &lt;item/&gt; element may possess the following attributes:<p>
*
* Each <item/> element may also contain one or more <group/> children specifying the natural-language name
* of a user-specified group, for the purpose of categorizing this contact into one or more roster groups.
* &lt;jid/&gt; -- The id of the contact being sent. This attribute is required.<br>
* &lt;name/&gt; -- A natural-language nickname for the contact. This attribute is optional.<p>
*
* Each &lt;item/&gt; element may also contain one or more &lt;group/&gt; children specifying the
* natural-language name of a user-specified group, for the purpose of categorizing this contact
* into one or more roster groups.
*
* @author Gaston Dombiak
*/
public class RosterExchange implements PacketExtension {
private List rosterItems = new ArrayList();
private List remoteRosterEntries = new ArrayList();
/**
* Creates a new empty roster exchange package.
*
*/
public RosterExchange(){
/**
* Creates a new empty roster exchange package.
*
*/
public RosterExchange() {
super();
}
/**
* Creates a new roster exchange package with the entries specified in roster.
*
* @param roster the roster to send to other XMPP entity.
*/
public RosterExchange(Roster roster){
RosterGroup rosterGroup = null;
// Loop through all roster groups and add their entries to the new RosterExchange
for (Iterator groups = roster.getGroups(); groups.hasNext(); ) {
rosterGroup = (RosterGroup) groups.next();
for (Iterator entries = rosterGroup.getEntries(); entries.hasNext(); ) {
this.addRosterItem((RosterEntry) entries.next());
}
}
// Add the roster unfiled entries to the new RosterExchange
for (Iterator unfiledEntries = roster.getUnfiledEntries(); unfiledEntries.hasNext();) {
this.addRosterItem((RosterEntry) unfiledEntries.next());
}
}
/**
* Adds a roster entry to the packet.
* Creates a new roster exchange package with the entries specified in roster.
*
* @param rosterEntry a roster item.
* @param roster the roster to send to other XMPP entity.
*/
public void addRosterItem(RosterEntry rosterEntry) {
RosterGroup rosterGroup = null;
// Create a new Item based on the rosterEntry and add it to the packet
Item item = new Item(rosterEntry.getUser(), rosterEntry.getName());
// Add the entry groups to the item
for (Iterator groups = rosterEntry.getGroups(); groups.hasNext(); ) {
rosterGroup = (RosterGroup) groups.next();
item.addGroupName(rosterGroup.getName());
public RosterExchange(Roster roster) {
// Add all the roster entries to the new RosterExchange
for (Iterator rosterEntries = roster.getEntries(); rosterEntries.hasNext();) {
this.addRosterEntry((RosterEntry) rosterEntries.next());
}
addRosterItem(item);
}
/**
* Adds a roster item to the packet.
* Adds a roster entry to the packet.
*
* @param item a roster item.
* @param rosterEntry a roster entry to add.
*/
public void addRosterItem(Item item) {
synchronized (rosterItems) {
rosterItems.add(item);
public void addRosterEntry(RosterEntry rosterEntry) {
RosterGroup rosterGroup = null;
// Create a new Entry based on the rosterEntry and add it to the packet
RemoteRosterEntry remoteRosterEntry = new RemoteRosterEntry(rosterEntry.getUser(), rosterEntry.getName());
// Add the entry groups to the Entry
for (Iterator groups = rosterEntry.getGroups(); groups.hasNext();) {
rosterGroup = (RosterGroup) groups.next();
remoteRosterEntry.addGroupName(rosterGroup.getName());
}
addRosterEntry(remoteRosterEntry);
}
/**
* Adds a remote roster entry to the packet.
*
* @param remoteRosterEntry a remote roster entry to add.
*/
public void addRosterEntry(RemoteRosterEntry remoteRosterEntry) {
synchronized (remoteRosterEntries) {
remoteRosterEntries.add(remoteRosterEntry);
}
}
/**
* Returns the XML element name of the extension sub-packet root element.
* Always returns "x"
@ -105,158 +150,54 @@ public class RosterExchange implements PacketExtension {
}
/**
* Returns an Iterator for the roster items in the packet.
* Returns an Iterator for the roster entries in the packet.
*
* @return and Iterator for the roster items in the packet.
* @return an Iterator for the roster entries in the packet.
*/
public Iterator getRosterItems() {
synchronized (rosterItems) {
List entries =
Collections.unmodifiableList(new ArrayList(rosterItems));
public Iterator getRosterEntries() {
synchronized (remoteRosterEntries) {
List entries = Collections.unmodifiableList(new ArrayList(remoteRosterEntries));
return entries.iterator();
}
}
/**
* Returns the XML representation of a Roster Item Exchange according the specification.
* Returns a count of the entries in the roster exchange.
*
* @return the number of entries in the roster exchange.
*/
public int getEntryCount() {
return remoteRosterEntries.size();
}
/**
* Returns the XML representation of a Roster Item Exchange according the specification.
*
* Usually the XML representation will be inside of a Message XML representation like
* in the following example:
* <message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack">
* <subject>Any subject you want</subject>
* <body>This message contains roster items.</body>
* <x xmlns="jabber:x:roster">
* <item jid="gato1@gato.home"/>
* <item jid="gato2@gato.home"/>
* </x>
* </message>
* <pre>
* &lt;message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"&gt;
* &lt;subject&gt;Any subject you want&lt;/subject&gt;
* &lt;body&gt;This message contains roster items.&lt;/body&gt;
* &lt;x xmlns="jabber:x:roster"&gt;
* &lt;item jid="gato1@gato.home"/&gt;
* &lt;item jid="gato2@gato.home"/&gt;
* &lt;/x&gt;
* &lt;/message&gt;
* </pre>
*
*/
public String toXML() {
StringBuffer buf = new StringBuffer();
buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">");
// Loop through all roster items and append them to the string buffer
for (Iterator i = getRosterItems(); i.hasNext();) {
Item entry = (Item) i.next();
buf.append(entry.toXML());
}
buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
"\">");
// Loop through all roster entries and append them to the string buffer
for (Iterator i = getRosterEntries(); i.hasNext();) {
RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) i.next();
buf.append(remoteRosterEntry.toXML());
}
buf.append("</").append(getElementName()).append(">");
return buf.toString();
}
/**
* A roster item, which consists of a JID and , their name and
* the groups the roster item belongs to.
*/
public static class Item {
private String user;
private String name;
private List groupNames;
/**
* Creates a new roster item.
*
* @param user the user.
* @param name the user's name.
*/
public Item(String user, String name) {
this.user = user;
this.name = name;
groupNames = new ArrayList();
}
/**
* Returns the user.
*
* @return the user.
*/
public String getUser() {
return user;
}
/**
* Returns the user's name.
*
* @return the user's name.
*/
public String getName() {
return name;
}
/**
* Sets the user's name.
*
* @param name the user's name.
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns an Iterator for the group names (as Strings) that the roster item
* belongs to.
*
* @return an Iterator for the group names.
*/
public Iterator getGroupNames() {
synchronized (groupNames) {
return Collections.unmodifiableList(groupNames).iterator();
}
}
/**
* Returns a String array for the group names that the roster item
* belongs to.
*
* @return a String[] for the group names.
*/
public String[] getGroupArrayNames() {
synchronized (groupNames) {
return (String[])(Collections.unmodifiableList(groupNames).toArray(new String[groupNames.size()]));
}
}
/**
* Adds a group name.
*
* @param groupName the group name.
*/
public void addGroupName(String groupName) {
synchronized (groupNames) {
if (!groupNames.contains(groupName)) {
groupNames.add(groupName);
}
}
}
/**
* Removes a group name.
*
* @param groupName the group name.
*/
public void removeGroupName(String groupName) {
synchronized (groupNames) {
groupNames.remove(groupName);
}
}
public String toXML() {
StringBuffer buf = new StringBuffer();
buf.append("<item jid=\"").append(user).append("\"");
if (name != null) {
buf.append(" name=\"").append(name).append("\"");
}
buf.append(">");
synchronized (groupNames) {
for (int i = 0; i < groupNames.size(); i++) {
String groupName = (String) groupNames.get(i);
buf.append("<group>").append(groupName).append("</group>");
}
}
buf.append("</item>");
return buf.toString();
}
}
}