001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.addressbook.folder.importfilter;
019:
020: import java.io.BufferedReader;
021: import java.io.File;
022: import java.io.FileReader;
023:
024: import org.columba.addressbook.folder.AbstractFolder;
025: import org.columba.addressbook.model.ContactModel;
026: import org.columba.addressbook.model.EmailModel;
027: import org.columba.addressbook.model.PhoneModel;
028: import org.columba.addressbook.util.AddressbookResourceLoader;
029:
030: /**
031: * @version 1.0
032: * @author
033: */
034: public class MozillaCSVAddressbookImporter extends
035: DefaultAddressbookImporter {
036:
037: private static final java.util.logging.Logger LOG = java.util.logging.Logger
038: .getLogger("org.columba.addressbook.folder.importfilter"); //$NON-NLS-1$
039:
040: public MozillaCSVAddressbookImporter() {
041: super ();
042: }
043:
044: public MozillaCSVAddressbookImporter(File sourceFile,
045: AbstractFolder destinationFolder) {
046: super (sourceFile, destinationFolder);
047: }
048:
049: public void importAddressbook(File file) throws Exception {
050: LOG.info("importing addressbook::::"); //$NON-NLS-1$
051:
052: BufferedReader in = new BufferedReader(new FileReader(file));
053: String str;
054:
055: while ((str = in.readLine()) != null) {
056: // start parsing line
057: int counter = -1;
058: ContactModel card = new ContactModel();
059:
060: StringBuffer token = new StringBuffer();
061: int pos = 0;
062:
063: while (pos < str.length()) {
064: char ch = str.charAt(pos);
065:
066: if (ch == ',') {
067: // found new token
068: counter++;
069:
070: if (counter == 0) {
071: card.setGivenName(token.toString());
072: } else if (counter == 1) {
073: card.setFamilyName(token.toString());
074: } else if (counter == 2) {
075: // backwards compatibility
076: card.setSortString(token.toString());
077: } else if (counter == 3) {
078: card.setNickName(token.toString());
079: } else if (counter == 4) {
080: card.addEmail(new EmailModel(token.toString(),
081: EmailModel.TYPE_WORK));
082: } else if (counter == 5) {
083: card.addEmail(new EmailModel(token.toString(),
084: EmailModel.TYPE_HOME));
085: } else if (counter == 8) {
086: card.addPhone(new PhoneModel(token.toString(),
087: PhoneModel.TYPE_BUSINESS_PHONE));
088: } else if (counter == 9) {
089: card.addPhone(new PhoneModel(token.toString(),
090: PhoneModel.TYPE_HOME_PHONE));
091: }
092:
093: token = new StringBuffer();
094: } else {
095: token.append(ch);
096: }
097:
098: pos++;
099: }
100:
101: saveContact(card);
102: }
103:
104: in.close();
105: }
106:
107: public String getDescription() {
108: return AddressbookResourceLoader.getString("dialog",
109: "addressbookimport",
110: "mozillacsvaddressbook_description");
111: }
112: }
|