KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > packet > RosterPacket


1 /**
2  * $RCSfile$
3  * $Revision: 2408 $
4  * $Date: 2004-11-02 20:53:30 -0300 (Tue, 02 Nov 2004) $
5  *
6  * Copyright 2003-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/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed 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.jivesoftware.smack.packet;
22
23 import java.util.*;
24
25 /**
26  * Represents XMPP roster packets.
27  *
28  * @author Matt Tucker
29  */

30 public class RosterPacket extends IQ {
31
32     private List rosterItems = new ArrayList();
33
34     /**
35      * Adds a roster item to the packet.
36      *
37      * @param item a roster item.
38      */

39     public void addRosterItem(Item item) {
40         synchronized (rosterItems) {
41             rosterItems.add(item);
42         }
43     }
44
45     /**
46      * Returns the number of roster items in this roster packet.
47      *
48      * @return the number of roster items.
49      */

50     public int getRosterItemCount() {
51         synchronized (rosterItems) {
52             return rosterItems.size();
53         }
54     }
55
56     /**
57      * Returns an Iterator for the roster items in the packet.
58      *
59      * @return and Iterator for the roster items in the packet.
60      */

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 JavaDoc getChildElementXML() {
69         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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     /**
82      * A roster item, which consists of a JID, their name, the type of subscription, and
83      * the groups the roster item belongs to.
84      */

85     public static class Item {
86
87         private String JavaDoc user;
88         private String JavaDoc name;
89         private ItemType itemType;
90         private ItemStatus itemStatus;
91         private List groupNames;
92
93         /**
94          * Creates a new roster item.
95          *
96          * @param user the user.
97          * @param name the user's name.
98          */

99         public Item(String JavaDoc user, String JavaDoc name) {
100             this.user = user;
101             this.name = name;
102             itemType = null;
103             itemStatus = null;
104             groupNames = new ArrayList();
105         }
106
107         /**
108          * Returns the user.
109          *
110          * @return the user.
111          */

112         public String JavaDoc getUser() {
113             return user;
114         }
115
116         /**
117          * Returns the user's name.
118          *
119          * @return the user's name.
120          */

121         public String JavaDoc getName() {
122             return name;
123         }
124
125         /**
126          * Sets the user's name.
127          *
128          * @param name the user's name.
129          */

130         public void setName(String JavaDoc name) {
131             this.name = name;
132         }
133
134         /**
135          * Returns the roster item type.
136          *
137          * @return the roster item type.
138          */

139         public ItemType getItemType() {
140             return itemType;
141         }
142
143         /**
144          * Sets the roster item type.
145          *
146          * @param itemType the roster item type.
147          */

148         public void setItemType(ItemType itemType) {
149             this.itemType = itemType;
150         }
151
152         /**
153          * Returns the roster item status.
154          *
155          * @return the roster item status.
156          */

157         public ItemStatus getItemStatus() {
158             return itemStatus;
159         }
160
161         /**
162          * Sets the roster item status.
163          *
164          * @param itemStatus the roster item status.
165          */

166         public void setItemStatus(ItemStatus itemStatus) {
167             this.itemStatus = itemStatus;
168         }
169
170         /**
171          * Returns an Iterator for the group names (as Strings) that the roster item
172          * belongs to.
173          *
174          * @return an Iterator for the group names.
175          */

176         public Iterator getGroupNames() {
177             synchronized (groupNames) {
178                 return Collections.unmodifiableList(groupNames).iterator();
179             }
180         }
181
182         /**
183          * Adds a group name.
184          *
185          * @param groupName the group name.
186          */

187         public void addGroupName(String JavaDoc groupName) {
188             synchronized (groupNames) {
189                 if (!groupNames.contains(groupName)) {
190                     groupNames.add(groupName);
191                 }
192             }
193         }
194
195         /**
196          * Removes a group name.
197          *
198          * @param groupName the group name.
199          */

200         public void removeGroupName(String JavaDoc groupName) {
201             synchronized (groupNames) {
202                 groupNames.remove(groupName);
203             }
204         }
205
206         public String JavaDoc toXML() {
207             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc groupName = (String JavaDoc)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     /**
231      * The subscription status of a roster item. An optional element that indicates
232      * the subscription status if a change request is pending.
233      */

234     public static class ItemStatus {
235
236         /**
237          * Request to subcribe.
238          */

239         public static final ItemStatus SUBSCRIPTION_PENDING = new ItemStatus("subscribe");
240
241         /**
242          * Request to unsubscribe.
243          */

244         public static final ItemStatus UNSUBCRIPTION_PENDING = new ItemStatus("unsubscribe");
245
246         public static ItemStatus fromString(String JavaDoc 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 JavaDoc value;
263
264         /**
265          * Returns the item status associated with the specified string.
266          *
267          * @param value the item status.
268          */

269         private ItemStatus(String JavaDoc value) {
270             this.value = value;
271         }
272
273         public String JavaDoc toString() {
274             return value;
275         }
276     }
277
278     /**
279      * The subscription type of a roster item.
280      */

281     public static class ItemType {
282
283         /**
284          * The user and subscriber have no interest in each other's presence.
285          */

286         public static final ItemType NONE = new ItemType("none");
287
288         /**
289          * The user is interested in receiving presence updates from the subscriber.
290          */

291         public static final ItemType TO = new ItemType("to");
292
293         /**
294          * The subscriber is interested in receiving presence updates from the user.
295          */

296         public static final ItemType FROM = new ItemType("from");
297
298         /**
299          * The user and subscriber have a mutual interest in each other's presence.
300          */

301         public static final ItemType BOTH = new ItemType("both");
302
303         /**
304          * The user wishes to stop receiving presence updates from the subscriber.
305          */

306         public static final ItemType REMOVE = new ItemType("remove");
307
308         public static ItemType fromString(String JavaDoc 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 JavaDoc value;
334
335         /**
336          * Returns the item type associated with the specified string.
337          *
338          * @param value the item type.
339          */

340         public ItemType(String JavaDoc value) {
341             this.value = value;
342         }
343
344         public String JavaDoc toString() {
345             return value;
346         }
347     }
348 }
Popular Tags