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
@@ -15,7 +15,7 @@ import java.util.List;
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
*/
public class CourseParser extends FileParser {
@@ -28,10 +28,11 @@ public class CourseParser extends FileParser {
this.doc = this.parseFile();
}
// TODO: should handle error / invalid file gracefully
public List<Corner> getCourse() {
compoundMarksMap = getCompoundMarks(doc);
compoundMarksMap = getCompoundMarks(doc.getDocumentElement());
List<Corner> corners = new ArrayList<>();
NodeList cMarksSequence = doc.getElementsByTagName("CompoundMarkSequence");
NodeList cMarksSequence = doc.getElementsByTagName("Corner");
for (int i = 0; i < cMarksSequence.getLength(); i++) {
corners.add(getCorner(cMarksSequence.item(i)));
@@ -47,7 +48,7 @@ public class CourseParser extends FileParser {
Integer seqId = Integer.valueOf(e.getAttribute("SeqID"));
Integer cMarkId = Integer.valueOf(e.getAttribute("CompoundMarkID"));
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"));
return new Corner(seqId, cMark, roundingType, zoneSize);
@@ -60,11 +61,11 @@ public class CourseParser extends FileParser {
if (node.getNodeType() == Node.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
for (int i = 0; i < course.getLength(); i++) {
CompoundMark cMark = getCompoundMark(course.item(i));
for (int i = 0; i < cMarks.getLength(); i++) {
CompoundMark cMark = getCompoundMark(cMarks.item(i));
if (cMark != null)
compoundMarksMap.put(cMark.getMarkID(), cMark);
}
@@ -78,7 +79,8 @@ public class CourseParser extends FileParser {
private CompoundMark getCompoundMark(Node node) {
if (node.getNodeType() == Node.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");
CompoundMark cMark = new CompoundMark(markID, name);