Moved boat xml creation from Event class to BoatData class
#story[778]
This commit is contained in:
@@ -14,23 +14,6 @@ public class Constants {
|
|||||||
//Knots x this = meters per second.
|
//Knots x this = meters per second.
|
||||||
public static final double KnotsToMetersPerSecondConversionFactor = 0.514444;
|
public static final double KnotsToMetersPerSecondConversionFactor = 0.514444;
|
||||||
|
|
||||||
public static final GPSCoordinate startLineMarker1 = new GPSCoordinate(32.296577, -64.854304);
|
|
||||||
public static final GPSCoordinate startLineMarker2 = new GPSCoordinate(32.293771, -64.855242);
|
|
||||||
public static final GPSCoordinate mark1 = new GPSCoordinate(32.293039, -64.843983);
|
|
||||||
public static final GPSCoordinate windwardGate1 = new GPSCoordinate(32.284680, -64.850045);
|
|
||||||
public static final GPSCoordinate windwardGate2 = new GPSCoordinate(32.280164, -64.847591);
|
|
||||||
public static final GPSCoordinate leewardGate1 = new GPSCoordinate(32.309693, -64.835249);
|
|
||||||
public static final GPSCoordinate leewardGate2 = new GPSCoordinate(32.308046, -64.831785);
|
|
||||||
public static final GPSCoordinate finishLineMarker1 = new GPSCoordinate(32.317379, -64.839291);
|
|
||||||
public static final GPSCoordinate finishLineMarker2 = new GPSCoordinate(32.317257, -64.836260);
|
|
||||||
|
|
||||||
public static final double wakeScale = 10;
|
public static final double wakeScale = 10;
|
||||||
|
|
||||||
public static final BoatInRace[] OFFICIAL_AC35_COMPETITORS = new BoatInRace[]
|
|
||||||
{new BoatInRace("Oracle Team USA", 30.0, Color.BLUEVIOLET, "Oracle", 121),
|
|
||||||
new BoatInRace("Land Rover BAR", 23.0, Color.BLACK, "BGR", 122),
|
|
||||||
new BoatInRace("SoftBank Team Japan", 27.0, Color.RED, "JPN", 123),
|
|
||||||
new BoatInRace("Groupama Team France", 25.0, Color.ORANGE, "FRA", 124),
|
|
||||||
new BoatInRace("Artemis Racing", 22.5, Color.DARKOLIVEGREEN, "SWE", 125),
|
|
||||||
new BoatInRace("Emirates Team New Zealand", 62, Color.LIMEGREEN, "ETNZ", 126)};
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,135 @@
|
|||||||
|
package seng302.Data;
|
||||||
|
|
||||||
|
import org.w3c.dom.Attr;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
|
||||||
|
import seng302.Exceptions.InvalidBoatDataException;
|
||||||
|
import seng302.Model.BoatInRace;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
import javax.xml.transform.Transformer;
|
||||||
|
import javax.xml.transform.TransformerException;
|
||||||
|
import javax.xml.transform.TransformerFactory;
|
||||||
|
import javax.xml.transform.dom.DOMSource;
|
||||||
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by esa46 on 25/04/17.
|
||||||
|
*/
|
||||||
|
public class BoatData {
|
||||||
|
|
||||||
|
private List<BoatInRace> boatData;
|
||||||
|
|
||||||
|
public BoatData(List<BoatInRace> boatData) {
|
||||||
|
this.boatData = boatData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String createXML() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
|
||||||
|
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
|
||||||
|
|
||||||
|
//root element
|
||||||
|
Document doc = docBuilder.newDocument();
|
||||||
|
Element rootElement = doc.createElement("BoatConfig");
|
||||||
|
doc.appendChild(rootElement);
|
||||||
|
|
||||||
|
//Boats element
|
||||||
|
Element boats = doc.createElement("Boats");
|
||||||
|
rootElement.appendChild(boats);
|
||||||
|
|
||||||
|
for (int i=0; i < boatData.size(); i++) {
|
||||||
|
|
||||||
|
//Boat element
|
||||||
|
Element boat = doc.createElement("Boat");
|
||||||
|
|
||||||
|
//Type attribute
|
||||||
|
Attr attrType = doc.createAttribute("Type");
|
||||||
|
attrType.setValue("Mark");
|
||||||
|
boat.setAttributeNode(attrType);
|
||||||
|
|
||||||
|
//SourceID attribute
|
||||||
|
Attr attrSourceID = doc.createAttribute("SourceID");
|
||||||
|
attrSourceID.setValue(Integer.toString(boatData.get(i).getSourceID()));
|
||||||
|
boat.setAttributeNode(attrSourceID);
|
||||||
|
|
||||||
|
//ShapeID attribute
|
||||||
|
Attr attrShapeID = doc.createAttribute("ShapeID");
|
||||||
|
attrShapeID.setValue("0");
|
||||||
|
boat.setAttributeNode(attrShapeID);
|
||||||
|
|
||||||
|
//HullNum attribute
|
||||||
|
Attr attrHullNum = doc.createAttribute("HullNum");
|
||||||
|
attrHullNum.setValue("RG01");
|
||||||
|
boat.setAttributeNode(attrHullNum);
|
||||||
|
|
||||||
|
//StoweName attribute
|
||||||
|
Attr attrStoweName = doc.createAttribute("StoweName");
|
||||||
|
attrStoweName.setValue(boatData.get(i).getAbbrev());
|
||||||
|
boat.setAttributeNode(attrStoweName);
|
||||||
|
|
||||||
|
//ShortName attribute
|
||||||
|
Attr attrShortName = doc.createAttribute("ShortName");
|
||||||
|
attrShortName.setValue(boatData.get(i).getAbbrev());
|
||||||
|
boat.setAttributeNode(attrShortName);
|
||||||
|
|
||||||
|
//BoatName attribute
|
||||||
|
Attr attrBoatName = doc.createAttribute("BoatName");
|
||||||
|
attrBoatName.setValue(boatData.get(i).toString());
|
||||||
|
boat.setAttributeNode(attrBoatName);
|
||||||
|
|
||||||
|
//GPSCoord for element
|
||||||
|
Element GPSCoord = doc.createElement("GPSposition");
|
||||||
|
|
||||||
|
//Z axis attribute
|
||||||
|
Attr attrZCoord = doc.createAttribute("Z");
|
||||||
|
attrZCoord.setValue("0");
|
||||||
|
GPSCoord.setAttributeNode(attrZCoord);
|
||||||
|
|
||||||
|
//Y axis attribute
|
||||||
|
Attr attrYCoord = doc.createAttribute("Y");
|
||||||
|
attrYCoord.setValue(Double.toString(boatData.get(i).getCurrentPosition().getLatitude()));
|
||||||
|
GPSCoord.setAttributeNode(attrYCoord);
|
||||||
|
|
||||||
|
//X axis attribute
|
||||||
|
Attr attrXCoord = doc.createAttribute("X");
|
||||||
|
attrXCoord.setValue(Double.toString(boatData.get(i).getCurrentPosition().getLongitude()));
|
||||||
|
GPSCoord.setAttributeNode(attrXCoord);
|
||||||
|
|
||||||
|
//Write GPSCoord to boat
|
||||||
|
boat.appendChild(GPSCoord);
|
||||||
|
|
||||||
|
//Write boat to boats
|
||||||
|
boats.appendChild(boat);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// write the content into xml file
|
||||||
|
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||||
|
Transformer transformer = transformerFactory.newTransformer();
|
||||||
|
DOMSource source = new DOMSource(doc);
|
||||||
|
|
||||||
|
//Serialize document.
|
||||||
|
StringWriter stringWriter = new StringWriter();
|
||||||
|
StreamResult result = new StreamResult(stringWriter);
|
||||||
|
transformer.transform(source,result);
|
||||||
|
|
||||||
|
return stringWriter.toString();
|
||||||
|
|
||||||
|
|
||||||
|
} catch (ParserConfigurationException pce) {
|
||||||
|
throw new InvalidBoatDataException();
|
||||||
|
} catch (TransformerException tfe) {
|
||||||
|
throw new InvalidBoatDataException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package seng302.Data;
|
|||||||
|
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
|
import seng302.Exceptions.InvalidRaceDataException;
|
||||||
import seng302.GPSCoordinate;
|
import seng302.GPSCoordinate;
|
||||||
import seng302.Model.BoatInRace;
|
import seng302.Model.BoatInRace;
|
||||||
import seng302.Model.Marker;
|
import seng302.Model.Marker;
|
||||||
@@ -156,13 +157,9 @@ public class RaceData {
|
|||||||
|
|
||||||
|
|
||||||
} catch (ParserConfigurationException pce) {
|
} catch (ParserConfigurationException pce) {
|
||||||
pce.printStackTrace();
|
throw new InvalidRaceDataException();
|
||||||
} catch (TransformerException tfe) {
|
} catch (TransformerException tfe) {
|
||||||
tfe.printStackTrace();
|
throw new InvalidRaceDataException();
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";//TEMP this is probably bad. This shouldn't really be reached, but seems necessary due to the use of catches above.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import javax.xml.parsers.DocumentBuilder;
|
|||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
import javax.xml.transform.Transformer;
|
import javax.xml.transform.Transformer;
|
||||||
import javax.xml.transform.TransformerConfigurationException;
|
|
||||||
import javax.xml.transform.TransformerException;
|
import javax.xml.transform.TransformerException;
|
||||||
import javax.xml.transform.TransformerFactory;
|
import javax.xml.transform.TransformerFactory;
|
||||||
import javax.xml.transform.dom.DOMSource;
|
import javax.xml.transform.dom.DOMSource;
|
||||||
@@ -98,13 +97,10 @@ public class RegattaData {
|
|||||||
|
|
||||||
|
|
||||||
} catch (ParserConfigurationException pce) {
|
} catch (ParserConfigurationException pce) {
|
||||||
pce.printStackTrace();
|
throw new InvalidRegattaDataException();
|
||||||
} catch (TransformerException tfe) {
|
} catch (TransformerException tfe) {
|
||||||
tfe.printStackTrace();
|
throw new InvalidRegattaDataException();
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";//TEMP this is probably bad. This shouldn't really be reached, but seems necessary due to the use of catches above.
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,19 @@
|
|||||||
package seng302.Model;
|
package seng302.Model;
|
||||||
|
|
||||||
import org.w3c.dom.Attr;
|
import seng302.Data.BoatData;
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.w3c.dom.Element;
|
|
||||||
import seng302.Data.RaceData;
|
import seng302.Data.RaceData;
|
||||||
import seng302.Data.RegattaData;
|
import seng302.Data.RegattaData;
|
||||||
import seng302.Mock.Regatta;
|
|
||||||
import seng302.Mock.RegattaDataSource;
|
import seng302.Mock.RegattaDataSource;
|
||||||
import seng302.Exceptions.InvalidBoatDataException;
|
import seng302.Exceptions.InvalidBoatDataException;
|
||||||
import seng302.Exceptions.InvalidRaceDataException;
|
import seng302.Exceptions.InvalidRaceDataException;
|
||||||
import seng302.Exceptions.InvalidRegattaDataException;
|
import seng302.Exceptions.InvalidRegattaDataException;
|
||||||
import seng302.Mock.RaceDataSource;
|
import seng302.Mock.RaceDataSource;
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import javax.xml.transform.Transformer;
|
|
||||||
import javax.xml.transform.TransformerConfigurationException;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
import javax.xml.transform.TransformerFactory;
|
|
||||||
import javax.xml.transform.dom.DOMSource;
|
|
||||||
import javax.xml.transform.stream.StreamResult;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by esa46 on 21/04/17.
|
* Created by esa46 on 21/04/17.
|
||||||
@@ -101,127 +90,17 @@ public class Event {
|
|||||||
|
|
||||||
public void sendBoatData() throws InvalidBoatDataException
|
public void sendBoatData() throws InvalidBoatDataException
|
||||||
{
|
{
|
||||||
List<BoatInRace> boatData = raceDataSource.getBoats();
|
BoatData boatData = new BoatData(raceDataSource.getBoats());
|
||||||
|
//Serialize race data to an XML as a string.
|
||||||
|
String xmlString = boatData.createXML();
|
||||||
|
|
||||||
|
|
||||||
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
|
|
||||||
DocumentBuilder docBuilder = null;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
docBuilder = docFactory.newDocumentBuilder();
|
|
||||||
}
|
|
||||||
catch (ParserConfigurationException e)
|
|
||||||
{
|
|
||||||
throw new InvalidBoatDataException();
|
|
||||||
}
|
|
||||||
|
|
||||||
//root element
|
|
||||||
Document doc = docBuilder.newDocument();
|
|
||||||
Element rootElement = doc.createElement("BoatConfig");
|
|
||||||
doc.appendChild(rootElement);
|
|
||||||
|
|
||||||
//Boats element
|
|
||||||
Element boats = doc.createElement("Boats");
|
|
||||||
rootElement.appendChild(boats);
|
|
||||||
|
|
||||||
for (int i=0; i < boatData.size(); i++) {
|
|
||||||
|
|
||||||
//Boat element
|
|
||||||
Element boat = doc.createElement("Boat");
|
|
||||||
|
|
||||||
//Type attribute
|
|
||||||
Attr attrType = doc.createAttribute("Type");
|
|
||||||
attrType.setValue("Mark");
|
|
||||||
boat.setAttributeNode(attrType);
|
|
||||||
|
|
||||||
//SourceID attribute
|
|
||||||
Attr attrSourceID = doc.createAttribute("SourceID");
|
|
||||||
attrSourceID.setValue(Integer.toString(boatData.get(i).getSourceID()));
|
|
||||||
boat.setAttributeNode(attrSourceID);
|
|
||||||
|
|
||||||
//ShapeID attribute
|
|
||||||
Attr attrShapeID = doc.createAttribute("ShapeID");
|
|
||||||
attrShapeID.setValue("0");
|
|
||||||
boat.setAttributeNode(attrShapeID);
|
|
||||||
|
|
||||||
//HullNum attribute
|
|
||||||
Attr attrHullNum = doc.createAttribute("HullNum");
|
|
||||||
attrHullNum.setValue("RG01");
|
|
||||||
boat.setAttributeNode(attrHullNum);
|
|
||||||
|
|
||||||
//StoweName attribute
|
|
||||||
Attr attrStoweName = doc.createAttribute("StoweName");
|
|
||||||
attrStoweName.setValue(boatData.get(i).getAbbrev());
|
|
||||||
boat.setAttributeNode(attrStoweName);
|
|
||||||
|
|
||||||
//ShortName attribute
|
|
||||||
Attr attrShortName = doc.createAttribute("ShortName");
|
|
||||||
attrShortName.setValue(boatData.get(i).getAbbrev());
|
|
||||||
boat.setAttributeNode(attrShortName);
|
|
||||||
|
|
||||||
//BoatName attribute
|
|
||||||
Attr attrBoatName = doc.createAttribute("BoatName");
|
|
||||||
attrBoatName.setValue(boatData.get(i).toString());
|
|
||||||
boat.setAttributeNode(attrBoatName);
|
|
||||||
|
|
||||||
//GPSCoord for element
|
|
||||||
Element GPSCoord = doc.createElement("GPSposition");
|
|
||||||
|
|
||||||
//Z axis attribute
|
|
||||||
Attr attrZCoord = doc.createAttribute("Z");
|
|
||||||
attrZCoord.setValue("0");
|
|
||||||
GPSCoord.setAttributeNode(attrZCoord);
|
|
||||||
|
|
||||||
//Y axis attribute
|
|
||||||
Attr attrYCoord = doc.createAttribute("Y");
|
|
||||||
attrYCoord.setValue(Double.toString(boatData.get(i).getCurrentPosition().getLatitude()));
|
|
||||||
GPSCoord.setAttributeNode(attrYCoord);
|
|
||||||
|
|
||||||
//X axis attribute
|
|
||||||
Attr attrXCoord = doc.createAttribute("X");
|
|
||||||
attrXCoord.setValue(Double.toString(boatData.get(i).getCurrentPosition().getLongitude()));
|
|
||||||
GPSCoord.setAttributeNode(attrXCoord);
|
|
||||||
|
|
||||||
//Write GPSCoord to boat
|
|
||||||
boat.appendChild(GPSCoord);
|
|
||||||
|
|
||||||
//Write boat to boats
|
|
||||||
boats.appendChild(boat);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
TransformerFactory trasformerFactory = TransformerFactory.newInstance();
|
|
||||||
Transformer transformer = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
transformer = trasformerFactory.newTransformer();
|
|
||||||
}
|
|
||||||
catch (TransformerConfigurationException e)
|
|
||||||
{
|
|
||||||
throw new InvalidBoatDataException();
|
|
||||||
}
|
|
||||||
DOMSource source = new DOMSource(doc);
|
|
||||||
|
|
||||||
|
|
||||||
//Serialize document.
|
|
||||||
StringWriter stringWriter = new StringWriter();
|
|
||||||
StreamResult result = new StreamResult(stringWriter);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
transformer.transform(source,result);
|
|
||||||
}
|
|
||||||
catch (TransformerException e)
|
|
||||||
{
|
|
||||||
throw new InvalidBoatDataException();
|
|
||||||
}
|
|
||||||
//TODO now we should place in XML message object.
|
//TODO now we should place in XML message object.
|
||||||
//TODO now we should serialize xml message object.
|
//TODO now we should serialize xml message object.
|
||||||
//TODO now we should write serialized xml message over this.outputStream.
|
//TODO now we should write serialized xml message over this.outputStream.
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
this.outputStream.write(stringWriter.toString().getBytes());//TEMP currently we output the XML doc, not the serialized message.
|
this.outputStream.write(xmlString.getBytes());//TEMP currently we output the XML doc, not the serialized message.
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user