Thursday, January 24, 2013

Working with iCal4j

Recently, I started working on ical4j. So, thought will post some of the my initial learnings. I will keep updating this as I learn more.

I created a project with maven. Here is how my pom.xml looks


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sunilpk</groupId>
    <artifactId>Ical4jProto</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Ical4jProto</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.mnode.ical4j</groupId>
            <artifactId>ical4j</artifactId>
            <version>1.0.4</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
  
</project>


Once that is done, now its times to learn some basics. Like creating a Calendar.


package com.sunilpk.ical4jproto;

import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.model.property.CalScale;

/**
 *
 */
public class CreateCalendar {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Calendar calendar = new Calendar();
        calendar.getProperties().add(new ProdId("-//CALENDAR//iCal4j 1.4.0//EN"));
        calendar.getProperties().add(Version.VERSION_2_0);
        calendar.getProperties().add(CalScale.GREGORIAN);
        System.out.println("The Calendar is: " + calendar);
    }
}

The output of the program:



The Calendar is: BEGIN:VCALENDAR
PRODID:-//CALENDAR//iCal4j 1.4.0//EN
VERSION:2.0
CALSCALE:GREGORIAN
END:VCALENDAR



Similarly, an .ics file can be read


package com.sunilpk.ical4jproto;

import java.io.FileInputStream;
import java.io.IOException;
import net.fortuna.ical4j.data.CalendarBuilder;
import net.fortuna.ical4j.data.ParserException;
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.util.CompatibilityHints;

/**
 *
 */
public class ReadCalendar {

    public static void main(String[] args) {
        try {
            FileInputStream fin = new FileInputStream("src/main/java/com/sunilpk/ical4jproto/invite.ics");

            CalendarBuilder builder = new CalendarBuilder();

            Calendar calendar = builder.build(fin);
            System.out.println("Product Id: " + calendar.getProductId());
            System.out.println("Method Id: " + calendar.getMethod());
        } catch (IOException e) {
            System.out.println("IO Exception.. " + e);
        } catch (ParserException pe) {
            System.out.println("Parse Exception. " + pe);
        }
    }
}



The output of the above program could be similar to following.


Product Id: PRODID: Calendar-Provider

Method Id: METHOD:CANCEL