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,11 +62,19 @@ public class Event {
}
public void sendRegattaData() {
try {
public void sendRegattaData() throws InvalidRegattaDataException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
DocumentBuilder docBuilder = null;
try
{
docBuilder = docFactory.newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
throw new InvalidRegattaDataException();
}
//root element
Document doc = docBuilder.newDocument();
@ -108,7 +122,15 @@ public class Event {
rootElement.appendChild(magneticVariation);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Transformer transformer = null;
try
{
transformer = transformerFactory.newTransformer();
}
catch (TransformerConfigurationException e)
{
throw new InvalidRegattaDataException();
}
DOMSource source = new DOMSource(doc);
@ -116,20 +138,31 @@ public class Event {
//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.
} 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,17 +172,34 @@ public class Event {
//TODO now we should serialize xml message object.
//TODO now we should write serialized xml message over this.outputStream.
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();
DocumentBuilder docBuilder = null;
try
{
docBuilder = docFactory.newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
throw new InvalidBoatDataException();
}
//root element
Document doc = docBuilder.newDocument();
@ -227,24 +277,42 @@ public class Event {
}
TransformerFactory trasformerFactory = TransformerFactory.newInstance();
Transformer transformer = trasformerFactory.newTransformer();
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 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 InvalidBoatDataException();
}
}

Loading…
Cancel
Save