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.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import seng302.Data.RaceData; import seng302.Data.RaceData;
import seng302.Exceptions.InvalidBoatDataException;
import seng302.Exceptions.InvalidRaceDataException;
import seng302.Exceptions.InvalidRegattaDataException;
import seng302.Model.Race; import seng302.Model.Race;
import seng302.RaceDataSource; import seng302.RaceDataSource;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
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.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
@ -40,7 +46,7 @@ public class Event {
this.outputStream = outputStream; this.outputStream = outputStream;
} }
public void start() throws IOException public void start()
{ {
System.out.println("\nREGATTA DATA\n");//TEMP REMOVE debug System.out.println("\nREGATTA DATA\n");//TEMP REMOVE debug
sendRegattaData(); sendRegattaData();
@ -56,80 +62,107 @@ public class Event {
} }
public void sendRegattaData() { public void sendRegattaData() throws InvalidRegattaDataException {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//root element DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
Document doc = docBuilder.newDocument(); DocumentBuilder docBuilder = null;
Element rootElement = doc.createElement("RegattaConfig"); try
doc.appendChild(rootElement); {
docBuilder = docFactory.newDocumentBuilder();
//regattaID element }
Element regattaID = doc.createElement("RegattaID"); catch (ParserConfigurationException e)
regattaID.appendChild(doc.createTextNode("3")); {
rootElement.appendChild(regattaID); throw new InvalidRegattaDataException();
}
//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(); //root element
Transformer transformer = transformerFactory.newTransformer(); Document doc = docBuilder.newDocument();
DOMSource source = new DOMSource(doc); 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. //Serialize document.
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter); StreamResult result = new StreamResult(stringWriter);
try
{
transformer.transform(source,result); transformer.transform(source,result);
//TODO now we should place in XML message object. }
//TODO now we should serialize xml message object. catch (TransformerException e)
//TODO now we should write serialized xml message over this.outputStream. {
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(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); RaceData raceData = new RaceData(raceDataSource);
//Serialize race data to an XML as a string. //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 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.
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(); List<BoatInRace> boatData = raceDataSource.getBoats();
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//root element DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
Document doc = docBuilder.newDocument(); DocumentBuilder docBuilder = null;
Element rootElement = doc.createElement("BoatConfig");
doc.appendChild(rootElement);
//Boats element try
Element boats = doc.createElement("Boats"); {
rootElement.appendChild(boats); 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 //Boats element
Element boat = doc.createElement("Boat"); Element boats = doc.createElement("Boats");
rootElement.appendChild(boats);
//Type attribute for (int i=0; i < boatData.size(); i++) {
Attr attrType = doc.createAttribute("Type");
attrType.setValue("Mark");
boat.setAttributeNode(attrType);
//SourceID attribute //Boat element
Attr attrSourceID = doc.createAttribute("SourceID"); Element boat = doc.createElement("Boat");
attrSourceID.setValue(Integer.toString(boatData.get(i).getSourceID()));
boat.setAttributeNode(attrSourceID);
//ShapeID attribute //Type attribute
Attr attrShapeID = doc.createAttribute("ShapeID"); Attr attrType = doc.createAttribute("Type");
attrShapeID.setValue("0"); attrType.setValue("Mark");
boat.setAttributeNode(attrShapeID); boat.setAttributeNode(attrType);
//HullNum attribute //SourceID attribute
Attr attrHullNum = doc.createAttribute("HullNum"); Attr attrSourceID = doc.createAttribute("SourceID");
attrHullNum.setValue("RG01"); attrSourceID.setValue(Integer.toString(boatData.get(i).getSourceID()));
boat.setAttributeNode(attrHullNum); boat.setAttributeNode(attrSourceID);
//StoweName attribute //ShapeID attribute
Attr attrStoweName = doc.createAttribute("StoweName"); Attr attrShapeID = doc.createAttribute("ShapeID");
attrStoweName.setValue(boatData.get(i).getAbbrev()); attrShapeID.setValue("0");
boat.setAttributeNode(attrStoweName); boat.setAttributeNode(attrShapeID);
//ShortName attribute //HullNum attribute
Attr attrShortName = doc.createAttribute("ShortName"); Attr attrHullNum = doc.createAttribute("HullNum");
attrShortName.setValue(boatData.get(i).getAbbrev()); attrHullNum.setValue("RG01");
boat.setAttributeNode(attrShortName); boat.setAttributeNode(attrHullNum);
//BoatName attribute //StoweName attribute
Attr attrBoatName = doc.createAttribute("BoatName"); Attr attrStoweName = doc.createAttribute("StoweName");
attrBoatName.setValue(boatData.get(i).toString()); attrStoweName.setValue(boatData.get(i).getAbbrev());
boat.setAttributeNode(attrBoatName); boat.setAttributeNode(attrStoweName);
//GPSCoord for element //ShortName attribute
Element GPSCoord = doc.createElement("GPSposition"); Attr attrShortName = doc.createAttribute("ShortName");
attrShortName.setValue(boatData.get(i).getAbbrev());
boat.setAttributeNode(attrShortName);
//Z axis attribute //BoatName attribute
Attr attrZCoord = doc.createAttribute("Z"); Attr attrBoatName = doc.createAttribute("BoatName");
attrZCoord.setValue("0"); attrBoatName.setValue(boatData.get(i).toString());
GPSCoord.setAttributeNode(attrZCoord); boat.setAttributeNode(attrBoatName);
//Y axis attribute //GPSCoord for element
Attr attrYCoord = doc.createAttribute("Y"); Element GPSCoord = doc.createElement("GPSposition");
attrYCoord.setValue(Double.toString(boatData.get(i).getCurrentPosition().getLatitude()));
GPSCoord.setAttributeNode(attrYCoord);
//X axis attribute //Z axis attribute
Attr attrXCoord = doc.createAttribute("X"); Attr attrZCoord = doc.createAttribute("Z");
attrXCoord.setValue(Double.toString(boatData.get(i).getCurrentPosition().getLongitude())); attrZCoord.setValue("0");
GPSCoord.setAttributeNode(attrXCoord); GPSCoord.setAttributeNode(attrZCoord);
//Write GPSCoord to boat //Y axis attribute
boat.appendChild(GPSCoord); Attr attrYCoord = doc.createAttribute("Y");
attrYCoord.setValue(Double.toString(boatData.get(i).getCurrentPosition().getLatitude()));
GPSCoord.setAttributeNode(attrYCoord);
//Write boat to boats //X axis attribute
boats.appendChild(boat); 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(); //Write boat to boats
Transformer transformer = trasformerFactory.newTransformer(); boats.appendChild(boat);
DOMSource source = new DOMSource(doc);
}
//Serialize document. TransformerFactory trasformerFactory = TransformerFactory.newInstance();
StringWriter stringWriter = new StringWriter(); Transformer transformer = null;
StreamResult result = new StreamResult(stringWriter); try
transformer.transform(source,result); {
//TODO now we should place in XML message object. transformer = trasformerFactory.newTransformer();
//TODO now we should serialize xml message object. }
//TODO now we should write serialized xml message over this.outputStream. 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) { try
e.printStackTrace(); {
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