Fabian Meier Fabian Meier - 20 days ago 4
Java Question

Java issues writing Multi-VCard

Good Day

I'm trying to Export an addressbook into a single multi-vcard, in order to use it with a Gigaset N520-IP Pro

The Following is an example Entry, which i created in the Gigaset Handphone, and exported it with the webgui Export function:


BEGIN:VCARD
VERSION:2.1
N:Bob;Bobbington

TEL;HOME:00412345689
TEL;WORK:00419876543

TEL;CELL:004112345432
END:VCARD

BEGIN:VCARD
VERSION:2.1
N:NotBob;NotBobbington

TEL;HOME:00412345689
TEL;WORK:00419876543

TEL;CELL:004112345432
END:VCARD

BEGIN:VCARD....


I used this as a template for generating the specific VCards needed for the Import, but whenever i try to Import it it does only accept the whole vcard as one Person, even if there are 20 entries in it.

My Entries look like this:


BEGIN:VCARD
VERSION:2.1
N:Max;Mustermann

TEL;HOME:00411234578
TEL;WORK:00411234567

TEL;CELL:00411234590
END:VCARD


BEGIN:VCARD
VERSION:2.1
N:MHUEUEx;MusterHEHEmann

TEL;HOME:00411234578
TEL;WORK:00411234567

TEL;CELL:00411234590
END:VCARD


I can't see a visual difference between mine, and the exported vcard.

If i copy the entry of mine into the other vcard it immediately stops working.

But if i dupliate an existing entry inside the working vcard, the vcard still works.

I suppose i've some Kind of Encoding issue with the whole file.

I also checked the VCard Wiki Site for possible mistakes of mine, and tried a few fixes.

I tried:

SafeCharing all Strings to the Alphabet only (a-z && A-Z) (Except the Numbers)

Temporary replacing the + in the numbers with a 00.

Reducing the VCARD String to ASCII only.

Using different line Separators ("\n, \r\n", System.lineSeparator())


System Data:

OS: Centos 6.8(Final)

Java-Version: 1.8.0_102-b14 (x64)


Now, here's the actual Code of mine:

//Code for Address book fetching would be here
log.debug("Received : " + Addressbook.size());

File ExportFile = new File(ExportFilePath);

if(ExportFile.exists())
{
ExportFile.delete();
}
ExportFile.createNewFile();

log.debug("Writing VCards to File: " + ExportFile.getAbsolutePath());

FileWriter FW = new FileWriter(ExportFile);

Map<String, DataEntry> Properties = null;

String homephone="";
String phone="";
String mobile="";
String firstname="";
String lastname="";

StringBuilder SB = null;
String Fix="";

for(AddressbookContact AC : Addressbook)
{
log.debug("Writing: " + AC.getName());
Properties = AC.getContactProperties();

SB = new StringBuilder();

SB.append("BEGIN:VCARD");
SB.append(System.lineSeparator());
SB.append("VERSION:2.1");
SB.append(System.lineSeparator());

firstname=NameFilter(AC.getFirstname());
lastname=NameFilter(AC.getFamilyname());

SB.append("N:"+firstname+";"+lastname);
SB.append(System.lineSeparator());

homephone= Properties.get("homephone").getValue();
log.debug("Homephone: " + homephone);

if(homephone.startsWith("+")) //Test only, to exlucde existing + as an errorsource
{
//TODO: If + was source of error, write proper regex replace function
homephone="00"+homephone.substring(1, homephone.length());
}
log.debug("Filtered: " + homephone);

phone = Properties.get("phone").getValue();
log.debug("Phone: " + phone);

if(phone.startsWith("+"))
{
phone="00"+phone.substring(1, phone.length());
}

log.debug("Filtered: " +phone);

mobile= Properties.get("mobile").getValue();
log.debug("Mobile: " + mobile);


if(mobile.startsWith("+"))
{
mobile ="00"+mobile.substring(1, mobile.length());
}

log.debug("Filtered: " + mobile);

if(!homephone.isEmpty())
{
SB.append("TEL;HOME:"+homephone);
SB.append(System.lineSeparator());
}

if(!phone.isEmpty())
{
SB.append("TEL;WORK:"+phone);
SB.append(System.lineSeparator());
}

if(!mobile.isEmpty())
{

SB.append("TEL;CELL:"+mobile);
SB.append(System.lineSeparator());
}

SB.append("END:VCARD");
SB.append(System.lineSeparator());
SB.append(System.lineSeparator());
Fix = SB.toString();
Fix = Fix.replaceAll("[^\\u0000-\\uFFFF]", ""); //ASCII only
FW.write(Fix);
}

log.debug("Done!");

FW.close();
}

String NameFilter(String Entry)
{
String safeChar="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ";
char[] allowed = safeChar.toCharArray();
char[] charArray = Entry.toCharArray();
StringBuilder Result = new StringBuilder();
for (char c : charArray)
{
for (char a : allowed)
{
if(c==a) Result.append(a);
}
}
return Result.toString();
}


How do i debug such an issue?

Sincerly Fabian95qw

Answer

I figured it out, the Export VCard as seen was never conform with the actual vcard Version 2.1

So i tried generating a multivcard using ezvcard. and now the completely vcard Version 2.1 conform multivcard works.

    log.debug("Writing VCards to File: " + ExportFile.getAbsolutePath());
    FileWriter FW = new FileWriter(ExportFile);
    VCardWriter VCW = new VCardWriter(FW, VCardVersion.V2_1);
    VCard VC = null;
    StructuredName SN = null;
    Telephone T = null;


    Map<String, DataEntry> Properties = null;

    String homephone="";
    String phone="";
    String mobile="";
    String firstname="";
    String lastname="";     

    for(AddressbookContact AC : Addressbook)
    {
        log.debug("Writing: " + AC.getName());
        Properties = AC.getContactProperties();

        firstname=NameFilter(AC.getFirstname());
        lastname=NameFilter(AC.getFamilyname());

        if(firstname.isEmpty() && lastname.isEmpty())
        {
            firstname = AC.getCompany();
        }

        VC = new VCard();
        SN = new StructuredName();

        SN.setFamily(lastname);
        SN.setGiven(firstname);

        VC.setStructuredName(SN);

        homephone= Properties.get("homephone").getValue();      
        phone = Properties.get("phone").getValue();
        mobile= Properties.get("mobile").getValue();


        if(!homephone.isEmpty())
        {

            T = new Telephone(homephone);
            T.addType(TelephoneType.HOME);
            VC.addTelephoneNumber(T);
        }

        if(!phone.isEmpty())
        {
            T = new Telephone(phone);
            T.addType(TelephoneType.WORK);
            VC.addTelephoneNumber(T);
        }

        if(!mobile.isEmpty())
        {

            T = new Telephone(mobile);
            T.addType(TelephoneType.CELL);
            VC.addTelephoneNumber(T);
        }

        VCW.write(VC);
    }
VCW.close();

The VCard File Looks Like this:

BEGIN:VCARD
VERSION:2.1
N:Testinator;Testinator;;;
TEL;WORK:101
X-PRODID:ezvcard 0.9.0
END:VCARD
BEGIN:VCARD
VERSION:2.1
N:Administrator;Test;;;
TEL;WORK:100
X-PRODID:ezvcard 0.9.0
END:VCARD

~ Problem Resolved

Sincerly

Fabian95qw

Comments