Refactored BoatData
#story[778]
This commit is contained in:
@@ -25,6 +25,8 @@ import java.util.List;
|
||||
public class BoatData {
|
||||
|
||||
private List<BoatInRace> boatData;
|
||||
Document doc;
|
||||
|
||||
|
||||
public BoatData(List<BoatInRace> boatData) {
|
||||
this.boatData = boatData;
|
||||
@@ -37,7 +39,7 @@ public class BoatData {
|
||||
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
|
||||
|
||||
//root element
|
||||
Document doc = docBuilder.newDocument();
|
||||
doc = docBuilder.newDocument();
|
||||
Element rootElement = doc.createElement("BoatConfig");
|
||||
doc.appendChild(rootElement);
|
||||
|
||||
@@ -45,71 +47,7 @@ public class BoatData {
|
||||
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);
|
||||
|
||||
}
|
||||
appendIndividualBoats(boats);
|
||||
|
||||
// write the content into xml file
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
@@ -132,4 +70,92 @@ public class BoatData {
|
||||
}
|
||||
|
||||
|
||||
private void appendIndividualBoats(Element boatsElement) {
|
||||
|
||||
for (int i=0; i < boatData.size(); i++) {
|
||||
Element boat = doc.createElement("Boat");
|
||||
appendType(boat);
|
||||
appendSourceID(boat, i);
|
||||
appendShapeID(boat);
|
||||
appendHullNum(boat);
|
||||
appendStoweName(boat, i);
|
||||
appendShortName(boat, i);
|
||||
appendBoatName(boat, i);
|
||||
appendGPSCoords(boat, i);
|
||||
//Write boat to boats
|
||||
boatsElement.appendChild(boat);
|
||||
}
|
||||
}
|
||||
|
||||
private void appendType(Element boat) {
|
||||
//Type attribute
|
||||
Attr attrType = doc.createAttribute("Type");
|
||||
attrType.setValue("Yacht");
|
||||
boat.setAttributeNode(attrType);
|
||||
}
|
||||
|
||||
private void appendSourceID(Element boat, int i) {
|
||||
//SourceID attribute
|
||||
Attr attrSourceID = doc.createAttribute("SourceID");
|
||||
attrSourceID.setValue(Integer.toString(boatData.get(i).getSourceID()));
|
||||
boat.setAttributeNode(attrSourceID);
|
||||
}
|
||||
|
||||
private void appendShapeID(Element boat) {
|
||||
//ShapeID attribute
|
||||
Attr attrShapeID = doc.createAttribute("ShapeID");
|
||||
attrShapeID.setValue("0");
|
||||
boat.setAttributeNode(attrShapeID);
|
||||
}
|
||||
|
||||
private void appendHullNum(Element boat) {
|
||||
//HullNum attribute
|
||||
Attr attrHullNum = doc.createAttribute("HullNum");
|
||||
attrHullNum.setValue("RG01");
|
||||
boat.setAttributeNode(attrHullNum);
|
||||
}
|
||||
|
||||
private void appendStoweName(Element boat, int i) {
|
||||
//StoweName attribute
|
||||
Attr attrStoweName = doc.createAttribute("StoweName");
|
||||
attrStoweName.setValue(boatData.get(i).getAbbrev());
|
||||
boat.setAttributeNode(attrStoweName);
|
||||
}
|
||||
|
||||
private void appendShortName(Element boat, int i) {
|
||||
//ShortName attribute
|
||||
Attr attrShortName = doc.createAttribute("ShortName");
|
||||
attrShortName.setValue(boatData.get(i).getAbbrev());
|
||||
boat.setAttributeNode(attrShortName);
|
||||
}
|
||||
|
||||
private void appendBoatName(Element boat, int i) {
|
||||
//BoatName attribute
|
||||
Attr attrBoatName = doc.createAttribute("BoatName");
|
||||
attrBoatName.setValue(boatData.get(i).toString());
|
||||
boat.setAttributeNode(attrBoatName);
|
||||
}
|
||||
|
||||
private void appendGPSCoords(Element boat, int i) {
|
||||
//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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user