1 20 21 package org.jivesoftware.smackx.packet; 22 23 import java.util.*; 24 25 import org.jivesoftware.smack.*; 26 import org.jivesoftware.smack.packet.PacketExtension; 27 import org.jivesoftware.smackx.*; 28 29 48 public class RosterExchange implements PacketExtension { 49 50 private List remoteRosterEntries = new ArrayList(); 51 52 56 public RosterExchange() { 57 super(); 58 } 59 60 65 public RosterExchange(Roster roster) { 66 for (Iterator rosterEntries = roster.getEntries(); rosterEntries.hasNext();) { 68 this.addRosterEntry((RosterEntry) rosterEntries.next()); 69 } 70 } 71 72 77 public void addRosterEntry(RosterEntry rosterEntry) { 78 ArrayList groupNamesList = new ArrayList(); 80 String [] groupNames; 81 for (Iterator groups = rosterEntry.getGroups(); groups.hasNext();) { 82 groupNamesList.add(((RosterGroup) groups.next()).getName()); 83 } 84 groupNames = (String []) groupNamesList.toArray(new String [groupNamesList.size()]); 85 86 RemoteRosterEntry remoteRosterEntry = new RemoteRosterEntry(rosterEntry.getUser(), rosterEntry.getName(), groupNames); 88 89 addRosterEntry(remoteRosterEntry); 90 } 91 92 97 public void addRosterEntry(RemoteRosterEntry remoteRosterEntry) { 98 synchronized (remoteRosterEntries) { 99 remoteRosterEntries.add(remoteRosterEntry); 100 } 101 } 102 103 109 public String getElementName() { 110 return "x"; 111 } 112 113 120 public String getNamespace() { 121 return "jabber:x:roster"; 122 } 123 124 129 public Iterator getRosterEntries() { 130 synchronized (remoteRosterEntries) { 131 List entries = Collections.unmodifiableList(new ArrayList(remoteRosterEntries)); 132 return entries.iterator(); 133 } 134 } 135 136 141 public int getEntryCount() { 142 return remoteRosterEntries.size(); 143 } 144 145 162 public String toXML() { 163 StringBuffer buf = new StringBuffer (); 164 buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append( 165 "\">"); 166 for (Iterator i = getRosterEntries(); i.hasNext();) { 168 RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) i.next(); 169 buf.append(remoteRosterEntry.toXML()); 170 } 171 buf.append("</").append(getElementName()).append(">"); 172 return buf.toString(); 173 } 174 175 } 176 | Popular Tags |