Fixed a bug of getCourse method as it didn't parse xml correctly.

- a typo 'CompoundmarkID'(should be 'CompoundMarkID') which caused parser failed to parse file.
- add typeOf method in RoundingType to convert strings to types

#story[828]
This commit is contained in:
Haoming Yin
2017-04-25 11:36:27 +12:00
parent 7f38191d03
commit b2ea8196d5
5 changed files with 59 additions and 10 deletions
@@ -21,6 +21,19 @@ public class CompoundMark {
} }
} }
/**
* Prints out compoundMark's info and its marks, good for testing
* @return a string showing its details
*/
@Override
public String toString(){
if (mark2 == null)
return String.format("CompoundMark: %d (%s), [%s]",
markID, name, mark1.toString());
return String.format("CompoundMark: %d (%s), [%s; %s]",
markID, name, mark1.toString(), mark2.toString());
}
public int getMarkID() { public int getMarkID() {
return markID; return markID;
} }
@@ -15,6 +15,16 @@ public class Corner {
this.zoneSize = zoneSize; this.zoneSize = zoneSize;
} }
/**
* Prints out corner's info and its compound mark, good for testing
* @return a string showing its details
*/
@Override
public String toString() {
return String.format("Corner: %d - %s - %d, %s\n",
seqID, roundingType.getType(), zoneSize, compoundMark.toString());
}
public int getSeqID() { public int getSeqID() {
return seqID; return seqID;
} }
@@ -18,6 +18,15 @@ public class Mark {
this.lng = lng; this.lng = lng;
} }
/**
* Prints out mark's info and its geo location, good for testing
* @return a string showing its details
*/
@Override
public String toString() {
return String.format("Mark: %d (%s), lat: %f, lng: %f", seqID, name, lat, lng);
}
public int getSeqID() { public int getSeqID() {
return seqID; return seqID;
} }
@@ -1,9 +1,9 @@
package seng302.server.simulator.mark; package seng302.server.simulator.mark;
public enum RoundingType{ public enum RoundingType {
// the mark should be rounded to port (boat's left) // the mark should be rounded to port (boat's left)
PORT("PS"), PORT("Port"),
// the mark should be rounded to starboard (boat's right) // the mark should be rounded to starboard (boat's right)
STARBOARD("Stbd"), STARBOARD("Stbd"),
@@ -25,4 +25,19 @@ public enum RoundingType{
public String getType() { public String getType() {
return this.type; return this.type;
} }
public static RoundingType typeOf(String type) {
switch (type) {
case "Port":
return PORT;
case "Stbd":
return STARBOARD;
case "SP":
return SP;
case "PS":
return PS;
default:
return null;
}
}
} }
@@ -15,7 +15,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* parse a course xml file * Parses the race xml file to get course details
* Created by Haoming Yin (hyi25) on 16/3/2017 * Created by Haoming Yin (hyi25) on 16/3/2017
*/ */
public class CourseParser extends FileParser { public class CourseParser extends FileParser {
@@ -28,10 +28,11 @@ public class CourseParser extends FileParser {
this.doc = this.parseFile(); this.doc = this.parseFile();
} }
// TODO: should handle error / invalid file gracefully
public List<Corner> getCourse() { public List<Corner> getCourse() {
compoundMarksMap = getCompoundMarks(doc); compoundMarksMap = getCompoundMarks(doc.getDocumentElement());
List<Corner> corners = new ArrayList<>(); List<Corner> corners = new ArrayList<>();
NodeList cMarksSequence = doc.getElementsByTagName("CompoundMarkSequence"); NodeList cMarksSequence = doc.getElementsByTagName("Corner");
for (int i = 0; i < cMarksSequence.getLength(); i++) { for (int i = 0; i < cMarksSequence.getLength(); i++) {
corners.add(getCorner(cMarksSequence.item(i))); corners.add(getCorner(cMarksSequence.item(i)));
@@ -47,7 +48,7 @@ public class CourseParser extends FileParser {
Integer seqId = Integer.valueOf(e.getAttribute("SeqID")); Integer seqId = Integer.valueOf(e.getAttribute("SeqID"));
Integer cMarkId = Integer.valueOf(e.getAttribute("CompoundMarkID")); Integer cMarkId = Integer.valueOf(e.getAttribute("CompoundMarkID"));
CompoundMark cMark = compoundMarksMap.get(cMarkId); CompoundMark cMark = compoundMarksMap.get(cMarkId);
RoundingType roundingType = RoundingType.valueOf(e.getAttribute("Rounding")); RoundingType roundingType = RoundingType.typeOf(e.getAttribute("Rounding"));
Integer zoneSize = Integer.valueOf(e.getAttribute("ZoneSize")); Integer zoneSize = Integer.valueOf(e.getAttribute("ZoneSize"));
return new Corner(seqId, cMark, roundingType, zoneSize); return new Corner(seqId, cMark, roundingType, zoneSize);
@@ -60,11 +61,11 @@ public class CourseParser extends FileParser {
if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node; Element element = (Element) node;
NodeList course = element.getElementsByTagName("Course"); NodeList cMarks = element.getElementsByTagName("CompoundMark");
// loop through all compound marks who are the children of course node // loop through all compound marks who are the children of course node
for (int i = 0; i < course.getLength(); i++) { for (int i = 0; i < cMarks.getLength(); i++) {
CompoundMark cMark = getCompoundMark(course.item(i)); CompoundMark cMark = getCompoundMark(cMarks.item(i));
if (cMark != null) if (cMark != null)
compoundMarksMap.put(cMark.getMarkID(), cMark); compoundMarksMap.put(cMark.getMarkID(), cMark);
} }
@@ -78,7 +79,8 @@ public class CourseParser extends FileParser {
private CompoundMark getCompoundMark(Node node) { private CompoundMark getCompoundMark(Node node) {
if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) node; Element e = (Element) node;
Integer markID = Integer.valueOf(e.getAttribute("CompoundmarkID")); Integer markID = Integer.valueOf(e.getAttribute("CompoundMarkID"));
String name = e.getAttribute("Name"); String name = e.getAttribute("Name");
CompoundMark cMark = new CompoundMark(markID, name); CompoundMark cMark = new CompoundMark(markID, name);