parent
721fb5f28a
commit
0719720585
@ -0,0 +1,135 @@
|
||||
package seng302.Data;
|
||||
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import seng302.Exceptions.InvalidBoatDataException;
|
||||
import seng302.Model.BoatInRace;
|
||||
|
||||
|
||||
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.io.StringWriter;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by esa46 on 25/04/17.
|
||||
*/
|
||||
public class BoatData {
|
||||
|
||||
private List<BoatInRace> boatData;
|
||||
|
||||
public BoatData(List<BoatInRace> boatData) {
|
||||
this.boatData = boatData;
|
||||
}
|
||||
|
||||
public String createXML() {
|
||||
|
||||
try {
|
||||
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
|
||||
|
||||
//root element
|
||||
Document doc = docBuilder.newDocument();
|
||||
Element rootElement = doc.createElement("BoatConfig");
|
||||
doc.appendChild(rootElement);
|
||||
|
||||
//Boats element
|
||||
Element boats = doc.createElement("Boats");
|
||||
rootElement.appendChild(boats);
|
||||
|
||||
for (int i=0; i < boatData.size(); i++) {
|
||||
|
||||
//Boat element
|
||||
Element boat = doc.createElement("Boat");
|
||||
|
||||
//Type attribute
|
||||
Attr attrType = doc.createAttribute("Type");
|
||||
attrType.setValue("Mark");
|
||||
boat.setAttributeNode(attrType);
|
||||
|
||||
//SourceID attribute
|
||||
Attr attrSourceID = doc.createAttribute("SourceID");
|
||||
attrSourceID.setValue(Integer.toString(boatData.get(i).getSourceID()));
|
||||
boat.setAttributeNode(attrSourceID);
|
||||
|
||||
//ShapeID attribute
|
||||
Attr attrShapeID = doc.createAttribute("ShapeID");
|
||||
attrShapeID.setValue("0");
|
||||
boat.setAttributeNode(attrShapeID);
|
||||
|
||||
//HullNum attribute
|
||||
Attr attrHullNum = doc.createAttribute("HullNum");
|
||||
attrHullNum.setValue("RG01");
|
||||
boat.setAttributeNode(attrHullNum);
|
||||
|
||||
//StoweName attribute
|
||||
Attr attrStoweName = doc.createAttribute("StoweName");
|
||||
attrStoweName.setValue(boatData.get(i).getAbbrev());
|
||||
boat.setAttributeNode(attrStoweName);
|
||||
|
||||
//ShortName attribute
|
||||
Attr attrShortName = doc.createAttribute("ShortName");
|
||||
attrShortName.setValue(boatData.get(i).getAbbrev());
|
||||
boat.setAttributeNode(attrShortName);
|
||||
|
||||
//BoatName attribute
|
||||
Attr attrBoatName = doc.createAttribute("BoatName");
|
||||
attrBoatName.setValue(boatData.get(i).toString());
|
||||
boat.setAttributeNode(attrBoatName);
|
||||
|
||||
//GPSCoord for element
|
||||
Element GPSCoord = doc.createElement("GPSposition");
|
||||
|
||||
//Z axis attribute
|
||||
Attr attrZCoord = doc.createAttribute("Z");
|
||||
attrZCoord.setValue("0");
|
||||
GPSCoord.setAttributeNode(attrZCoord);
|
||||
|
||||
//Y axis attribute
|
||||
Attr attrYCoord = doc.createAttribute("Y");
|
||||
attrYCoord.setValue(Double.toString(boatData.get(i).getCurrentPosition().getLatitude()));
|
||||
GPSCoord.setAttributeNode(attrYCoord);
|
||||
|
||||
//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);
|
||||
|
||||
//Write boat to boats
|
||||
boats.appendChild(boat);
|
||||
|
||||
}
|
||||
|
||||
// write the content into xml file
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMSource source = new DOMSource(doc);
|
||||
|
||||
//Serialize document.
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
StreamResult result = new StreamResult(stringWriter);
|
||||
transformer.transform(source,result);
|
||||
|
||||
return stringWriter.toString();
|
||||
|
||||
|
||||
} catch (ParserConfigurationException pce) {
|
||||
throw new InvalidBoatDataException();
|
||||
} catch (TransformerException tfe) {
|
||||
throw new InvalidBoatDataException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue