Application of Java5.0 in the project

xiaoxiao2021-03-06  21

/ ** matt1.22 * $ RCSFILE: Roster.java, V $ matt1.63 * $ Revision: 1.6 $ 4 * $ DATE: 2005/03/10 22:35:03 $ matt1.15 * 6 * CopyRight 2004 JIVE . Software 7 * 8 * All rights reserved Licensed under the Apache License, Version 2.0 (the "License");.. 9 * you may not use this file except in compliance with the License 10 * you may obtain a copy of the License AT 11 * 12 * http://www.apache.org/license/license-2.0 13 * 14 * unless to in Writing, Software 15 * Distributed Under The license is distributed on an "as is" .. BASIS, 16 * WITHOUT WARRANTIES oR CONDITIONS OF ANY KIND, either express or implied 17 * See the License for the specific language governing permissions and 18 * limitations under the License 19 * / 20 21 package org.xmpp.packet; 22 23 Import org.dom4j. *; 24 25 Import java.util. *; 26 27 28 / ** 29 * Roster Packet. The roster is a list of jids (type other user) That 30 * The user wishes to track the Presence of Each R Oster item is keyed by 31 * jid and contacts a nickname (optional), Subscription Type, and list of 32 * groups (optional). 33 * 34 * @Author matt tucker 35 * / 36 public class roster extends IQ {37 38 / ** 39 * Constructs a new roster with an automatically generated id and a type 40 * of {@Link Type # get q.type.get}. 41 * / 42 public roster () {43 super (); 44 element.addelement ("Query", "Jabber: IQ: roster"); 45} 46 47 / ** 48 * Constructs a new roster Using the specified type. a packet id will 49 * be automaticly generated. 50 * 51 * @Param Type IQ TYPE. 52 * / 53 public roster (Type Type) {54 Super (Type);

55 Element.addelement ("Query", "Jabber: IQ: Roster"); 56} 57 58 / ** 59 * Constructs a new roster Using The Specified Type and ID. 60 * 61 * @Param Type The IQ TYPE. 62 * @Param ID The Packet ID of the IQ. 63 * / 64 Public Roster (Type Type, String ID) {65 Super (Type, ID); 66 Element.addelement ("Query", "Jabber: IQ: roster") ; 67} 68 69 / ** Matt1.670 * Constructs a new roster this is a copy of an existing roster. 71 * 72 * @Param roster the roster packet. 73 * @see #createcopy () 74 * / 75 private roster (Roster roster) {76 Element elementCopy = roster.element.createCopy ();. 77 docFactory.createDocument () add (elementCopy); 78 this.element = elementCopy; 79} 80 81 / ** matt1.182 * Constructs a new Roster Using An Existing Element. This is useful 83 * for Parsing Incoming Roster Elements INTO ROSTER Objects. 84 * 85 * @Param Element the Roster ELEM ENT. 86 * / 87 public roster (Element Element) {88 Super (Element); 89} 90 91 / ** 92 * Adds a new item to the roster. The name and groups are set to null 93 * If the roster packet already contains an item using the same JID, the 94 * information in the existing item will be overwritten with the new information.

95 * 96 * The XMPP specification recommends that if the roster item is associated With another 97 * instant message user (human), That the Jid Be in Bare Form (EG User @ domain). 98 * use the {@link jid # tobarejid () Tobarejid ()} Method for a bare jid. 99 * 100 * @

param jid the JID. 101 * @param subscription the subscription type. gaston1.3102 * @return the newly created item. matt1.1103 * / gaston1.3104 public Item addItem (String jid, Subscription subscription) {matt1.1105 if (getType () == q.type.get || getType () == q.type.error) {106 throw new illegalstateException ("iq type must be 'result' or 'set'); 107} 108 if (JID = = NULL) {109 Throw new NullPointersRexception ("Jid Cannot Be Null"); 110} Gaston1.3111 Return AddIst (JID), NULL, NULL, SUBSCRIPTION, NULL); Matt1.1112} 113 114 / ** 115 * Adds a new item to the roster. The name and groups are set to null 116 * If the roster packet already contains an item using the same JID, the 117 * information in the existing item will be overwritten WITH The New Information.

118 * 119 * The xmpp specification recommends That if the roster item is associated with noothe R 120 * Instant Messaging User (Human), That The Jid Be in Bare Form (EG User @ Domain). 121 * Use the {@Link Jid # tobarejid () TobareJid ()} Method for a Bare Jid. 122 * 123 * @param jid the JID. 124 * @param subscription the subscription type. gaston1.3125 * @return the newly created item. matt1.1126 * / gaston1.3127 public Item addItem (JID jid, Subscription subscription) {gaston1.5128 if ( Gettype ()! = q.type.result && gettype ()! = iq.type.set) {matt1.1129 throw new iLlegalStateException ("iq type must be 'result' or 'set');

131 IF (JID == NULL) {132 Throw new NullPointerexception ("JID Cannot Be Null"); 133} Gaston1.3134 Return AddIst (JID, NULL, NULL, SUBSCRIPTION, NULL); Matt1.1135} 136 137 / ** 138 * Adds a new item to the roster. If the roster packet already contains an item 139 * using the same JID, the information in the existing item will be overwritten 140 * with the new information.

141 * 142 * The XMPP specification recommends that if the roster item is associated with another 143 * instant messaging user (human), that the JID be in bare form (eg user @ domain). 144 * Use the {@link JID # toBareJID () toBareJID ( Method for a bare jid. 145 * 146 * @Param Jid The Jid. 147 * @Param name the nickname. Gaston1.3148 * @Param ask the ask type. Matt1.1149 * @Param Subscription Thepe. 150 * @Param Groups a Collection of Groups. Gaston1.3151 * @return the newly created item. matt1.1152 * / gaston1.3153 public Item addItem (JID jid, String name, Ask ask, Subscription subscription, 154 Collection groups) matt1.1155 {156 if (jid == null) {157 throw new NullPointerException ( "JID can not be null"); 158} 159 if (subscription == null) {160 throw new NullPointerException ( "Subscription can not 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.elementItemrator ("item"); I.hasNext ();) {168 Element EL = (element) i.next (); 169 IF ("jid"). Equals (jid.tostring ())) {170 item = EL; 171} 172} 173 if (item == null) {174 item = query.addelement ("item"); 175} Gaston1.5176 item.addattribute ("jid", jid.tobarejid ()); matt1.1177 item.addattribute ("name", name); gaston1.3178 IF (ask! = null) {179 item.addattribute ("ask", ask.TOSTRING ()); 180} Matt1.1181 item.addattribute ("subscription", subscription.toString ()); 182 // ERASE EXISTING GROUPS In Case the item previously existed. 183 for (iTerator i = Item.eMetterator ("group"); I.hasNext ();) {184 item.remove ((Element) i.next ()); 185} 186 // Add in groups. 18 7 IF (Groups! = Null) {188 for (String Group: Groups) {189 item.addelement ("group"). SetText (group); 190} 191} Gaston1.3192 Return New Item (JID, Name, Ask, Subscription, Groups; Matt1.1193} 194 195 / ** 196 * Removes An Item from this Roster. 197 * 198 * @Param Jid The Jid of The item to remove. 199 * / 200 public void RemoveItem (JID JID) { 201 element query = element.ement (new qname, namespace.get ("jabber: q: 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 / ** 214 * Returns an unmodifiable copy of the {@link items} in the roster packet. 215 * 216 * @return an unmodifable copy of the {@link Item items} in the roster packet. 217 * / 218 public Collection getItems () {219 Collection items = new ArrayList (); 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 ("J id "); 225 String name = item.attributeValue (" name "); gaston1.3226 String ask = item.attributeValue (" ask "); matt1.1227 String subscription = item.attributeValue (" subscription "); 228 Collection < String> Groups = new arraylist (); 229 for (Iterator J = Item.eMTiterator ("group"); j.hasnext ();) {230 element group = (element) j.Next (); 231 Groups .add (Group.getTextTrim ()); 232} Gaston1.4233 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)); matt1.1237} 238} 239 return Collections.unmodifiableCollection (items);. 240} 241 242 / ** matt1.2243 * Returns a deep copy of this Roster 244 * 245 * @return a deep copy Of this roster. 246 * / 247 public roster createcopy () {matt1.6248 return new roster (this); matt1.2249} 250 251 / ** matt1.1252 * Item in a roster, Which represents an Individual Contact. Each Contact 253 * has a JID, an optional nickname, a subscription type, and can belong to 254 * one ore more groups 255 * / 256 public static class Item {257 258 private JID jid;. 259 private String name; gaston1.3260 private Ask ask; matt1.1261 private Subscription subscription; 262 private Collection groups;. 263 264 / ** 265 * Constructs a new roster item 266 * 267 * @param jid the JID 268 * @param name the nickname gaston1.3269 * @param ask.. the ask state. matt1.1270 * @param subscription the subscription state. 271 * @param groups the item groups. 272 ​​* / gaston1.3273 private Item (JID jid, String name, Ask ask, Subscription subscription, 274 Collection Groups) {matt1.1275 this.jid = JID; 276 this.name = name; gaston1.3277 this.ask =

ask; matt1.1278 this.subscription = subscription; 279 this.groups = groups;. 280} 281 282 / ** 283 * Returns the JID associated with this item The JID is the "key" in the 284 * list of items that Make Up A Roster. There Can Only Be A Single Item Per 285 * Jid in A Roster. 286 * 287 * @return The Jid Associated with this item. 288 * / 289 public jid getjid () {290 returnjid; 291} 292 293 / ** 294 * Returns The Nickname Associated with this item. If no nickname exists, 295 * null is returned. 296 * 297 * @return the nickname, or null IT doesn't exist. 298 * / 299 public string getName () {300 return name; 301} 302 303 / ** gast1.3304 * Returns the ask State of this item. 305 * 306 * @return the ask State of This item. 307 * / 308 public Ask getAsk () {309 return ask; 310}.. 311 312 / ** matt1.1313 * Returns the subscription state of this item 314 * 315 * @return the subscription state of this item 316 * / 317 public Subscription getSubscription () {318 return subscription; 319} 320 321 / ** 322 * Returns a Collection of the groups defined in this item If 323 * no groups are defined, an empty Collection is returned 324 * 325 * @return the groups.. In this item. 326 * / 327 public collection

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 (""); 344} 345 else {346 buf.append ("> / n"); 347 for (String Group: groups) {348 BUF.Append (""). Append (group) .append (" / n"); 349} 350 buf.append (""); 351} 352 return buf.tostring ( ); 353} 354} 355 356 / ** 357 * TYPE-SAFE ENUMERATION for the roster subscription type. Valid Subcription Types: 358 * 359 *

    360 *
  • {@ link #none roster.subscription.none} - The User Does Not Have A 361 * Subscription To The Contact's Presence Information, and The Contact 362 * Does Not Have A Subscription To The User's Presence Information. 363 *
  • {@

    link #to Roster.Subscription.to} -. the user has a subscription to 364 * the contact's presence information, but the contact does not have a 365 * subscription to the user's presence information 366 *

  • {@ link #from Roster.Subscription.from} -. the contact has a subscription 367 * to the user's presence information, but the user does not have a 368 * subscription to the contact's presence information gaston1.3369 *
  • {@ link #both Roster .Subscription.both} - both the user and the contact matt1.1370 * have subscriptions to each other's presence information gaston1.3371 *
  • {@ link #remove Roster.Subscription.remove} -. the user is removing a 372 * Contact from his or her roster. Matt1.1373 * 374 * / 375 public enum subscription {376 377 / ** 378 * The user does not have a subscription to the contact's presence information, 379 * And the contact does not have a subscription to the user's presence information. 380 * / 381 none, 382 383 / ** 384 * The user has a subscription to the contact's presence information, but the 385 * contact does not have a subscription to The User's Presence Information. 386 * / 387 TO, 388 389/387 TO, 388 389 / ** 390 * The Contact Has A Subscription To The User's Presence Information, But The 391 * User Does Not Have A Subscription To the Contact '

    Spesence Information. 392 * / 393 from, 394 395 / ** 396 * Both the user and the contact has subscribhip to each other's present 397 * information. 398 * / gaston1.3399 Both, 400 401 / ** 402 * The user is removing a contact from his or her roster. The user's server will 403 * 1) automatically cancel any existing presence subscription between the user and the 404 * contact, 2) remove the roster item from the user's roster and inform all of the user's 405 * available resources that have requested the roster of the roster item removal, 3) inform 406 * the resource that initiated the removal of success and 4) send unavailable presence from 407 * all of the user's available resources to the contact. 408 * / 409 REMOVE; 410} 411 412 / ** 413 * Type-Safe Enumeration for the Roster Ask Type. Valid Ask Types: 414 * 415 *

      416 *
    • {@ link #subscribe Roster.Ask.subscribe} -. The roster item has been asked 417 * for permission to subscribe to their presence but no response has been received 418 *
    • {@ link #unsubscribe Roster .Ask.unsubscribe} - the roster owner Has asked 419 * to the roster item to unsubscribe from it's presence but has not received 420 * confirmation. 421 *

转载请注明原文地址:https://www.9cbs.com/read-40506.html

New Post(0)