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
@@ -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,35 +1,37 @@
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 {
private String filePath;
private String filePath;
public FileParser(String path) {
this.filePath = path;
}
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;
}
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;
}
}
}
}
+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>