-
Notifications
You must be signed in to change notification settings - Fork 92
Plain text
vCard data is most commonly encoded in a special plain-text format. The specifications for the latest version of this format (4.0) is defined in RFC 6350. An older version (3.0) is defined in RFC 2426. An even older version (2.1) is defined here. All three versions have similar, but not idential, syntax. And they support a similar, but not identical, set of properties. See the Property List for a listing of all the vCard properties, along with what vCard versions support them.
Most vCard applications support versions 2.1 and 3.0. Widespread adoption of version 4.0 has yet to take place. All three versions are supported by ez-vcard.
The VCardReader
class handles the parsing of plain-text vCard data. The data is read in a streaming fashion, meaning it parses the data as it is read off the wire. This results in a smaller memory footprint than other approaches.
VCardReader
automatically detects the version of the vCard data that's being read, and adjusts its parsing algorithm accordingly.
readNext()
Parses and returns the next VCard
object in the data stream. The method returns null
when the end of stream has been reached.
getWarnings()
VCardReader
does its best to skip over invalid or unparseable data and log it as a warning (as opposed to throwing an exception and aborting the entire parsing process). The getWarnings()
method returns any problems the parser encountered while parsing the ICalendar object that was last returned by readNext()
. Examples of things that could cause warnings are: invalid syntax or unparseable property values (such as malformed date values).
close()
As with all I/O operations, it's important to call the close()
method when you are done with the VCardReader
object in order to properly close the input stream.
Please see the Javadocs for a complete listing of all the methods.
The example below outputs the names and birthdays of each vCard in the data stream.
File file = new File("vcards.vcf");
VCardReader reader = new VCardReader(file);
try {
VCard vcard;
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
while ((vcard = reader.readNext()) != null) {
FormattedName fn = vcard.getFormattedName();
String name = (fn == null) ? null : fn.getValue();
Birthday bday = vcard.getBirthday();
Date date = (bday == null) ? null : bday.getDate();
String birthday = (date == null) ? null : df.format(date);
System.out.println(name + " " + birthday);
}
} finally {
reader.close();
}
If a property's value cannot be parsed (for example, due to a malformed date value), the property is treated as an extended property and a warning is logged. The code below demonstrates this:
String str =
"BEGIN:VCARD\r\n" +
"VERSION:2.1\r\n" +
"BDAY:Jan 5, 1980\r\n" +
"END:VCARD\r\n";
VCardReader reader = new VCardReader(str);
VCard vcard = reader.readNext();
reader.close();
List<String> warnings = reader.getWarnings();
System.out.println("Warnings: " + warnings);
System.out.println("BDAY property: " + vcard.getBirthday());
RawProperty prop = vcard.getExtendedProperty("BDAY");
System.out.println("Extended property: " + prop.getValue());
The above code produces the following output:
Warnings: [Line 3 (BDAY property): Property value could not be parsed. It will be saved as an extended property instead.
Reason: Could not parse date string.
Value: Jan 5, 1980]
BDAY property: null
Extended property: Jan 5, 1980
The VCardWriter
class handles the serialization of plain-text vCard data. Its constructor takes a VCardVersion
object, which specifies the vCard version that the data will be written in.
write()
Writes the contents of a VCard
object to the data stream.
close()
As with all I/O operations, it's important to call the close()
method when you are done with the VCardWriter
object in order to properly close the output stream.
Please see the Javadocs for a complete listing of all the methods.
The example below writes a list of VCard
objects to a file using the latest vCard version.
List<VCard> vcards = ...
File file = new File("vcards.vcf");
VCardWriter writer = new VCardWriter(file, VCardVersion.V4_0);
try {
for (VCard vcard : vcards) {
writer.write(vcard);
}
} finally {
writer.close();
}
ez-vcard is maintained by Michael Angstadt
Table of Contents
Getting started
Examples
FAQ
Javadocs
Downloads
1 An Overview of the vCard data format
2 Reading and Writing vCard data with ez-vcard
2.1 Plain-text (traditional)
2.2 XML-encoded (xCard)
2.3 JSON-encoded (jCard)
2.4 HTML-encoded (hCard)
3 Differences between the vCard versions
4 Dealing with Non-standard Data
4.1 Working with non-standard properties and parameters
4.2 Property scribe
5 Project Information
5.1 News
5.2 Dependencies
5.3 Supported Specifications
5.4 Changelog
6 Reference
6.1 vCard Property Reference
6.2 Javadocs