mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user