1 package org.columba.addressbook.parser; 17 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 20 import java.util.Date ; 21 import java.util.Iterator ; 22 23 import net.wimpi.pim.Pim; 24 import net.wimpi.pim.contact.basicimpl.AddressImpl; 25 import net.wimpi.pim.contact.basicimpl.CommunicationsImpl; 26 import net.wimpi.pim.contact.basicimpl.ContactImpl; 27 import net.wimpi.pim.contact.basicimpl.EmailAddressImpl; 28 import net.wimpi.pim.contact.basicimpl.OrganizationImpl; 29 import net.wimpi.pim.contact.basicimpl.OrganizationalIdentityImpl; 30 import net.wimpi.pim.contact.basicimpl.PersonalIdentityImpl; 31 import net.wimpi.pim.contact.io.ContactMarshaller; 32 import net.wimpi.pim.contact.io.ContactUnmarshaller; 33 import net.wimpi.pim.contact.model.Address; 34 import net.wimpi.pim.contact.model.Communications; 35 import net.wimpi.pim.contact.model.EmailAddress; 36 import net.wimpi.pim.contact.model.OrganizationalIdentity; 37 import net.wimpi.pim.contact.model.PersonalIdentity; 38 import net.wimpi.pim.contact.model.PhoneNumber; 39 import net.wimpi.pim.factory.ContactIOFactory; 40 41 import org.columba.addressbook.model.AddressModel; 42 import org.columba.addressbook.model.ContactModel; 43 import org.columba.addressbook.model.EmailModel; 44 import org.columba.addressbook.model.IContactModel; 45 import org.columba.addressbook.model.IEmailModel; 46 import org.columba.addressbook.model.PhoneModel; 47 48 56 public class VCardParser { 57 58 66 public static void write(IContactModel c, OutputStream out) { 67 ContactIOFactory ciof = Pim.getContactIOFactory(); 68 ContactMarshaller marshaller = ciof.createContactMarshaller(); 69 marshaller.setEncoding("UTF-8"); 70 71 net.wimpi.pim.contact.model.Contact exportContact = new ContactImpl(); 73 74 PersonalIdentity identity = new PersonalIdentityImpl(); 75 exportContact.setPersonalIdentity(identity); 76 77 79 identity.setSortString(c.getSortString()); 80 81 identity.setFirstname(c.getGivenName()); 83 identity.setFormattedName(c.getFormattedName()); 85 87 identity.setLastname(c.getFamilyName()); 88 89 identity.setBirthDate(c.getBirthday()); 90 91 String [] s = ParserUtil.getArrayOfString(c.getAdditionalNames(), ","); 93 for (int i = 0; i < s.length; i++) { 94 identity.addAdditionalName(s[i]); 95 } 96 97 s = ParserUtil.getArrayOfString(c.getNickName(), ","); 99 for (int i = 0; i < s.length; i++) { 100 identity.addNickname(s[i]); 101 } 102 103 s = ParserUtil.getArrayOfString(c.getNamePrefix(), ","); 105 for (int i = 0; i < s.length; i++) { 106 identity.addPrefix(s[i]); 107 } 108 109 s = ParserUtil.getArrayOfString(c.getNameSuffix(), ","); 111 for (int i = 0; i < s.length; i++) { 112 identity.addSuffix(s[i]); 113 } 114 115 exportContact.setURL(c.getHomePage()); 117 118 Communications communications = new CommunicationsImpl(); 119 exportContact.setCommunications(communications); 120 121 123 Iterator it = c.getEmailIterator(); 124 while (it.hasNext()) { 125 IEmailModel model = (IEmailModel) it.next(); 126 EmailAddress adr = new EmailAddressImpl(); 127 adr.setType(EmailAddress.TYPE_INTERNET); 128 adr.setAddress(model.getAddress()); 129 communications.addEmailAddress(adr); 130 } 131 132 Iterator it2 = c.getAddressIterator(); 134 while (it2.hasNext()) { 135 AddressModel model = (AddressModel) it2.next(); 136 Address adr = new AddressImpl(); 137 adr.setCity(model.getCity()); 138 adr.setPostalCode(model.getZipPostalCode()); 139 adr.setPostBox(model.getPoBox()); 140 adr.setRegion(model.getStateProvinceCounty()); 141 adr.setStreet(model.getStreet()); 142 adr.setLabel(model.getLabel()); 143 exportContact.addAddress(adr); 144 } 145 146 OrganizationalIdentity organizationalIdentity = new OrganizationalIdentityImpl(); 147 exportContact.setOrganizationalIdentity(organizationalIdentity); 148 organizationalIdentity.setOrganization(new OrganizationImpl()); 149 150 organizationalIdentity.getOrganization().setName(c.getOrganisation()); 152 153 exportContact.setNote(c.getNote()); 154 155 marshaller.marshallContact(out, exportContact); 157 } 158 159 167 public static ContactModel fillContactModel(net.wimpi.pim.contact.model.Contact importContact) { 168 ContactModel c = new ContactModel(); 169 170 OrganizationalIdentity organisationalIdentity = importContact 171 .getOrganizationalIdentity(); 172 173 174 if (importContact.hasPersonalIdentity()) { 175 PersonalIdentity identity = importContact.getPersonalIdentity(); 176 177 c.setSortString(identity.getSortString()); 179 180 if (identity.getNicknameCount() > 0) 182 c.setNickName(ParserUtil.getStringOfArray(identity 183 .listNicknames(), ",")); 184 185 if (identity.listPrefixes().length > 0) 187 c.setNamePrefix(ParserUtil.getStringOfArray(identity 188 .listPrefixes(), ",")); 189 190 c.setFamilyName(identity.getLastname()); 191 c.setGivenName(identity.getFirstname()); 192 193 if (identity.listAdditionalNames().length > 0) 195 c.setAdditionalNames(ParserUtil.getStringOfArray(identity 196 .listAdditionalNames(), ",")); 197 198 if (identity.listSuffixes().length > 0) 200 c.setNameSuffix(ParserUtil.getStringOfArray(identity 201 .listSuffixes(), ",")); 202 203 c.setFormattedName(identity.getFormattedName()); 205 206 Date birthday = importContact.getPersonalIdentity().getBirthDate(); 208 if ( birthday != null) 209 c.setBirthday(birthday); 210 211 } 212 213 c.setHomePage(importContact.getURL()); 215 216 if (importContact.hasCommunications()) { 218 Communications communications = importContact.getCommunications(); 219 220 Iterator it = communications.getEmailAddresses(); 221 while (it.hasNext()) { 222 EmailAddress adr = (EmailAddress) it.next(); 223 224 int type = EmailModel.TYPE_WORK; 225 if (adr.isType("HOME")) 226 type = EmailModel.TYPE_HOME; 227 else if (adr.isType("WORK")) 228 type = EmailModel.TYPE_WORK; 229 else if (adr.isType("OTHER")) 230 type = EmailModel.TYPE_OTHER; 231 232 c.addEmail(new EmailModel(adr.getAddress(), 233 type)); 234 } 235 236 it = communications.getPhoneNumbers(); 237 while (it.hasNext()) { 238 PhoneNumber phone = (PhoneNumber)it.next(); 239 240 int type = PhoneModel.TYPE_BUSINESS_PHONE; 241 if ( phone.isCar()) 242 type = PhoneModel.TYPE_CAR_PHONE; 243 else if ( phone.isCellular()) 244 type = PhoneModel.TYPE_MOBILE_PHONE; 245 else if ( phone.isFax()) 246 type = PhoneModel.TYPE_HOME_FAX; 247 else if ( phone.isHome()) 248 type = PhoneModel.TYPE_HOME_PHONE; 249 else if ( phone.isISDN()) 250 type = PhoneModel.TYPE_ISDN; 251 else if ( phone.isPager()) 252 type = PhoneModel.TYPE_PAGER; 253 else if ( phone.isWork()) 254 type = PhoneModel.TYPE_BUSINESS_PHONE; 255 256 c.addPhone(new PhoneModel(phone.getNumber(), type)); 257 } 258 } 259 260 if ( importContact.listAddresses().length > 0 ) { 262 Address[] addresses = importContact.listAddresses(); 264 for ( int i=0; i<addresses.length; i++) { 265 Address a = addresses[i]; 266 267 int type = -1; 268 if ( a.isHome()) 269 type = AddressModel.TYPE_HOME; 270 else if ( a.isWork()) 271 type = AddressModel.TYPE_WORK; 272 else 273 type = AddressModel.TYPE_OTHER; 274 275 276 AddressModel adr = new AddressModel(a.getPostBox(), a.getStreet(), a.getCity(), a.getPostalCode(), a.getRegion(), a.getCountry(), a.getLabel(), type ); 277 278 c.addAddress(adr); 279 } 280 } 281 282 if ( organisationalIdentity != null) { 284 if (organisationalIdentity.hasOrganization()) 285 c.setOrganisation(organisationalIdentity.getOrganization().getName()); 286 287 c.setTitle(organisationalIdentity.getTitle()); 288 c.setProfession(organisationalIdentity.getRole()); 289 } 290 291 c.setNote(importContact.getNote()); 292 293 if (!c.getEmailIterator().hasNext()) 295 c.addEmail(new EmailModel("", EmailModel.TYPE_WORK)); 296 297 return c; 298 } 299 300 307 public static IContactModel[] read(InputStream in) { 308 ContactIOFactory ciof = Pim.getContactIOFactory(); 309 ContactUnmarshaller unmarshaller = ciof.createContactUnmarshaller(); 310 unmarshaller.setEncoding("UTF-8"); 311 312 net.wimpi.pim.contact.model.Contact[] importContacts = unmarshaller 313 .unmarshallContacts(in); 314 315 ContactModel[] contacts = new ContactModel[importContacts.length]; 316 317 for (int i = 0; i < importContacts.length; i++) { 318 contacts[i] = fillContactModel(importContacts[i]); 319 } 320 321 return contacts; 322 } 323 324 } | Popular Tags |