KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > VCard


1 import org.jivesoftware.smack.filter.*;
2 import org.jivesoftware.smack.packet.*;
3 import org.jivesoftware.smack.*;
4 import whisper.PublicKey;
5 import org.jivesoftware.smack.util.StringUtils;
6
7 /** A VCard class for use with the <a HREF="http://www.jivesoftware.com/xmpp/smack/" target="_blank">SMACK jabber library</a>.
8 * </p><p>
9 * You should refer to the <a HREF="http://www.jabber.org/jeps/jep-0054.html" target="_blank">JEP-54 documentation</a>.
10 * </p><p>
11 * Please note that this class is incomplete but it does provide the most commonly found information in vCards.
12 * Also remember that VCard transfer is not a standard, and the protocol may change or be replaced.
13 * </p><p>
14 * When recieving a vcard and address, telephone or email data is found and it is not specified whether it is for work or home,
15 * it is treated as if it is for work, so it is usefull when retrieving a vcard to call getTel_Work_Voice(), getEmail_Work(),
16 * etc first. {@link #hasHomeAddress() hasHomeAddress()} and {@link #hasWorkAddress() hasWorkAddress()} can be useful.
17 * </p><p>
18 * <b>Usage:</b> <ul>
19 * <li> Add VCard.class and VCardIQProvider.class to your classpath. </li>
20 * <li> Call <code> {@link VCardIQProvider#Install() VCardIQProvider#Install()} </code> at the beginning of your code, <br>
21 * <b>Or</b> <br>
22 * Add the following in the smack.providers file in the META-INF directory of the smack jar: <br>
23 * <pre><code>&lt;iqProvider&gt;
24          &lt;elementName&gt;vCard&lt;/elementName&gt;
25          &lt;namespace&gt;vcard-temp&lt;/namespace&gt;
26          &lt;className&gt;VCardIQProvider&lt;/className&gt;
27      &lt;/iqProvider&gt;
28 * </code></pre>
29 * See the <a HREF="http://www.jivesoftware.com/builds/docs/smack/latest/documentation/providers.html" target="_blank">SMACK extensions doumentation</a> for more information.</i></li>
30 * <li> Use the <code>{@link #fetch(XMPPConnection connection) fetch}</code> method to retrieve the users Vcard, the <code>set</code> methods to alter it,
31 * and <code>{@link #upload(XMPPConnection connection) upload}</code> to upload it. </li>
32 * <li> Use the <code>{@link #fetch(XMPPConnection connection, String user) fetch}</code> to retrieve another users VCard and
33 * the <code>get</code> methods to obtain element data. </li>
34 * <li> The <code>get</code> methods will return null if no data for that element is present.</li> </ul>
35 * </p><p>
36 * Licence: GPL.
37 * @author Pheet
38 * @version 0.1 */

39 public final class VCard extends IQ{
40
41     private boolean set=false;
42     private boolean work_address=false;
43     private boolean home_address=false;
44     
45     private String JavaDoc FullName;
46     private String JavaDoc Name_Given;
47     private String JavaDoc Name_Middle;
48     private String JavaDoc Name_Family;
49     private String JavaDoc Name_Prefix;
50     private String JavaDoc Name_Suffix;
51     private String JavaDoc Nickname;
52     private String JavaDoc URL;
53     private String JavaDoc Birthday;
54     private String JavaDoc Title;
55     private String JavaDoc Role;
56     private String JavaDoc Org_Name;
57     private String JavaDoc Org_Unit;
58     private String JavaDoc Tel_Work_Voice;
59     private String JavaDoc Tel_Work_Fax;
60     private String JavaDoc Tel_Work_Msg;
61     private String JavaDoc Tel_Home_Voice;
62     private String JavaDoc Tel_Home_Fax;
63     private String JavaDoc Tel_Home_Msg;
64     private String JavaDoc Address_Work_House;
65     private String JavaDoc Address_Work_Street;
66     private String JavaDoc Address_Work_Locality;
67     private String JavaDoc Address_Work_Region;
68     private String JavaDoc Address_Work_PCode;
69     private String JavaDoc Address_Work_Country;
70     private String JavaDoc Address_Home_House;
71     private String JavaDoc Address_Home_Street;
72     private String JavaDoc Address_Home_Locality;
73     private String JavaDoc Address_Home_Region;
74     private String JavaDoc Address_Home_PCode;
75     private String JavaDoc Address_Home_Country;
76     private String JavaDoc Email_Work;
77     private String JavaDoc Email_Home;
78     private String JavaDoc Description;
79     private String JavaDoc JabberID;
80         
81     private PublicKey Key;
82     
83     /** Creates a new blank VCard.*/
84     public VCard(){
85         //nothing to do
86
}
87     
88     // get Methods
89

90     public String JavaDoc getFullName(){
91         return FullName;
92     }
93     public String JavaDoc getName_Given(){
94         return Name_Given;
95     }
96     public String JavaDoc getName_Middle(){
97         return Name_Middle;
98     }
99     public String JavaDoc getName_Family(){
100         return Name_Family;
101     }
102     public String JavaDoc getName_Prefix(){
103         return Name_Prefix;
104     }
105     public String JavaDoc getName_Suffix(){
106         return Name_Suffix;
107     }
108     public String JavaDoc getNickname(){
109         return Nickname;
110     }
111     public String JavaDoc getURL(){
112         return URL;
113     }
114     public String JavaDoc getBirthday(){
115         return Birthday;
116     }
117     public String JavaDoc getTitle(){
118         return Title;
119     }
120     public String JavaDoc getRole(){
121         return Role;
122     }
123     public String JavaDoc getOrg_Name(){
124         return Org_Name;
125     }
126     public String JavaDoc getOrg_Unit(){
127         return Org_Unit;
128     }
129     public String JavaDoc getTel_Work_Voice(){
130         return Tel_Work_Voice;
131     }
132     public String JavaDoc getTel_Work_Fax(){
133         return Tel_Work_Fax;
134     }
135     public String JavaDoc getTel_Work_Msg(){
136         return Tel_Work_Msg;
137     }
138     public String JavaDoc getTel_Home_Voice(){
139         return Tel_Home_Voice;
140     }
141     public String JavaDoc getTel_Home_Fax(){
142         return Tel_Home_Fax;
143     }
144     public String JavaDoc getTel_Home_Msg(){
145         return Tel_Home_Msg;
146     }
147     public String JavaDoc getAddress_Work_House(){
148         return Address_Work_House;
149     }
150     public String JavaDoc getAddress_Work_Street(){
151         return Address_Work_Street;
152     }
153     public String JavaDoc getAddress_Work_Locality(){
154         return Address_Work_Locality;
155     }
156     public String JavaDoc getAddress_Work_Region(){
157         return Address_Work_Region;
158     }
159     public String JavaDoc getAddress_Work_PCode(){
160         return Address_Work_PCode;
161     }
162     public String JavaDoc getAddress_Work_Country(){
163         return Address_Work_Country;
164     }
165     public String JavaDoc getAddress_Home_House(){
166         return Address_Home_House;
167     }
168     public String JavaDoc getAddress_Home_Street(){
169         return Address_Home_Street;
170     }
171     public String JavaDoc getAddress_Home_Locality(){
172         return Address_Home_Locality;
173     }
174     public String JavaDoc getAddress_Home_Region(){
175         return Address_Home_Region;
176     }
177     public String JavaDoc getAddress_Home_PCode(){
178         return Address_Home_PCode;
179     }
180     public String JavaDoc getAddress_Home_Country(){
181         return Address_Home_Country;
182     }
183     public String JavaDoc getEmail_Work(){
184         return Email_Work;
185     }
186     public String JavaDoc getEmail_Home(){
187         return Email_Home;
188     }
189     public String JavaDoc getDescription(){
190         return Description;
191     }
192     public String JavaDoc getJabberID(){
193         return JabberID;
194     }
195     
196     
197     public PublicKey getKey(){
198         return Key;
199     }
200     
201     // Set Methods
202

203     public void setFullName(String JavaDoc value){
204         if(value==null){return;}
205         set=true;
206         FullName=value;
207     }
208     public void setName_Given(String JavaDoc value){
209         if(value==null){return;}
210         set=true;
211         Name_Given=value;
212     }
213     public void setName_Middle(String JavaDoc value){
214         if(value==null){return;}
215         set=true;
216         Name_Middle=value;
217     }
218     public void setName_Family(String JavaDoc value){
219         if(value==null){return;}
220         set=true;
221         Name_Family=value;
222     }
223     public void setName_Prefix(String JavaDoc value){
224         if(value==null){return;}
225         set=true;
226         Name_Prefix=value;
227     }
228     public void setName_Suffix(String JavaDoc value){
229         if(value==null){return;}
230         set=true;
231         Name_Suffix=value;
232     }
233     public void setNickname(String JavaDoc value){
234         if(value==null){return;}
235         set=true;
236         Nickname=value;
237     }
238     public void setURL(String JavaDoc value){
239         if(value==null){return;}
240         set=true;
241         URL=value;
242     }
243     public void setBirthday(String JavaDoc value){
244         if(value==null){return;}
245         set=true;
246         Birthday=value;
247     }
248     public void setTitle(String JavaDoc value){
249         if(value==null){return;}
250         set=true;
251         Title=value;
252     }
253     public void setRole(String JavaDoc value){
254         if(value==null){return;}
255         set=true;
256         Role=value;
257     }
258     public void setOrg_Name(String JavaDoc value){
259         if(value==null){return;}
260         set=true;
261         Org_Name=value;
262     }
263     public void setOrg_Unit(String JavaDoc value){
264         if(value==null){return;}
265         set=true;
266         Org_Unit=value;
267     }
268     public void setTel_Work_Voice(String JavaDoc value){
269         if(value==null){return;}
270         set=true;
271         Tel_Work_Voice=value;
272     }
273     public void setTel_Work_Fax(String JavaDoc value){
274         if(value==null){return;}
275         set=true;
276         Tel_Work_Fax=value;
277     }
278     public void setTel_Work_Msg(String JavaDoc value){
279         if(value==null){return;}
280         set=true;
281         Tel_Work_Msg=value;
282     }
283     public void setTel_Home_Voice(String JavaDoc value){
284         if(value==null){return;}
285         set=true;
286         Tel_Home_Voice=value;
287     }
288     public void setTel_Home_Fax(String JavaDoc value){
289         if(value==null){return;}
290         set=true;
291         Tel_Home_Fax=value;
292     }
293     public void setTel_Home_Msg(String JavaDoc value){
294         if(value==null){return;}
295         set=true;
296         Tel_Home_Msg=value;
297     }
298     public void setAddress_Work_House(String JavaDoc value){
299         if(value==null){return;}
300         set=work_address=true;
301         Address_Work_House=value;
302     }
303     public void setAddress_Work_Street(String JavaDoc value){
304         if(value==null){return;}
305         set=work_address=true;
306         Address_Work_Street=value;
307     }
308     public void setAddress_Work_Locality(String JavaDoc value){
309         if(value==null){return;}
310         set=work_address=true;
311         Address_Work_Locality=value;
312     }
313     public void setAddress_Work_Region(String JavaDoc value){
314         if(value==null){return;}
315         set=work_address=true;
316         Address_Work_Region=value;
317     }
318     public void setAddress_Work_PCode(String JavaDoc value){
319         if(value==null){return;}
320         set=work_address=true;
321         Address_Work_PCode=value;
322     }
323     public void setAddress_Work_Country(String JavaDoc value){
324         if(value==null){return;}
325         set=work_address=true;
326         Address_Work_Country=value;
327     }
328     public void setAddress_Home_House(String JavaDoc value){
329         if(value==null){return;}
330         set=home_address=true;
331         Address_Home_House=value;
332     }
333     public void setAddress_Home_Street(String JavaDoc value){
334         if(value==null){return;}
335         set=home_address=true;
336         Address_Home_Street=value;
337     }
338     public void setAddress_Home_Locality(String JavaDoc value){
339         if(value==null){return;}
340         set=home_address=true;
341         Address_Home_Locality=value;
342     }
343     public void setAddress_Home_Region(String JavaDoc value){
344         if(value==null){return;}
345         set=home_address=true;
346         Address_Home_Region=value;
347     }
348     public void setAddress_Home_PCode(String JavaDoc value){
349         if(value==null){return;}
350         set=home_address=true;
351         Address_Home_PCode=value;
352     }
353     public void setAddress_Home_Country(String JavaDoc value){
354         if(value==null){return;}
355         set=home_address=true;
356         Address_Home_Country=value;
357     }
358     public void setEmail_Work(String JavaDoc value){
359         if(value==null){return;}
360         set=true;
361         Email_Work=value;
362     }
363     public void setEmail_Home(String JavaDoc value){
364         if(value==null){return;}
365         set=true;
366         Email_Home=value;
367     }
368     public void setDescription(String JavaDoc value){
369         if(value==null){return;}
370         set=true;
371         Description=value;
372     }
373     public void setJabberID(String JavaDoc value){
374         if(value==null){return;}
375         set=true;
376         JabberID=value;
377     }
378     
379     public void setKey(PublicKey key){
380         Key=key;
381     }
382     // Util methods
383

384     /** Fetches the vcard for the current user.
385     * Warning - This method blocks until either the connection times out,
386     * or the vcard is retrieved.
387     * @param connection The jabber connection.
388     * @return The vcard for the connected user.
389     * @exception XMPPException If there is a problem retrieving the vcard.*/

390     public static VCard fetch(XMPPConnection connection) throws XMPPException, NullPointerException JavaDoc {
391         if(connection==null){throw new NullPointerException JavaDoc("Null connection passed to fetch method");}
392         if(!connection.isConnected()){
393             throw new XMPPException("Not connected");
394         }
395         if(connection.isAnonymous()){
396             throw new XMPPException("User must be logged in");
397         }
398         VCard iq=new VCard();
399         iq.setType(IQ.Type.GET);
400         iq.setFrom(connection.getUser());
401         PacketCollector collector=connection.createPacketCollector(new PacketIDFilter(iq.getPacketID()));
402         connection.sendPacket(iq);
403         VCard result;
404         IQ iqresult=null;
405         try{
406             iqresult=(IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
407             result=(VCard) iqresult;
408         }
409         catch (ClassCastException JavaDoc cce){
410             // probably returned a result IQ when user has no stored vcard yet
411
result=new VCard(); // return an empty vcard
412
}
413         if(result==null){
414             throw new XMPPException(new XMPPError(408,"Request Timeout"));
415         }
416         if(result.getError()!=null){
417             throw new XMPPException(result.getError());
418         }
419         return result;
420     }
421     
422     /** Fetches the vcard for another user.
423     * Warning - This method blocks until either the connection times out,
424     * or the vcard is retrieved.
425     * @param connection The jabber connection.
426     * @param user The full username of the user we want the vcard of,
427     * eg fred@ jabber.org
428     * @return The vcard for other user.
429     * @exception XMPPException If there is a problem retrieving the vcard.*/

430     public static VCard fetch(XMPPConnection connection, String JavaDoc user) throws XMPPException, NullPointerException JavaDoc {
431         if(connection==null || user==null){
432             throw new NullPointerException JavaDoc("Null parameter passed to fetch method");
433         }
434         if(!connection.isConnected()){
435             throw new XMPPException("Not connected");
436         }
437         VCard iq=new VCard();
438         iq.setType(IQ.Type.GET);
439         iq.setTo(user);
440         PacketCollector collector=connection.createPacketCollector(new PacketIDFilter(iq.getPacketID()));
441         connection.sendPacket(iq);
442         VCard result;
443         IQ iqresult=null;
444         try{
445             iqresult=(IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
446             result=(VCard) iqresult;
447         }
448         catch (ClassCastException JavaDoc cce){
449             // probably returned a result IQ when user has no stored vcard yet
450
result=new VCard(); // return an empty vcard
451
}
452         if(iqresult==null){
453             throw new XMPPException(new XMPPError(408,"Request Timeout"));
454         }
455         if(iqresult.getError()!=null){
456             throw new XMPPException(iqresult.getError());
457         }
458         return result;
459     }
460     
461     /** Stores the vcard for the current user.
462     * Warning - This method blocks until either the connection times out,
463     * or the vcard is uploaded, or an error is returned.
464     * @param connection The jabber connection.
465     * @exception XMPPException If there is a problem retrieving the vcard.*/

466     public void upload(XMPPConnection connection) throws XMPPException, NullPointerException JavaDoc {
467         if(connection==null){throw new NullPointerException JavaDoc("Null connection passed to fetch method");}
468         if(!connection.isConnected()){
469             throw new XMPPException("Not connected");
470         }
471         if(connection.isAnonymous()){
472             throw new XMPPException("User must be logged in");
473         }
474         this.setType(IQ.Type.SET);
475         this.setFrom(connection.getUser());
476         PacketCollector collector=connection.createPacketCollector(new PacketIDFilter(this.getPacketID()));
477         connection.sendPacket(this);
478         IQ result=(IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
479         if(result==null){
480             throw new XMPPException(new XMPPError(408,"Request Timeout"));
481         }
482         if(result.getError()!=null){
483             throw new XMPPException(result.getError());
484         }
485     }
486     
487     /** Returns true if this VCard contains a home address.*/
488     public boolean hasHomeAddress(){
489         return home_address;
490     }
491     
492     /** Returns true if this VCard contains a work address.*/
493     public boolean hasWorkAddress(){
494         return work_address;
495     }
496     
497     private static String JavaDoc print(String JavaDoc text, String JavaDoc tag){
498         if(text==null){
499             return ("<"+tag+"/>");
500         }
501         return ("<"+tag+">"+text+"</"+tag+">");
502     }
503     
504     /** Method used internally by smack.*/
505     public String JavaDoc getChildElementXML(){
506         if(!set){
507             return ("<vCard xmlns='vcard-temp'/>");
508         }
509         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
510         buf.append("<vCard xmlns='vcard-temp'>");
511         // full name
512
buf.append(print(FullName,"FN"));
513         // name
514
buf.append("<N>");
515         if(Name_Prefix!=null){
516             buf.append(print(Name_Prefix,"PREFIX"));
517         }
518         buf.append(print(Name_Given,"GIVEN")).append(print(Name_Middle,"MIDDLE"));
519         buf.append(print(Name_Family,"FAMILY"));
520         if(Name_Suffix!=null){
521             buf.append(print(Name_Suffix,"SUFFIX"));
522         }
523         buf.append("</N>");
524         buf.append(print(Nickname,"NICKNAME"));
525         if(URL!=null){
526             buf.append("<URL>"+URL+"</URL>");
527         }
528         if(Birthday!=null){
529             buf.append("<BDAY>"+Birthday+"</BDAY>");
530         }
531         if(Title!=null){
532             buf.append("<TITLE>"+Title+"</TITLE>");
533         }
534         if(Role!=null){
535             buf.append("<ROLE>"+Role+"</ROLE>");
536         }
537         // org
538
if(!(Org_Name==null && Org_Unit==null)){
539             buf.append("<ORG>").append(print(Org_Name,"ORGNAME")).append(print(Org_Unit,"ORGUNIT")).append("</ORG>");
540         }
541         // work address
542
if(work_address){
543             buf.append("<ADR>").append("<WORK/>");
544             buf.append(print(Address_Work_House,"EXTADR"));
545             buf.append(print(Address_Work_Street,"STREET"));
546             buf.append(print(Address_Work_Locality,"LOCALITY"));
547             buf.append(print(Address_Work_Region,"REGION"));
548             buf.append(print(Address_Work_PCode,"PCODE"));
549             buf.append(print(Address_Work_Country,"CTRY")).append("</ADR>");
550         }
551         // home address
552
if(home_address){
553             buf.append("<ADR>").append("<HOME/>");
554             buf.append(print(Address_Home_House,"EXTADR"));
555             buf.append(print(Address_Home_Street,"STREET"));
556             buf.append(print(Address_Home_Locality,"LOCALITY"));
557             buf.append(print(Address_Home_Region,"REGION"));
558             buf.append(print(Address_Home_PCode,"PCODE"));
559             buf.append(print(Address_Home_Country,"CTRY")).append("</ADR>");
560         }
561         // email
562
if(Email_Work!=null){
563             buf.append("<EMAIL><INTERNET/><WORK/><USERID>"+Email_Work+"</USERID></EMAIL>");
564         }
565         if(Email_Home!=null){
566             buf.append("<EMAIL><INTERNET/><HOME/><USERID>"+Email_Home+"</USERID></EMAIL>");
567         }
568         // phone
569
if(Tel_Work_Voice!=null){
570             buf.append("<TEL><WORK/><VOICE/><NUMBER>"+Tel_Work_Voice+"</NUMBER></TEL>");
571         }
572         if(Tel_Work_Fax!=null){
573             buf.append("<TEL><WORK/><FAX/><NUMBER>"+Tel_Work_Fax+"</NUMBER></TEL>");
574         }
575         if(Tel_Work_Msg!=null){
576             buf.append("<TEL><WORK/><MSG/><NUMBER>"+Tel_Work_Msg+"</NUMBER></TEL>");
577         }
578         if(Tel_Home_Voice!=null){
579             buf.append("<TEL><HOME/><VOICE/><NUMBER>"+Tel_Home_Voice+"</NUMBER></TEL>");
580         }
581         if(Tel_Home_Fax!=null){
582             buf.append("<TEL><HOME/><FAX/><NUMBER>"+Tel_Home_Fax+"</NUMBER></TEL>");
583         }
584         if(Tel_Home_Msg!=null){
585             buf.append("<TEL><HOME/><MSG/><NUMBER>"+Tel_Home_Msg+"</NUMBER></TEL>");
586         }
587         if(Description!=null){
588             buf.append("<DESC>"+Description+"</DESC>");
589         }
590         if(JabberID!=null){
591             buf.append("<JABBERID>"+JabberID+"</JABBERID>");
592         }
593         
594         if(Key!=null){
595             buf.append("<KEY>"+Key.getText()+"</KEY>");
596         }
597         
598         buf.append("</vCard>");
599         return buf.toString();
600     }
601 }
Popular Tags