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
@@ -1,16 +1,22 @@
package seng302.controllers;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.*;
import javafx.scene.paint.Color;
import seng302.models.Boat;
import seng302.models.parsers.StreamPacket;
import seng302.models.parsers.StreamParser;
@@ -33,6 +39,14 @@ public class Controller implements Initializable {
private Button streamButton;
@FXML
private Button switchToRaceViewButton;
@FXML
private TableView teamList;
@FXML
private TableColumn boatNameCol;
@FXML
private TableColumn shortNameCol;
@FXML
private TableColumn countryCol;
private void setContentPane(String jfxUrl){
try{
@@ -72,6 +86,7 @@ public class Controller implements Initializable {
timeTillLive.setText("Race finished! Waiting for new race...");
switchToRaceViewButton.setDisable(true);
} else if (StreamParser.getTimeSinceStart() > 0 && StreamParser.getTimeSinceStart() % 10 == 0) {
updateTeamList();
timeTillLive.setTextFill(Color.RED);
switchToRaceViewButton.setDisable(false);
Long timerMinute = StreamParser.getTimeSinceStart() / 60;
@@ -79,6 +94,7 @@ public class Controller implements Initializable {
String timerString = "-" + timerMinute + "." + timerSecond + " minutes";
timeTillLive.setText(timerString);
} else if (StreamParser.getTimeSinceStart() % 10 == 0) {
updateTeamList();
timeTillLive.setTextFill(Color.BLACK);
switchToRaceViewButton.setDisable(false);
Long timerMinute = -1 * StreamParser.getTimeSinceStart() / 60;
@@ -98,4 +114,21 @@ public class Controller implements Initializable {
public void switchToRaceView() {
setContentPane("/views/RaceView.fxml");
}
private void updateTeamList() {
ObservableList<Boat> data = FXCollections.observableArrayList();
teamList.setItems(data);
boatNameCol.setCellValueFactory(
new PropertyValueFactory<Boat,String>("boatName")
);
shortNameCol.setCellValueFactory(
new PropertyValueFactory<Boat,String>("shortName")
);
countryCol.setCellValueFactory(
new PropertyValueFactory<Boat,String>("country")
);
for (Boat boat : StreamParser.getBoats()) {
data.add(boat);
}
}
}
+30
View File
@@ -21,6 +21,10 @@ public class Boat {
private int markLastPast;
private String shortName;
private int id;
// new attributes to boat
private int sourceID;
private String boatName;
private String country;
public Boat(String teamName) {
this.teamName = teamName;
@@ -45,6 +49,21 @@ public class Boat {
this.id = id;
}
/**
* New instance created by BoatsParser.
*
* @param sourceID source ID of the boat
* @param boatName full name of the boat
* @param shortName short name of the boat
* @param country country of the boat
*/
public Boat(int sourceID, String boatName, String shortName, String country) {
this.sourceID = sourceID;
this.boatName = boatName;
this.shortName = shortName;
this.country = country;
}
/**
* Returns the name of the team sailing the boat
*
@@ -141,4 +160,15 @@ public class Boat {
return id;
}
public int getSourceID() {
return sourceID;
}
public String getBoatName() {
return boatName;
}
public String getCountry() {
return country;
}
}
@@ -0,0 +1,77 @@
package seng302.models.parsers;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import seng302.models.Boat;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
/**
* Created by ryan_ on 30/04/2017.
*/
public class BoatsParser extends FileParser {
private Document doc;
public BoatsParser(String xmlString) {
this.doc = this.parseFile(xmlString);
}
/**
* Create a boat instance from a given node if 'Type' is 'Yacht'
*
* @param node a boat node
* @return an instance of Boat
*/
private Boat parseBoat(Node node) {
try {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
if (element.getAttribute("Type").equals("Yacht")) {
String sourceID = element.getAttribute("SourceID");
String boatName = element.getAttribute("BoatName");
String shortName = element.getAttribute("ShortName");
String stoweName = element.getAttribute("StoweName");
String country = element.getAttribute("Country");
Boat boat = new Boat(Integer.parseInt(sourceID), boatName, shortName, country);
return boat;
}
} else {
throw new NoSuchElementException("Cannot generate a boat by given node");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Returns a list of boats from the xml.
*
* @return a list of boats
*/
public List<Boat> getBoats() {
ArrayList<Boat> boats = new ArrayList<>();
try {
NodeList nodes = this.doc.getElementsByTagName("Boat");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
Boat boat = parseBoat(node);
if (!(boat == null)) {
boats.add(boat);
}
}
return boats;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
@@ -1,12 +1,14 @@
package seng302.models.parsers;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
/**
* Created by Haoming Yin (hyi25) on 16/3/2017
@@ -15,6 +17,8 @@ public abstract class FileParser {
private String filePath;
public FileParser() {}
public FileParser(String path) {
this.filePath = path;
}
@@ -32,6 +36,19 @@ public abstract class FileParser {
e.printStackTrace();
return null;
}
}
protected Document parseFile(String xmlString) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
// optional, in order to recover info from broken line.
doc.getDocumentElement().normalize();
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
@@ -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;
}
}