View Javadoc

1   /*------------------------------------------------------------------------------
2    * The contents of this file are subject to the Mozilla Public License Version
3    * 1.1 (the "License"); you may not use this file except in compliance with
4    * the License. You may obtain a copy of the License at
5    * http://www.mozilla.org/MPL/
6    * Software distributed under the License is distributed on an "AS IS" basis,
7    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
8    * the specific language governing rights and limitations under the License.
9    *
10   * The Original Code is levelonelabs.com code.
11   * The Initial Developer of the Original Code is Level One Labs. Portions
12   * created by the Initial Developer are Copyright (C) 2001 the Initial
13   * Developer. All Rights Reserved.
14   *
15   *         Contributor(s):
16   *             Scott Oster      (ostersc@alum.rpi.edu)
17   *             Steve Zingelwicz (sez@po.cwru.edu)
18   *             William Gorman   (willgorman@hotmail.com)
19   *
20   * Alternatively, the contents of this file may be used under the terms of
21   * either the GNU General Public License Version 2 or later (the "GPL"), or
22   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23   * in which case the provisions of the GPL or the LGPL are applicable
24   * instead of those above. If you wish to allow use of your version of this
25   * file only under the terms of either the GPL or the LGPL, and not to allow
26   * others to use your version of this file under the terms of the NPL, indicate
27   * your decision by deleting the provisions above and replace them with the
28   * notice and other provisions required by the GPL or the LGPL. If you do not
29   * delete the provisions above, a recipient may use your version of this file
30   * under the terms of any one of the NPL, the GPL or the LGPL.
31   *----------------------------------------------------------------------------*/
32  
33  package com.levelonelabs.aim;
34  
35  import org.w3c.dom.*;
36  
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.Iterator;
40  
41  
42  /***
43   * Represents and AIM Buddy
44   * 
45   * @author Will Gorman, Scott Oster
46   * @created November 8, 2001
47   */
48  public class AIMBuddy implements XMLizable {
49      String name;
50      transient boolean online;
51      transient int warningAmount = 0;
52      boolean banned;
53      ArrayList messages = new ArrayList();
54      HashMap roles = new HashMap();
55      HashMap preferences = new HashMap();
56      String group;
57  
58  
59      /***
60       * Constructor for the AIMBuddy object
61       * 
62       * @param name
63       */
64      public AIMBuddy(String name) {
65          this(name, AIMClient.DEFAULT_GROUP);
66      }
67  
68  
69      /***
70       * Constructor for the AIMBuddy object
71       * 
72       * @param name
73       * @param group
74       *            buddy group in buddylist
75       */
76      public AIMBuddy(String name, String group) {
77          this.name = name;
78          setGroup(group);
79      }
80  
81  
82      /***
83       * Sets the name attribute of the AIMBuddy object
84       * 
85       * @param name
86       *            The new name value
87       */
88      public void setName(String name) {
89          this.name = name;
90      }
91  
92  
93      /***
94       * Sets the online attribute of the AIMBuddy object
95       * 
96       * @param online
97       *            The new online value
98       */
99      public void setOnline(boolean online) {
100         this.online = online;
101     }
102 
103 
104     /***
105      * Sets the preference attribute of the AIMBuddy object
106      * 
107      * @param pref
108      *            The new preference value
109      * @param val
110      *            The new preference value
111      */
112     public void setPreference(String pref, String val) {
113         preferences.put(pref, val);
114     }
115 
116 
117     /***
118      * Gets the name attribute of the AIMBuddy object
119      * 
120      * @return The name value
121      */
122     public String getName() {
123         return name;
124     }
125 
126 
127     /***
128      * Gets the online attribute of the AIMBuddy object
129      * 
130      * @return The online value
131      */
132     public boolean isOnline() {
133         return online;
134     }
135 
136 
137     /***
138      * Gets the preference attribute of the AIMBuddy object
139      * 
140      * @param pref
141      * @return The preference value
142      */
143     public String getPreference(String pref) {
144         return (String) preferences.get(pref);
145     }
146 
147 
148     /***
149      * Gets the preferences attribute of the AIMBuddy object
150      * 
151      * @return The preferences value
152      */
153     public HashMap getPreferences() {
154         return preferences;
155     }
156 
157 
158     /***
159      * Adds a feature to the Role attribute of the AIMBuddy object
160      * 
161      * @param role
162      *            The feature to be added to the Role attribute
163      */
164     public void addRole(String role) {
165         roles.put(role, role);
166     }
167 
168 
169     /***
170      * Gets the messages attribute of the AIMBuddy object
171      * 
172      * @return The messages value
173      */
174     public ArrayList getMessages() {
175         return messages;
176     }
177 
178 
179     /***
180      * Adds a feature to the Message attribute of the AIMBuddy object
181      * 
182      * @param message
183      *            The feature to be added to the Message attribute
184      */
185     public void addMessage(String message) {
186         messages.add(message);
187     }
188 
189 
190     /***
191      * Remove all messages
192      */
193     public void clearMessages() {
194         messages.clear();
195     }
196 
197 
198     /***
199      * Does buddy have messages?
200      * 
201      * @return true for more than 0 messages
202      */
203     public boolean hasMessages() {
204         return !messages.isEmpty();
205     }
206 
207 
208     /***
209      * Do I have specified role
210      * 
211      * @param role
212      * @return true if buddy has the role
213      */
214     public boolean hasRole(String role) {
215         return roles.containsKey(role);
216     }
217 
218 
219     /***
220      * Returns the banned.
221      * 
222      * @return boolean
223      */
224     public boolean isBanned() {
225         return banned;
226     }
227 
228 
229     /***
230      * Sets the banned.
231      * 
232      * @param banned
233      *            The banned to set
234      */
235     public void setBanned(boolean banned) {
236         this.banned = banned;
237     }
238 
239 
240     /***
241      * @see com.levelonelabs.aim.XMLizable#readState(Element)
242      */
243     public void readState(Element fullStateElement) {
244         // parse group
245         String group = fullStateElement.getAttribute("group");
246         if (group == null || group.trim().equals("")) {
247             group = AIMSender.DEFAULT_GROUP;
248         }
249         setGroup(group);
250 
251         // parse banned
252         String ban = fullStateElement.getAttribute("isBanned");
253         if (ban.equalsIgnoreCase("true")) {
254             setBanned(true);
255         } else {
256             setBanned(false);
257         }
258 
259         // parse roles
260         roles = new HashMap();
261         NodeList list = fullStateElement.getElementsByTagName("role");
262         for (int i = 0; i < list.getLength(); i++) {
263             Element roleElem = (Element) list.item(i);
264             String role = roleElem.getAttribute("name");
265             addRole(role);
266         }
267 
268         // parse messages
269         messages = new ArrayList();
270         list = fullStateElement.getElementsByTagName("message");
271         for (int i = 0; i < list.getLength(); i++) {
272             Element messElem = (Element) list.item(i);
273             NodeList cdatas = messElem.getChildNodes();
274             for (int j = 0; j < cdatas.getLength(); j++) {
275                 Node node = cdatas.item(j);
276                 if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
277                     String message = node.getNodeValue();
278                     addMessage(message);
279                     break;
280                 }
281             }
282         }
283 
284         // parse prefs
285         preferences = new HashMap();
286         list = fullStateElement.getElementsByTagName("preference");
287         for (int i = 0; i < list.getLength(); i++) {
288             Element prefElem = (Element) list.item(i);
289             String pref = prefElem.getAttribute("name");
290             String val = prefElem.getAttribute("value");
291             this.setPreference(pref, val);
292         }
293     }
294 
295 
296     /***
297      * @see com.levelonelabs.aim.XMLizable#writeState(Element)
298      */
299     public void writeState(Element emptyStateElement) {
300         Document doc = emptyStateElement.getOwnerDocument();
301         emptyStateElement.setAttribute("name", this.getName());
302         emptyStateElement.setAttribute("group", this.getGroup());
303         emptyStateElement.setAttribute("isBanned", Boolean.toString(this.isBanned()));
304 
305         Iterator roleit = roles.keySet().iterator();
306         while (roleit.hasNext()) {
307             String role = (String) roleit.next();
308             Element roleElem = doc.createElement("role");
309             roleElem.setAttribute("name", role);
310             emptyStateElement.appendChild(roleElem);
311         }
312 
313         Iterator prefs = preferences.keySet().iterator();
314         while (prefs.hasNext()) {
315             String pref = (String) prefs.next();
316             Element prefElem = doc.createElement("preference");
317             prefElem.setAttribute("name", pref);
318             prefElem.setAttribute("value", (String) preferences.get(pref));
319             emptyStateElement.appendChild(prefElem);
320         }
321 
322         for (int i = 0; i < messages.size(); i++) {
323             String message = (String) messages.get(i);
324             Element messElem = doc.createElement("message");
325             CDATASection data = doc.createCDATASection(message);
326             messElem.appendChild(data);
327             emptyStateElement.appendChild(messElem);
328         }
329     }
330 
331 
332     /***
333      * Gets the current warning amount
334      * 
335      * @return the warning amount
336      */
337     public int getWarningAmount() {
338         return warningAmount;
339     }
340 
341 
342     /***
343      * Sets the current warning amount
344      * 
345      * @param amount
346      */
347     public void setWarningAmount(int amount) {
348         warningAmount = amount;
349     }
350 
351 
352     /***
353      * Set the group the buddy is in, in the buddy list
354      * 
355      * @param group
356      *            The name of the group this buddy belongs to.
357      */
358     public void setGroup(String group) {
359         this.group = group;
360     }
361 
362 
363     /***
364      * The name of the group this buddy belongs to, in the buddylist
365      * 
366      * @return The name of the group this buddy belongs to.
367      */
368     public String getGroup() {
369         return group;
370     }
371 }