diff options
Diffstat (limited to 'src/net/cbaines/suca/SotonCalendarParser.java')
-rw-r--r-- | src/net/cbaines/suca/SotonCalendarParser.java | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/net/cbaines/suca/SotonCalendarParser.java b/src/net/cbaines/suca/SotonCalendarParser.java new file mode 100644 index 0000000..6e9a9ac --- /dev/null +++ b/src/net/cbaines/suca/SotonCalendarParser.java @@ -0,0 +1,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); + } +} |