Refactored BoatData

#story[778]
main
Erika Savell 9 years ago
parent 302f2c6869
commit ba5c965fd6

@ -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,49 +47,98 @@ public class BoatData {
Element boats = doc.createElement("Boats");
rootElement.appendChild(boats);
for (int i=0; i < boatData.size(); i++) {
appendIndividualBoats(boats);
// 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();
//Boat element
} catch (ParserConfigurationException pce) {
throw new InvalidBoatDataException();
} catch (TransformerException tfe) {
throw new InvalidBoatDataException();
}
}
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("Mark");
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");
@ -105,31 +156,6 @@ public class BoatData {
//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…
Cancel
Save