mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Began fixing bugs with caused by asynchronous listener calls.
#bug
This commit is contained in:
@@ -1,159 +0,0 @@
|
||||
package seng302.model.stream.xml.generator;
|
||||
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import seng302.server.messages.XMLMessageSubType;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* An XML generator to generate the Race, Boat, and Regatta XML dynamically
|
||||
*/
|
||||
public class XMLGenerator {
|
||||
private static final String XML_TEMPLATE_DIR = "/server_config/xml_templates";
|
||||
private static final String REGATTA_TEMPLATE_NAME = "regatta.ftlh";
|
||||
private static final String BOATS_TEMPLATE_NAME = "boats.ftlh";
|
||||
private static final String RACE_TEMPLATE_NAME = "race.ftlh";
|
||||
private Configuration configuration;
|
||||
private Regatta regatta;
|
||||
private Race race;
|
||||
|
||||
/**
|
||||
* Set up a configuration instance for Apache Freemake
|
||||
*/
|
||||
private void setupConfiguration() {
|
||||
configuration = new Configuration(Configuration.VERSION_2_3_26);
|
||||
|
||||
try {
|
||||
configuration.setClassForTemplateLoading(getClass(), XML_TEMPLATE_DIR);
|
||||
} catch (NullPointerException e){
|
||||
System.out.println("[FATAL] Server could not load XML Template directory, ensure this directory isn't empty");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of the XML Generator
|
||||
*/
|
||||
public XMLGenerator(){
|
||||
setupConfiguration();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the race regatta to send to players
|
||||
* Note: This must be set before a regatta message can be generated
|
||||
* @param regatta The race regatta
|
||||
*/
|
||||
public void setRegatta(Regatta regatta){
|
||||
this.regatta = regatta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the race to send to players
|
||||
* Note: This must be set before a boat or race message can be generated
|
||||
* @param race The race
|
||||
*/
|
||||
public void setRace(Race race){
|
||||
this.race = race;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse an XML template and generate the output as a string
|
||||
* @param templateName The templates file name
|
||||
* @param type The XML message sub type
|
||||
*/
|
||||
private String parseToXmlString(String templateName, XMLMessageSubType type) throws IOException, TemplateException {
|
||||
Template template;
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
OutputStreamWriter writer = new OutputStreamWriter(os);
|
||||
|
||||
template = configuration.getTemplate(templateName);
|
||||
|
||||
switch (type) {
|
||||
case REGATTA:
|
||||
template.process(regatta, writer);
|
||||
break;
|
||||
|
||||
case BOAT:
|
||||
template.process(race, writer);
|
||||
break;
|
||||
|
||||
case RACE:
|
||||
template.process(race, writer);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
try {
|
||||
return os.toString("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
System.out.println("[FATAL] UTF-8 Not supported");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the race regatta as a string
|
||||
* Note: Regatta must be set before calling this
|
||||
* @return String containing the regatta XML, null if there was an error
|
||||
*/
|
||||
public String getRegattaAsXml(){
|
||||
String result = null;
|
||||
|
||||
if (regatta == null) return null;
|
||||
|
||||
try {
|
||||
result = parseToXmlString(REGATTA_TEMPLATE_NAME, XMLMessageSubType.REGATTA);
|
||||
} catch (TemplateException e) {
|
||||
System.out.println("[FATAL] Error parsing regatta");
|
||||
} catch (IOException e) {
|
||||
System.out.println("[FATAL] Error reading regatta");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the boats XML as a string
|
||||
* Note: Race must be set before calling this
|
||||
* @return String containing the boats XML, null if there was an error
|
||||
*/
|
||||
public String getBoatsAsXml() {
|
||||
String result = null;
|
||||
|
||||
if (race == null) return null;
|
||||
|
||||
try {
|
||||
result = parseToXmlString(BOATS_TEMPLATE_NAME, XMLMessageSubType.BOAT);
|
||||
} catch (TemplateException e) {
|
||||
System.out.println("[FATAL] Error parsing boats");
|
||||
} catch (IOException e) {
|
||||
System.out.println("[FATAL] Error reading boats");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the race XML as a string
|
||||
* Note: Race must be set before calling this
|
||||
* @return String containing the race XML, null if there was an error
|
||||
*/
|
||||
public String getRaceAsXml() {
|
||||
String result = null;
|
||||
|
||||
if (race == null) return null;
|
||||
|
||||
try {
|
||||
result = parseToXmlString(RACE_TEMPLATE_NAME, XMLMessageSubType.RACE);
|
||||
} catch (TemplateException e) {
|
||||
System.out.println("[FATAL] Error parsing race");
|
||||
} catch (IOException e) {
|
||||
System.out.println("[FATAL] Error reading race");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ public class RaceXMLData {
|
||||
private List<Limit> courseLimit;
|
||||
private Map<Integer, Mark> individualMarks;
|
||||
|
||||
RaceXMLData(List<Integer> participants, List<Mark> compoundMarks, List<Corner> markSequence,
|
||||
public RaceXMLData(List<Integer> participants, List<Mark> compoundMarks, List<Corner> markSequence,
|
||||
List<Limit> courseLimit) {
|
||||
this.participants = participants;
|
||||
this.markSequence = markSequence;
|
||||
|
||||
@@ -12,7 +12,7 @@ public class RegattaXMLData {
|
||||
private Double centralLng;
|
||||
private Integer utcOffset;
|
||||
|
||||
RegattaXMLData (Integer regattaID, String regattaName, String courseName,
|
||||
public RegattaXMLData (Integer regattaID, String regattaName, String courseName,
|
||||
Double centralLat, Double centralLng, Integer utcOffset) {
|
||||
this.regattaID = regattaID;
|
||||
this.regattaName = regattaName;
|
||||
|
||||
@@ -1,290 +0,0 @@
|
||||
package seng302.model.stream.xml.parser;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.model.Corner;
|
||||
import seng302.model.Limit;
|
||||
import seng302.model.mark.GateMark;
|
||||
import seng302.model.mark.Mark;
|
||||
import seng302.model.mark.MarkType;
|
||||
import seng302.model.mark.SingleMark;
|
||||
|
||||
/**
|
||||
* Utilities for parsing XML documents
|
||||
*/
|
||||
public class XMLParser {
|
||||
|
||||
/**
|
||||
* Returns the text content of a given child element tag, assuming it exists, as an Integer.
|
||||
*
|
||||
* @param ele Document Element with child elements.
|
||||
* @param tag Tag to find in document elements child elements.
|
||||
* @return Text content from tag if found, null otherwise.
|
||||
*/
|
||||
private static Integer getElementInt(Element ele, String tag) {
|
||||
NodeList tagList = ele.getElementsByTagName(tag);
|
||||
if (tagList.getLength() > 0) {
|
||||
return Integer.parseInt(tagList.item(0).getTextContent());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the text content of a given child element tag, assuming it exists, as an String.
|
||||
*
|
||||
* @param ele Document Element with child elements.
|
||||
* @param tag Tag to find in document elements child elements.
|
||||
* @return Text content from tag if found, null otherwise.
|
||||
*/
|
||||
private static String getElementString(Element ele, String tag) {
|
||||
NodeList tagList = ele.getElementsByTagName(tag);
|
||||
if (tagList.getLength() > 0) {
|
||||
return tagList.item(0).getTextContent();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the text content of a given child element tag, assuming it exists, as a Double.
|
||||
*
|
||||
* @param ele Document Element with child elements.
|
||||
* @param tag Tag to find in document elements child elements.
|
||||
* @return Text content from tag if found, null otherwise.
|
||||
*/
|
||||
private static Double getElementDouble(Element ele, String tag) {
|
||||
NodeList tagList = ele.getElementsByTagName(tag);
|
||||
if (tagList.getLength() > 0) {
|
||||
return Double.parseDouble(tagList.item(0).getTextContent());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the text content of an attribute of a given Node, assuming it exists, as a String.
|
||||
*
|
||||
* @param n A node object that should have some attributes
|
||||
* @param attr The attribute you want to get from the given node.
|
||||
* @return The String representation of the text content of an attribute in the given node, else
|
||||
* returns null.
|
||||
*/
|
||||
private static String getNodeAttributeString(Node n, String attr) {
|
||||
Node attrItem = n.getAttributes().getNamedItem(attr);
|
||||
if (attrItem != null) {
|
||||
return attrItem.getTextContent();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the text content of an attribute of a given Node, assuming it exists, as an Integer.
|
||||
*
|
||||
* @param n A node object that should have some attributes
|
||||
* @param attr The attribute you want to get from the given node.
|
||||
* @return The Integer representation of the text content of an attribute in the given node,
|
||||
* else returns null.
|
||||
*/
|
||||
private static Integer getNodeAttributeInt(Node n, String attr) {
|
||||
Node attrItem = n.getAttributes().getNamedItem(attr);
|
||||
if (attrItem != null) {
|
||||
return Integer.parseInt(attrItem.getTextContent());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the text content of an attribute of a given Node, assuming it exists, as a Double.
|
||||
*
|
||||
* @param n A node object that should have some attributes
|
||||
* @param attr The attribute you want to get from the given node.
|
||||
* @return The Double representation of the text content of an attribute in the given node, else
|
||||
* returns null.
|
||||
*/
|
||||
private static Double getNodeAttributeDouble(Node n, String attr) {
|
||||
Node attrItem = n.getAttributes().getNamedItem(attr);
|
||||
if (attrItem != null) {
|
||||
return Double.parseDouble(attrItem.getTextContent());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Produces a mapping of boat sourceIDS to boat objects created from the given xml document.
|
||||
* @param doc XML Document Object
|
||||
* @return Mapping of sourceIds to Boats.
|
||||
*/
|
||||
public static Map<Integer, Yacht> parseBoats(Document doc){
|
||||
Map<Integer, Yacht> competingBoats = new HashMap<>();
|
||||
|
||||
Element docEle = doc.getDocumentElement();
|
||||
|
||||
NodeList boatsList = docEle.getElementsByTagName("Boats").item(0).getChildNodes();
|
||||
for (int i = 0; i < boatsList.getLength(); i++) {
|
||||
Node currentBoat = boatsList.item(i);
|
||||
if (currentBoat.getNodeName().equals("Boat")) {
|
||||
// Boat boat = new Boat(currentBoat);
|
||||
Yacht yacht = new Yacht(XMLParser.getNodeAttributeString(currentBoat, "Type"),
|
||||
XMLParser.getNodeAttributeInt(currentBoat, "SourceID"),
|
||||
XMLParser.getNodeAttributeString(currentBoat, "HullNum"),
|
||||
XMLParser.getNodeAttributeString(currentBoat, "ShortName"),
|
||||
XMLParser.getNodeAttributeString(currentBoat, "BoatName"),
|
||||
XMLParser.getNodeAttributeString(currentBoat, "Country"));
|
||||
if (yacht.getBoatType().equals("Yacht")) {
|
||||
competingBoats.put(yacht.getSourceId(), yacht);
|
||||
}
|
||||
}
|
||||
}
|
||||
return competingBoats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an object containing the data extracted from the given xml formatted document
|
||||
*
|
||||
* @param doc XML Document Object
|
||||
* @return Object containing regatta data
|
||||
*/
|
||||
public static RegattaXMLData parseRegatta(Document doc) {
|
||||
|
||||
Element docEle = doc.getDocumentElement();
|
||||
Integer regattaID = XMLParser.getElementInt(docEle, "RegattaID");
|
||||
String regattaName = XMLParser.getElementString(docEle, "RegattaName");
|
||||
String courseName = XMLParser.getElementString(docEle, "CourseName");
|
||||
Double centralLat = XMLParser.getElementDouble(docEle, "CentralLatitude");
|
||||
Double centralLng = XMLParser.getElementDouble(docEle, "CentralLongitude");
|
||||
Integer utcOffset = XMLParser.getElementInt(docEle, "UtcOffset");
|
||||
return new RegattaXMLData(
|
||||
regattaID, regattaName, courseName, centralLat, centralLng, utcOffset
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an object containing the data extracted from the given xml formatted document
|
||||
*
|
||||
* @param doc XML document
|
||||
* @return object containing race data
|
||||
*/
|
||||
public static RaceXMLData parseRace(Document doc) {
|
||||
Element docEle = doc.getDocumentElement();
|
||||
return new RaceXMLData(
|
||||
extractParticpantIDs(docEle),
|
||||
extractCompoundMarks(docEle),
|
||||
extractMarkOrder(docEle),
|
||||
extractCourseLimit(docEle)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts course limit data
|
||||
*/
|
||||
private static List<Limit> extractCourseLimit(Element docEle) {
|
||||
List<Limit> courseLimit = new ArrayList<>();
|
||||
NodeList limitList = docEle.getElementsByTagName("CourseLimit").item(0).getChildNodes();
|
||||
for (int i = 0; i < limitList.getLength(); i++) {
|
||||
Node limitNode = limitList.item(i);
|
||||
if (limitNode.getNodeName().equals("Limit")) {
|
||||
courseLimit.add(
|
||||
new Limit(
|
||||
XMLParser.getNodeAttributeInt(limitNode, "SeqID"),
|
||||
XMLParser.getNodeAttributeDouble(limitNode, "Lat"),
|
||||
XMLParser.getNodeAttributeDouble(limitNode, "lon")
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
return courseLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts course order data
|
||||
*/
|
||||
private static List<Corner> extractMarkOrder (Element docEle) {
|
||||
List<Corner> compoundMarkSequence = new ArrayList<>();
|
||||
NodeList cornerList = docEle.getElementsByTagName("CompoundMarkSequence").item(0)
|
||||
.getChildNodes();
|
||||
for (int i = 0; i < cornerList.getLength(); i++) {
|
||||
Node cornerNode = cornerList.item(i);
|
||||
if (cornerNode.getNodeName().equals("Corner")) {
|
||||
compoundMarkSequence.add(
|
||||
new Corner(
|
||||
XMLParser.getNodeAttributeInt(cornerNode, "SeqID"),
|
||||
XMLParser.getNodeAttributeInt(cornerNode, "CompoundMarkID"),
|
||||
XMLParser.getNodeAttributeString(cornerNode, "Rounding"),
|
||||
XMLParser.getNodeAttributeInt(cornerNode, "ZoneSize")
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
return compoundMarkSequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts course participants data
|
||||
*/
|
||||
private static List<Integer> extractParticpantIDs (Element docEle) {
|
||||
List<Integer> boatIDs = new ArrayList<>();
|
||||
NodeList pList = docEle.getElementsByTagName("Participants").item(0).getChildNodes();
|
||||
for (int i = 0; i < pList.getLength(); i++) {
|
||||
Node pNode = pList.item(i);
|
||||
if (pNode.getNodeName().equals("Yacht")) {
|
||||
boatIDs.add(XMLParser.getNodeAttributeInt(pNode, "SourceID"));
|
||||
}
|
||||
}
|
||||
return boatIDs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts course mark data
|
||||
*/
|
||||
private static List<Mark> extractCompoundMarks(Element docEle) {
|
||||
List<Mark> allMarks = new ArrayList<>();
|
||||
NodeList cMarkList = docEle.getElementsByTagName("Course").item(0).getChildNodes();
|
||||
for (int i = 0; i < cMarkList.getLength(); i++) {
|
||||
Node cMarkNode = cMarkList.item(i);
|
||||
if (cMarkNode.getNodeName().equals("CompoundMark")) {
|
||||
allMarks.add(createMark(cMarkNode));
|
||||
}
|
||||
}
|
||||
return allMarks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates marks objects from the given node
|
||||
*/
|
||||
private static Mark createMark(Node compoundMark) {
|
||||
List<SingleMark> subMarks = new ArrayList<>();
|
||||
Integer compoundMarkID = XMLParser.getNodeAttributeInt(compoundMark, "CompoundMarkID");
|
||||
String cMarkName = XMLParser.getNodeAttributeString(compoundMark, "Name");
|
||||
|
||||
NodeList childMarks = compoundMark.getChildNodes();
|
||||
for (int i = 0; i < childMarks.getLength(); i++) {
|
||||
Node markNode = childMarks.item(i);
|
||||
if (markNode.getNodeName().equals("Mark")) {
|
||||
Integer sourceID = XMLParser.getNodeAttributeInt(markNode, "SourceID");
|
||||
String markName = XMLParser.getNodeAttributeString(markNode, "Name");
|
||||
Double targetLat = XMLParser.getNodeAttributeDouble(markNode, "TargetLat");
|
||||
Double targetLng = XMLParser.getNodeAttributeDouble(markNode, "TargetLng");
|
||||
SingleMark mark = new SingleMark(markName, targetLat, targetLng, sourceID,
|
||||
compoundMarkID);
|
||||
subMarks.add(mark);
|
||||
}
|
||||
}
|
||||
if (subMarks.size() == 1) {
|
||||
return subMarks.get(0);
|
||||
} else {
|
||||
return new GateMark( cMarkName, MarkType.OPEN_GATE, subMarks.get(0), subMarks.get(1),
|
||||
subMarks.get(0).getLatitude(), subMarks.get(0).getLongitude(), compoundMarkID
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user