1 20 21 package org.jivesoftware.smack.packet; 22 23 import java.util.*; 24 25 30 public class RosterPacket extends IQ { 31 32 private List rosterItems = new ArrayList(); 33 34 39 public void addRosterItem(Item item) { 40 synchronized (rosterItems) { 41 rosterItems.add(item); 42 } 43 } 44 45 50 public int getRosterItemCount() { 51 synchronized (rosterItems) { 52 return rosterItems.size(); 53 } 54 } 55 56 61 public Iterator getRosterItems() { 62 synchronized (rosterItems) { 63 List entries = Collections.unmodifiableList(new ArrayList(rosterItems)); 64 return entries.iterator(); 65 } 66 } 67 68 public String getChildElementXML() { 69 StringBuffer buf = new StringBuffer (); 70 buf.append("<query xmlns=\"jabber:iq:roster\">"); 71 synchronized (rosterItems) { 72 for (int i=0; i<rosterItems.size(); i++) { 73 Item entry = (Item)rosterItems.get(i); 74 buf.append(entry.toXML()); 75 } 76 } 77 buf.append("</query>"); 78 return buf.toString(); 79 } 80 81 85 public static class Item { 86 87 private String user; 88 private String name; 89 private ItemType itemType; 90 private ItemStatus itemStatus; 91 private List groupNames; 92 93 99 public Item(String user, String name) { 100 this.user = user; 101 this.name = name; 102 itemType = null; 103 itemStatus = null; 104 groupNames = new ArrayList(); 105 } 106 107 112 public String getUser() { 113 return user; 114 } 115 116 121 public String getName() { 122 return name; 123 } 124 125 130 public void setName(String name) { 131 this.name = name; 132 } 133 134 139 public ItemType getItemType() { 140 return itemType; 141 } 142 143 148 public void setItemType(ItemType itemType) { 149 this.itemType = itemType; 150 } 151 152 157 public ItemStatus getItemStatus() { 158 return itemStatus; 159 } 160 161 166 public void setItemStatus(ItemStatus itemStatus) { 167 this.itemStatus = itemStatus; 168 } 169 170 176 public Iterator getGroupNames() { 177 synchronized (groupNames) { 178 return Collections.unmodifiableList(groupNames).iterator(); 179 } 180 } 181 182 187 public void addGroupName(String groupName) { 188 synchronized (groupNames) { 189 if (!groupNames.contains(groupName)) { 190 groupNames.add(groupName); 191 } 192 } 193 } 194 195 200 public void removeGroupName(String groupName) { 201 synchronized (groupNames) { 202 groupNames.remove(groupName); 203 } 204 } 205 206 public String toXML() { 207 StringBuffer buf = new StringBuffer (); 208 buf.append("<item jid=\"").append(user).append("\""); 209 if (name != null) { 210 buf.append(" name=\"").append(name).append("\""); 211 } 212 if (itemType != null) { 213 buf.append(" subscription=\"").append(itemType).append("\""); 214 } 215 if (itemStatus != null) { 216 buf.append(" ask=\"").append(itemStatus).append("\""); 217 } 218 buf.append(">"); 219 synchronized (groupNames) { 220 for (int i=0; i<groupNames.size(); i++) { 221 String groupName = (String )groupNames.get(i); 222 buf.append("<group>").append(groupName).append("</group>"); 223 } 224 } 225 buf.append("</item>"); 226 return buf.toString(); 227 } 228 } 229 230 234 public static class ItemStatus { 235 236 239 public static final ItemStatus SUBSCRIPTION_PENDING = new ItemStatus("subscribe"); 240 241 244 public static final ItemStatus UNSUBCRIPTION_PENDING = new ItemStatus("unsubscribe"); 245 246 public static ItemStatus fromString(String value) { 247 if (value == null) { 248 return null; 249 } 250 value = value.toLowerCase(); 251 if ("unsubscribe".equals(value)) { 252 return SUBSCRIPTION_PENDING; 253 } 254 else if ("subscribe".equals(value)) { 255 return SUBSCRIPTION_PENDING; 256 } 257 else { 258 return null; 259 } 260 } 261 262 private String value; 263 264 269 private ItemStatus(String value) { 270 this.value = value; 271 } 272 273 public String toString() { 274 return value; 275 } 276 } 277 278 281 public static class ItemType { 282 283 286 public static final ItemType NONE = new ItemType("none"); 287 288 291 public static final ItemType TO = new ItemType("to"); 292 293 296 public static final ItemType FROM = new ItemType("from"); 297 298 301 public static final ItemType BOTH = new ItemType("both"); 302 303 306 public static final ItemType REMOVE = new ItemType("remove"); 307 308 public static ItemType fromString(String value) { 309 if (value == null) { 310 return null; 311 } 312 value = value.toLowerCase(); 313 if ("none".equals(value)) { 314 return NONE; 315 } 316 else if ("to".equals(value)) { 317 return TO; 318 } 319 else if ("from".equals(value)) { 320 return FROM; 321 } 322 else if ("both".equals(value)) { 323 return BOTH; 324 } 325 else if ("remove".equals(value)) { 326 return REMOVE; 327 } 328 else { 329 return null; 330 } 331 } 332 333 private String value; 334 335 340 public ItemType(String value) { 341 this.value = value; 342 } 343 344 public String toString() { 345 return value; 346 } 347 } 348 } | Popular Tags |