shared.dataInput:

Added shared.enums.XMLFileType, which is used to specify if a string contains a file path or file contents.
Updated the XML readers to use it.

Testing:
Moved the networking tests and resources into racevisionGame module.
Moved the remaining tests from visualiser into racevisionGame, and added a few empty tests to implement later.
This commit is contained in:
fjc40
2017-07-19 23:18:52 +12:00
parent 10e80bca05
commit 4bcfc01ceb
45 changed files with 233 additions and 576 deletions
@@ -8,6 +8,7 @@ import network.Messages.LatestMessages;
import network.Messages.XMLMessage;
import org.xml.sax.SAXException;
import shared.dataInput.*;
import shared.enums.XMLFileType;
import shared.exceptions.InvalidBoatDataException;
import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.InvalidRegattaDataException;
@@ -61,9 +62,9 @@ public class Event {
sendXMLs();
//Parse the XML files into data sources.
RaceDataSource raceDataSource = new RaceXMLReader(this.raceXML);
BoatDataSource boatDataSource = new BoatXMLReader(this.boatXML);
RegattaDataSource regattaDataSource = new RegattaXMLReader(this.regattaXML);
RaceDataSource raceDataSource = new RaceXMLReader(this.raceXML, XMLFileType.ResourcePath);
BoatDataSource boatDataSource = new BoatXMLReader(this.boatXML, XMLFileType.ResourcePath);
RegattaDataSource regattaDataSource = new RegattaXMLReader(this.regattaXML, XMLFileType.ResourcePath);
//Create and start race.
MockRace newRace = new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale);
@@ -2,6 +2,7 @@ package shared.dataInput;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import shared.enums.XMLFileType;
import shared.exceptions.InvalidBoatDataException;
import shared.exceptions.XMLReaderException;
import shared.model.Boat;
@@ -32,11 +33,12 @@ public class BoatXMLReader extends XMLReader implements BoatDataSource {
* Constructor for Boat XML using a file read as a resource.
*
* @param fileContents Contents of xml file.
* @param type How to read the file - e.g., load as resource.
* @throws XMLReaderException Thrown if the file cannot be parsed.
* @throws InvalidBoatDataException Thrown if the file cannot be parsed correctly.
*/
public BoatXMLReader(String fileContents) throws XMLReaderException, InvalidBoatDataException {
super(fileContents);
public BoatXMLReader(String fileContents, XMLFileType type) throws XMLReaderException, InvalidBoatDataException {
super(fileContents, type);
//Attempt to read boat xml file.
try {
@@ -5,6 +5,7 @@ import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import shared.enums.XMLFileType;
import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.XMLReaderException;
import shared.model.*;
@@ -83,12 +84,13 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
/**
* Constructor for Streamed Race XML
* @param fileContents Contents of xml file.
* @param type How to read the file - e.g., load as resource.
* @throws XMLReaderException Thrown if an XML reader cannot be constructed for the given file.
* @throws InvalidRaceDataException Thrown if the XML file is invalid in some way.
*/
public RaceXMLReader(String fileContents) throws XMLReaderException, InvalidRaceDataException {
public RaceXMLReader(String fileContents, XMLFileType type) throws XMLReaderException, InvalidRaceDataException {
super(fileContents);
super(fileContents, type);
//Attempt to read race xml file.
@@ -4,6 +4,7 @@ import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import shared.dataInput.XMLReader;
import shared.enums.XMLFileType;
import shared.exceptions.InvalidRegattaDataException;
import shared.exceptions.XMLReaderException;
import shared.model.GPSCoordinate;
@@ -67,11 +68,12 @@ public class RegattaXMLReader extends XMLReader implements RegattaDataSource {
* Constructor for Regatta XML using a file read as a resource.
*
* @param fileContents Contents of xml file.
* @param type How to read the file - e.g., load as resource.
* @throws XMLReaderException Thrown if the file cannot be parsed.
* @throws InvalidRegattaDataException Thrown if the file cannot be parsed correctly.
*/
public RegattaXMLReader(String fileContents) throws XMLReaderException, InvalidRegattaDataException {
super(fileContents);
public RegattaXMLReader(String fileContents, XMLFileType type) throws XMLReaderException, InvalidRegattaDataException {
super(fileContents, type);
//Attempt to read boat xml file.
try {
@@ -5,6 +5,7 @@ import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import shared.enums.XMLFileType;
import shared.exceptions.XMLReaderException;
import javax.xml.parsers.DocumentBuilder;
@@ -27,20 +28,43 @@ public abstract class XMLReader {
protected Document doc;
/**
* Read an XML file.
* @param fileContents Contents of the xml file.
* @throws XMLReaderException Thrown if the file cannot be parsed.
*/
public XMLReader(String fileContents) throws XMLReaderException {
//Wrap file contents in input stream.
InputStream xmlInputStream = new ByteArrayInputStream(fileContents.getBytes(StandardCharsets.UTF_8));
/**
* Reads an XML file.
* @param file The file to read.
* @param type How to read the file - e.g., load as resource.
* @throws XMLReaderException Throw if the file cannot be parsed.
*/
public XMLReader(String file, XMLFileType type) throws XMLReaderException {
InputStream xmlInputStream = null;
//Create an input stream. Method depends on type parameter.
if (type == XMLFileType.Contents) {
//Wrap file contents in input stream.
xmlInputStream = new ByteArrayInputStream(file.getBytes(StandardCharsets.UTF_8));
} else if (type == XMLFileType.ResourcePath) {
xmlInputStream = XMLReader.class.getClassLoader().getResourceAsStream(file);
} else if (type == XMLFileType.FilePath) {
try {
xmlInputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new XMLReaderException("Could not open file " + file, e);
}
}
this.doc = parseInputStream(xmlInputStream);
}
/**
* Reads an XML file from an input stream.
* @param xmlInputStream The input stream to parse.
@@ -58,7 +82,7 @@ public abstract class XMLReader {
* @return The parsed document.
* @throws XMLReaderException Thrown when a document builder cannot be constructed, or the stream cannot be parsed.
*/
private Document parseInputStream(InputStream inputStream) throws XMLReaderException {
private static Document parseInputStream(InputStream inputStream) throws XMLReaderException {
//Create document builder.
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
@@ -142,18 +166,15 @@ public abstract class XMLReader {
* @param path path of the XML
* @param encoding encoding of the xml
* @return A string containing the contents of the specified file.
* @throws IOException No file etc
* @throws ParserConfigurationException Issue with the XML formatting
* @throws SAXException Issue with XML formatting
* @throws TransformerException Issue with the XML format
* @throws XMLReaderException Thrown if file cannot be read for some reason.
*/
public static String readXMLFileToString(String path, Charset encoding) throws IOException, ParserConfigurationException, SAXException, TransformerException {
public static String readXMLFileToString(String path, Charset encoding) throws TransformerException, XMLReaderException {
InputSource fileSource = new InputSource(XMLReader.class.getClassLoader().getResourceAsStream(path));
InputStream fileStream = XMLReader.class.getClassLoader().getResourceAsStream(path);
Document doc = XMLReader.parseInputStream(fileStream);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fileSource);
doc.getDocumentElement().normalize();
return XMLReader.getContents(doc);
@@ -0,0 +1,24 @@
package shared.enums;
/**
* Represents the various ways in which we want to open/read an xml file - e.g., a string may contain the file contents, or the file path.
*/
public enum XMLFileType {
/**
* This means that a provided string contains the contents of an XML file.
*/
Contents,
/**
* This means that a provided string contains the path to an XML file.
*/
FilePath,
/**
* This means that a provided string contains the path, to be loaded as a resource, of an XML file.
*/
ResourcePath;
}
@@ -13,6 +13,7 @@ import javafx.scene.paint.Color;
import network.Messages.Enums.RaceStatusEnum;
import network.Messages.LatestMessages;
import shared.dataInput.*;
import shared.enums.XMLFileType;
import shared.exceptions.InvalidBoatDataException;
import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.InvalidRegattaDataException;
@@ -115,9 +116,9 @@ public class StartController extends Controller implements Observer {
private void startRace(LatestMessages latestMessages) throws XMLReaderException, InvalidRaceDataException, InvalidBoatDataException, InvalidRegattaDataException {
//Create data sources from latest messages for the race.
RaceDataSource raceDataSource = new RaceXMLReader(latestMessages.getRaceXMLMessage().getXmlMessage());
BoatDataSource boatDataSource = new BoatXMLReader(latestMessages.getBoatXMLMessage().getXmlMessage());
RegattaDataSource regattaDataSource = new RegattaXMLReader(latestMessages.getRegattaXMLMessage().getXmlMessage());
RaceDataSource raceDataSource = new RaceXMLReader(latestMessages.getRaceXMLMessage().getXmlMessage(), XMLFileType.ResourcePath);
BoatDataSource boatDataSource = new BoatXMLReader(latestMessages.getBoatXMLMessage().getXmlMessage(), XMLFileType.ResourcePath);
RegattaDataSource regattaDataSource = new RegattaXMLReader(latestMessages.getRegattaXMLMessage().getXmlMessage(), XMLFileType.ResourcePath);
//Create race.
this.visualiserRace = new VisualiserRace(boatDataSource, raceDataSource, regattaDataSource, latestMessages, this.colors);