1 20 21 package org.xmpp.packet; 22 23 import org.dom4j.*; 24 25 import java.util.*; 26 27 28 36 public class Roster extends IQ { 37 38 42 public Roster() { 43 super(); 44 element.addElement("query", "jabber:iq:roster"); 45 } 46 47 53 public Roster(Type type) { 54 super(type); 55 element.addElement("query", "jabber:iq:roster"); 56 } 57 58 64 public Roster(Type type, String ID) { 65 super(type, ID); 66 element.addElement("query", "jabber:iq:roster"); 67 } 68 69 75 private Roster(Roster roster) { 76 Element elementCopy = roster.element.createCopy(); 77 docFactory.createDocument().add(elementCopy); 78 this.element = elementCopy; 79 } 80 81 87 public Roster(Element element) { 88 super(element); 89 } 90 91 104 public Item addItem(String jid, Subscription subscription) { 105 if (getType() == IQ.Type.get || getType() == IQ.Type.error) { 106 throw new IllegalStateException ("IQ type must be 'result' or 'set'"); 107 } 108 if (jid == null) { 109 throw new NullPointerException ("JID cannot be null"); 110 } 111 return addItem(new JID(jid), null, null, subscription, null); 112 } 113 114 127 public Item addItem(JID jid, Subscription subscription) { 128 if (getType() != IQ.Type.result && getType() != IQ.Type.set) { 129 throw new IllegalStateException ("IQ type must be 'result' or 'set'"); 130 } 131 if (jid == null) { 132 throw new NullPointerException ("JID cannot be null"); 133 } 134 return addItem(jid, null, null, subscription, null); 135 } 136 137 153 public Item addItem(JID jid, String name, Ask ask, Subscription subscription, 154 Collection<String > groups) 155 { 156 if (jid == null) { 157 throw new NullPointerException ("JID cannot be null"); 158 } 159 if (subscription == null) { 160 throw new NullPointerException ("Subscription cannot be null"); 161 } 162 Element query = element.element(new QName("query", Namespace.get("jabber:iq:roster"))); 163 if (query == null) { 164 query = element.addElement("query", "jabber:iq:roster"); 165 } 166 Element item = null; 167 for (Iterator i=query.elementIterator("item"); i.hasNext(); ) { 168 Element el = (Element)i.next(); 169 if (el.attributeValue("jid").equals(jid.toString())) { 170 item = el; 171 } 172 } 173 if (item == null) { 174 item = query.addElement("item"); 175 } 176 item.addAttribute("jid", jid.toBareJID()); 177 item.addAttribute("name", name); 178 if (ask != null) { 179 item.addAttribute("ask", ask.toString()); 180 } 181 item.addAttribute("subscription", subscription.toString()); 182 for (Iterator i=item.elementIterator("group"); i.hasNext(); ) { 184 item.remove((Element)i.next()); 185 } 186 if (groups != null) { 188 for (String group : groups) { 189 item.addElement("group").setText(group); 190 } 191 } 192 return new Item(jid, name, ask, subscription, groups); 193 } 194 195 200 public void removeItem(JID jid) { 201 Element query = element.element(new QName("query", Namespace.get("jabber:iq:roster"))); 202 if (query != null) { 203 for (Iterator i=query.elementIterator("item"); i.hasNext(); ) { 204 Element item = (Element)i.next(); 205 if (item.attributeValue("jid").equals(jid.toString())) { 206 query.remove(item); 207 return; 208 } 209 } 210 } 211 } 212 213 218 public Collection<Item> getItems() { 219 Collection<Item> items = new ArrayList<Item>(); 220 Element query = element.element(new QName("query", Namespace.get("jabber:iq:roster"))); 221 if (query != null) { 222 for (Iterator i=query.elementIterator("item"); i.hasNext(); ) { 223 Element item = (Element)i.next(); 224 String jid = item.attributeValue("jid"); 225 String name = item.attributeValue("name"); 226 String ask = item.attributeValue("ask"); 227 String subscription = item.attributeValue("subscription"); 228 Collection<String > groups = new ArrayList<String >(); 229 for (Iterator j=item.elementIterator("group"); j.hasNext(); ) { 230 Element group = (Element)j.next(); 231 groups.add(group.getTextTrim()); 232 } 233 Ask askStatus = ask == null ? null : Ask.valueOf(ask); 234 Subscription subStatus = subscription == null ? 235 null : Subscription.valueOf(subscription); 236 items.add(new Item(new JID(jid), name, askStatus, subStatus, groups)); 237 } 238 } 239 return Collections.unmodifiableCollection(items); 240 } 241 242 247 public Roster createCopy() { 248 return new Roster(this); 249 } 250 251 256 public static class Item { 257 258 private JID jid; 259 private String name; 260 private Ask ask; 261 private Subscription subscription; 262 private Collection<String > groups; 263 264 273 private Item(JID jid, String name, Ask ask, Subscription subscription, 274 Collection<String > groups) { 275 this.jid = jid; 276 this.name = name; 277 this.ask = ask; 278 this.subscription = subscription; 279 this.groups = groups; 280 } 281 282 289 public JID getJID() { 290 return jid; 291 } 292 293 299 public String getName() { 300 return name; 301 } 302 303 308 public Ask getAsk() { 309 return ask; 310 } 311 312 317 public Subscription getSubscription() { 318 return subscription; 319 } 320 321 327 public Collection<String > getGroups() { 328 if (groups == null) { 329 return Collections.emptyList(); 330 } 331 return groups; 332 } 333 334 public String toString() { 335 StringBuffer buf = new StringBuffer (); 336 buf.append("<item "); 337 buf.append("jid=\"").append(jid).append("\""); 338 if (name != null) { 339 buf.append(" name=\"").append(name).append("\""); 340 } 341 buf.append(" subscrption=\"").append(subscription).append("\""); 342 if (groups == null || groups.isEmpty()) { 343 buf.append("/>"); 344 } 345 else { 346 buf.append(">\n"); 347 for (String group : groups) { 348 buf.append(" <group>").append(group).append("</group>\n"); 349 } 350 buf.append("</item>"); 351 } 352 return buf.toString(); 353 } 354 } 355 356 375 public enum Subscription { 376 377 381 none, 382 383 387 to, 388 389 393 from, 394 395 399 both, 400 401 409 remove; 410 } 411 412 423 public enum Ask { 424 425 429 subscribe, 430 431 435 unsubscribe; 436 } 437 } | Popular Tags |