From fe480d5cb6c41ff03f50931646806dde29f08e08 Mon Sep 17 00:00:00 2001 From: Alistair McIntyre Date: Fri, 28 Apr 2017 15:41:12 +1200 Subject: [PATCH] Finished parsing the Race XML data. Began making some optimizations to hopefully make parsing the Boat Data a quicker and simpler task. #story[820] --- .../seng302/models/parsers/StreamParser.java | 84 +++++++++++++++---- .../models/parsers/StreamReceiver.java | 4 +- 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/src/main/java/seng302/models/parsers/StreamParser.java b/src/main/java/seng302/models/parsers/StreamParser.java index 0c982ec6..dff14a7f 100644 --- a/src/main/java/seng302/models/parsers/StreamParser.java +++ b/src/main/java/seng302/models/parsers/StreamParser.java @@ -159,7 +159,7 @@ public class StreamParser { "CentralAltitude", "UtcOffset", "MagneticVariation", "ShorelineName"}; Map outputMap = parseAtomicElements(docEle, regattaElements); // Regatta contains only atomic elements - System.out.println(outputMap); + //System.out.println(outputMap); //return outputMap; } @@ -176,30 +176,56 @@ public class StreamParser { raceStartMap.put("Postpone", getNodeNamedAttribute(raceStartTime, "Postpone")); outputMap.put("RaceStartTime", raceStartMap); - //participants - Map participantMap = new HashMap<>(); + //Race Participants NodeList participants = docEle.getElementsByTagName("Participants").item(0).getChildNodes(); + outputMap.put("Participants", parseRaceParticipants(participants)); + + //Course (CompoundMarks) + NodeList course = docEle.getElementsByTagName("Course").item(0).getChildNodes(); + outputMap.put("Course", parseCourse(course)); + + //CompoundMark Sequence + NodeList markSequence = docEle.getElementsByTagName("CompoundMarkSequence").item(0).getChildNodes(); + outputMap.put("CourseMarkSequence", parseMarkSequence(markSequence)); + + //Course Limits + NodeList courseLimits = docEle.getElementsByTagName("CourseLimit").item(0).getChildNodes(); + outputMap.put("CourseLimit", parseCourseLimits(courseLimits)); + + System.out.println(outputMap.get("Course")); +// for (Map.Entry entry : outputMap.entrySet()) { +// System.out.println(entry); +// } + } + + private static ArrayList parseRaceParticipants(NodeList participants) { + ArrayList participantList = new ArrayList<>(); for (int i = 0; i < participants.getLength(); i++) { + Map participantMap = new HashMap<>(); Integer sourceID = null; String entry = null; Node participant = participants.item(i); if (participant.getNodeName().equals("Yacht")) { - //sourceID = Integer.parseInt(participant.getAttributes().getNamedItem("SourceID").getTextContent()); sourceID = Integer.parseInt(getNodeNamedAttribute(participant, "SourceID")); if (participant.getAttributes().getLength() == 2) { entry = getNodeNamedAttribute(participant, "Entry"); } - participantMap.put(sourceID, entry); + participantMap.put("sourceID", sourceID); + participantMap.put("Entry", entry); + participantList.add(participantMap); } } - outputMap.put("Participants", participantMap); + return participantList; + } - //Course - Order matters. + private static Map parseCourse(NodeList course) { Map courseMap = new TreeMap<>(); - NodeList course = docEle.getElementsByTagName("Course").item(0).getChildNodes(); + ArrayList courseList = new ArrayList<>(); for (int i = 0; i < course.getLength(); i++) { + Integer compoundMarkID = null; String name = null; + //map for an individual CompoundMark Map compoundMarkMap = new TreeMap<>(); Node compoundMark = course.item(i); if (compoundMark.getNodeName().equals("CompoundMark")) { @@ -208,6 +234,7 @@ public class StreamParser { //get marks for compound mark NodeList marks = compoundMark.getChildNodes(); for (int j = 0; j < marks.getLength(); j++) { + //map for individual mark details within a compound mark Map markMap = new TreeMap<>(); Node mark = marks.item(j); if (mark.getNodeName().equals("Mark")) { @@ -215,18 +242,47 @@ public class StreamParser { markMap.put("TargetLat", Double.parseDouble(getNodeNamedAttribute(mark, "TargetLat"))); markMap.put("TargetLng", Double.parseDouble(getNodeNamedAttribute(mark, "TargetLng"))); markMap.put("SourceID", Integer.parseInt(getNodeNamedAttribute(mark, "SourceID"))); - compoundMarkMap.put(Integer.parseInt(getNodeNamedAttribute(mark, "SeqID")), markMap); - } } - + courseMap.put(compoundMarkID, compoundMarkMap); } - courseMap.put(compoundMarkID, compoundMarkMap); } - //outputMap.put("Course", courseMap); - System.out.println(outputMap); + return courseMap; + } + + private static Map parseMarkSequence(NodeList markSequence) { + Map markSequenceMap = new TreeMap<>(); + + for (int i = 0; i < markSequence.getLength(); i++) { + Map cornerMap = new TreeMap<>(); + Node corner = markSequence.item(i); + if (corner.getNodeName().equals("Corner")) { + cornerMap.put("CompoundMarkID", Integer.parseInt(getNodeNamedAttribute(corner, "CompoundMarkID"))); + cornerMap.put("Rounding", getNodeNamedAttribute(corner, "Rounding")); + cornerMap.put("ZoneSize", getNodeNamedAttribute(corner, "ZoneSize")); + markSequenceMap.put(Integer.parseInt(getNodeNamedAttribute(corner, "SeqID")), cornerMap); + } + } + + return markSequenceMap; + } + + private static Map parseCourseLimits(NodeList courseLimits) { + Map courseLimitMap = new TreeMap<>(); + + for (int i = 0; i < courseLimits.getLength(); i++) { + Map limitMap = new HashMap<>(); + Node limit = courseLimits.item(i); + if (limit.getNodeName().equals("Limit")) { + limitMap.put("Lat", Double.parseDouble(getNodeNamedAttribute(limit,"Lat"))); + limitMap.put("Lon", Double.parseDouble(getNodeNamedAttribute(limit,"Lon"))); + courseLimitMap.put(Integer.parseInt(getNodeNamedAttribute(limit, "SeqID")), limitMap); + } + } + + return courseLimitMap; } private static void parseBoatXML(Document doc) { diff --git a/src/main/java/seng302/models/parsers/StreamReceiver.java b/src/main/java/seng302/models/parsers/StreamReceiver.java index 128683a3..31912815 100644 --- a/src/main/java/seng302/models/parsers/StreamReceiver.java +++ b/src/main/java/seng302/models/parsers/StreamReceiver.java @@ -126,8 +126,8 @@ public class StreamReceiver { return (int) (s1.getTimeStamp() - s2.getTimeStamp()); } }); - StreamReceiver sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941, pq); - //StreamReceiver sr = new StreamReceiver("livedata.americascup.com", 4941, pq); + //StreamReceiver sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941, pq); + StreamReceiver sr = new StreamReceiver("livedata.americascup.com", 4941, pq); sr.connect(); } }