mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Started parsing the different types of XML messages to Map objects so that we can extract the relevant data for the visualizer.
#story[820]
This commit is contained in:
@@ -19,7 +19,7 @@ public class StreamPacket {
|
|||||||
this.payload = payload;
|
this.payload = payload;
|
||||||
// System.out.println("type = " + type);
|
// System.out.println("type = " + type);
|
||||||
//switch the packet type to deal with what ever specific packet you want to deal with
|
//switch the packet type to deal with what ever specific packet you want to deal with
|
||||||
if (this.type == PacketType.AVG_WIND){
|
if (this.type == PacketType.XML_MESSAGE){
|
||||||
StreamParser.parsePacket(this);
|
StreamParser.parsePacket(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ package seng302.models.parsers;
|
|||||||
|
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.InputSource;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
@@ -13,6 +16,8 @@ import java.io.IOException;
|
|||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by kre39 on 23/04/17.
|
* Created by kre39 on 23/04/17.
|
||||||
@@ -127,6 +132,7 @@ public class StreamParser {
|
|||||||
while (payloadStream.available() > 0 && (currentChar = payloadStream.read()) != 0) {
|
while (payloadStream.available() > 0 && (currentChar = payloadStream.read()) != 0) {
|
||||||
xmlMessage += (char)currentChar;
|
xmlMessage += (char)currentChar;
|
||||||
}
|
}
|
||||||
|
if (xmlMessageSubType == 6) System.out.println(xmlMessage);
|
||||||
|
|
||||||
//Create XML document Object
|
//Create XML document Object
|
||||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||||
@@ -134,7 +140,12 @@ public class StreamParser {
|
|||||||
try {
|
try {
|
||||||
db = dbf.newDocumentBuilder();
|
db = dbf.newDocumentBuilder();
|
||||||
Document doc = db.parse(new InputSource(new StringReader(xmlMessage)));
|
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.
|
switch(xmlMessageSubType) {
|
||||||
|
case 5: parseRegattaXML(doc);
|
||||||
|
case 6: parseRaceXML(doc);
|
||||||
|
case 7: parseBoatXML(doc);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (ParserConfigurationException e) {
|
} catch (ParserConfigurationException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (SAXException e) {
|
} catch (SAXException e) {
|
||||||
@@ -145,6 +156,82 @@ public class StreamParser {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void parseRegattaXML(Document doc) {
|
||||||
|
Element docEle = doc.getDocumentElement();
|
||||||
|
String[] regattaElements = {"RegattaID", "RegattaName", "CourseName", "CentralLatitude", "CentralLongitude",
|
||||||
|
"CentralAltitude", "UtcOffset", "MagneticVariation", "ShorelineName"};
|
||||||
|
Map<String, Object> outputMap = parseAtomicElements(docEle, regattaElements); // Regatta contains only atomic elements
|
||||||
|
|
||||||
|
System.out.println(outputMap);
|
||||||
|
//return outputMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void parseRaceXML(Document doc) {
|
||||||
|
Element docEle = doc.getDocumentElement();
|
||||||
|
String[] atomicRaceElements = {"RaceID", "RaceType", "CreationTimeDate"};
|
||||||
|
Map<String, Object> outputMap = parseAtomicElements(docEle, atomicRaceElements);
|
||||||
|
|
||||||
|
//Race Start Time
|
||||||
|
Map<String, Object> raceStartMap = new HashMap<>();
|
||||||
|
Node raceStartTime = docEle.getElementsByTagName("RaceStartTime").item(0);
|
||||||
|
raceStartMap.put("Start", getNodeNamedAttribute(raceStartTime, "Start"));
|
||||||
|
raceStartMap.put("Postpone", getNodeNamedAttribute(raceStartTime, "Postpone"));
|
||||||
|
outputMap.put("RaceStartTime", raceStartMap);
|
||||||
|
|
||||||
|
//participants
|
||||||
|
Map<Integer, String> participantMap = new HashMap<>();
|
||||||
|
NodeList participants = docEle.getElementsByTagName("Participants").item(0).getChildNodes();
|
||||||
|
for (int i = 0; i < participants.getLength(); i++) {
|
||||||
|
Integer sourceID = null;
|
||||||
|
String entry = null;
|
||||||
|
Node participant = participants.item(i);
|
||||||
|
if (participant.getNodeName().equals("Yacht")) {
|
||||||
|
//sourceID = Integer.parseInt(participant.getAttributes().getNamedItem("SourceID").getTextContent());
|
||||||
|
sourceID = Integer.parseInt(getNodeNamedAttribute(participant, "SourceID"));
|
||||||
|
if (participant.getAttributes().getLength() == 2) {
|
||||||
|
entry = getNodeNamedAttribute(participant, "Entry");
|
||||||
|
}
|
||||||
|
participantMap.put(sourceID, entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outputMap.put("Participants", participantMap);
|
||||||
|
|
||||||
|
//Course
|
||||||
|
|
||||||
|
System.out.println(outputMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void parseBoatXML(Document doc) {
|
||||||
|
// TODO: 27/04/17 ajm412
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getNodeNamedAttribute(Node n, String attr) {
|
||||||
|
return n.getAttributes().getNamedItem(attr).getTextContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> parseAtomicElements(Element docEle, String[] elements) {
|
||||||
|
Map <String, Object> outputMap = new HashMap<>();
|
||||||
|
for (String element : elements) {
|
||||||
|
Object elementValue = null;
|
||||||
|
if (docEle.getElementsByTagName(element).getLength() == 1) {
|
||||||
|
String elementText = docEle.getElementsByTagName(element).item(0).getTextContent();
|
||||||
|
// TODO: 27/04/17 ajm412: this seems messy, trying to parse values as ints/doubles to the map rather than as Strings. Possibly use RegEx.
|
||||||
|
try {
|
||||||
|
elementValue = Integer.parseInt(elementText);
|
||||||
|
} catch (NumberFormatException nfe1) {
|
||||||
|
try {
|
||||||
|
elementValue = Double.parseDouble(elementText);
|
||||||
|
} catch (NumberFormatException nfe2) {
|
||||||
|
elementValue = elementText;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outputMap.put(element, elementValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputMap;
|
||||||
|
}
|
||||||
|
|
||||||
private static void extractRaceStartStatus(StreamPacket packet){
|
private static void extractRaceStartStatus(StreamPacket packet){
|
||||||
byte[] payload = packet.getPayload();
|
byte[] payload = packet.getPayload();
|
||||||
int messageVersionNo = payload[0];
|
int messageVersionNo = payload[0];
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.concurrent.PriorityBlockingQueue;
|
import java.util.concurrent.PriorityBlockingQueue;
|
||||||
import java.util.zip.CRC32;
|
import java.util.zip.CRC32;
|
||||||
|
|||||||
Reference in New Issue
Block a user