Permalink
Browse files

Added XMLTV parser and modified Duke, Abstract Parser for generic Eve…

…nt parameters
  • Loading branch information...
1 parent 8a0ea65 commit dde4346e34b25d588c8cfb9f872f4d986326f436 @jameshong jameshong committed Feb 27, 2012
Showing with 170 additions and 53 deletions.
  1. +7 −3 src/parsing/AbstractXMLParser.java
  2. +22 −50 src/parsing/DukeXMLParser.java
  3. +141 −0 src/parsing/TVXMLParser.java
@@ -25,7 +25,6 @@ public final void loadFile(String filename){
}*/
public List<Event> processEvents(){
-
List<Element> xmlEventsList = parseGetEventsList();
List<Event> parsedEventsList = new ArrayList<Event>();
for(Element event : xmlEventsList){
@@ -34,8 +33,11 @@ public final void loadFile(String filename){
String eventLocation = parseLocation(event);
DateTime startTime = parseStartTime(event);
DateTime endTime = parseEndTime(event);
-
- Event newEvent = new Event(eventTitle, startTime, endTime, eventDescription, eventLocation, false);
+ boolean allDay = isAllDay(event);
+ HashMap<String,ArrayList<String>> properties = getExtraProperties(event);
+
+ Event newEvent = new Event(eventTitle, startTime, endTime,
+ eventDescription, eventLocation, allDay, properties);
parsedEventsList.add(newEvent);
}
@@ -51,6 +53,8 @@ public final void loadFile(String filename){
protected abstract DateTime parseStartTime(Element event);
protected abstract DateTime parseEndTime(Element event);
+ protected abstract boolean isAllDay(Element event);
+ protected abstract HashMap<String,ArrayList<String>> getExtraProperties(Element event);
//implement this is subclasses
//public abstract boolean isThisType(String URL);
@@ -1,9 +1,11 @@
package parsing;
-import model.PhysicalEvent;
-import model.AbstractEvent;
import java.util.*;
+
+import model.Event;
+
import org.jdom.*;
import org.joda.time.*;
+import org.joda.time.format.*;
public class DukeXMLParser extends AbstractXMLParser{
@@ -51,42 +53,8 @@ protected String parseLocation(Element event) {
Element eventLocation = event.getChild(LOCATION);
return eventLocation.getChildText("address");
}
-
- /**
- * Gets the year of the time sub-node of an event
- */
- protected int parseYear(Element time){
- return Integer.parseInt(time.getChildText("year"));
- }
-
- /**
- * Gets the month of the time sub-node of an event
- */
- protected int parseMonth(Element time){
- return Integer.parseInt(time.getChildText("month"));
- }
/**
- * Gets the day of the time sub-node of an event
- */
- protected int parseDay(Element time){
- return Integer.parseInt(time.getChildText("day"));
- }
-
- /**
- * Gets the hour in 24h format of the time sub-node of an event
- */
- protected int parseHour24(Element time){
- return Integer.parseInt(time.getChildText("hour24"));
- }
- /**
- * Gets the minute of the time sub-node of an event
- */
- protected int parseMinute(Element time){
- return Integer.parseInt(time.getChildText("minute"));
- }
-
- /**
* Gets the start time node and calls parseTime()
* to parse it
*/
@@ -104,27 +72,31 @@ protected DateTime parseEndTime(Element event) {
return parseTime(event.getChild(END_TIME));
}
- /**
- * Gets the time zone of the time sub-node of an event
- */
- protected DateTimeZone parseTimeZone(Element time){
- Element timeZone = time.getChild("timezone");
- DateTimeZone parsedTimeZone;
- if(timeZone.getChildText("islocal").equals("true"))
- parsedTimeZone = DateTimeZone.getDefault();
- else
- parsedTimeZone = DateTimeZone.forID(timeZone.getChildText("id"));
- return parsedTimeZone;
+ protected DateTime parseTime(Element time){
+ DateTimeFormatter dtparser = DateTimeFormat.forPattern("yyyyMMdd'T'HHmmss'Z'");
+ String utcdate = time.getChildText("utcdate");
+ return dtparser.parseDateTime(utcdate);
+ }
+
+ protected boolean isAllDay(Element event) {
+ String allDayField = event.getChild(START_TIME).getChildText("allday");
+ return Boolean.parseBoolean(allDayField);
+ }
+
+ @Override
+ protected HashMap<String, ArrayList<String>> getExtraProperties(
+ Element event) {
+ // TODO Auto-generated method stub
+ return null;
}
public static void main (String[] args){
DukeXMLParser parser = new DukeXMLParser();
parser.loadFile("http://www.cs.duke.edu/courses/spring12/cps108/assign/02_tivoo/data/dukecal.xml");
- List<AbstractEvent> listOfEvents = parser.processEvents();
+ List<Event> listOfEvents = parser.processEvents();
- for(AbstractEvent event : listOfEvents){
+ for(Event event : listOfEvents){
System.out.println(event.toString());
}
}
-
}
@@ -0,0 +1,141 @@
+package parsing;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import model.Event;
+
+import org.jdom.Element;
+import org.joda.time.DateTime;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+import org.joda.time.format.ISODateTimeFormat;
+
+public class TVXMLParser extends AbstractXMLParser {
+
+ /**
+ * Labels for specific nodes in the event tree
+ */
+ private String EVENT_NODE = "programme";
+ private String NAME = "title";
+ private String DESCRIPTION ="desc";
+ private String START_TIME = "start";
+ private String END_TIME = "stop";
+ private String LOCATION = "channel";
+ private String CREDITS = "credits";
+ private String RATING = "rating";
+
+ /**
+ * Gets the root node that contains all event nodes
+ */
+ @Override
+ protected List<Element> parseGetEventsList(){
+ Element eventsRoot = doc.getRootElement();
+ System.err.println("HAHA");//+eventsRoot.getName());
+ return eventsRoot.getChildren(EVENT_NODE);
+ }
+
+ /**
+ * Gets the title of the event, given an event node
+ */
+ @Override
+ protected String parseTitle(Element event) {
+ return event.getChildText(NAME);
+ }
+
+ /**
+ * Gets the description of the event, given an event node
+ */
+ @Override
+ protected String parseDescription(Element event) {
+ return event.getChildText(DESCRIPTION);
+ }
+
+ /**
+ * Gets the location (channel) of the event, given an event node
+ */
+ @Override
+ protected String parseLocation(Element event) {
+ return event.getAttributeValue(LOCATION);
+ }
+
+ /**
+ * Gets the start time node and calls parseTime()
+ * to parse it
+ */
+ @Override
+ protected DateTime parseStartTime(Element event) {
+ return parseTime(event, START_TIME);
+ }
+
+ /**
+ * Gets the end time node and calls parseTime()
+ * to parse it
+ */
+ @Override
+ protected DateTime parseEndTime(Element event) {
+ return parseTime(event, END_TIME);
+ }
+
+ protected DateTime parseTime(Element event, String attrib){
+ DateTimeFormatter dtparser = DateTimeFormat.forPattern("yyyyMMddHHmmss Z");
+ String utcdate = event.getAttributeValue(attrib);
+ return dtparser.parseDateTime(utcdate);
+ }
+
+ protected HashMap<String,ArrayList<String>> getExtraProperties(Element event){
+ HashMap<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>();
+
+ addCreditsToMap(event,map);
+ addRatingToMap(event,map);
+
+ return map;
+ }
+
+ protected void addCreditsToMap(Element event, HashMap<String,ArrayList<String>> map){
+ List<Element> creditsList;
+ try{
+ creditsList = event.getChild(CREDITS).getChildren();
+ } catch (NullPointerException e){
+ return;
+ }
+
+ for(Element credit : creditsList){
+ String creditName = credit.getName();
+ String creditValue = credit.getText();
+ if(!map.containsKey(creditName))
+ map.put(creditName, new ArrayList<String>());
+ map.get(creditName).add(creditValue);
+ }
+ }
+
+ protected void addRatingToMap(Element event, HashMap<String,ArrayList<String>> map){
+ if(!map.containsKey(RATING))
+ map.put(RATING, new ArrayList<String>() );
+ String ratingValue;
+ try{
+ ratingValue = event.getChild(RATING).getChildText("value");
+ map.get(RATING).add(ratingValue);
+ } catch (NullPointerException e){
+ return;
+ }
+ }
+
+ @Override
+ protected boolean isAllDay(Element event) {
+ // XMLTV does not support all day events
+ return false;
+ }
+
+ public static void main (String[] args){
+ TVXMLParser parser = new TVXMLParser();
+ parser.loadFile("/Users/jameshong/Downloads/tv 2.xml");
+ System.err.println("done loading!");
+ List<Event> listOfEvents = parser.processEvents();
+
+ for(Event event : listOfEvents){
+ System.out.println(event.toString());
+ }
+ }
+}

0 comments on commit dde4346

Please sign in to comment.