KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > VCardIQProvider


1 import org.jivesoftware.smack.packet.*;
2 import org.jivesoftware.smack.provider.*;
3
4
5 import whisper.*;
6
7 /** An IQ Provider for vcards.
8 * </p><p> Licence: GPL
9 * @author Pheet
10 * @version 0.1
11 * @see VCard */

12 public final class VCardIQProvider implements IQProvider{
13     
14     
15     public VCardIQProvider(){
16         // nothing to do
17
}
18     
19     /** Installs the IQ provider.
20     * Call this method once at the beginning of your code to programatically
21     * register the IQ provider.*/

22     public static void Install(){
23         ProviderManager.addIQProvider("vCard","vcard-temp",new VCardIQProvider());
24     }
25     
26     private static boolean tagPresent(String JavaDoc text, String JavaDoc tag){
27         int i=text.indexOf("<"+tag+">");
28         if (i==-1){
29             return false;
30         }
31         return (text.indexOf("</"+tag+">",i)!=-1);
32     }
33     
34     /**Gets the content of the tag from the text.
35     * The tag itelf is removed from the result.
36     * @return The contents of the tag or <b>null</b> if the tag is not present.*/

37     private static String JavaDoc getTag(String JavaDoc text, String JavaDoc tag){
38         if(!tagPresent(text,tag)){
39             return null;
40         }
41         String JavaDoc result=text.substring(text.indexOf("<"+tag+">")+2+tag.length(),text.indexOf("</"+tag+">"));
42         if(result.equals("")){
43             return null;
44         }
45         return result;
46     }
47     
48     /** Pares method used internally by the SMACK library.*/
49     public IQ parseIQ(org.xmlpull.v1.XmlPullParser parser) throws Exception JavaDoc{
50         VCard iq=new VCard();
51         if (parser.isEmptyElementTag()){
52             return iq;
53         }
54         StringBuffer JavaDoc buff=new StringBuffer JavaDoc();
55         int event=parser.getEventType();
56         // get the content
57
while (!(event==parser.END_TAG && parser.getName().equals("vCard"))){
58             if(event==parser.TEXT){
59                 buff.append(parser.getText());
60             }
61             if(event==parser.START_TAG){
62                 buff.append("<"+parser.getName()+">");
63             }
64             if(event==parser.END_TAG){
65                 buff.append("</"+parser.getName()+">");
66             }
67             event=parser.next();
68         }
69         // easier to parse the content this way than using the xml parser.
70
// It's not elegant, but neither is the vCard DTD....
71
String JavaDoc content=buff.toString();
72         iq.setFullName(getTag(content,"FN"));
73         String JavaDoc name=getTag(content,"N");
74         if(name!=null){
75             iq.setName_Given(getTag(name,"GIVEN"));
76             iq.setName_Middle(getTag(name,"MIDDLE"));
77             iq.setName_Family(getTag(name,"FAMILY"));
78             iq.setName_Prefix(getTag(name,"PREFIX"));
79             iq.setName_Suffix(getTag(name,"SUFFIX"));
80         }
81         iq.setNickname(getTag(content,"NICKNAME"));
82         iq.setURL(getTag(content,"URL"));
83         iq.setBirthday(getTag(content,"BDAY"));
84         iq.setTitle(getTag(content,"TITLE"));
85         iq.setRole(getTag(content,"ROLE"));
86         String JavaDoc org=getTag(content,"ORG");
87         if(org!=null){
88             iq.setOrg_Name(getTag(org,"ORGNAME"));
89             iq.setOrg_Unit(getTag(org,"ORGUNIT"));
90         }
91         iq.setDescription(getTag(content,"DESC"));
92         iq.setJabberID(getTag(content,"JABBERID"));
93         // now get email(s)
94
String JavaDoc section=new String JavaDoc(content);
95         String JavaDoc subsection=getTag(section,"EMAIL");
96         String JavaDoc data;
97         while (subsection!=null){ // while theres still EMAIL tags,
98
data=getTag(subsection,"USERID");
99             if(data!=null){ // make sure the userid tag is there
100
if(subsection.indexOf("<WORK/>")>-1){ // its for work
101
iq.setEmail_Work(data);
102                 }
103                 else{ // its not for work
104
if(subsection.indexOf("<HOME/>")>-1){ // its for home
105
iq.setEmail_Home(data);
106                     }
107                     else{ // its not specified, so...
108
if(iq.getEmail_Work()==null){ // work not set yet so put it there
109
iq.setEmail_Work(data);
110                         }
111                         else{ //work set already, but..
112
if(iq.getEmail_Home()==null){ // home not so put there
113
iq.setEmail_Home(data);
114                             }
115                         }
116                     } // end of its not specified
117
} // end of else its not for work
118
} // end if data!=null
119
section=section.substring(section.indexOf("</EMAIL>")+8); // search space is after the email tag we just processed
120
subsection=getTag(section,"EMAIL");
121         } //end while
122
// Now get address(s)
123
section=content;
124         subsection=getTag(section,"ADR");
125         while (subsection!=null){ // while theres still ADR tags,
126
if(subsection.indexOf("<WORK/>")>-1){ // its for work
127
iq.setAddress_Work_House(getTag(subsection,"EXTADD"));
128                 iq.setAddress_Work_Street(getTag(subsection,"STREET"));
129                 iq.setAddress_Work_Locality(getTag(subsection,"LOCALITY"));
130                 iq.setAddress_Work_Region(getTag(subsection,"REGION"));
131                 iq.setAddress_Work_PCode(getTag(subsection,"PCODE"));
132                 iq.setAddress_Work_Country(getTag(subsection,"CTRY"));
133             }
134             else{ // its not for work
135
if(subsection.indexOf("<HOME/>")>-1){ // its for home
136
iq.setAddress_Home_House(getTag(subsection,"EXTADD"));
137                     iq.setAddress_Home_Street(getTag(subsection,"STREET"));
138                     iq.setAddress_Home_Locality(getTag(subsection,"LOCALITY"));
139                     iq.setAddress_Home_Region(getTag(subsection,"REGION"));
140                     iq.setAddress_Home_PCode(getTag(subsection,"PCODE"));
141                     iq.setAddress_Home_Country(getTag(subsection,"CTRY"));
142                 }
143                 else{ // its not specified, so...
144
if(!iq.hasWorkAddress()){ // work not set yet so put it there
145
iq.setAddress_Work_House(getTag(subsection,"EXTADD"));
146                         iq.setAddress_Work_Street(getTag(subsection,"STREET"));
147                         iq.setAddress_Work_Locality(getTag(subsection,"LOCALITY"));
148                         iq.setAddress_Work_Region(getTag(subsection,"REGION"));
149                         iq.setAddress_Work_PCode(getTag(subsection,"PCODE"));
150                         iq.setAddress_Work_Country(getTag(subsection,"CTRY"));
151                     }
152                     else{ //work set already, but..
153
if(!iq.hasHomeAddress()){ // home not so put there
154
iq.setAddress_Home_House(getTag(subsection,"EXTADD"));
155                             iq.setAddress_Home_Street(getTag(subsection,"STREET"));
156                             iq.setAddress_Home_Locality(getTag(subsection,"LOCALITY"));
157                             iq.setAddress_Home_Region(getTag(subsection,"REGION"));
158                             iq.setAddress_Home_PCode(getTag(subsection,"PCODE"));
159                             iq.setAddress_Home_Country(getTag(subsection,"CTRY"));
160                         }
161                     }
162                 } // end of its not specified
163
} // end of else its not for work
164
section=section.substring(section.indexOf("</ADR>")+6); // search space is after the email tag we just processed
165
subsection=getTag(section,"ADR");
166         } //end while
167
// Now telephone numbers
168
section=content;
169         subsection=getTag(section,"TEL");
170         while (subsection!=null){ // while theres still TEL tags,
171
data=getTag(subsection,"NUMBER");
172             if(data!=null){ // make sure the number tag is there
173
if(subsection.indexOf("<HOME/>")>-1){ // its for home
174
if(subsection.indexOf("<FAX/>")>-1){ // its fax
175
iq.setTel_Home_Fax(data);
176                     }
177                     else{ // not fax
178
if(subsection.indexOf("<MSG/>")>-1){ // its ansa machine
179
iq.setTel_Home_Msg(data);
180                         }
181                         else{ // its voice
182
iq.setTel_Home_Voice(data);
183                         } // end else its voice
184
} // end else not fax
185
}
186                 else{ // its not for home, so assume work
187
if(subsection.indexOf("<FAX/>")>-1){ // its fax
188
iq.setTel_Work_Fax(data);
189                     }
190                     else{ // not fax
191
if(subsection.indexOf("<MSG/>")>-1){ // its ansa machine
192
iq.setTel_Work_Msg(data);
193                         }
194                         else{ // its voice
195
iq.setTel_Work_Voice(data);
196                         } // end else its voice
197
} // end else not fax
198
}// end else is not for home
199
} // end if data!=null
200
section=section.substring(section.indexOf("</TEL>")+6); // search space is after the email tag we just processed
201
subsection=getTag(section,"TEL");
202         } //end while
203

204         // whisper extra
205
String JavaDoc key=getTag(content,"KEY");
206         if(key!=null){
207             try{
208                 PublicKey pk=new PublicKey(key);
209                 iq.setKey(pk);
210             }
211             catch (Exception JavaDoc e){
212                 // invalid key - ignore
213
}
214         }
215         // finally return the VCard IQ
216
return iq;
217     }
218 }
219     
220     
Popular Tags