1 import org.jivesoftware.smack.packet.*; 2 import org.jivesoftware.smack.provider.*; 3 4 5 import whisper.*; 6 7 12 public final class VCardIQProvider implements IQProvider{ 13 14 15 public VCardIQProvider(){ 16 } 18 19 22 public static void Install(){ 23 ProviderManager.addIQProvider("vCard","vcard-temp",new VCardIQProvider()); 24 } 25 26 private static boolean tagPresent(String text, String 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 37 private static String getTag(String text, String tag){ 38 if(!tagPresent(text,tag)){ 39 return null; 40 } 41 String 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 49 public IQ parseIQ(org.xmlpull.v1.XmlPullParser parser) throws Exception { 50 VCard iq=new VCard(); 51 if (parser.isEmptyElementTag()){ 52 return iq; 53 } 54 StringBuffer buff=new StringBuffer (); 55 int event=parser.getEventType(); 56 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 String content=buff.toString(); 72 iq.setFullName(getTag(content,"FN")); 73 String 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 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 String section=new String (content); 95 String subsection=getTag(section,"EMAIL"); 96 String data; 97 while (subsection!=null){ data=getTag(subsection,"USERID"); 99 if(data!=null){ if(subsection.indexOf("<WORK/>")>-1){ iq.setEmail_Work(data); 102 } 103 else{ if(subsection.indexOf("<HOME/>")>-1){ iq.setEmail_Home(data); 106 } 107 else{ if(iq.getEmail_Work()==null){ iq.setEmail_Work(data); 110 } 111 else{ if(iq.getEmail_Home()==null){ iq.setEmail_Home(data); 114 } 115 } 116 } } } section=section.substring(section.indexOf("</EMAIL>")+8); subsection=getTag(section,"EMAIL"); 121 } section=content; 124 subsection=getTag(section,"ADR"); 125 while (subsection!=null){ if(subsection.indexOf("<WORK/>")>-1){ 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{ if(subsection.indexOf("<HOME/>")>-1){ 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{ if(!iq.hasWorkAddress()){ 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{ if(!iq.hasHomeAddress()){ 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 } } section=section.substring(section.indexOf("</ADR>")+6); subsection=getTag(section,"ADR"); 166 } section=content; 169 subsection=getTag(section,"TEL"); 170 while (subsection!=null){ data=getTag(subsection,"NUMBER"); 172 if(data!=null){ if(subsection.indexOf("<HOME/>")>-1){ if(subsection.indexOf("<FAX/>")>-1){ iq.setTel_Home_Fax(data); 176 } 177 else{ if(subsection.indexOf("<MSG/>")>-1){ iq.setTel_Home_Msg(data); 180 } 181 else{ iq.setTel_Home_Voice(data); 183 } } } 186 else{ if(subsection.indexOf("<FAX/>")>-1){ iq.setTel_Work_Fax(data); 189 } 190 else{ if(subsection.indexOf("<MSG/>")>-1){ iq.setTel_Work_Msg(data); 193 } 194 else{ iq.setTel_Work_Voice(data); 196 } } } } section=section.substring(section.indexOf("</TEL>")+6); subsection=getTag(section,"TEL"); 202 } 204 String key=getTag(content,"KEY"); 206 if(key!=null){ 207 try{ 208 PublicKey pk=new PublicKey(key); 209 iq.setKey(pk); 210 } 211 catch (Exception e){ 212 } 214 } 215 return iq; 217 } 218 } 219 220 | Popular Tags |