Added the ability to reads boats from both race.xml and boat.xml. Linked the two xmls via the StreamedCource class. #story[782]

main
Joseph Gardner 9 years ago
parent a59342a668
commit 3461a2c0b7

@ -11,17 +11,16 @@ import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.text.ParseException; import java.text.ParseException;
import java.util.Collection; import java.util.*;
import java.util.HashMap;
import java.util.Map;
/** /**
* Created by Joseph on 24/04/2017. * Created by Joseph on 24/04/2017.
*/ */
public class BoatXMLReader extends XMLReader { public class BoatXMLReader extends XMLReader {
Map<Integer, StreamedBoat> streamedBoatMap = new HashMap<>(); Map<Integer, StreamedBoat> streamedBoatMap = new HashMap<>();
private Color[] colours = {Color.BLUEVIOLET, Color.BLACK, Color.RED, Color.ORANGE, Color.DARKOLIVEGREEN, Color.LIMEGREEN}; Map<Integer, StreamedBoat> participants = new HashMap<>();
private int currentColourIndex = 0; private List<Color> colours;
private static int currentColourIndex = 0;
/** /**
* Constructor for Boat XML Reader * Constructor for Boat XML Reader
@ -31,7 +30,8 @@ public class BoatXMLReader extends XMLReader {
* @throws ParserConfigurationException error * @throws ParserConfigurationException error
*/ */
public BoatXMLReader(String filePath) throws IOException, SAXException, ParserConfigurationException, ParseException { public BoatXMLReader(String filePath) throws IOException, SAXException, ParserConfigurationException, ParseException {
this(filePath, true); this(filePath, false);
makeColours();
} }
/** /**
@ -44,12 +44,13 @@ public class BoatXMLReader extends XMLReader {
*/ */
public BoatXMLReader(String filePath, boolean read) throws IOException, SAXException, ParserConfigurationException, ParseException { public BoatXMLReader(String filePath, boolean read) throws IOException, SAXException, ParserConfigurationException, ParseException {
super(filePath); super(filePath);
makeColours();
if (read) { if (read) {
read(); read();
} }
} }
private void read() { public void read() {
readSettings(); readSettings();
readShapes(); readShapes();
readBoats(); readBoats();
@ -80,35 +81,46 @@ public class BoatXMLReader extends XMLReader {
Node boat = nBoats.getChildNodes().item(i); Node boat = nBoats.getChildNodes().item(i);
if (boat.getNodeName().equals("Boat")) { if (boat.getNodeName().equals("Boat")) {
readSingleBoat(boat); readSingleBoat(boat);
currentColourIndex++; currentColourIndex = (currentColourIndex + 1) % colours.size();
} }
} }
} }
/** /**
* Reads the information about one boat * Reads the information about one boat
* Ignored values: Type, ShapeID, StoweName, HullNum, Skipper * Ignored values: ShapeID, StoweName, HullNum, Skipper
*/ */
private void readSingleBoat(Node boat) { private void readSingleBoat(Node boat) {
StreamedBoat streamedBoat; StreamedBoat streamedBoat;
String type = "None";
int sourceID = -1;
String boatName = null;
String shortName = null;
String country = null; String country = null;
int sourceID = Integer.parseInt(boat.getAttributes().getNamedItem("SourceID").getTextContent()); if (exists(boat, "Type")) type = boat.getAttributes().getNamedItem("Type").getTextContent();
String boatName = boat.getAttributes().getNamedItem("BoatName").getTextContent(); if (exists(boat, "SourceID")) sourceID = Integer.parseInt(boat.getAttributes().getNamedItem("SourceID").getTextContent());
String shortName = boat.getAttributes().getNamedItem("ShortName").getTextContent(); if (exists(boat, "BoatName")) boatName = boat.getAttributes().getNamedItem("BoatName").getTextContent();
if (boat.getAttributes().getNamedItem("Country") != null) country = boat.getAttributes().getNamedItem("Country").getTextContent(); if (exists(boat, "ShortName")) shortName = boat.getAttributes().getNamedItem("ShortName").getTextContent();
if (exists(boat, "Country")) country = boat.getAttributes().getNamedItem("Country").getTextContent();
if (!streamedBoatMap.containsKey(sourceID)) {
if (country != null) { // Ignore all non participating boats
streamedBoat = new StreamedBoat(sourceID, boatName, colours[currentColourIndex], country); if (participants.containsKey(sourceID)) {
} else {
streamedBoat = new StreamedBoat(sourceID, boatName, colours[currentColourIndex], shortName); if (!streamedBoatMap.containsKey(sourceID)) {
if (country != null) {
streamedBoat = new StreamedBoat(sourceID, boatName, colours.get(currentColourIndex), country);
} else {
streamedBoat = new StreamedBoat(sourceID, boatName, colours.get(currentColourIndex), shortName);
}
streamedBoatMap.put(sourceID, streamedBoat);
// Override boat with new boat
participants.put(sourceID, streamedBoat);
} }
streamedBoatMap.put(sourceID, streamedBoat);
}
for (int i = 0; i < boat.getChildNodes().getLength(); i++) { for (int i = 0; i < boat.getChildNodes().getLength(); i++) {
Node GPSPosition = boat.getChildNodes().item(i); Node GPSPosition = boat.getChildNodes().item(i);
if (GPSPosition.getNodeName().equals("GPSposition")) readBoatPositionInformation(sourceID, GPSPosition); if (GPSPosition.getNodeName().equals("GPSposition")) readBoatPositionInformation(sourceID, GPSPosition);
}
} }
} }
@ -120,7 +132,25 @@ public class BoatXMLReader extends XMLReader {
// TODO Get relative point before implementing. (GPSposition is based off a relative point). // TODO Get relative point before implementing. (GPSposition is based off a relative point).
} }
private Map<Integer, StreamedBoat> getBoatMap() { private void makeColours() {
colours = new ArrayList<Color>(Arrays.asList(
Color.BLUEVIOLET,
Color.BLACK,
Color.RED,
Color.ORANGE,
Color.DARKOLIVEGREEN,
Color.LIMEGREEN,
Color.PURPLE,
Color.DARKGRAY,
Color.YELLOW
));
}
public void setParticipants(Map<Integer, StreamedBoat> participants) {
this.participants = participants;
}
public Map<Integer, StreamedBoat> getStreamedBoatMap() {
return streamedBoatMap; return streamedBoatMap;
} }
} }

@ -10,10 +10,18 @@ import seng302.Model.BoatInRace;
*/ */
public class StreamedBoat extends BoatInRace { public class StreamedBoat extends BoatInRace {
private int sourceID; private int sourceID;
private boolean complete;
public StreamedBoat(int sourceID, String name, Color colour, String abbrev) { public StreamedBoat(int sourceID, String name, Color colour, String abbrev) {
super(name, colour, abbrev); super(name, colour, abbrev);
this.sourceID = sourceID; this.sourceID = sourceID;
this.complete = true;
}
public StreamedBoat(int sourceID) {
super("None", Color.BLACK, "None");
this.sourceID = sourceID;
this.complete = false;
} }
@ -32,6 +40,5 @@ public class StreamedBoat extends BoatInRace {
* @param velocity of boat * @param velocity of boat
*/ */
public void setScaledVelocity(double velocity) { public void setScaledVelocity(double velocity) {
} }
} }

@ -13,13 +13,34 @@ import java.util.List;
* Created by jjg64 on 21/04/17. * Created by jjg64 on 21/04/17.
*/ */
public class StreamedCourse implements RaceDataSource { public class StreamedCourse implements RaceDataSource {
StreamedCourseXMLReader streamedCourseXMLReader; StreamedCourseXMLReader streamedCourseXMLReader = null;
BoatXMLReader boatXMLReader = null;
List<GPSCoordinate> boundary = new ArrayList<>(); List<GPSCoordinate> boundary = new ArrayList<>();
public StreamedCourse(StreamedCourseXMLReader streamedCourseXMLReader) { public StreamedCourse(StreamedCourseXMLReader streamedCourseXMLReader) {
this.streamedCourseXMLReader = streamedCourseXMLReader; this.streamedCourseXMLReader = streamedCourseXMLReader;
} }
public StreamedCourse(BoatXMLReader boatXMLReader) {
this.boatXMLReader = boatXMLReader;
}
public void setBoatXMLReader(BoatXMLReader boatXMLReader) {
this.boatXMLReader = boatXMLReader;
if (streamedCourseXMLReader != null) {
boatXMLReader.setParticipants(streamedCourseXMLReader.getParticipants());
boatXMLReader.read();
}
}
public void setStreamedCourseXMLReader(StreamedCourseXMLReader streamedCourseXMLReader) {
this.streamedCourseXMLReader = streamedCourseXMLReader;
if (streamedCourseXMLReader != null) {
boatXMLReader.setParticipants(streamedCourseXMLReader.getParticipants());
boatXMLReader.read();
}
}
public List<BoatInRace> getBoats() { public List<BoatInRace> getBoats() {
return null; return null;
} }
@ -43,7 +64,4 @@ public class StreamedCourse implements RaceDataSource {
public GPSCoordinate getMapBottomRight() { public GPSCoordinate getMapBottomRight() {
return null; return null;
} }
} }

@ -21,6 +21,7 @@ public class StreamedCourseXMLReader extends XMLReader {
private static double COORDINATEPADDING = 0.0005; private static double COORDINATEPADDING = 0.0005;
private GPSCoordinate mapTopLeft, mapBottomRight; private GPSCoordinate mapTopLeft, mapBottomRight;
private List<GPSCoordinate> boundary = new ArrayList<>(); private List<GPSCoordinate> boundary = new ArrayList<>();
private Map<Integer, StreamedBoat> participants = new HashMap<>();
Date creationTimeDate; Date creationTimeDate;
Date raceStartTime; Date raceStartTime;
int raceID; int raceID;
@ -72,8 +73,17 @@ public class StreamedCourseXMLReader extends XMLReader {
} }
private void readParticipants() { private void readParticipants() {
//TODO Modify BoatInRace to take in a sourceID Element nParticipants = (Element) doc.getElementsByTagName("Participants").item(0);
//TODO Produce list of BoatInRace for (int i = 0; i < nParticipants.getChildNodes().getLength(); i++) {
int sourceID;
Node yacht = nParticipants.getChildNodes().item(i);
if (yacht.getNodeName().equals("Yacht")) {
if (exists(yacht, "SourceID")) {
sourceID = Integer.parseInt(yacht.getAttributes().getNamedItem("SourceID").getTextContent());
participants.put(sourceID, new StreamedBoat(sourceID));
}
}
}
} }
private void readCourse() { private void readCourse() {
@ -146,4 +156,8 @@ public class StreamedCourseXMLReader extends XMLReader {
public boolean isPostpone() { public boolean isPostpone() {
return postpone; return postpone;
} }
public Map<Integer, StreamedBoat> getParticipants() {
return participants;
}
} }

@ -2,6 +2,7 @@ package seng302;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@ -33,6 +34,10 @@ public abstract class XMLReader {
return n.getElementsByTagName(tagName).item(0).getTextContent(); return n.getElementsByTagName(tagName).item(0).getTextContent();
} }
public boolean exists(Node node, String attribute) {
return node.getAttributes().getNamedItem(attribute) != null;
}
public String getAttribute(Element n, String attr) { public String getAttribute(Element n, String attr) {
return n.getAttribute(attr); return n.getAttribute(attr);
} }

@ -1,20 +1,49 @@
package seng302.Mock; package seng302.Mock;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/** /**
* Created by jjg64 on 21/04/17. * Created by jjg64 on 21/04/17.
*/ */
public class BoatsXMLTest { public class BoatsXMLTest {
BoatXMLReader boatXMLReader;
@Before
public void setup() {
try {
boatXMLReader = new BoatXMLReader("mockXML/boatXML/boatTest.xml", false);
} catch (Exception e) {
e.printStackTrace();
//fail("Cannot find mockXML/raceXML/raceTest.xml in the resources folder");
}
}
@Test
public void testInvalidParticipant() {
Map<Integer, StreamedBoat> inputParticipants = new HashMap<>();
inputParticipants.put(420, new StreamedBoat(420));
boatXMLReader.setParticipants(inputParticipants);
boatXMLReader.read();
assertEquals(boatXMLReader.getStreamedBoatMap().size(), 0);
}
@Test @Test
public void test() throws SAXException, ParserConfigurationException, ParseException, IOException { public void testValidParticipant() {
new BoatXMLReader("mockXML/boatXML/boatTest.xml"); Map<Integer, StreamedBoat> inputParticipants = new HashMap<>();
//new BoatXMLReader("mockXML/raceXML/raceTest.xml", true); inputParticipants.put(101, new StreamedBoat(101));
boatXMLReader.setParticipants(inputParticipants);
boatXMLReader.read();
assertTrue(boatXMLReader.getStreamedBoatMap().containsKey(101));
} }
} }

@ -5,8 +5,10 @@ import org.junit.Test;
import seng302.GPSCoordinate; import seng302.GPSCoordinate;
import java.util.List; import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
/** /**
@ -57,7 +59,9 @@ public class StreamedRaceTest {
} }
@Test @Test
public void testRaceSettings() { public void testParticipants() {
Map<Integer, StreamedBoat> participants = streamedCourseXMLReader.getParticipants();
assertTrue(participants.containsKey(107));
assertTrue(participants.containsKey(108));
} }
} }

Loading…
Cancel
Save