Merge remote-tracking branch 'origin/master'

This commit is contained in:
zyt10
2017-03-17 16:18:24 +13:00
11 changed files with 297 additions and 29 deletions
+2 -2
View File
@@ -18,11 +18,11 @@ public class OldApp {
*/
public static Race createRace(String configFile) throws Exception {
Race race = new Race();
FileParser fp;
OldFileParser fp;
// Read team names from file
try{
fp = new FileParser(configFile);
fp = new OldFileParser(configFile);
}
catch (FileNotFoundException e){
System.out.println("Config file does not exist");
@@ -16,7 +16,7 @@ import java.util.Map;
* efficiently from external files.
*/
public class FileParser {
public class OldFileParser {
private String filePath;
private JSONObject content;
@@ -27,7 +27,7 @@ public class FileParser {
* @param filePath a string like path to show location of desired file to
* be parsed
*/
public FileParser(String filePath) throws Exception {
public OldFileParser(String filePath) throws Exception {
this.filePath = filePath;
this.readFile();
}
@@ -0,0 +1,125 @@
package seng302.models.parsers;
import org.w3c.dom.*;
import seng302.models.mark.*;
import java.util.ArrayList;
import java.util.NoSuchElementException;
/**
* parse a course xml file
* Created by Haoming Yin (hyi25) on 16/3/2017
*/
public class CourseParser extends FileParser {
private Document doc;
public CourseParser(String path) {
super(path);
this.doc = this.parseFile();
}
/**
* create a mark by given node
*
* @param node
* @return a mark, or null if fails to create a mark
*/
private SingleMark generateSingleMark(Node node) {
try {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String name = element.getElementsByTagName("name").item(0).getTextContent();
double lat = Double.valueOf(element.getElementsByTagName("latitude").item(0).getTextContent());
double lon = Double.valueOf(element.getElementsByTagName("longitude").item(0).getTextContent());
SingleMark singleMark = new SingleMark(name, lat, lon);
return singleMark;
} else {
throw new NoSuchElementException("Cannot generate a mark by given node.");
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* generate an arrayList of gates
*
* @return an arrayList of gates, or null if no gate has been found.
*/
public ArrayList<GateMark> getGateMarks() {
ArrayList<GateMark> gateMarks = new ArrayList<>();
try {
NodeList nodes = doc.getElementsByTagName("gate");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String name = element.getElementsByTagName("name").item(0).getTextContent();
SingleMark mark1 = generateSingleMark(element.getElementsByTagName("mark").item(0));
SingleMark mark2 = generateSingleMark(element.getElementsByTagName("mark").item(1));
GateMark gateMark = new GateMark(name, mark1, mark2);
gateMarks.add(gateMark);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return gateMarks;
}
/**
* generate an arrayList of marks
*
* @return an arrayList of marks, or null if no gate has been found.
*/
public ArrayList<SingleMark> getSingleMarks() {
ArrayList<SingleMark> singleMarks = new ArrayList<>();
try {
// find the "marks" tag
Node node = doc.getElementsByTagName("marks").item(0);
// iterate all "marks"'s children
for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
// if node's tag name is "mark"
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) n;
if (element.getNodeName() == "mark") {
singleMarks.add(generateSingleMark(n));
}
}
}
return singleMarks;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* return the order of all the marks along a course
*
* @return an arrayList of the names of ordered course marks
*/
public ArrayList<String> getOrder() {
ArrayList<String> markOrder = new ArrayList<>();
try {
Node orderNode = doc.getElementsByTagName("order").item(0);
for (Node node = orderNode.getFirstChild(); node != null; node = node.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String name = element.getTextContent();
markOrder.add(name);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return markOrder;
}
}
@@ -0,0 +1,35 @@
package seng302.models.parsers;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
/**
*
*
* 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 {
File file = new File(this.filePath);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
// optional, in order to recover info from broken line.
doc.getDocumentElement().normalize();
return doc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
@@ -0,0 +1,11 @@
package seng302.models.parsers;
public class teamsParser extends FileParser {
public teamsParser(String path) {
super(path);
}
}