From 22075ee415df51589c839f6d2cddb5656e536194 Mon Sep 17 00:00:00 2001 From: Joseph Date: Tue, 25 Apr 2017 02:13:57 +1200 Subject: [PATCH] Started on the boat XML parser, reads only boat data from the XML and makes a StreamedBoat for each boat in the XML. #story[861] --- .../main/java/seng302/Mock/BoatXMLReader.java | 126 ++++++++++++++++++ .../main/java/seng302/Mock/StreamedBoat.java | 37 +++++ .../main/java/seng302/Mock/StreamedRace.java | 2 +- .../src/main/java/seng302/Model/Boat.java | 11 ++ .../main/java/seng302/Model/BoatInRace.java | 50 ++++--- .../test/java/seng302/Mock/BoatsXMLTest.java | 13 ++ 6 files changed, 220 insertions(+), 19 deletions(-) create mode 100644 visualiser/src/main/java/seng302/Mock/BoatXMLReader.java create mode 100644 visualiser/src/main/java/seng302/Mock/StreamedBoat.java diff --git a/visualiser/src/main/java/seng302/Mock/BoatXMLReader.java b/visualiser/src/main/java/seng302/Mock/BoatXMLReader.java new file mode 100644 index 00000000..5a9b8e4c --- /dev/null +++ b/visualiser/src/main/java/seng302/Mock/BoatXMLReader.java @@ -0,0 +1,126 @@ +package seng302.Mock; + +import javafx.scene.paint.Color; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; +import seng302.XMLReader; + +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; +import java.lang.annotation.ElementType; +import java.text.ParseException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Joseph on 24/04/2017. + */ +public class BoatXMLReader extends XMLReader { + Map streamedBoatMap = new HashMap<>(); + private Color[] colours = {Color.BLUEVIOLET, Color.BLACK, Color.RED, Color.ORANGE, Color.DARKOLIVEGREEN, Color.LIMEGREEN}; + private int currentColourIndex = 0; + + /** + * Constructor for Boat XML Reader + * @param filePath path of the file + * @throws IOException error + * @throws SAXException error + * @throws ParserConfigurationException error + */ + public BoatXMLReader(String filePath) throws IOException, SAXException, ParserConfigurationException, ParseException { + this(filePath, true); + } + + /** + * Constructor for Boat XML Reader + * @param filePath file path to read + * @param read whether or not to read and store the files straight away. + * @throws IOException error + * @throws SAXException error + * @throws ParserConfigurationException error + */ + public BoatXMLReader(String filePath, boolean read) throws IOException, SAXException, ParserConfigurationException, ParseException { + super(filePath); + if (read) { + read(); + } + } + + private void read() { + readSettings(); + readShapes(); + readBoats(); + } + + /** + * Reads boats settings. + * INFORMATION FROM HERE IS IGNORED FOR NOW + */ + private void readSettings() { + + } + + /** + * Reads different kinds of boat. + * INFORMATION FROM HERE IS IGNORED FOR NOW + */ + private void readShapes() { + + } + + /** + * Reads the boats in the race + */ + private void readBoats() { + Element nBoats = (Element) doc.getElementsByTagName("Boats").item(0); + for (int i = 0; i < nBoats.getChildNodes().getLength(); i++) { + Node boat = nBoats.getChildNodes().item(i); + if (boat.getNodeName().equals("Boat")) { + readSingleBoat(boat); + currentColourIndex++; + } + } + } + + /** + * Reads the information about one boat + * Ignored values: Type, ShapeID, StoweName, HullNum, Skipper + */ + private void readSingleBoat(Node boat) { + StreamedBoat streamedBoat; + String country = null; + int sourceID = Integer.parseInt(boat.getAttributes().getNamedItem("SourceID").getTextContent()); + String boatName = boat.getAttributes().getNamedItem("BoatName").getTextContent(); + String shortName = boat.getAttributes().getNamedItem("ShortName").getTextContent(); + if (boat.getAttributes().getNamedItem("Country") != null) country = boat.getAttributes().getNamedItem("Country").getTextContent(); + + if (!streamedBoatMap.containsKey(sourceID)) { + if (country != null) { + streamedBoat = new StreamedBoat(sourceID, boatName, colours[currentColourIndex], country); + } else { + streamedBoat = new StreamedBoat(sourceID, boatName, colours[currentColourIndex], shortName); + } + streamedBoatMap.put(sourceID, streamedBoat); + } + + for (int i = 0; i < boat.getChildNodes().getLength(); i++) { + Node GPSPosition = boat.getChildNodes().item(i); + if (GPSPosition.getNodeName().equals("GPSposition")) readBoatPositionInformation(sourceID, GPSPosition); + } + } + + /** + * Reads the positional information about a boat + * Ignored values: FlagPosition, MastTop, Z value of GPSposition + */ + private void readBoatPositionInformation(int sourceID, Node GPSPosition) { + // TODO Get relative point before implementing. (GPSposition is based off a relative point). + } + + private Map getBoatMap() { + return streamedBoatMap; + } +} diff --git a/visualiser/src/main/java/seng302/Mock/StreamedBoat.java b/visualiser/src/main/java/seng302/Mock/StreamedBoat.java new file mode 100644 index 00000000..1f483d40 --- /dev/null +++ b/visualiser/src/main/java/seng302/Mock/StreamedBoat.java @@ -0,0 +1,37 @@ +package seng302.Mock; + +import javafx.scene.paint.Color; +import seng302.GPSCoordinate; +import seng302.Model.Boat; +import seng302.Model.BoatInRace; + +/** + * Created by Joseph on 24/04/2017. + */ +public class StreamedBoat extends BoatInRace { + private int sourceID; + + public StreamedBoat(int sourceID, String name, Color colour, String abbrev) { + super(name, colour, abbrev); + this.sourceID = sourceID; + } + + + /** + * Overridden to ignore this function + * @deprecated + * @return 0 + */ + public double getScaledVelocity() { + return 0; + } + + /** + * Overridden to ignore this function + * @deprecated + * @param velocity of boat + */ + public void setScaledVelocity(double velocity) { + + } +} diff --git a/visualiser/src/main/java/seng302/Mock/StreamedRace.java b/visualiser/src/main/java/seng302/Mock/StreamedRace.java index 3fa93dec..56b6a114 100644 --- a/visualiser/src/main/java/seng302/Mock/StreamedRace.java +++ b/visualiser/src/main/java/seng302/Mock/StreamedRace.java @@ -12,7 +12,7 @@ import java.util.*; /** * Created by jjg64 on 21/04/17. */ -public class StreamedRace extends Race{ +public class StreamedRace extends Race { private RaceDataSource raceData; public StreamedRace(RaceDataSource raceData, RaceController controller, int scaleFactor) { diff --git a/visualiser/src/main/java/seng302/Model/Boat.java b/visualiser/src/main/java/seng302/Model/Boat.java index 49b5a0f9..a3e269d4 100644 --- a/visualiser/src/main/java/seng302/Model/Boat.java +++ b/visualiser/src/main/java/seng302/Model/Boat.java @@ -27,6 +27,17 @@ public class Boat { this.name = new SimpleStringProperty(name); } + /** + * Boat initialiser which keeps all of the information of the boat. + * + * @param name Name of the Boat. + * @param abbrev nam abbreviation + */ + public Boat(String name, String abbrev) { + this.abbrev = abbrev; + this.name = new SimpleStringProperty(name); + } + /** * @return Name of the boat */ diff --git a/visualiser/src/main/java/seng302/Model/BoatInRace.java b/visualiser/src/main/java/seng302/Model/BoatInRace.java index 520e7b06..6f835593 100644 --- a/visualiser/src/main/java/seng302/Model/BoatInRace.java +++ b/visualiser/src/main/java/seng302/Model/BoatInRace.java @@ -17,25 +17,25 @@ import java.util.concurrent.ConcurrentLinkedQueue; */ public class BoatInRace extends Boat { - private Leg currentLeg; + protected Leg currentLeg; private double scaledVelocity; - private double distanceTravelledInLeg; - private GPSCoordinate currentPosition; - private long timeFinished; - private Color colour; - private boolean finished = false; - private StringProperty currentLegName; - private boolean started = false; - private StringProperty position; - private double heading; - - private Queue track = new ConcurrentLinkedQueue<>(); - private long nextValidTime = 0; - - private static final float BASE_TRACK_POINT_TIME_INTERVAL = 5000; - private static float trackPointTimeInterval = 5000; // every 1 seconds - private final int TRACK_POINT_LIMIT = 10; - private boolean trackVisible = true; + protected double distanceTravelledInLeg; + protected GPSCoordinate currentPosition; + protected long timeFinished; + protected Color colour; + protected boolean finished = false; + protected StringProperty currentLegName; + protected boolean started = false; + protected StringProperty position; + protected double heading; + + protected Queue track = new ConcurrentLinkedQueue<>(); + protected long nextValidTime = 0; + + protected static final float BASE_TRACK_POINT_TIME_INTERVAL = 5000; + protected static float trackPointTimeInterval = 5000; // every 1 seconds + protected final int TRACK_POINT_LIMIT = 10; + protected boolean trackVisible = true; /** * Constructor method. @@ -52,6 +52,20 @@ public class BoatInRace extends Boat { position = new SimpleStringProperty("-"); } + /** + * Constructor method. + * + * @param name Name of the boat. + * @param colour Colour the boat will be displayed as on the map + * @param abbrev of boat + */ + public BoatInRace(String name, Color colour, String abbrev) { + super(name, abbrev); + setColour(colour); + currentLegName = new SimpleStringProperty(""); + position = new SimpleStringProperty("-"); + } + /** * Calculates the azimuth of the travel via map coordinates of the raceMarkers * diff --git a/visualiser/src/test/java/seng302/Mock/BoatsXMLTest.java b/visualiser/src/test/java/seng302/Mock/BoatsXMLTest.java index 990e3a49..507bf60e 100644 --- a/visualiser/src/test/java/seng302/Mock/BoatsXMLTest.java +++ b/visualiser/src/test/java/seng302/Mock/BoatsXMLTest.java @@ -1,7 +1,20 @@ package seng302.Mock; +import org.junit.Test; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; +import java.text.ParseException; + /** * Created by jjg64 on 21/04/17. */ public class BoatsXMLTest { + + @Test + public void test() throws SAXException, ParserConfigurationException, ParseException, IOException { + new BoatXMLReader("mockXML/boatXML/boatTest.xml"); + //new BoatXMLReader("mockXML/raceXML/raceTest.xml", true); + } }