Category: Mozilla Thunderbird

Google Parser to (now vCard), version 3.0

Google Parser to (now vCard), version 3.0

 

I have not dedicated time to the Google Address Book converter, only the Alpine part was done, but today I decided to also include the converter for vCard (v2.1), seems working fine in Mozilla Thunderbird when I import the generated file.

Shortly the code (only of the converter)

package at.mavila.gcontactsalp.converters;

import at.mavila.gcontactsalp.pojos.AddressBook;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Converts the List of Address book to a CharSequence.
 *
 * Created by mavila on 4/16/17.
 */
public class VisitCardConverter implements Converter<List<AddressBook>, CharSequence> {


 private static final Pattern NAME_SPLIT = Pattern.compile("(.+)(\\s+)(.+)");


 @Override
 public CharSequence convert(final List<AddressBook> addressBooks) {

 if (addressBooks == null || addressBooks.isEmpty()) {
 return StringUtils.EMPTY;
 }


 final StringBuilder stringBuilder = new StringBuilder();


 addressBooks.stream().filter(addressBook -> addressBook != null).forEach(addressBook -> {

 stringBuilder.append("begin:vcard\r\n");
 stringBuilder.append("fn:").append(addressBook.getFullname()).append("\r\n");


 stringBuilder.append("n:").append(createN(addressBook.getFullname())).append("\r\n");

 final AtomicInteger atomicInteger = new AtomicInteger(1);
 addressBook.getAddress().forEach(emailAddress -> stringBuilder.append("email;internet;" + (atomicInteger.getAndIncrement() == 1 ? "HOME:" : "WORK:")).append(emailAddress).append("\r\n"));

 if (!addressBook.getComment().contains("Google")) {
 stringBuilder.append("note:").append(addressBook.getComment()).append("\r\n");
 }

 stringBuilder.append("version:2.1\r\n");
 stringBuilder.append("end:vcard\r\n");
 stringBuilder.append("\r\n");
 });


 return stringBuilder.toString();
 }


 private String createN(final String fullName) {

 if (StringUtils.isEmpty(fullName)) {
 return StringUtils.EMPTY;
 }


 final Matcher matcher = NAME_SPLIT.matcher(fullName);

 if (matcher.find()) {
 return matcher.group(3) + ";" + matcher.group(1);
 }


 return fullName+";";


 }
}

The list of Address book is already parsed by the SAX parser, so we just deal with the conversion in

VisitCardConverter

To call it as a web service:

curl localhost:8080/importVCard -d @mutt.xml --header "Content-Type:text/xml" > vas.vcf

Where mutt.xml is produced by the Google dump command, see my other post related.

Grab the code from:

https://github.com/totopoloco/GoogleContactsParserToAlpine

Advertisements