Reformatted and refactored the fileparser to get xml from resource folder

#fix #refactor #story[377] #pair[xyi25, zyt10]
This commit is contained in:
Haoming Yin
2017-03-24 12:55:11 +13:00
parent e8b1720fee
commit 304f30ece6
12 changed files with 148 additions and 103 deletions
-31
View File
@@ -1,31 +0,0 @@
{
"race-name": "AC35",
"time-scale": 1.0,
"race-size": 6,
"teams": [
{
"team-name": "Oracle Team USA",
"velocity": 30.9
},
{
"team-name": "Artemis Racing",
"velocity": 59.3
},
{
"team-name": "Emirates Team New Zealand",
"velocity": 51.5
},
{
"team-name": "Groupama Team France",
"velocity": 29.9
},
{
"team-name": "Land Rover BAR",
"velocity": 99.6
},
{
"team-name": "SoftBank Team Japan",
"velocity": 45.6
}
]
}
@@ -142,7 +142,7 @@ public class CanvasController {
});
//set wind direction!!!!!!! can't find another place to put my code --haoming
double windDirection = new ConfigParser("doc/examples/config.xml").getWindDirection();
double windDirection = new ConfigParser("/config.xml").getWindDirection();
windDirectionText.setText(String.format("%.1f°", windDirection));
windArrowText.setRotate(windDirection);
}
@@ -20,8 +20,8 @@ public class RaceController {
Race race = null;
public void initializeRace() {
String raceConfigFile = "doc/examples/config.xml";
String teamsConfigFile = "doc/examples/teams.xml";
String raceConfigFile = "/config.xml";
String teamsConfigFile = "/teams.xml";
try {
race = createRace(raceConfigFile, teamsConfigFile);
@@ -68,7 +68,7 @@ public class RaceController {
return null;
}
CourseParser course = new CourseParser("doc/examples/course.xml");
CourseParser course = new CourseParser("/course.xml");
race.addCourse(course.getCourse());
return race;
@@ -1,12 +1,14 @@
package seng302.models.parsers;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
*
*
* Created by Haoming Yin (hyi25) on 16/3/2017
*/
public abstract class FileParser {
@@ -19,10 +21,10 @@ public abstract class FileParser {
protected Document parseFile() {
try {
File file = new File(this.filePath);
InputStream is = getClass().getResourceAsStream(this.filePath);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
Document doc = builder.parse(is);
// optional, in order to recover info from broken line.
doc.getDocumentElement().normalize();
return doc;
+9
View File
@@ -0,0 +1,9 @@
<?xml version="1.0" ?>
<configurations>
<race-name>AC35</race-name>
<race-size>6</race-size>
<time-scale>1.0</time-scale>
<wind-direction>135</wind-direction>
</configurations>
+71
View File
@@ -0,0 +1,71 @@
<?xml version="1.0" ?>
<course>
<marks>
<gate>
<name type="start-line">Start</name>
<mark>
<name>Start1</name>
<latitude>32.296577</latitude>
<longitude>-64.854304</longitude>
</mark>
<mark>
<name>Start2</name>
<latitude>32.293771</latitude>
<longitude>-64.855242</longitude>
</mark>
</gate>
<mark>
<name>Mid Mark</name>
<latitude>32.293039</latitude>
<longitude>-64.843983</longitude>
</mark>
<gate>
<name>Leeward Gate</name>
<mark>
<name>Leeward Gate1</name>
<latitude>32.284680</latitude>
<longitude>-64.850045</longitude>
</mark>
<mark>
<name>Leeward Gate2</name>
<latitude>32.280164</latitude>
<longitude>-64.847591</longitude>
</mark>
</gate>
<gate>
<name>Windward Gate</name>
<mark>
<name>Windward Gate1</name>
<latitude>32.309693</latitude>
<longitude>-64.835249</longitude>
</mark>
<mark>
<name>Windward Gate2</name>
<latitude>32.308046</latitude>
<longitude>-64.831785</longitude>
</mark>
</gate>
<gate type="finish-line">
<name>Finish</name>
<mark>
<name>Finish1</name>
<latitude>32.317379</latitude>
<longitude>-64.839291</longitude>
</mark>
<mark>
<name>Finish2</name>
<latitude>32.317257</latitude>
<longitude>-64.836260</longitude>
</mark>
</gate>
</marks>
<order>
<one>Start</one>
<two>Mid Mark</two>
<three>Leeward Gate</three>
<four>Windward Gate</four>
<five>Leeward Gate</five>
<six>Finish</six>
</order>
</course>
+34
View File
@@ -0,0 +1,34 @@
<?xml version="1.0" ?>
<teams>
<team>
<name>Oracle Team USA</name>
<alias>USA</alias>
<velocity>30.9</velocity>
</team>
<team>
<name>Artemis Racing</name>
<alias>ART</alias>
<velocity>59.3</velocity>
</team>
<team>
<name>Emirates Team New Zealand</name>
<alias>NZL</alias>
<velocity>51.5</velocity>
</team>
<team>
<name>Land Rover BAR</name>
<alias>BAR</alias>
<velocity>29.9</velocity>
</team>
<team>
<name>SoftBank Team Japan</name>
<alias>JAP</alias>
<velocity>99.6</velocity>
</team>
<team>
<name>Groupama Team France</name>
<alias>FRC</alias>
<velocity>45.6</velocity>
</team>
</teams>
-9
View File
@@ -1,9 +0,0 @@
{
"time-scale": "abc",
"race-name": 123,
"teams-with-wrong-name": [
"team1",
"team2",
"team3"
]
}
@@ -14,7 +14,7 @@ public class ConfigParserTest {
@Before
public void initializeParser() throws Exception {
cp = new ConfigParser("doc/examples/config.xml");
cp = new ConfigParser("/config.xml");
}
@Test
@@ -19,7 +19,7 @@ public class CourseParserTest {
@Before
public void initializeParser() throws Exception {
cp = new CourseParser("doc/examples/course.xml");
cp = new CourseParser("/course.xml");
}
@Test
@@ -16,7 +16,7 @@ public class TeamsParserTest {
private TeamsParser tp;
@Before
public void readFile() {
tp = new TeamsParser("doc/examples/teams.xml");
tp = new TeamsParser("/teams.xml");
}
@Test
@@ -26,10 +26,10 @@ public class TeamsParserTest {
assertEquals(6, boats.size(), 1e-10);
assertEquals("Oracle Team USA", boats.get(0).getTeamName());
assertEquals(10, boats.get(0).getVelocity(), 1e-10);
assertEquals(30.9, boats.get(0).getVelocity(), 1e-10);
assertEquals("Groupama Team France", boats.get(5).getTeamName());
assertEquals(10, boats.get(5).getVelocity(), 1e-10);
assertEquals(45.6, boats.get(5).getVelocity(), 1e-10);
}
}
-31
View File
@@ -1,31 +0,0 @@
{
"race-name": "AC35",
"time-scale": 1,
"race-size": 2,
"teams": [
{
"team-name": "Oracle Team USA",
"velocity": 20.9
},
{
"team-name": "Artemis Racing",
"velocity": 18.3
},
{
"team-name": "Emirates Team New Zealand",
"velocity": 21.5
},
{
"team-name": "Groupama Team France",
"velocity": 19.9
},
{
"team-name": "Land Rover BAR",
"velocity": 17.6
},
{
"team-name": "SoftBank Team Japan",
"velocity": 16.6
}
]
}