Removed regatta xml creation from Event class into RegattaData class
#story[778]
This commit is contained in:
@@ -4,6 +4,7 @@ package seng302;
|
||||
import javafx.application.Application;
|
||||
import javafx.stage.Stage;
|
||||
import org.xml.sax.SAXException;
|
||||
import seng302.Mock.RaceDataSource;
|
||||
import seng302.Mock.RegattaDataSource;
|
||||
import seng302.Mock.RegattaXMLReader;
|
||||
import seng302.Model.Event;
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
package seng302.Data;
|
||||
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import seng302.GPSCoordinate;
|
||||
import seng302.Model.Boat;
|
||||
import seng302.Model.BoatInRace;
|
||||
import seng302.Model.Leg;
|
||||
import seng302.Model.Marker;
|
||||
import seng302.RaceDataSource;
|
||||
import seng302.Mock.RaceDataSource;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package seng302.Data;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import seng302.Exceptions.InvalidRegattaDataException;
|
||||
import seng302.Mock.Regatta;
|
||||
import seng302.Mock.RegattaDataSource;
|
||||
|
||||
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.StringWriter;
|
||||
|
||||
/**
|
||||
* Created by esa46 on 25/04/17.
|
||||
*/
|
||||
public class RegattaData {
|
||||
|
||||
|
||||
private RegattaDataSource regattaDataSource;
|
||||
|
||||
|
||||
public RegattaData(RegattaDataSource regattaDataSource) {
|
||||
this.regattaDataSource = regattaDataSource;
|
||||
}
|
||||
|
||||
public String createXML() {
|
||||
|
||||
|
||||
Regatta regatta = regattaDataSource.getRegatta();
|
||||
|
||||
try {
|
||||
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
|
||||
|
||||
//root element
|
||||
Document doc = docBuilder.newDocument();
|
||||
Element rootElement = doc.createElement("RegattaConfig");
|
||||
doc.appendChild(rootElement);
|
||||
|
||||
//regattaID element
|
||||
Element regattaID = doc.createElement("RegattaID");
|
||||
regattaID.appendChild(doc.createTextNode(Integer.toString(regatta.getRegattaID())));
|
||||
rootElement.appendChild(regattaID);
|
||||
|
||||
//regattaName element
|
||||
Element regattaName = doc.createElement("RegattaName");
|
||||
regattaName.appendChild(doc.createTextNode(regatta.getRegattaName()));
|
||||
rootElement.appendChild(regattaName);
|
||||
|
||||
//courseName element
|
||||
Element courseName = doc.createElement("CourseName");
|
||||
courseName.appendChild(doc.createTextNode(regatta.getCourseName()));
|
||||
rootElement.appendChild(courseName);
|
||||
|
||||
//centralLatitude element
|
||||
Element centralLat = doc.createElement("CentralLatitude");
|
||||
centralLat.appendChild(doc.createTextNode(Double.toString(regatta.getCentralLatitude())));
|
||||
rootElement.appendChild(centralLat);
|
||||
|
||||
//centralLongitude element
|
||||
Element centralLong = doc.createElement("CentralLongitude");
|
||||
centralLong.appendChild(doc.createTextNode(Double.toString(regatta.getCentralLongitude())));
|
||||
rootElement.appendChild(centralLong);
|
||||
|
||||
//centralAltitude element
|
||||
Element centralAlt = doc.createElement("CentralAltitude");
|
||||
centralAlt.appendChild(doc.createTextNode(Double.toString(regatta.getCentralAltitude())));
|
||||
rootElement.appendChild(centralAlt);
|
||||
|
||||
//utcOffset element
|
||||
Element utcOffset = doc.createElement("UtcOffset");
|
||||
utcOffset.appendChild(doc.createTextNode(Double.toString(regatta.getUtcOffset())));
|
||||
rootElement.appendChild(utcOffset);
|
||||
|
||||
//magneticVariation element
|
||||
Element magneticVariation = doc.createElement("MagneticVariation");
|
||||
magneticVariation.appendChild(doc.createTextNode(Double.toString(regatta.getMagneticVariation())));
|
||||
rootElement.appendChild(magneticVariation);
|
||||
|
||||
// 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) {
|
||||
pce.printStackTrace();
|
||||
} catch (TransformerException tfe) {
|
||||
tfe.printStackTrace();
|
||||
}
|
||||
|
||||
return "";//TEMP this is probably bad. This shouldn't really be reached, but seems necessary due to the use of catches above.
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package seng302;
|
||||
package seng302.Mock;
|
||||
|
||||
import seng302.GPSCoordinate;
|
||||
import seng302.Model.BoatInRace;
|
||||
import seng302.Model.Leg;
|
||||
import seng302.Model.Marker;
|
||||
@@ -4,13 +4,13 @@ import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import seng302.Data.RaceData;
|
||||
import seng302.Data.RegattaData;
|
||||
import seng302.Mock.Regatta;
|
||||
import seng302.Mock.RegattaDataSource;
|
||||
import seng302.Exceptions.InvalidBoatDataException;
|
||||
import seng302.Exceptions.InvalidRaceDataException;
|
||||
import seng302.Exceptions.InvalidRegattaDataException;
|
||||
import seng302.Model.Race;
|
||||
import seng302.RaceDataSource;
|
||||
import seng302.Mock.RaceDataSource;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
@@ -60,96 +60,16 @@ public class Event {
|
||||
|
||||
public void sendRegattaData() throws InvalidRegattaDataException {
|
||||
|
||||
Regatta regatta = regattaDataSource.getRegatta();
|
||||
RegattaData regattaData = new RegattaData(regattaDataSource);
|
||||
String xmlString = regattaData.createXML();
|
||||
|
||||
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder docBuilder = null;
|
||||
try
|
||||
{
|
||||
docBuilder = docFactory.newDocumentBuilder();
|
||||
}
|
||||
catch (ParserConfigurationException e)
|
||||
{
|
||||
throw new InvalidRegattaDataException();
|
||||
}
|
||||
|
||||
//root element
|
||||
Document doc = docBuilder.newDocument();
|
||||
Element rootElement = doc.createElement("RegattaConfig");
|
||||
doc.appendChild(rootElement);
|
||||
|
||||
//regattaID element
|
||||
Element regattaID = doc.createElement("RegattaID");
|
||||
regattaID.appendChild(doc.createTextNode(Integer.toString(regatta.getRegattaID())));
|
||||
rootElement.appendChild(regattaID);
|
||||
|
||||
//regattaName element
|
||||
Element regattaName = doc.createElement("RegattaName");
|
||||
regattaName.appendChild(doc.createTextNode(regatta.getRegattaName()));
|
||||
rootElement.appendChild(regattaName);
|
||||
|
||||
//courseName element
|
||||
Element courseName = doc.createElement("CourseName");
|
||||
courseName.appendChild(doc.createTextNode(regatta.getCourseName()));
|
||||
rootElement.appendChild(courseName);
|
||||
|
||||
//centralLatitude element
|
||||
Element centralLat = doc.createElement("CentralLatitude");
|
||||
centralLat.appendChild(doc.createTextNode(Double.toString(regatta.getCentralLatitude())));
|
||||
rootElement.appendChild(centralLat);
|
||||
|
||||
//centralLongitude element
|
||||
Element centralLong = doc.createElement("CentralLongitude");
|
||||
centralLong.appendChild(doc.createTextNode(Double.toString(regatta.getCentralLongitude())));
|
||||
rootElement.appendChild(centralLong);
|
||||
|
||||
//centralAltitude element
|
||||
Element centralAlt = doc.createElement("CentralAltitude");
|
||||
centralAlt.appendChild(doc.createTextNode(Double.toString(regatta.getCentralAltitude())));
|
||||
rootElement.appendChild(centralAlt);
|
||||
|
||||
//utcOffset element
|
||||
Element utcOffset = doc.createElement("UtcOffset");
|
||||
utcOffset.appendChild(doc.createTextNode(Double.toString(regatta.getUtcOffset())));
|
||||
rootElement.appendChild(utcOffset);
|
||||
|
||||
//magneticVariation element
|
||||
Element magneticVariation = doc.createElement("MagneticVariation");
|
||||
magneticVariation.appendChild(doc.createTextNode(Double.toString(regatta.getMagneticVariation())));
|
||||
rootElement.appendChild(magneticVariation);
|
||||
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Transformer transformer = null;
|
||||
try
|
||||
{
|
||||
transformer = transformerFactory.newTransformer();
|
||||
}
|
||||
catch (TransformerConfigurationException e)
|
||||
{
|
||||
throw new InvalidRegattaDataException();
|
||||
}
|
||||
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 InvalidRegattaDataException();
|
||||
}
|
||||
//TODO now we should place in XML message object.
|
||||
//TODO now we should serialize xml message object.
|
||||
//TODO now we should write serialized xml message over this.outputStream.
|
||||
|
||||
try
|
||||
{
|
||||
this.outputStream.write(stringWriter.toString().getBytes());//TEMP currently we output the XML doc, not the serialized message.
|
||||
this.outputStream.write(xmlString.toString().getBytes());//TEMP currently we output the XML doc, not the serialized message.
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
@@ -181,8 +101,6 @@ public class Event {
|
||||
|
||||
public void sendBoatData() throws InvalidBoatDataException
|
||||
{
|
||||
|
||||
|
||||
List<BoatInRace> boatData = raceDataSource.getBoats();
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package seng302.Model;
|
||||
|
||||
|
||||
import com.sun.org.apache.xpath.internal.SourceTree;
|
||||
import javafx.animation.AnimationTimer;
|
||||
|
||||
import javafx.collections.FXCollections;
|
||||
@@ -10,7 +9,7 @@ import javafx.collections.ObservableList;
|
||||
import org.geotools.referencing.GeodeticCalculator;
|
||||
import seng302.Constants;
|
||||
import seng302.GPSCoordinate;
|
||||
import seng302.RaceDataSource;
|
||||
import seng302.Mock.RaceDataSource;
|
||||
import seng302.RaceEventMessages.BoatLocationMessage;
|
||||
|
||||
|
||||
@@ -18,7 +17,6 @@ import java.awt.geom.Point2D;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import javafx.scene.paint.Color;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
import seng302.Mock.RaceDataSource;
|
||||
import seng302.Model.BoatInRace;
|
||||
import seng302.Model.Leg;
|
||||
import seng302.Model.Marker;
|
||||
|
||||
Reference in New Issue
Block a user