@ -13,6 +13,9 @@ import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory ;
import javax.xml.transform.dom.DOMSource ;
import javax.xml.transform.stream.StreamResult ;
import java.io.IOException ;
import java.io.OutputStream ;
import java.io.StringWriter ;
import java.util.List ;
/ * *
@ -22,16 +25,33 @@ public class Event {
RaceDataSource raceDataSource ;
public Event ( RaceDataSource raceData ) {
///The stream to which we send all data.
private OutputStream outputStream ;
/ * *
* Ctor .
* @param raceData
* @param outputStream OutputStream to write data to .
* /
public Event ( RaceDataSource raceData , OutputStream outputStream ) {
this . raceDataSource = raceData ;
this . outputStream = outputStream ;
}
public void start ( ) {
public void start ( ) throws IOException
{
System . out . println ( "\nREGATTA DATA\n" ) ; //TEMP REMOVE debug
sendRegattaData ( ) ;
System . out . println ( "\nRACE DATA\n" ) ; //TEMP REMOVE debug
sendRaceData ( ) ;
System . out . println ( "\nBOAT DATA\n" ) ; //TEMP REMOVE debug
sendBoatData ( ) ;
Race newRace = new Race ( raceDataSource , 15 ) ;
System . out . println ( "RACE STARTING!!\n\n" ) ; //TEMP REMOVE debug
Race newRace = new Race ( raceDataSource , 15 , this . outputStream ) ;
new Thread ( ( newRace ) ) . start ( ) ;
}
@ -87,13 +107,21 @@ public class Event {
magneticVariation . appendChild ( doc . createTextNode ( Double . toString ( 14.76 ) ) ) ;
rootElement . appendChild ( magneticVariation ) ;
TransformerFactory tra sformerFactory = TransformerFactory . newInstance ( ) ;
Transformer transformer = tra sformerFactory. newTransformer ( ) ;
TransformerFactory tra n sformerFactory = TransformerFactory . newInstance ( ) ;
Transformer transformer = tra n sformerFactory. newTransformer ( ) ;
DOMSource source = new DOMSource ( doc ) ;
//print XML object to check for correctness
StreamResult result = new StreamResult ( System . out ) ;
//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.
this . outputStream . write ( stringWriter . toString ( ) . getBytes ( ) ) ; //TEMP currently we output the XML doc, not the serialized message.
} catch ( Exception e ) {
e . printStackTrace ( ) ;
@ -101,9 +129,17 @@ public class Event {
}
public void sendRaceData ( ) {
public void sendRaceData ( ) throws IOException
{
RaceData raceData = new RaceData ( raceDataSource ) ;
raceData . createXML ( ) ;
//Serialize race data to an XML as a string.
String xmlString = raceData . createXML ( ) ;
//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.
this . outputStream . write ( xmlString . getBytes ( ) ) ; //TEMP currently we output the XML doc, not the serialized message.
}
public void sendBoatData ( ) {
@ -194,9 +230,17 @@ public class Event {
Transformer transformer = trasformerFactory . newTransformer ( ) ;
DOMSource source = new DOMSource ( doc ) ;
//print XML object to check for correctness
StreamResult result = new StreamResult ( System . out ) ;
//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.
this . outputStream . write ( stringWriter . toString ( ) . getBytes ( ) ) ; //TEMP currently we output the XML doc, not the serialized message.
} catch ( Exception e ) {
e . printStackTrace ( ) ;