mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Rewrote course parser to parse race xml file specified in AC35 spec.
#story[828]
This commit is contained in:
@@ -0,0 +1,115 @@
|
|||||||
|
package seng302.server.simulator.parsers;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
import seng302.server.simulator.mark.CompoundMark;
|
||||||
|
import seng302.server.simulator.mark.Corner;
|
||||||
|
import seng302.server.simulator.mark.Mark;
|
||||||
|
import seng302.server.simulator.mark.RoundingType;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* parse a course xml file
|
||||||
|
* Created by Haoming Yin (hyi25) on 16/3/2017
|
||||||
|
*/
|
||||||
|
public class CourseParser extends FileParser {
|
||||||
|
|
||||||
|
private Document doc;
|
||||||
|
private Map<Integer, CompoundMark> compoundMarksMap;
|
||||||
|
|
||||||
|
public CourseParser(String path) {
|
||||||
|
super(path);
|
||||||
|
this.doc = this.parseFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Corner> getCourse() {
|
||||||
|
compoundMarksMap = getCompoundMarks(doc);
|
||||||
|
List<Corner> corners = new ArrayList<>();
|
||||||
|
NodeList cMarksSequence = doc.getElementsByTagName("CompoundMarkSequence");
|
||||||
|
|
||||||
|
for (int i = 0; i < cMarksSequence.getLength(); i++) {
|
||||||
|
corners.add(getCorner(cMarksSequence.item(i)));
|
||||||
|
}
|
||||||
|
return corners;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Corner getCorner(Node node) {
|
||||||
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
Element e = (Element) node;
|
||||||
|
|
||||||
|
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"));
|
||||||
|
Integer zoneSize = Integer.valueOf(e.getAttribute("ZoneSize"));
|
||||||
|
|
||||||
|
return new Corner(seqId, cMark, roundingType, zoneSize);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<Integer, CompoundMark> getCompoundMarks(Node node) {
|
||||||
|
Map<Integer, CompoundMark> compoundMarksMap = new HashMap<>();
|
||||||
|
|
||||||
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
Element element = (Element) node;
|
||||||
|
NodeList course = element.getElementsByTagName("Course");
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
if (cMark != null)
|
||||||
|
compoundMarksMap.put(cMark.getMarkID(), cMark);
|
||||||
|
}
|
||||||
|
|
||||||
|
return compoundMarksMap;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private CompoundMark getCompoundMark(Node node) {
|
||||||
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
Element e = (Element) node;
|
||||||
|
Integer markID = Integer.valueOf(e.getAttribute("CompoundmarkID"));
|
||||||
|
String name = e.getAttribute("Name");
|
||||||
|
CompoundMark cMark = new CompoundMark(markID, name);
|
||||||
|
|
||||||
|
NodeList marks = e.getElementsByTagName("Mark");
|
||||||
|
for (int i = 0; i < marks.getLength(); i++) {
|
||||||
|
Mark mark = getMark(marks.item(i));
|
||||||
|
if (mark != null)
|
||||||
|
cMark.addMark(mark.getSeqID(), mark);
|
||||||
|
}
|
||||||
|
return cMark;
|
||||||
|
}
|
||||||
|
System.out.println("Failed to create compound mark.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Mark getMark(Node node) {
|
||||||
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
Element e = (Element) node;
|
||||||
|
Integer seqId = Integer.valueOf(e.getAttribute("SeqID"));
|
||||||
|
String name = e.getAttribute("Name");
|
||||||
|
Double lat = Double.valueOf(e.getAttribute("TargetLat"));
|
||||||
|
Double lng = Double.valueOf(e.getAttribute("TargetLng"));
|
||||||
|
|
||||||
|
Mark mark = new Mark(name, lat, lng);
|
||||||
|
mark.setSeqID(seqId);
|
||||||
|
|
||||||
|
return mark;
|
||||||
|
}
|
||||||
|
System.out.println("Failed to create mark.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package seng302.server.simulator.parsers;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Haoming Yin (hyi25) on 16/3/2017
|
||||||
|
*/
|
||||||
|
public abstract class FileParser {
|
||||||
|
|
||||||
|
private String filePath;
|
||||||
|
|
||||||
|
public FileParser(String path) {
|
||||||
|
this.filePath = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Document parseFile() {
|
||||||
|
try {
|
||||||
|
InputStream is = getClass().getResourceAsStream(this.filePath);
|
||||||
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||||
|
Document doc = builder.parse(is);
|
||||||
|
// optional, in order to recover info from broken line.
|
||||||
|
doc.getDocumentElement().normalize();
|
||||||
|
return doc;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user