mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -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");
|
||||
|
||||
+2
-2
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+5
-5
@@ -1,7 +1,7 @@
|
||||
package seng302;
|
||||
|
||||
import org.junit.Test;
|
||||
import seng302.models.FileParser;
|
||||
import seng302.models.OldFileParser;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
@@ -11,14 +11,14 @@ import static org.junit.Assert.assertEquals;
|
||||
* Unit test for FileParser class
|
||||
* Created by Haoming on 5/03/17.
|
||||
*/
|
||||
public class FileParserTest {
|
||||
public class OldFileParserTest {
|
||||
|
||||
/**
|
||||
* test if it fails from reading non existed file
|
||||
*/
|
||||
@Test(expected = FileNotFoundException.class)
|
||||
public void readNonExistedFile() throws Exception {
|
||||
FileParser fileParser = new FileParser("test/java/seng302/non-existed.json");
|
||||
OldFileParser fileParser = new OldFileParser("test/java/seng302/non-existed.json");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -26,7 +26,7 @@ public class FileParserTest {
|
||||
*/
|
||||
@Test
|
||||
public void readValidFile() throws Exception {
|
||||
FileParser fileParser = new FileParser("src/test/java/seng302/valid.json");
|
||||
OldFileParser fileParser = new OldFileParser("src/test/java/seng302/valid.json");
|
||||
|
||||
assertEquals("AC35", fileParser.getRaceName());
|
||||
|
||||
@@ -42,7 +42,7 @@ public class FileParserTest {
|
||||
*/
|
||||
@Test
|
||||
public void readInvalidFile() throws Exception {
|
||||
FileParser fileParser = new FileParser("src/test/java/seng302/invalid.json");
|
||||
OldFileParser fileParser = new OldFileParser("src/test/java/seng302/invalid.json");
|
||||
|
||||
assertEquals(null, fileParser.getRaceName());
|
||||
assertEquals(null, fileParser.getTeams());
|
||||
@@ -0,0 +1,65 @@
|
||||
package seng302.models.parsers;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import seng302.models.mark.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* To test if course parser works as expected.
|
||||
* Created by Haoming on 17/03/17.
|
||||
*/
|
||||
public class CourseParserTest {
|
||||
|
||||
private CourseParser cp;
|
||||
|
||||
@Before
|
||||
public void initializeParser() throws Exception {
|
||||
cp = new CourseParser("doc/examples/course.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getGates() throws Exception {
|
||||
ArrayList<GateMark> gateMarks = cp.getGateMarks();
|
||||
assertEquals(4, gateMarks.size());
|
||||
|
||||
assertEquals("Start", gateMarks.get(0).getName());
|
||||
assertEquals("Leeward Gate", gateMarks.get(1).getName());
|
||||
assertEquals("Windward Gate", gateMarks.get(2).getName());
|
||||
assertEquals("Finish", gateMarks.get(3).getName());
|
||||
|
||||
assertEquals("Start1", gateMarks.get(0).getSingleMark1().getName());
|
||||
assertEquals("Start2", gateMarks.get(0).getSingleMark2().getName());
|
||||
assertEquals(32.293834, gateMarks.get(0).getSingleMark2().getLatitude(), 0.00000001);
|
||||
assertEquals(-64.855195, gateMarks.get(0).getSingleMark2().getLongitude(), 0.00000001);
|
||||
|
||||
assertEquals("Finish1", gateMarks.get(3).getSingleMark1().getName());
|
||||
assertEquals("Finish2", gateMarks.get(3).getSingleMark2().getName());
|
||||
assertEquals(32.318303, gateMarks.get(3).getSingleMark2().getLatitude(), 0.00000001);
|
||||
assertEquals(-64.834974, gateMarks.get(3).getSingleMark2().getLongitude(), 0.00000001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMarks() throws Exception {
|
||||
ArrayList<SingleMark> marks = cp.getSingleMarks();
|
||||
assertEquals(1, marks.size());
|
||||
assertEquals("Mid Mark", marks.get(0).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getOrder() throws Exception {
|
||||
ArrayList<String> order = cp.getOrder();
|
||||
|
||||
assertEquals(6, order.size());
|
||||
assertEquals("Start", order.get(0));
|
||||
assertEquals("Mid Mark", order.get(1));
|
||||
assertEquals("Leeward Gate", order.get(2));
|
||||
assertEquals("Windward Gate", order.get(3));
|
||||
assertEquals("Leeward Gate", order.get(4));
|
||||
assertEquals("Finish", order.get(5));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user