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: import java.nio.ByteBuffer;
024:
025: import org.columba.addressbook.folder.AbstractFolder;
026: import org.columba.addressbook.model.ContactModel;
027: import org.columba.addressbook.model.EmailModel;
028: import org.columba.addressbook.model.PhoneModel;
029: import org.columba.addressbook.util.AddressbookResourceLoader;
030: import org.columba.ristretto.coder.Base64;
031:
032: /**
033: * Import addressbook data in LDIF format.
034: */
035: public class NetscapeLDIFAddressbookImporter extends
036: DefaultAddressbookImporter {
037: public NetscapeLDIFAddressbookImporter() {
038: super ();
039: }
040:
041: public NetscapeLDIFAddressbookImporter(File sourceFile,
042: AbstractFolder destinationFolder) {
043: super (sourceFile, destinationFolder);
044: }
045:
046: public void importAddressbook(File file) throws Exception {
047: BufferedReader in = new BufferedReader(new FileReader(file));
048: String str;
049: ContactModel card = new ContactModel();
050:
051: while ((str = in.readLine()) != null) {
052: // start parsing line by line
053: if (str.length() == 0) {
054: // empty line, means new contactcard
055: saveContact(card);
056:
057: card = new ContactModel();
058: } else {
059: // parse key:value lines
060: int index = str.indexOf(':');
061:
062: if ((index > 0) && (index < (str.length() - 1))) {
063: String key = str.substring(0, index);
064: String value;
065:
066: if (str.charAt(index + 1) == ':') {
067: ByteBuffer bytes = Base64.decode(str.substring(
068: index + 2).trim());
069: value = new String(bytes.array(), "UTF-8");
070: } else {
071: value = str.substring(index + 1);
072: }
073:
074: value = value.trim();
075:
076: if (key.equalsIgnoreCase("cn")) {
077: card.setSortString(value);
078: } else if (key.equalsIgnoreCase("givenname")) {
079: card.setGivenName(value);
080: } else if (key.equalsIgnoreCase("sn")) {
081: card.setFamilyName(value);
082: } else if (key.equalsIgnoreCase("mail")) {
083: card.addEmail(new EmailModel(value,
084: EmailModel.TYPE_WORK));
085: } else if (key.equalsIgnoreCase("xmozillanickname")) {
086: card.setNickName(value);
087: } else if (key.equalsIgnoreCase("o")) {
088: card.setOrganisation(value);
089: } else if (key.equalsIgnoreCase("telephonenumber")) {
090: card.addPhone(new PhoneModel(value,
091: PhoneModel.TYPE_BUSINESS_PHONE));
092: } else if (key.equalsIgnoreCase("homephone")) {
093: card.addPhone(new PhoneModel(value,
094: PhoneModel.TYPE_HOME_PHONE));
095: } else if (key
096: .equalsIgnoreCase("facsimiletelephonenumber")) {
097: card.addPhone(new PhoneModel(value,
098: PhoneModel.TYPE_BUSINESS_FAX));
099: } else if (key.equalsIgnoreCase("pagerphone")) {
100: card.addPhone(new PhoneModel(value,
101: PhoneModel.TYPE_PAGER));
102: } else if (key.equalsIgnoreCase("cellphone")) {
103: card.addPhone(new PhoneModel(value,
104: PhoneModel.TYPE_MOBILE_PHONE));
105: } else if (key.equalsIgnoreCase("homeurl")) {
106: card.setHomePage(value);
107: }
108: }
109: }
110: }
111:
112: in.close();
113: }
114:
115: public String getDescription() {
116: return AddressbookResourceLoader.getString("dialog",
117: "addressbookimport",
118: "netscapeldifaddressbook_description");
119: }
120: }
|