Added some exception classes to encapsulate the internal exceptions that can occur when sending Boat/race/regatta data.

#story[778]
main
fjc40 9 years ago
parent bf5ea193af
commit 2876eec1e4

@ -0,0 +1,21 @@
package seng302.Exceptions;
/**
* Created by f123 on 25-Apr-17.
*/
/**
* An exception thrown when we cannot generate Boats.xml and send an XML message.
*/
public class InvalidBoatDataException extends RuntimeException
{
public InvalidBoatDataException()
{
}
public InvalidBoatDataException(String message)
{
super(message);
}
}

@ -0,0 +1,20 @@
package seng302.Exceptions;
/**
* Created by f123 on 25-Apr-17.
*/
/**
* Exception thrown when we cannot generate Race.xml data, and send an XML message.
*/
public class InvalidRaceDataException extends RuntimeException
{
public InvalidRaceDataException()
{
}
public InvalidRaceDataException(String message)
{
super(message);
}
}

@ -0,0 +1,20 @@
package seng302.Exceptions;
/**
* Created by f123 on 25-Apr-17.
*/
/**
* An exception thrown when a Regatta.xml message cannot be generated and sent.
*/
public class InvalidRegattaDataException extends RuntimeException
{
public InvalidRegattaDataException()
{
}
public InvalidRegattaDataException(String message)
{
super(message);
}
}

@ -4,12 +4,18 @@ import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import seng302.Data.RaceData;
import seng302.Exceptions.InvalidBoatDataException;
import seng302.Exceptions.InvalidRaceDataException;
import seng302.Exceptions.InvalidRegattaDataException;
import seng302.Model.Race;
import seng302.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;
@ -40,7 +46,7 @@ public class Event {
this.outputStream = outputStream;
}
public void start() throws IOException
public void start()
{
System.out.println("\nREGATTA DATA\n");//TEMP REMOVE debug
sendRegattaData();
@ -56,80 +62,107 @@ public class Event {
}
public void sendRegattaData() {
try {
public void sendRegattaData() throws InvalidRegattaDataException {
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("3"));
rootElement.appendChild(regattaID);
//regattaName element
Element regattaName = doc.createElement("RegattaName");
regattaName.appendChild(doc.createTextNode("New Zealand Test"));
rootElement.appendChild(regattaName);
//courseName element
Element courseName = doc.createElement("CourseName");
courseName.appendChild(doc.createTextNode("North Head"));
rootElement.appendChild(courseName);
//centralLatitude element
Element centralLat = doc.createElement("CentralLatitude");
centralLat.appendChild(doc.createTextNode(Double.toString(32.293039)));
rootElement.appendChild(centralLat);
//centralLongitude element
Element centralLong = doc.createElement("CentralLongitude");
centralLong.appendChild(doc.createTextNode(Double.toString(-64.843983)));
rootElement.appendChild(centralLong);
//centralAltitude element
Element centralAlt = doc.createElement("CentralAltitude");
centralAlt.appendChild(doc.createTextNode(Double.toString(0)));
rootElement.appendChild(centralAlt);
//utcOffset element
Element utcOffset = doc.createElement("UtcOffset");
utcOffset.appendChild(doc.createTextNode(Double.toString(-3)));
rootElement.appendChild(utcOffset);
//magneticVariation element
Element magneticVariation = doc.createElement("MagneticVariation");
magneticVariation.appendChild(doc.createTextNode(Double.toString(14.76)));
rootElement.appendChild(magneticVariation);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
try
{
docBuilder = docFactory.newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
throw new InvalidRegattaDataException();
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
//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("3"));
rootElement.appendChild(regattaID);
//regattaName element
Element regattaName = doc.createElement("RegattaName");
regattaName.appendChild(doc.createTextNode("New Zealand Test"));
rootElement.appendChild(regattaName);
//courseName element
Element courseName = doc.createElement("CourseName");
courseName.appendChild(doc.createTextNode("North Head"));
rootElement.appendChild(courseName);
//centralLatitude element
Element centralLat = doc.createElement("CentralLatitude");
centralLat.appendChild(doc.createTextNode(Double.toString(32.293039)));
rootElement.appendChild(centralLat);
//centralLongitude element
Element centralLong = doc.createElement("CentralLongitude");
centralLong.appendChild(doc.createTextNode(Double.toString(-64.843983)));
rootElement.appendChild(centralLong);
//centralAltitude element
Element centralAlt = doc.createElement("CentralAltitude");
centralAlt.appendChild(doc.createTextNode(Double.toString(0)));
rootElement.appendChild(centralAlt);
//utcOffset element
Element utcOffset = doc.createElement("UtcOffset");
utcOffset.appendChild(doc.createTextNode(Double.toString(-3)));
rootElement.appendChild(utcOffset);
//magneticVariation element
Element magneticVariation = doc.createElement("MagneticVariation");
magneticVariation.appendChild(doc.createTextNode(Double.toString(14.76)));
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);
//Serialize document.
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
try
{
transformer.transform(source,result);
//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.
}
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.
} catch (Exception e){
e.printStackTrace();
}
catch (IOException e)
{
throw new InvalidRegattaDataException();
}
}
public void sendRaceData() throws IOException
public void sendRaceData() throws InvalidRaceDataException
{
RaceData raceData = new RaceData(raceDataSource);
//Serialize race data to an XML as a string.
@ -139,112 +172,147 @@ public class Event {
//TODO now we should serialize xml message object.
//TODO now we should write serialized xml message over this.outputStream.
this.outputStream.write(xmlString.getBytes());//TEMP currently we output the XML doc, not the serialized message.
try
{
this.outputStream.write(xmlString.getBytes());//TEMP currently we output the XML doc, not the serialized message.
}
catch (IOException e)
{
throw new InvalidRaceDataException();
}
}
public void sendBoatData() {
public void sendBoatData() throws InvalidBoatDataException
{
List<BoatInRace> boatData = raceDataSource.getBoats();
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//root element
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("BoatConfig");
doc.appendChild(rootElement);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
//Boats element
Element boats = doc.createElement("Boats");
rootElement.appendChild(boats);
try
{
docBuilder = docFactory.newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
throw new InvalidBoatDataException();
}
for (int i=0; i < boatData.size(); i++) {
//root element
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("BoatConfig");
doc.appendChild(rootElement);
//Boat element
Element boat = doc.createElement("Boat");
//Boats element
Element boats = doc.createElement("Boats");
rootElement.appendChild(boats);
//Type attribute
Attr attrType = doc.createAttribute("Type");
attrType.setValue("Mark");
boat.setAttributeNode(attrType);
for (int i=0; i < boatData.size(); i++) {
//SourceID attribute
Attr attrSourceID = doc.createAttribute("SourceID");
attrSourceID.setValue(Integer.toString(boatData.get(i).getSourceID()));
boat.setAttributeNode(attrSourceID);
//Boat element
Element boat = doc.createElement("Boat");
//ShapeID attribute
Attr attrShapeID = doc.createAttribute("ShapeID");
attrShapeID.setValue("0");
boat.setAttributeNode(attrShapeID);
//Type attribute
Attr attrType = doc.createAttribute("Type");
attrType.setValue("Mark");
boat.setAttributeNode(attrType);
//HullNum attribute
Attr attrHullNum = doc.createAttribute("HullNum");
attrHullNum.setValue("RG01");
boat.setAttributeNode(attrHullNum);
//SourceID attribute
Attr attrSourceID = doc.createAttribute("SourceID");
attrSourceID.setValue(Integer.toString(boatData.get(i).getSourceID()));
boat.setAttributeNode(attrSourceID);
//StoweName attribute
Attr attrStoweName = doc.createAttribute("StoweName");
attrStoweName.setValue(boatData.get(i).getAbbrev());
boat.setAttributeNode(attrStoweName);
//ShapeID attribute
Attr attrShapeID = doc.createAttribute("ShapeID");
attrShapeID.setValue("0");
boat.setAttributeNode(attrShapeID);
//ShortName attribute
Attr attrShortName = doc.createAttribute("ShortName");
attrShortName.setValue(boatData.get(i).getAbbrev());
boat.setAttributeNode(attrShortName);
//HullNum attribute
Attr attrHullNum = doc.createAttribute("HullNum");
attrHullNum.setValue("RG01");
boat.setAttributeNode(attrHullNum);
//BoatName attribute
Attr attrBoatName = doc.createAttribute("BoatName");
attrBoatName.setValue(boatData.get(i).toString());
boat.setAttributeNode(attrBoatName);
//StoweName attribute
Attr attrStoweName = doc.createAttribute("StoweName");
attrStoweName.setValue(boatData.get(i).getAbbrev());
boat.setAttributeNode(attrStoweName);
//GPSCoord for element
Element GPSCoord = doc.createElement("GPSposition");
//ShortName attribute
Attr attrShortName = doc.createAttribute("ShortName");
attrShortName.setValue(boatData.get(i).getAbbrev());
boat.setAttributeNode(attrShortName);
//Z axis attribute
Attr attrZCoord = doc.createAttribute("Z");
attrZCoord.setValue("0");
GPSCoord.setAttributeNode(attrZCoord);
//BoatName attribute
Attr attrBoatName = doc.createAttribute("BoatName");
attrBoatName.setValue(boatData.get(i).toString());
boat.setAttributeNode(attrBoatName);
//Y axis attribute
Attr attrYCoord = doc.createAttribute("Y");
attrYCoord.setValue(Double.toString(boatData.get(i).getCurrentPosition().getLatitude()));
GPSCoord.setAttributeNode(attrYCoord);
//GPSCoord for element
Element GPSCoord = doc.createElement("GPSposition");
//X axis attribute
Attr attrXCoord = doc.createAttribute("X");
attrXCoord.setValue(Double.toString(boatData.get(i).getCurrentPosition().getLongitude()));
GPSCoord.setAttributeNode(attrXCoord);
//Z axis attribute
Attr attrZCoord = doc.createAttribute("Z");
attrZCoord.setValue("0");
GPSCoord.setAttributeNode(attrZCoord);
//Write GPSCoord to boat
boat.appendChild(GPSCoord);
//Y axis attribute
Attr attrYCoord = doc.createAttribute("Y");
attrYCoord.setValue(Double.toString(boatData.get(i).getCurrentPosition().getLatitude()));
GPSCoord.setAttributeNode(attrYCoord);
//Write boat to boats
boats.appendChild(boat);
//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);
TransformerFactory trasformerFactory = TransformerFactory.newInstance();
Transformer transformer = trasformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
//Write boat to boats
boats.appendChild(boat);
}
//Serialize document.
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
transformer.transform(source,result);
//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.
TransformerFactory trasformerFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try
{
transformer = trasformerFactory.newTransformer();
}
catch (TransformerConfigurationException e)
{
throw new InvalidBoatDataException();
}
DOMSource source = new DOMSource(doc);
this.outputStream.write(stringWriter.toString().getBytes());//TEMP currently we output the XML doc, not the serialized message.
//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 serialize xml message object.
//TODO now we should write serialized xml message over this.outputStream.
} catch (Exception e) {
e.printStackTrace();
try
{
this.outputStream.write(stringWriter.toString().getBytes());//TEMP currently we output the XML doc, not the serialized message.
}
catch (IOException e)
{
throw new InvalidBoatDataException();
}
}

Loading…
Cancel
Save