KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CSVAddressbookImporter


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18

19 import java.io.BufferedReader JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileReader JavaDoc;
22
23 import org.columba.addressbook.folder.AbstractFolder;
24 import org.columba.addressbook.folder.importfilter.DefaultAddressbookImporter;
25 import org.columba.addressbook.model.ContactModel;
26 import org.columba.addressbook.model.EmailModel;
27 import org.columba.addressbook.model.PhoneModel;
28 import org.columba.addressbook.util.AddressbookResourceLoader;
29
30 /**
31  *
32  * Import a CSV, comma separated list of contacts
33  *
34  * @version 1.0
35  * @author fdietz
36  */

37 public class CSVAddressbookImporter extends DefaultAddressbookImporter {
38
39     public CSVAddressbookImporter() {
40         super();
41     }
42
43     public CSVAddressbookImporter(File JavaDoc sourceFile,
44             AbstractFolder destinationFolder) {
45         super(sourceFile, destinationFolder);
46     }
47
48     public void importAddressbook(File JavaDoc file) throws Exception JavaDoc {
49
50         // open file
51
BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
52         String JavaDoc str;
53
54         while ((str = in.readLine()) != null) {
55             // start parsing line
56
int counter = -1;
57
58             // create new contact card
59
ContactModel card = new ContactModel();
60
61             StringBuffer JavaDoc token = new StringBuffer JavaDoc();
62             int pos = 0;
63             while (pos < str.length()) {
64                 char ch = str.charAt(pos);
65
66                 if (ch == ',') {
67                     // found new token
68
counter++;
69
70                     if (counter == 0) {
71                         card.setGivenName(token.toString());
72                     } else if (counter == 1) {
73                         card.setFamilyName(token.toString());
74                     } else if (counter == 2) {
75                         card.setSortString(token.toString());
76                     } else if (counter == 3) {
77                         card.setNickName(token.toString());
78                     } else if (counter == 4) {
79                         card.addEmail(new EmailModel(token.toString(),
80                                 EmailModel.TYPE_WORK));
81                     } else if (counter == 5) {
82                         card.addEmail(new EmailModel(token.toString(),
83                                 EmailModel.TYPE_HOME));
84                     } else if (counter == 8) {
85                         card.addPhone(new PhoneModel(token.toString(),
86                                 PhoneModel.TYPE_BUSINESS_PHONE));
87                     } else if (counter == 9) {
88                         card.addPhone(new PhoneModel(token.toString(),
89                                 PhoneModel.TYPE_HOME_PHONE));
90                     }
91
92                     token = new StringBuffer JavaDoc();
93
94                 } else {
95                     token.append(ch);
96                 }
97
98                 pos++;
99
100             }
101
102             // add contact to addressbook
103
saveContact(card);
104
105         }
106
107         in.close();
108     }
109
110     public String JavaDoc getDescription() {
111         return AddressbookResourceLoader.getString("dialog",
112                 "addressbookimport", "mozillacsvaddressbook_description");
113     }
114
115 }
116
Popular Tags