mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Began figuring out how to implement XML data in place of mock data.
#story[820] #pair[ajm412, wmu16]
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package seng302.models.parsers;
|
||||
|
||||
|
||||
import com.sun.deploy.util.StringUtils;
|
||||
import javafx.geometry.Point3D;
|
||||
import jdk.internal.util.xml.impl.Pair;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
@@ -33,6 +35,7 @@ public class StreamParser extends Thread{
|
||||
private String threadName;
|
||||
private Thread t;
|
||||
private static boolean raceStarted = false;
|
||||
public static XMLParser xmlObject;
|
||||
private static boolean raceFinished = false;
|
||||
private static boolean streamStatus = false;
|
||||
private static long timeSinceStart = -1;
|
||||
@@ -50,6 +53,7 @@ public class StreamParser extends Thread{
|
||||
try {
|
||||
System.out.println("START OF STREAM");
|
||||
streamStatus = true;
|
||||
xmlObject = new XMLParser();
|
||||
while (StreamReceiver.packetBuffer == null || StreamReceiver.packetBuffer.size() < 1) {
|
||||
Thread.sleep(1);
|
||||
}
|
||||
@@ -220,41 +224,24 @@ public class StreamParser extends Thread{
|
||||
private static void extractXmlMessage(StreamPacket packet){
|
||||
|
||||
byte[] payload = packet.getPayload();
|
||||
String xmlMessage = "";
|
||||
|
||||
ByteArrayInputStream payloadStream = new ByteArrayInputStream(payload);
|
||||
|
||||
//Bunch of data we don't want (Message Version Number, AckNumber, Timestamp)
|
||||
payloadStream.skip(9);
|
||||
int xmlMessageSubType = payloadStream.read();
|
||||
payloadStream.skip(2);
|
||||
|
||||
//checks the length of the xml message itself
|
||||
int xmlMessageLength = payloadStream.read() | payloadStream.read() << 8;
|
||||
|
||||
//Converts XML message to string to be parsed
|
||||
int currentChar;
|
||||
while (payloadStream.available() > 0 && (currentChar = payloadStream.read()) != 0) {
|
||||
xmlMessage += (char)currentChar;
|
||||
}
|
||||
|
||||
// Parse boat xml from server
|
||||
if (xmlMessageSubType == 7) {
|
||||
BoatsParser boatsParser = new BoatsParser(xmlMessage);
|
||||
boats = boatsParser.getBoats();
|
||||
}
|
||||
int messageType = payload[9];
|
||||
long messagelength = bytesToLong(Arrays.copyOfRange(payload,12,14));
|
||||
String xmlMessage = new String((Arrays.copyOfRange(payload,14,(int) (14 + messagelength)))).trim();
|
||||
//System.out.println("xmlMessage2 = " + xmlMessage);
|
||||
|
||||
//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;
|
||||
Document doc = null;
|
||||
try {
|
||||
db = dbf.newDocumentBuilder();
|
||||
doc = db.parse(new InputSource(new StringReader(xmlMessage)));
|
||||
} catch (ParserConfigurationException | IOException | SAXException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
xmlObject.constructXML(doc, messageType);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -473,5 +460,10 @@ public class StreamParser extends Thread{
|
||||
public static List<Boat> getBoats() {
|
||||
return boats;
|
||||
}
|
||||
|
||||
|
||||
public static XMLParser getXmlObject() {
|
||||
return xmlObject;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,35 +9,51 @@ import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Class to create an XML object from the XML Packet Messages.
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* Document doc; // some xml document
|
||||
* Integer xmlMessageType; // an Integer of value 5, 6, 7
|
||||
*
|
||||
* xmlP = new XMLParser(doc, xmlMessageType);
|
||||
* RegattaXMLObject rXmlObj = xmlP.createRegattaXML(); // creates a regattaXML object.
|
||||
*
|
||||
*/
|
||||
class XMLParser {
|
||||
public class XMLParser {
|
||||
|
||||
/**
|
||||
* Creates a Regatta XML Object from the data in a Regatta XML Message
|
||||
* @param doc XML Document Object
|
||||
* @return A new RegattaXMLObject from the input Document.
|
||||
*/
|
||||
RegattaXMLObject createRegattaXML(Document doc) {
|
||||
return new RegattaXMLObject(doc);
|
||||
private Document xmlDoc;
|
||||
|
||||
private RaceXMLObject raceXML;
|
||||
private RegattaXMLObject regattaXML;
|
||||
private BoatXMLObject boatXML;
|
||||
|
||||
public XMLParser() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Race XML Object from the data in a Regatta XML Message
|
||||
* @param doc XML Document Object
|
||||
* @return A new RaceXMLObject from the input Document.
|
||||
* Constructor for XMLParser
|
||||
* @param doc Document to create XML object.
|
||||
* @param messageType Defines if a message is a RegattaXML(5), RaceXML(6), BoatXML(7).
|
||||
*/
|
||||
RaceXMLObject createRaceXML(Document doc) {
|
||||
return new RaceXMLObject(doc);
|
||||
public void constructXML(Document doc, Integer messageType) {
|
||||
this.xmlDoc = doc;
|
||||
switch (messageType) {
|
||||
case 5:
|
||||
regattaXML = new RegattaXMLObject(this.xmlDoc);
|
||||
break;
|
||||
case 6:
|
||||
raceXML = new RaceXMLObject(this.xmlDoc);
|
||||
break;
|
||||
case 7:
|
||||
boatXML = new BoatXMLObject(this.xmlDoc);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Boat XML Object from the data in a Regatta XML Message
|
||||
* @param doc XML Document Object
|
||||
* @return A new BoatXMLObject from the input Document.
|
||||
*/
|
||||
BoatXMLObject createBoatXML(Document doc) {
|
||||
return new BoatXMLObject(doc);
|
||||
}
|
||||
public RaceXMLObject getRaceXML() { return raceXML; }
|
||||
public RegattaXMLObject getRegattaXML() { return regattaXML; }
|
||||
public BoatXMLObject getBoatXML() { return boatXML; }
|
||||
|
||||
|
||||
/**
|
||||
* Returns the text content of a given child element tag, assuming it exists, as an Integer.
|
||||
@@ -129,7 +145,7 @@ class XMLParser {
|
||||
}
|
||||
}
|
||||
|
||||
class RegattaXMLObject {
|
||||
public class RegattaXMLObject {
|
||||
//Regatta Info
|
||||
private Integer regattaID;
|
||||
private String regattaName;
|
||||
@@ -163,7 +179,7 @@ class XMLParser {
|
||||
|
||||
}
|
||||
|
||||
class RaceXMLObject {
|
||||
public class RaceXMLObject {
|
||||
|
||||
// Race Info
|
||||
private Integer raceID;
|
||||
@@ -266,7 +282,7 @@ class XMLParser {
|
||||
public ArrayList<Corner> getCompoundMarkSequence() { return compoundMarkSequence; }
|
||||
public ArrayList<Limit> getCourseLimit() { return courseLimit; }
|
||||
|
||||
class Participant {
|
||||
public class Participant {
|
||||
Integer sourceID;
|
||||
String entry;
|
||||
|
||||
@@ -279,7 +295,7 @@ class XMLParser {
|
||||
public String getEntry() { return entry; }
|
||||
}
|
||||
|
||||
class CompoundMark {
|
||||
public class CompoundMark {
|
||||
private Integer markID;
|
||||
private String cMarkName;
|
||||
private ArrayList<Mark> marks;
|
||||
@@ -302,7 +318,7 @@ class XMLParser {
|
||||
public String getcMarkName() { return cMarkName; }
|
||||
public ArrayList<Mark> getMarks() { return marks; }
|
||||
|
||||
class Mark {
|
||||
public class Mark {
|
||||
private Integer seqID;
|
||||
private Integer sourceID;
|
||||
private String markName;
|
||||
@@ -310,13 +326,11 @@ class XMLParser {
|
||||
private Double targetLng;
|
||||
|
||||
Mark(Node markNode) {
|
||||
|
||||
this.seqID = getNodeAttributeInt(markNode, "SeqID");
|
||||
this.sourceID = getNodeAttributeInt(markNode, "SourceID");
|
||||
this.markName = getNodeAttributeString(markNode, "Name");
|
||||
this.targetLat = getNodeAttributeDouble(markNode, "TargetLat");
|
||||
this.targetLng = getNodeAttributeDouble(markNode, "TargetLng");
|
||||
|
||||
}
|
||||
|
||||
public Integer getSeqID() { return seqID; }
|
||||
@@ -327,7 +341,7 @@ class XMLParser {
|
||||
}
|
||||
}
|
||||
|
||||
class Corner {
|
||||
public class Corner {
|
||||
private Integer seqID;
|
||||
private Integer compoundMarkID;
|
||||
private String rounding;
|
||||
@@ -346,7 +360,7 @@ class XMLParser {
|
||||
public Integer getZoneSize() { return zoneSize; }
|
||||
}
|
||||
|
||||
class Limit {
|
||||
public class Limit {
|
||||
private Integer seqID;
|
||||
private Double lat;
|
||||
private Double lng;
|
||||
@@ -364,7 +378,7 @@ class XMLParser {
|
||||
|
||||
}
|
||||
|
||||
class BoatXMLObject {
|
||||
public class BoatXMLObject {
|
||||
|
||||
private String lastModified;
|
||||
private Integer version;
|
||||
@@ -429,7 +443,7 @@ class XMLParser {
|
||||
public ArrayList<Double> getZoneLimits() { return zoneLimits; }
|
||||
public ArrayList<Boat> getBoats() { return boats; }
|
||||
|
||||
class Boat {
|
||||
public class Boat {
|
||||
|
||||
private String boatType;
|
||||
private Integer sourceID;
|
||||
|
||||
Reference in New Issue
Block a user