KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > parser > VCardParser


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.addressbook.parser;
17
18 import java.io.InputStream JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.Iterator JavaDoc;
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 /**
49  * Contact data parser for a vCard-standard compliant text/plain file.
50  * <p>
51  * It makes use of the jpim library. Its not really a wrapper. It only
52  * creates a mapping between the jpim data model and our data model.
53  *
54  * @author fdietz
55  */

56 public class VCardParser {
57
58     /**
59      * Write vcard contact to outpustream.
60      *
61      * @param c
62      * contact data
63      * @param out
64      * outputstream
65      */

66     public static void write(IContactModel c, OutputStream JavaDoc out) {
67         ContactIOFactory ciof = Pim.getContactIOFactory();
68         ContactMarshaller marshaller = ciof.createContactMarshaller();
69         marshaller.setEncoding("UTF-8");
70
71         // create jpim contact instance
72
net.wimpi.pim.contact.model.Contact exportContact = new ContactImpl();
73
74         PersonalIdentity identity = new PersonalIdentityImpl();
75         exportContact.setPersonalIdentity(identity);
76
77         // set sort-string/displayname
78

79         identity.setSortString(c.getSortString());
80
81         // set first name
82
identity.setFirstname(c.getGivenName());
83         // set formatted name
84
identity.setFormattedName(c.getFormattedName());
85         // set last name
86

87         identity.setLastname(c.getFamilyName());
88         
89         identity.setBirthDate(c.getBirthday());
90
91         // add all additional names (middle names)
92
String JavaDoc[] s = ParserUtil.getArrayOfString(c.getAdditionalNames(), ",");
93         for (int i = 0; i < s.length; i++) {
94             identity.addAdditionalName(s[i]);
95         }
96
97         // add all nicknames
98
s = ParserUtil.getArrayOfString(c.getNickName(), ",");
99         for (int i = 0; i < s.length; i++) {
100             identity.addNickname(s[i]);
101         }
102
103         // add all prefixes
104
s = ParserUtil.getArrayOfString(c.getNamePrefix(), ",");
105         for (int i = 0; i < s.length; i++) {
106             identity.addPrefix(s[i]);
107         }
108
109         // add all suffixes
110
s = ParserUtil.getArrayOfString(c.getNameSuffix(), ",");
111         for (int i = 0; i < s.length; i++) {
112             identity.addSuffix(s[i]);
113         }
114
115         // set website/homepage
116
exportContact.setURL(c.getHomePage());
117
118         Communications communications = new CommunicationsImpl();
119         exportContact.setCommunications(communications);
120
121         // add email addresses
122

123         Iterator JavaDoc 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         // add all addresses
133
Iterator JavaDoc 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         // set name of organization
151
organizationalIdentity.getOrganization().setName(c.getOrganisation());
152
153         exportContact.setNote(c.getNote());
154         
155         // save contact to outputstream
156
marshaller.marshallContact(out, exportContact);
157     }
158
159     /**
160      * Fill the contact model from the import contact
161      *
162      * @param importContact import contact
163      *
164      * @return contact model
165      *
166      */

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             // sort-string
178
c.setSortString(identity.getSortString());
179
180             // list of nick names
181
if (identity.getNicknameCount() > 0)
182                 c.setNickName(ParserUtil.getStringOfArray(identity
183                         .listNicknames(), ","));
184
185             // list of prefixes
186
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             // list of additional names (middle names)
194
if (identity.listAdditionalNames().length > 0)
195                 c.setAdditionalNames(ParserUtil.getStringOfArray(identity
196                         .listAdditionalNames(), ","));
197
198             // list of suffices
199
if (identity.listSuffixes().length > 0)
200                 c.setNameSuffix(ParserUtil.getStringOfArray(identity
201                         .listSuffixes(), ","));
202
203             // formatted name
204
c.setFormattedName(identity.getFormattedName());
205             
206             // birthday
207
Date JavaDoc birthday = importContact.getPersonalIdentity().getBirthDate();
208             if ( birthday != null)
209                 c.setBirthday(birthday);
210             
211         }
212
213         // url to website/homepage
214
c.setHomePage(importContact.getURL());
215
216         // email addresses and phone numbers
217
if (importContact.hasCommunications()) {
218             Communications communications = importContact.getCommunications();
219
220             Iterator JavaDoc 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         // address list
261
if ( importContact.listAddresses().length > 0 ) {
262             // not that the editor ui only supports max of 3 addresses to edit
263
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         // name of organisation
283
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         // dummy address
294
if (!c.getEmailIterator().hasNext())
295             c.addEmail(new EmailModel("", EmailModel.TYPE_WORK));
296         
297         return c;
298     }
299
300     /**
301      * Parse vCard contact data from inputstream.
302      *
303      * @param in
304      * inputstream to vCard data
305      * @return contact
306      */

307     public static IContactModel[] read(InputStream JavaDoc 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