Back to project page SELP2013.
The source code is released under:
License ======= This work is licensed under the BSD 2-clause license as follows. # BSD 2-clause license Copyright (c) 2013, Sky Welch All rights reserved. Redistribution and use in source and ...
If you think the Android project SELP2013 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package uk.co.skywelch.selp2013; /* ww w. ja v a 2 s .co m*/ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import android.util.Log; import android.util.Xml; public class TimetableXmlParser { public static final String TAG = "TimetableXmlParser"; private static final String ns = null; public ArrayList<Lecture> parse(InputStream in) throws XmlPullParserException, IOException { try { XmlPullParser parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); parser.setInput(in, null); parser.nextTag(); return readFeed(parser); } finally { in.close(); } } private ArrayList<Lecture> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException { ArrayList<Lecture> lectures = new ArrayList<Lecture>(); parser.require(XmlPullParser.START_TAG, ns, "timetable"); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String name = parser.getName(); if (name.equals("semester")) { int semester = -1; String sSemester = parser.getAttributeValue(null, "number"); if (sSemester != null) semester = Integer.parseInt(sSemester); readSemester(parser, lectures, semester); } else { XMLUtilities.skip(parser); } } return lectures; } private ArrayList<Lecture> readSemester(XmlPullParser parser, ArrayList<Lecture> lectures, int semester) throws XmlPullParserException, IOException { parser.require(XmlPullParser.START_TAG, ns, "semester"); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String tag = parser.getName(); if (tag.equals("week")) { readWeek(parser, lectures, semester); } else { XMLUtilities.skip(parser); } } return lectures; } private void readWeek(XmlPullParser parser, ArrayList<Lecture> lectures, int semester) throws IOException, XmlPullParserException { parser.require(XmlPullParser.START_TAG, ns, "week"); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String tag = parser.getName(); if (tag.equals("day")) { readDay(parser, lectures, semester, parser.getAttributeValue(null, "name")); } else { XMLUtilities.skip(parser); } } } private void readDay(XmlPullParser parser, ArrayList<Lecture> lectures, int semester, String day) throws IOException, XmlPullParserException { parser.require(XmlPullParser.START_TAG, ns, "day"); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String tag = parser.getName(); if (tag.equals("time")) { HourMinute start = new HourMinute(parser.getAttributeValue(null, "start")); HourMinute finish = new HourMinute(parser.getAttributeValue(null, "finish")); readTime(parser, lectures, semester, day, start, finish); } else { XMLUtilities.skip(parser); } } } private void readTime(XmlPullParser parser, ArrayList<Lecture> lectures, int semester, String day, HourMinute start, HourMinute finish) throws IOException, XmlPullParserException { parser.require(XmlPullParser.START_TAG, ns, "time"); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String tag = parser.getName(); if (tag.equals("lecture")) { Lecture lecture = readLecture(parser, lectures, semester, day, start, finish); if (lecture == null) { Log.e(TAG, "Failed to parse a lecture"); } else { lectures.add(lecture); } } else { XMLUtilities.skip(parser); } } } private Lecture readLecture(XmlPullParser parser, ArrayList<Lecture> lectures, int semester, String day, HourMinute start, HourMinute finish) throws IOException, XmlPullParserException { parser.require(XmlPullParser.START_TAG, ns, "lecture"); String course = ""; ArrayList<Integer> years = new ArrayList<Integer>(); String room = ""; String building = ""; String comment = ""; while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String tag = parser.getName(); if (tag.equals("course")) { course = XMLUtilities.readString(parser, ns, "course"); } else if (tag.equals("years")) { years.addAll(readYears(parser)); } else if (tag.equals("venue")) { String[] rb = readVenue(parser); room = rb[0]; building = rb[1]; } else if (tag.equals("comment")) { comment = XMLUtilities.readString(parser, ns, "comment"); } else { XMLUtilities.skip(parser); } } return new Lecture(course, start, finish, semester, day, years, room, building, comment); } private ArrayList<Integer> readYears(XmlPullParser parser) throws IOException, XmlPullParserException { parser.require(XmlPullParser.START_TAG, ns, "years"); ArrayList<Integer> years = new ArrayList<Integer>(); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String tag = parser.getName(); if (tag.equals("year")) { Integer year = XMLUtilities.readInteger(parser, ns, "year"); if (year != null) years.add(year); } else { XMLUtilities.skip(parser); } } return years; } private String[] readVenue(XmlPullParser parser) throws IOException, XmlPullParserException { parser.require(XmlPullParser.START_TAG, ns, "venue"); String room = ""; String building = ""; while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String tag = parser.getName(); if (tag.equals("room")) { room = XMLUtilities.readString(parser, ns, "room"); } else if (tag.equals("building")) { building = XMLUtilities.readString(parser, ns, "building"); } else { XMLUtilities.skip(parser); } } return new String[] { room, building }; } }