mangstadt / ez-vcard

A vCard parser library for Java
Other
399 stars 92 forks source link

[Enhancement] Don't write unnecessary ";" at end of structured properties #57

Closed rfc2822 closed 8 years ago

rfc2822 commented 8 years ago

When playing around with iCloud, I have found out that they use the whole line of the structured name (N) as the display name for groups. So, this group:

VERSION:3.0
X-ADDRESSBOOKSERVER-KIND:group
FN:My Group
N:My Group,,,,

will be shown as "My Group,,,,". While this is clearly an iCloud bug, I wonder whether it might be possible to just generate the actually used components, for example N:My Group instead of N:My Group,,,, when only the family name is set. See also the first example in RFC 2426 3.1.2 where the Honorific Suffixes are left out without a trailing ;:

N:Public;John;Quinlan;Mr.;Esq.

instead of

N:Public;John;Quinlan;Mr.;Esq.;

This should be applicable to other structured fields too and could reduce the complexity (and size) of generated VCards.

mangstadt commented 8 years ago

Is there a typo in your first example? Should those commas be semi-colons?

Commas and semi-colons are treated differently by the N property. Commas are used as delimiters when a component has more than one item in it.

For example, the following property has two prefixes:

N:Public;John;Quinlan;Mr.,Dr.;Esq.;

The first example you gave technically translates as a list of family names. According to the specification, it is not possible to have more than one family name, so iCloud may be treating the comma characters as normal characters, which is why iCloud is using "My Group,,,," as the group name.

What happens when you pass N:GroupName;;;; into iCloud? Does iCloud include the trailing semicolons in the group name?

rfc2822 commented 8 years ago

What happens when you pass N:GroupName;;;; into iCloud? Does iCloud include the trailing semicolons in the group name?

You're right, this is a typo. I have passed a StructuredName which just family name set into iCloud and the result was a group called Name,,,, or Name;;;;. I don't remember anymore, but I guess it was "Name;;;;" (because this was the full N in the uploaded VCard). When doing vCard.addExtendedProperty("N", fn); instead, it works.

mangstadt commented 8 years ago

Fixed in e2a4dd3318e7bf846f83e925029c2a2512be3803.

Call VCardWriter.setTargetApplication() like so:

VCard vcard = new VCard();
StructuredName n = new StructuredName();
n.setFamily("GroupName");
vcard.setStructuredName(n);

VCardWriter writer = new VCardWriter(System.out, VCardVersion.V3_0);
writer.setTargetApplication(TargetApplication.ICLOUD);
writer.write(vcard);
writer.close();

Output:

BEGIN:VCARD
VERSION:3.0
PRODID:ez-vcard 0.9.11-SNAPSHOT
N:GroupName
END:VCARD

Thank you for reporting this.

rfc2822 commented 8 years ago

Thanks, but one question: As far as I understand it, the trailing ;s are unnecessary in any case, so wouldn't it best to always remove them (and not only for a specific target application)?

mangstadt commented 8 years ago

The example you gave in your first post isn't quite accurate.

N:Public;John;Quinlan;Mr.;Esq.

The reason why there is no trailing semicolon in this example is that there are no more components after the "Esq." component. "Esq." is the "suffix" component, which is the last one.

The formal grammar in the vCard 3.0 spec does state that trailing semicolons are optional:

 ;For name="N"
 ;This type MUST be included in a vCard object.
 param = text-param
 ; Text parameters allowed
 value = n-value
 n-value = 0*4(text-value *("," text-value) ";")
 text-value *("," text-value)
 ; Family; Given; Middle; Prefix; Suffix.
 ; Example: Public;John;Quincy,Adams;Reverend Dr. III

And the vCard 2.1 spec gives an example without the trailing semi-colons:

N:Veni, Vidi, Vici;The Restaurant.

However, vCard 4.0 requires all four semicolons to be there:

N-value = list-component 4(";" list-component)

From a performance perspective, the better approach would be to trim the semicolons, since it reduces the size of the vCard file.

Trailing semicolons are now trimmed by default for 2.1 and 3.0 vCards. I removed TargetApplication.ICLOUD and added a method to VCardReader that lets you adjust this setting if the consumer you're targeting requires something specific.

See commits 04cd7672c54696f868ba60cc703c63887bc37210 and 882ee75bc575b68d2ce50fea49e8c668793efe18.

rfc2822 commented 8 years ago

From a performance perspective, the better approach would be to trim the semicolons, since it reduces the size of the vCard file.

Yes, this was my first thought.

Thank you very much for digging into this and providing this enhancement!

mangstadt commented 8 years ago

No problem. Thanks for bringing the issue to my attention!