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;
}
}
}
}