Began xml creator for race info message
#story[778]
This commit is contained in:
@@ -1,8 +1,23 @@
|
|||||||
package seng302.Data;
|
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.Boat;
|
||||||
|
import seng302.Model.BoatInRace;
|
||||||
|
import seng302.Model.Leg;
|
||||||
import seng302.Model.Marker;
|
import seng302.Model.Marker;
|
||||||
|
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.TransformerException;
|
||||||
|
import javax.xml.transform.TransformerFactory;
|
||||||
|
import javax.xml.transform.dom.DOMSource;
|
||||||
|
import javax.xml.transform.stream.StreamResult;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -11,13 +26,81 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class RaceData {
|
public class RaceData {
|
||||||
|
|
||||||
|
private RaceDataSource dataSource;
|
||||||
private String raceID;
|
private String raceID;
|
||||||
private String raceType;
|
private String raceType;
|
||||||
private OffsetDateTime creationTimeDate;
|
private OffsetDateTime creationTimeDate;
|
||||||
private OffsetDateTime raceStartTime;
|
private OffsetDateTime raceStartTime;
|
||||||
private List<Boat> participants;
|
private List<BoatInRace> participants;
|
||||||
// private List<Marker>
|
private List<Leg> course;
|
||||||
|
private List<GPSCoordinate> courseLimit;
|
||||||
|
|
||||||
|
public RaceData(RaceDataSource dataSource) {
|
||||||
|
this.dataSource = dataSource;
|
||||||
|
this.raceID = dataSource.getRaceId();
|
||||||
|
this.raceType = "Fleet";
|
||||||
|
creationTimeDate = OffsetDateTime.now();
|
||||||
|
raceStartTime = OffsetDateTime.now().plusMinutes(3);
|
||||||
|
participants = dataSource.getBoats();
|
||||||
|
course = dataSource.getLegs();
|
||||||
|
courseLimit = dataSource.getBoundary();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void createXML() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
|
||||||
|
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
|
||||||
|
|
||||||
|
// root elements
|
||||||
|
Document doc = docBuilder.newDocument();
|
||||||
|
Element rootElement = doc.createElement("Race");
|
||||||
|
doc.appendChild(rootElement);
|
||||||
|
|
||||||
|
// shorten way
|
||||||
|
// staff.setAttribute("id", "1");
|
||||||
|
|
||||||
|
Element raceIdElement = doc.createElement("RaceID");
|
||||||
|
raceIdElement.appendChild(doc.createTextNode(raceID));
|
||||||
|
rootElement.appendChild(raceIdElement);
|
||||||
|
|
||||||
|
Element raceTypeElement = doc.createElement("RaceType");
|
||||||
|
raceTypeElement.appendChild(doc.createTextNode(raceType));
|
||||||
|
rootElement.appendChild(raceTypeElement);
|
||||||
|
|
||||||
|
Element creationTimeElement = doc.createElement("CreationTimeDate");
|
||||||
|
creationTimeElement.appendChild(doc.createTextNode(creationTimeDate.toString()));
|
||||||
|
rootElement.appendChild(creationTimeElement);
|
||||||
|
|
||||||
|
Element startTimeElement = doc.createElement("RaceStartTime");
|
||||||
|
startTimeElement.setAttribute("Time", raceStartTime.toString());
|
||||||
|
startTimeElement.setAttribute("Postpone", "false");
|
||||||
|
rootElement.appendChild(startTimeElement);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// write the content into xml file
|
||||||
|
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||||
|
Transformer transformer = transformerFactory.newTransformer();
|
||||||
|
DOMSource source = new DOMSource(doc);
|
||||||
|
StreamResult result = new StreamResult(System.out);
|
||||||
|
|
||||||
|
// Output to console for testing
|
||||||
|
// StreamResult result = new StreamResult(System.out);
|
||||||
|
|
||||||
|
transformer.transform(source, result);
|
||||||
|
|
||||||
|
System.out.println("File saved!");
|
||||||
|
|
||||||
|
|
||||||
|
} catch (ParserConfigurationException pce) {
|
||||||
|
pce.printStackTrace();
|
||||||
|
} catch (TransformerException tfe) {
|
||||||
|
tfe.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ import seng302.RaceDataSource;
|
|||||||
*/
|
*/
|
||||||
public class Event {
|
public class Event {
|
||||||
|
|
||||||
RaceDataSource raceData;
|
RaceDataSource raceDataSource;
|
||||||
|
|
||||||
public Event(RaceDataSource raceData) {
|
public Event(RaceDataSource raceData) {
|
||||||
this.raceData = raceData;
|
this.raceDataSource = raceData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
@@ -20,7 +20,7 @@ public class Event {
|
|||||||
sendRaceData();
|
sendRaceData();
|
||||||
sendBoatData();
|
sendBoatData();
|
||||||
|
|
||||||
Race newRace = new Race(raceData, 15);
|
Race newRace = new Race(raceDataSource, 15);
|
||||||
new Thread((newRace)).start();
|
new Thread((newRace)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,7 +29,8 @@ public class Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void sendRaceData() {
|
public void sendRaceData() {
|
||||||
RaceData raceData = new RaceData();
|
RaceData raceData = new RaceData(raceDataSource);
|
||||||
|
raceData.createXML();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ public interface RaceDataSource {
|
|||||||
List<Leg> getLegs();
|
List<Leg> getLegs();
|
||||||
List<GPSCoordinate> getBoundary();
|
List<GPSCoordinate> getBoundary();
|
||||||
|
|
||||||
|
String getRaceId();
|
||||||
|
|
||||||
GPSCoordinate getMark();
|
GPSCoordinate getMark();
|
||||||
GPSCoordinate getMapTopLeft();
|
GPSCoordinate getMapTopLeft();
|
||||||
GPSCoordinate getMapBottomRight();
|
GPSCoordinate getMapBottomRight();
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import java.util.List;
|
|||||||
* Created by fwy13 on 26/03/2017.
|
* Created by fwy13 on 26/03/2017.
|
||||||
*/
|
*/
|
||||||
public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||||
|
private String raceID;
|
||||||
private List<BoatInRace> boats = new ArrayList<>();
|
private List<BoatInRace> boats = new ArrayList<>();
|
||||||
private Color[] colors = {Color.BLUEVIOLET, Color.BLACK, Color.RED, Color.ORANGE, Color.DARKOLIVEGREEN, Color.LIMEGREEN};//TODO make this established in xml or come up with a better system.
|
private Color[] colors = {Color.BLUEVIOLET, Color.BLACK, Color.RED, Color.ORANGE, Color.DARKOLIVEGREEN, Color.LIMEGREEN};//TODO make this established in xml or come up with a better system.
|
||||||
private List<Leg> legs = new ArrayList<>();
|
private List<Leg> legs = new ArrayList<>();
|
||||||
@@ -55,11 +56,20 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
|||||||
* Read the files
|
* Read the files
|
||||||
*/
|
*/
|
||||||
private void read() {
|
private void read() {
|
||||||
|
readID();
|
||||||
readCourse();
|
readCourse();
|
||||||
readLegs();
|
readLegs();
|
||||||
readBoats();
|
readBoats();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void readID() {
|
||||||
|
|
||||||
|
NodeList race = doc.getElementsByTagName("race");
|
||||||
|
System.out.println(race.getLength());
|
||||||
|
|
||||||
|
raceID = getTextValueOfNode((Element) race.item(0), "raceId");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read all the boats in the XML file
|
* Read all the boats in the XML file
|
||||||
*/
|
*/
|
||||||
@@ -319,4 +329,6 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
|||||||
public GPSCoordinate getMapBottomRight() {
|
public GPSCoordinate getMapBottomRight() {
|
||||||
return mapBottomRight;
|
return mapBottomRight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getRaceId() { return raceID; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<race>
|
<race>
|
||||||
|
<raceId>5326</raceId>
|
||||||
<boats>
|
<boats>
|
||||||
<boat>
|
<boat>
|
||||||
<name>ORACLE TEAM USA</name>
|
<name>ORACLE TEAM USA</name>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<race>
|
<race>
|
||||||
|
|
||||||
<boats>
|
<boats>
|
||||||
<boat>
|
<boat>
|
||||||
<name>ORACLE TEAM USA</name>
|
<name>ORACLE TEAM USA</name>
|
||||||
|
|||||||
Reference in New Issue
Block a user