aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suca/SotonCalendarParser.java
blob: 6e9a9ac7243d4ef678229006729b9833012ffe41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package net.cbaines.suca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;

import net.fortuna.ical4j.data.CalendarParser;
import net.fortuna.ical4j.data.ContentHandler;
import net.fortuna.ical4j.data.ParserException;
import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Property;

public class SotonCalendarParser implements CalendarParser {
	private final static String TAG = "SotonCalendarParser";

	/**
	 * Start dates for the Autumn term's for each Academic Year starting from
	 * 2011 to
	 */
	// private static final GregorianCalendar[] startOfAutumnTerm = {new
	// GregorianCalendar(2011, Calendar.SEPTEMBER, 19),new
	// GregorianCalendar(2011, Calendar.SEPTEMBER, 19)

	private Calendar startOfYear = new GregorianCalendar(2011,
			Calendar.SEPTEMBER, 19);

	public void parse(InputStream in, ContentHandler handler)
			throws IOException, ParserException {
		parse(new InputStreamReader(in), handler);
	}

	public void parse(Reader in, ContentHandler handler) throws IOException,
			ParserException {
		parse(new BufferedReader(in, 3), handler);
	}

	public void parse(BufferedReader in, ContentHandler handler)
			throws IOException, ParserException {

		try {
			handler.startCalendar();
			handler.startProperty(Property.NAME);
			handler.propertyValue("2.0");
			handler.endProperty(Property.NAME);
			handler.startProperty(Property.PRODID);
			handler.propertyValue("-//Chris Baines + Henco Appel//SouthamptonUniversityOrganiser//EN");
			handler.endProperty(Property.NAME);
			String line;
			while ((line = in.readLine()) != null) {
				if (!line.trim().equals("")) {
					System.out.println("Line: " + line);
					String[] lecture = line.split("\",\"");
					// Remove the starting " off of the date
					lecture[0] = lecture[0].substring(1,
							lecture[0].length() - 1);
					// Remove the trailing " off of the weeks
					lecture[6] = lecture[6].substring(0,
							lecture[0].length() - 1);
					String[] people = lecture[4].split(",");
					String[] weeks = lecture[6].split("[\\-,]");
					// System.out.println(Arrays.toString(lecture));
					for (int i = 0; i < weeks.length; i += 2) {
						int weeksStart = Integer.parseInt(weeks[i].trim());
						int weeksEnd = weeksStart;
						if (i + 1 < weeks.length) {
							weeksEnd = Integer.parseInt(weeks[i + 1].trim());
						}
						for (int week = weeksStart; week <= weeksEnd; week++) {
							// System.out.println(week +
							// Arrays.toString(lecture));
							createEvent(lecture, handler, week);
						}
					}

				}
			}
			handler.endCalendar();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
	}

	// void String getRecuranceRule(String weeks) {
	// StringBuilder rrule = new StringBuilder(50);
	// rrule.append("RRULE:FREQ=WEEKLY;");
	//
	// }

	private void createEvent(String[] lecture, ContentHandler handler, int week)
			throws ParseException, URISyntaxException, IOException,
			NumberFormatException {
		handler.startComponent(Component.VEVENT);
		handler.startProperty(Property.UID);
		handler.propertyValue("email");
		handler.endProperty(Property.UID);
		handler.startProperty(Property.DTSTAMP);
		handler.propertyValue(new DateTime(java.util.Calendar.getInstance()
				.getTime()).toString());
		handler.endProperty(Property.DTSTAMP);
		// handler.startProperty(Property.ORGANIZER);
		// handler.propertyValue("Organiser");
		// handler.endProperty(Property.ORGANIZER);
		Calendar time = (Calendar) startOfYear.clone();
		int day = 0;
		if (lecture[0].equals("Monday")) {
			day = 0;
		} else if (lecture[0].equals("Tuesday")) {
			day = 1;
		} else if (lecture[0].equals("Wednesday")) {
			day = 2;
		} else if (lecture[0].equals("Thursday")) {
			day = 3;
		} else if (lecture[0].equals("Friday")) {
			day = 4;
		} else {
			throw new ParseException(Arrays.toString(lecture), 0);
		}
		time.add(Calendar.DAY_OF_MONTH, day);
		time.set(Calendar.HOUR_OF_DAY,
				Integer.parseInt(lecture[1].substring(0, 2)));
		time.add(Calendar.WEEK_OF_YEAR, week);
		handler.startProperty(Property.DTSTART);
		handler.propertyValue(new DateTime(time.getTime()).toString());
		handler.endProperty(Property.DTSTART);
		time.set(Calendar.HOUR_OF_DAY,
				Integer.parseInt(lecture[2].substring(0, 2)));
		handler.startProperty(Property.DTEND);
		handler.propertyValue(new DateTime(time.getTime()).toString());
		handler.endProperty(Property.DTEND);
		handler.startProperty(Property.LOCATION);
		handler.propertyValue(lecture[5]);
		handler.endProperty(Property.LOCATION);
		handler.startProperty(Property.SUMMARY);
		handler.propertyValue(lecture[3] + " with " + lecture[4] + " in "
				+ lecture[5]);
		handler.endProperty(Property.SUMMARY);
		handler.endComponent(Component.VEVENT);
	}
}