
I have been researching the vCard file format for the purpose of adding it as an output format for my email collection app. As I mentioned previously, the file format is documented in RFC 6350. There is even a java library for reading and writing vCard files that was extracted from the Android source.
I was trying to think of a graphical way to represent RFC 6350. But how do you describe a wall of text? I came up with the screen shot on the left. If you want to see the gory details of a text based file format go ahead and take a look at RFC 6350 for yourself.
The Email Sign Up app only collects a person’s name and email address. Surely it doesn’t need to implement every possible field in the format. This leads to the question of what is the minimum number of fields for a valid vCard file? My first attempt to answer this question was to google it. The web pages I reviewed did not answer the question directly.
Of course I could integrate the existing java library for reading and writing vCards. However, this would have the drawback of creating an external dependency for my app. It would also increase the size of the app and use only a tiny fraction of the features provided by the library.
Towards A Minimal Implementation
The magic decoder ring for answering the question of what are the minimum number of fields in a vCard file comes from the RFC. See this excerpt:
1 2 3 4 5 6 7 8 |
+-------------+--------------------------------------------------+ | Cardinality | Meaning | +-------------+--------------------------------------------------+ | 1 | Exactly one instance per vCard MUST be present. | | *1 | Exactly one instance per vCard MAY be present. | | 1* | One or more instances per vCard MUST be present. | | * | One or more instances per vCard MAY be present. | +-------------+--------------------------------------------------+ |
This is saying that all required fields have a Cardinality that begins with 1 and may have an asterisk after. Manually searching through 4147 lines of plain text to find out which fields match the pattern would be tedious and error prone.
Some have said that unix is the best text processing machine ever built. If you doubt the claim see this post for inspiration about what can be accomplished with six unix commands. Now I am no unix wizard but I do have some tricks up my sleeve.
How Many Fields Are Required In A vCard?
Here is how you can use grep in combination with wc to get the answer (four):
1 2 |
$ grep "Cardinality: 1" rfc6350.txt | wc -l 4 |
Now all that remains is to find what are the four fields that are required. Searching for the answer within a file is easy, once you know what to look for. Here is a minimal vCard that shows the four required fields.
1 2 3 4 |
BEGIN:VCARD VERSION:3.0 FN:John Doe END:VCARD |
Conclusion
Analyzing the vCard format helped avoid integrating an entire library just to output a few lines of text.