Cap4j
Introduction
Cap4j is based on iCal4j library. All you can do with iCal4j library can be done with Cap4j.
"iCal4j may be used for modifying existing iCalendar data or creating new iCalendar data from scratch. Here you will find a few examples indicating the recommended usage of this library" see iCal4j introduction.
Cap4j extends iCal4j library and add the specification of Calendar Access Protocol (CAP) (which is still in draft state)
Parseing a Cap iCalendar file
All parsing model is provided by iCal4j. Cap4j extends Components, Properties, Parameters builders and override them with Cap draft speciifcations.
Calendar calendar = CapUtils.buildCalendar(file, isValidate);
or
FileInputStream fin = new FileInputStream("MyCapCalendar.ics");
CapCalendarBuilder builder = new CapCalendarBuilder();
Calendar calendar = builder.build(fin);
Iterating over a Calendar
As iCal4j API, Cap4j is designed to conform with the standard Java collections API. As such, you will find that for searching and manipulating a calendar object model you can make use of familiar concepts such as Lists, Iterators, etc.
for (Iterator i = calendar.getComponents().iterator(); i.hasNext();) {
Component component = (Component) i.next();
System.out.println("Component [" + component.getName() + "]");
for (Iterator j = component.getProperties().iterator; j.hasNext();) {
Property property = (Property) j.next();
System.out.println("Property [" + property.getName() + ", " + property.getValue() + "]");
}
}
Creating a new calendar
As iCal4j API, creating a new Cap Calendar must contains a list of Properties and Components. You can verify that a calendar is valid via the method Calendar.validate().
Calendar capCalendar = new Calendar();
capCalendar .getProperties().add(new ProdId("-//OpenCap01//EN"));
capCalendar .getProperties().add(Version.VERSION_2_0);
capCalendar .getProperties().add(CalScale.GREGORIAN);
capCalendar .getProperties().add(new Cmd(CapCommand.CREATE, new Options("creation01")));
TARGET:cal.example.com
// Add agendas, events, etc..
Output:
BEGIN:VCALENDAR PRODID:-//OpenCap01//EN VERSION:2.0 CALSCALE:GREGORIAN CMD;OPTIONS=creation01:CREATE END:VCALENDAR
Creating an vagenda
To create a VAgenda you must set an owner.
VAgenda agenda = new VAgenda(new Owner("bill"));
agenda.getProperties().add(new Calid("relcalz1"));
agenda.getProperties().add(new Name("Bill's Soccer Team",new Language("en_US")));
agenda.getProperties().add(new Calmaster("mailto:bill@example.com"));
agenda.getProperties().add(new TzId("US/Pacific"));
Output:
BEGIN:VAGENDA OWNER:bill CALID:relcalz1 NAME;LANGUAGE=en_US:Bill's Soccer Team CALMASTER:mailto:bill@example.com TZID:US/Pacific END:VAGENDA
Saving a Cap iCalendar file
Cap4j saving mecanism is the same as iCal4j. When saving a Cap iCalendar file, Cap4j will automatically validate your calendar object model to ensure it complies with the iCalendar RFC specification (done in iCal4j) and with Cap draft specification (done in Cap4j). If you would prefer not to validate your calendar data you can disable the validation by calling CalendarOutputter.setValidating(false).
FileOutputStream fout = new FileOutputStream("MyCapCalendar.ics");
CalendarOutputter outputter = new CalendarOutputter();
outputter.output(calendar, fout);
