Created BoatsParser.java to parse boats from server boat.xml and created a table on the start screen to display all the teams from server

#story[572]
This commit is contained in:
Zhi You Tan
2017-04-30 17:17:47 +12:00
parent f3ee618900
commit 25038da2a1
6 changed files with 203 additions and 15 deletions
@@ -5,6 +5,7 @@ import javafx.geometry.Point3D;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import seng302.models.Boat;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -35,6 +36,7 @@ public class StreamParser extends Thread{
private static boolean raceFinished = false;
private static boolean streamStatus = false;
private static long timeSinceStart = -1;
private static List<Boat> boats = new ArrayList<>();
public StreamParser(String threadName){
this.threadName = threadName;
@@ -221,19 +223,25 @@ public class StreamParser extends Thread{
//Converts XML message to string to be parsed
int currentChar;
while (payloadStream.available() > 0 && (currentChar = payloadStream.read()) != 0) {
xmlMessage += (char)currentChar;
xmlMessage += (char)currentChar;
}
// Parse boat xml from server
if (xmlMessageSubType == 7) {
BoatsParser boatsParser = new BoatsParser(xmlMessage);
boats = boatsParser.getBoats();
}
//Create XML document Object
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xmlMessage)));
// TODO: 25/04/17 ajm412: Check that the object matches expected structure and return Document object.
} catch (ParserConfigurationException | IOException | SAXException e) {
e.printStackTrace();
}
// DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// DocumentBuilder db = null;
// try {
// db = dbf.newDocumentBuilder();
// Document doc = db.parse(new InputSource(new StringReader(xmlMessage)));
// // TODO: 25/04/17 ajm412: Check that the object matches expected structure and return Document object.
// } catch (ParserConfigurationException | IOException | SAXException e) {
// e.printStackTrace();
// }
}
@@ -444,5 +452,14 @@ public class StreamParser extends Thread{
public static boolean isRaceFinished() {
return raceFinished;
}
/**
* return list of boats from the server
*
* @return list of boats
*/
public static List<Boat> getBoats() {
return boats;
}
}