Adjusted XMLParser to use model Mark objects rather than the simple datatype that existed in the XMLParser previously. Began attempting to implement them into the canvas controller but have issues

#[issue10]
This commit is contained in:
alistairjmcintyre
2017-05-17 19:46:05 +12:00
parent 23bc643c91
commit 3fd13ddc0a
3 changed files with 251 additions and 160 deletions
@@ -125,7 +125,7 @@ public class CanvasController {
// TODO: 1/05/17 cir27 - Make the RaceObjects update on the actual delay. // TODO: 1/05/17 cir27 - Make the RaceObjects update on the actual delay.
elapsedNanos = 1000 / 60; elapsedNanos = 1000 / 60;
updateRaceObjects(); //updateRaceObjects();
if (StreamParser.isRaceFinished()) { if (StreamParser.isRaceFinished()) {
this.stop(); this.stop();
} }
@@ -423,20 +423,26 @@ public class CanvasController {
* are scaled according to the distanceScaleFactor variable. * are scaled according to the distanceScaleFactor variable.
*/ */
private void givePointsXY() { private void givePointsXY() {
List<XMLParser.RaceXMLObject.CompoundMark> allPoints = StreamParser.getXmlObject().getRaceXML().getCompoundMarks(); Map<Integer, Mark> allPoints = StreamParser.getXmlObject().getRaceXML().getCompoundMarks();
List<XMLParser.RaceXMLObject.CompoundMark> processed = new ArrayList<>(); List<Mark> processed = new ArrayList<>();
MarkGroup markGroup; MarkGroup markGroup;
for (XMLParser.RaceXMLObject.CompoundMark mark : allPoints) { for (Map.Entry<Integer, Mark> cMark : allPoints) {
Integer cMarkId = cMark.getKey();
Mark mark = cMark.getValue();
if (!processed.contains(mark)) { if (!processed.contains(mark)) {
if (mark.getMarkType() != MarkType.SINGLE_MARK) { if (mark.getMarkType() != MarkType.SINGLE_MARK) {
markGroup = new MarkGroup(mark, findScaledXY(mark.getMarks().get(0)), findScaledXY(mark.getMarks().get(1))); GateMark gMark = (GateMark) mark;
markGroup = new MarkGroup(mark, findScaledXY(gMark.getSingleMark1()), findScaledXY(gMark.getSingleMark2())); //should be 2 objects in the list.
markGroups.add(markGroup); markGroups.add(markGroup);
} else { } else {
markGroup = new MarkGroup(mark, findScaledXY(mark.getMarks().get(0))); SingleMark sMark = (SingleMark) mark;
markGroup = new MarkGroup(mark, findScaledXY(sMark));
markGroups.add(markGroup); markGroups.add(markGroup);
} }
processed.add(mark); processed.add((mark));
} }
} }
group.getChildren().addAll(boatGroups); group.getChildren().addAll(boatGroups);
@@ -132,5 +132,4 @@ public abstract class Mark {
public void setId(int id) { public void setId(int id) {
this.id = id; this.id = id;
} }
} }
+219 -133
View File
@@ -1,15 +1,19 @@
package seng302.models.stream; package seng302.models.stream;
import java.util.List;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import seng302.models.Yacht; import seng302.models.Yacht;
import seng302.models.mark.GateMark;
import seng302.models.mark.Mark;
import seng302.models.mark.MarkType; import seng302.models.mark.MarkType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import seng302.models.mark.SingleMark;
/** /**
* Class to create an XML object from the XML Packet Messages. * Class to create an XML object from the XML Packet Messages.
@@ -21,7 +25,6 @@ import java.util.Map;
* *
* xmlP = new XMLParser(doc, xmlMessageType); * xmlP = new XMLParser(doc, xmlMessageType);
* RegattaXMLObject rXmlObj = xmlP.createRegattaXML(); // creates a regattaXML object. * RegattaXMLObject rXmlObj = xmlP.createRegattaXML(); // creates a regattaXML object.
*
*/ */
public class XMLParser { public class XMLParser {
@@ -31,10 +34,12 @@ public class XMLParser {
private RegattaXMLObject regattaXML; private RegattaXMLObject regattaXML;
private BoatXMLObject boatXML; private BoatXMLObject boatXML;
public XMLParser() {} public XMLParser() {
}
/** /**
* Constructor for XMLParser * Constructor for XMLParser
*
* @param doc Document to create XML object. * @param doc Document to create XML object.
* @param messageType Defines if a message is a RegattaXML(5), RaceXML(6), BoatXML(7). * @param messageType Defines if a message is a RegattaXML(5), RaceXML(6), BoatXML(7).
*/ */
@@ -53,13 +58,22 @@ public class XMLParser {
} }
} }
public RaceXMLObject getRaceXML() { return raceXML; } public RaceXMLObject getRaceXML() {
public RegattaXMLObject getRegattaXML() { return regattaXML; } return raceXML;
public BoatXMLObject getBoatXML() { return boatXML; } }
public RegattaXMLObject getRegattaXML() {
return regattaXML;
}
public BoatXMLObject getBoatXML() {
return boatXML;
}
/** /**
* Returns the text content of a given child element tag, assuming it exists, as an Integer. * Returns the text content of a given child element tag, assuming it exists, as an Integer.
*
* @param ele Document Element with child elements. * @param ele Document Element with child elements.
* @param tag Tag to find in document elements child elements. * @param tag Tag to find in document elements child elements.
* @return Text content from tag if found, null otherwise. * @return Text content from tag if found, null otherwise.
@@ -75,6 +89,7 @@ public class XMLParser {
/** /**
* Returns the text content of a given child element tag, assuming it exists, as an String. * Returns the text content of a given child element tag, assuming it exists, as an String.
*
* @param ele Document Element with child elements. * @param ele Document Element with child elements.
* @param tag Tag to find in document elements child elements. * @param tag Tag to find in document elements child elements.
* @return Text content from tag if found, null otherwise. * @return Text content from tag if found, null otherwise.
@@ -90,6 +105,7 @@ public class XMLParser {
/** /**
* Returns the text content of a given child element tag, assuming it exists, as a Double. * Returns the text content of a given child element tag, assuming it exists, as a Double.
*
* @param ele Document Element with child elements. * @param ele Document Element with child elements.
* @param tag Tag to find in document elements child elements. * @param tag Tag to find in document elements child elements.
* @return Text content from tag if found, null otherwise. * @return Text content from tag if found, null otherwise.
@@ -105,9 +121,11 @@ public class XMLParser {
/** /**
* Returns the text content of an attribute of a given Node, assuming it exists, as a String. * Returns the text content of an attribute of a given Node, assuming it exists, as a String.
*
* @param n A node object that should have some attributes * @param n A node object that should have some attributes
* @param attr The attribute you want to get from the given node. * @param attr The attribute you want to get from the given node.
* @return The String representation of the text content of an attribute in the given node, else returns null. * @return The String representation of the text content of an attribute in the given node, else
* returns null.
*/ */
private static String getNodeAttributeString(Node n, String attr) { private static String getNodeAttributeString(Node n, String attr) {
Node attrItem = n.getAttributes().getNamedItem(attr); Node attrItem = n.getAttributes().getNamedItem(attr);
@@ -120,9 +138,11 @@ public class XMLParser {
/** /**
* Returns the text content of an attribute of a given Node, assuming it exists, as an Integer. * Returns the text content of an attribute of a given Node, assuming it exists, as an Integer.
*
* @param n A node object that should have some attributes * @param n A node object that should have some attributes
* @param attr The attribute you want to get from the given node. * @param attr The attribute you want to get from the given node.
* @return The Integer representation of the text content of an attribute in the given node, else returns null. * @return The Integer representation of the text content of an attribute in the given node,
* else returns null.
*/ */
private static Integer getNodeAttributeInt(Node n, String attr) { private static Integer getNodeAttributeInt(Node n, String attr) {
Node attrItem = n.getAttributes().getNamedItem(attr); Node attrItem = n.getAttributes().getNamedItem(attr);
@@ -135,9 +155,11 @@ public class XMLParser {
/** /**
* Returns the text content of an attribute of a given Node, assuming it exists, as a Double. * Returns the text content of an attribute of a given Node, assuming it exists, as a Double.
*
* @param n A node object that should have some attributes * @param n A node object that should have some attributes
* @param attr The attribute you want to get from the given node. * @param attr The attribute you want to get from the given node.
* @return The Double representation of the text content of an attribute in the given node, else returns null. * @return The Double representation of the text content of an attribute in the given node, else
* returns null.
*/ */
private static Double getNodeAttributeDouble(Node n, String attr) { private static Double getNodeAttributeDouble(Node n, String attr) {
Node attrItem = n.getAttributes().getNamedItem(attr); Node attrItem = n.getAttributes().getNamedItem(attr);
@@ -149,6 +171,7 @@ public class XMLParser {
} }
public class RegattaXMLObject { public class RegattaXMLObject {
//Regatta Info //Regatta Info
private Integer regattaID; private Integer regattaID;
private String regattaName; private String regattaName;
@@ -160,6 +183,7 @@ public class XMLParser {
/** /**
* Constructor for a RegattaXMLObject. * Constructor for a RegattaXMLObject.
* Takes the information from a Document object and creates a more usable format. * Takes the information from a Document object and creates a more usable format.
*
* @param doc XML Document Object * @param doc XML Document Object
*/ */
RegattaXMLObject(Document doc) { RegattaXMLObject(Document doc) {
@@ -173,12 +197,29 @@ public class XMLParser {
this.utcOffset = getElementInt(docEle, "UtcOffset"); this.utcOffset = getElementInt(docEle, "UtcOffset");
} }
public Integer getRegattaID() { return regattaID; } public Integer getRegattaID() {
public String getRegattaName() { return regattaName; } return regattaID;
public String getCourseName() { return courseName; } }
public Double getCentralLat() { return centralLat; }
public Double getCentralLng() { return centralLng; } public String getRegattaName() {
public Integer getUtcOffset() { return utcOffset; } return regattaName;
}
public String getCourseName() {
return courseName;
}
public Double getCentralLat() {
return centralLat;
}
public Double getCentralLng() {
return centralLng;
}
public Integer getUtcOffset() {
return utcOffset;
}
} }
@@ -195,13 +236,14 @@ public class XMLParser {
//Non atomic race attributes //Non atomic race attributes
private ArrayList<Participant> participants; private ArrayList<Participant> participants;
private ArrayList<CompoundMark> course; private Map<Integer, Mark> course;
private ArrayList<Corner> compoundMarkSequence; private ArrayList<Corner> compoundMarkSequence;
private ArrayList<Limit> courseLimit; private ArrayList<Limit> courseLimit;
/** /**
* Constructor for a RaceXMLObject. * Constructor for a RaceXMLObject.
* Takes the information from a Document object and creates a more usable format. * Takes the information from a Document object and creates a more usable format.
*
* @param doc XML Document Object * @param doc XML Document Object
*/ */
RaceXMLObject(Document doc) { RaceXMLObject(Document doc) {
@@ -214,7 +256,8 @@ public class XMLParser {
Node raceStart = docEle.getElementsByTagName("RaceStartTime").item(0); Node raceStart = docEle.getElementsByTagName("RaceStartTime").item(0);
this.raceStartTime = getNodeAttributeString(raceStart, "Start"); this.raceStartTime = getNodeAttributeString(raceStart, "Start");
this.postponeStatus = Boolean.parseBoolean(getNodeAttributeString(raceStart, "Postpone")); this.postponeStatus = Boolean
.parseBoolean(getNodeAttributeString(raceStart, "Postpone"));
//Participants //Participants
participants = new ArrayList<>(); participants = new ArrayList<>();
@@ -238,21 +281,13 @@ public class XMLParser {
} }
//Course //Course
course = new ArrayList<>(); course = createCompoundMarks(docEle);
NodeList cMarkList = docEle.getElementsByTagName("Course").item(0).getChildNodes();
for (int i = 0; i < cMarkList.getLength(); i++) {
Node cMarkNode = cMarkList.item(i);
if (cMarkNode.getNodeName().equals("CompoundMark")) {
CompoundMark cMark = new CompoundMark(cMarkNode);
course.add(cMark);
}
}
//Course Mark Sequence //Course Mark Sequence
compoundMarkSequence = new ArrayList<>(); compoundMarkSequence = new ArrayList<>();
NodeList cornerList = docEle.getElementsByTagName("CompoundMarkSequence").item(0).getChildNodes(); NodeList cornerList = docEle.getElementsByTagName("CompoundMarkSequence").item(0)
.getChildNodes();
for (int i = 0; i < cornerList.getLength(); i++) { for (int i = 0; i < cornerList.getLength(); i++) {
Node cornerNode = cornerList.item(i); Node cornerNode = cornerList.item(i);
if (cornerNode.getNodeName().equals("Corner")) { if (cornerNode.getNodeName().equals("Corner")) {
@@ -274,18 +309,97 @@ public class XMLParser {
} }
} }
public Integer getRaceID() { return raceID; }
public String getRaceType() { return raceType; }
public String getCreationTimeDate() { return creationTimeDate; }
public String getRaceStartTime() { return raceStartTime; }
public Boolean getPostponeStatus() { return postponeStatus; }
public ArrayList<Participant> getParticipants() { return participants; } private Map<Integer, Mark> createCompoundMarks(Element docEle) {
public ArrayList<CompoundMark> getCompoundMarks() { return course; } Map<Integer, Mark> cMarks = new HashMap<>();
public ArrayList<Corner> getCompoundMarkSequence() { return compoundMarkSequence; }
public ArrayList<Limit> getCourseLimit() { return courseLimit; } NodeList cMarkList = docEle.getElementsByTagName("Course").item(0).getChildNodes();
for (int i = 0; i < cMarkList.getLength(); i++) {
Node cMarkNode = cMarkList.item(i);
if (cMarkNode.getNodeName().equals("CompoundMark")) {
Integer markID = getNodeAttributeInt(cMarkNode, "CompoundMarkID");
Mark mark = createMark(cMarkNode);
cMarks.put(markID, mark);
}
}
return cMarks;
}
private Mark createMark(Node compoundMark) {
List<SingleMark> marksList = new ArrayList<>();
String cMarkName = getNodeAttributeString(compoundMark, "Name");
NodeList childMarks = compoundMark.getChildNodes();
for (int i = 0; i < childMarks.getLength(); i++) {
Node markNode = childMarks.item(i);
if (markNode.getNodeName().equals("Mark")) {
Integer sourceID = getNodeAttributeInt(markNode, "SourceID");
String markName = getNodeAttributeString(markNode, "Name");
Double targetLat = getNodeAttributeDouble(markNode, "TargetLat");
Double targetLng = getNodeAttributeDouble(markNode, "TargetLng");
SingleMark mark = new SingleMark(markName, targetLat, targetLng, sourceID);
marksList.add(mark);
}
}
System.out.println(marksList.size());
if (marksList.size() == 1) {
return marksList.get(0);
} else if (marksList.size() == 2) {
return new GateMark(cMarkName, MarkType.OPEN_GATE, marksList.get(0),
marksList.get(1), marksList.get(0).getLatitude(),
marksList.get(0).getLongitude());
} else {
return null;
}
}
public Integer getRaceID() {
return raceID;
}
public String getRaceType() {
return raceType;
}
public String getCreationTimeDate() {
return creationTimeDate;
}
public String getRaceStartTime() {
return raceStartTime;
}
public Boolean getPostponeStatus() {
return postponeStatus;
}
public ArrayList<Participant> getParticipants() {
return participants;
}
public Map<Integer, Mark> getCompoundMarks() {
return course;
}
public ArrayList<Corner> getCompoundMarkSequence() {
return compoundMarkSequence;
}
public ArrayList<Limit> getCourseLimit() {
return courseLimit;
}
public class Participant { public class Participant {
Integer sourceID; Integer sourceID;
String entry; String entry;
@@ -294,65 +408,17 @@ public class XMLParser {
this.entry = entry; this.entry = entry;
} }
public Integer getsourceID() { return sourceID; } public Integer getsourceID() {
public String getEntry() { return entry; } return sourceID;
} }
public class CompoundMark { public String getEntry() {
private Integer markID; return entry;
private String cMarkName;
private MarkType markType;
private ArrayList<Mark> marks;
CompoundMark(Node compoundMark) {
marks = new ArrayList<>();
this.markID = getNodeAttributeInt(compoundMark, "CompoundMarkID");
this.cMarkName = getNodeAttributeString(compoundMark, "Name");
NodeList childMarks = compoundMark.getChildNodes();
if (childMarks.getLength() > 1){
markType = MarkType.OPEN_GATE;
} else {
markType = MarkType.SINGLE_MARK;
}
for (int i = 0; i < childMarks.getLength(); i++) {
Node markNode = childMarks.item(i);
if (markNode.getNodeName().equals("Mark")) {
Mark mark = new Mark(markNode);
marks.add(mark);
}
}
}
public Integer getMarkID() { return markID; }
public String getcMarkName() { return cMarkName; }
public MarkType getMarkType() { return markType; }
public ArrayList<Mark> getMarks() { return marks; }
public class Mark {
private Integer seqID;
private Integer sourceID;
private String markName;
private Double targetLat;
private Double targetLng;
Mark(Node markNode) {
this.seqID = getNodeAttributeInt(markNode, "SeqID");
this.sourceID = getNodeAttributeInt(markNode, "SourceID");
this.markName = getNodeAttributeString(markNode, "Name");
this.targetLat = getNodeAttributeDouble(markNode, "TargetLat");
this.targetLng = getNodeAttributeDouble(markNode, "TargetLng");
}
public Integer getSeqID() { return seqID; }
public Integer getSourceID() { return sourceID; }
public String getMarkName() { return markName; }
public Double getTargetLat() { return targetLat; }
public Double getTargetLng() { return targetLng; }
} }
} }
public class Corner { public class Corner {
private Integer seqID; private Integer seqID;
private Integer compoundMarkID; private Integer compoundMarkID;
private String rounding; private String rounding;
@@ -365,13 +431,25 @@ public class XMLParser {
this.zoneSize = getNodeAttributeInt(cornerNode, "ZoneSize"); this.zoneSize = getNodeAttributeInt(cornerNode, "ZoneSize");
} }
public Integer getSeqID() { return seqID; } public Integer getSeqID() {
public Integer getCompoundMarkID() { return compoundMarkID; } return seqID;
public String getRounding() { return rounding; } }
public Integer getZoneSize() { return zoneSize; }
public Integer getCompoundMarkID() {
return compoundMarkID;
}
public String getRounding() {
return rounding;
}
public Integer getZoneSize() {
return zoneSize;
}
} }
public class Limit { public class Limit {
private Integer seqID; private Integer seqID;
private Double lat; private Double lat;
private Double lng; private Double lng;
@@ -382,9 +460,17 @@ public class XMLParser {
this.lng = getNodeAttributeDouble(limitNode, "Lon"); this.lng = getNodeAttributeDouble(limitNode, "Lon");
} }
public Integer getSeqID() { return seqID; } public Integer getSeqID() {
public Double getLat() { return lat; } return seqID;
public Double getLng() { return lng; } }
public Double getLat() {
return lat;
}
public Double getLng() {
return lng;
}
} }
} }
@@ -410,6 +496,7 @@ public class XMLParser {
/** /**
* Constructor for a BoatXMLObject. * Constructor for a BoatXMLObject.
* Takes the information from a Document object and creates a more usable format. * Takes the information from a Document object and creates a more usable format.
*
* @param doc XML Document Object * @param doc XML Document Object
*/ */
BoatXMLObject(Document doc) { BoatXMLObject(Document doc) {
@@ -450,51 +537,50 @@ public class XMLParser {
competingBoats.put(boat.getSourceID(), boat); competingBoats.put(boat.getSourceID(), boat);
} }
} }
//System.out.println(this.getBoats());
} }
} }
public String getLastModified() { return lastModified; } public String getLastModified() {
public Integer getVersion() { return version; } return lastModified;
public String getBoatType() { return boatType; } }
public Double getBoatLength() { return boatLength; }
public Double getHullLength() { return hullLength; } public Integer getVersion() {
public Double getMarkZoneSize() { return markZoneSize; } return version;
public Double getCourseZoneSize() { return courseZoneSize; } }
public ArrayList<Double> getZoneLimits() { return zoneLimits; }
public ArrayList<Yacht> getBoats() { return boats; } public String getBoatType() {
return boatType;
}
public Double getBoatLength() {
return boatLength;
}
public Double getHullLength() {
return hullLength;
}
public Double getMarkZoneSize() {
return markZoneSize;
}
public Double getCourseZoneSize() {
return courseZoneSize;
}
public ArrayList<Double> getZoneLimits() {
return zoneLimits;
}
public ArrayList<Yacht> getBoats() {
return boats;
}
public Map<Integer, Yacht> getCompetingBoats() { public Map<Integer, Yacht> getCompetingBoats() {
return competingBoats; return competingBoats;
} }
// public class Boat {
//
// private String boatType;
// private Integer sourceID;
// private String hullID; //matches HullNum in the XML spec.
// private String shortName;
// private String boatName;
// private String country;
//
// Boat(Node boatNode) {
// this.boatType = getNodeAttributeString(boatNode, "Type");
// this.sourceID = getNodeAttributeInt(boatNode, "SourceID");
// this.hullID = getNodeAttributeString(boatNode, "HullNum");
// this.shortName = getNodeAttributeString(boatNode, "ShortName");
// this.boatName = getNodeAttributeString(boatNode, "BoatName");
// this.country = getNodeAttributeString(boatNode, "Country");
// }
//
// public String getBoatType() { return boatType; }
// public Integer getSourceID() { return sourceID; }
// public String getHullID() { return hullID; }
// public String getShortName() { return shortName; }
// public String getBoatName() { return boatName; }
// public String getCountry() { return country; }
//
// }
} }
} }