- Creates marks for building CompoundMarks (formerly Markers) - Rearranged Boat constructor and removed velocity #story[782]main
parent
6e9386f4fb
commit
69cd44bda8
@ -0,0 +1,111 @@
|
|||||||
|
package seng302.DataInput;
|
||||||
|
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
import seng302.Model.Boat;
|
||||||
|
import seng302.Model.Mark;
|
||||||
|
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by cbt24 on 10/05/17.
|
||||||
|
*/
|
||||||
|
public class BoatXMLReader extends XMLReader implements BoatDataSource {
|
||||||
|
private final Map<Integer, Boat> boatMap = new HashMap<>();
|
||||||
|
private final Map<Integer, Mark> markerMap = new HashMap<>();
|
||||||
|
|
||||||
|
public BoatXMLReader(String filePath) throws ParserConfigurationException, SAXException, IOException {
|
||||||
|
this(filePath, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* COnstructor for Boat XML
|
||||||
|
*
|
||||||
|
* @param filePath file path to read
|
||||||
|
* @param read whether or not to read and store the files straight away.
|
||||||
|
* @throws IOException error
|
||||||
|
* @throws SAXException error
|
||||||
|
* @throws ParserConfigurationException error
|
||||||
|
*/
|
||||||
|
public BoatXMLReader(String filePath, boolean read) throws IOException, SAXException, ParserConfigurationException {
|
||||||
|
super(filePath);
|
||||||
|
if (read) {
|
||||||
|
read();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void read() {
|
||||||
|
readSettings();
|
||||||
|
readShapes();
|
||||||
|
readBoats();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readBoats() {
|
||||||
|
Element nBoats = (Element) doc.getElementsByTagName("Boats").item(0);
|
||||||
|
for (int i = 0; i < nBoats.getChildNodes().getLength(); i++) {
|
||||||
|
Node boat = nBoats.getChildNodes().item(i);
|
||||||
|
if (boat.getNodeName().equals("Boat")) {
|
||||||
|
readSingleBoat(boat);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ignored data
|
||||||
|
*/
|
||||||
|
private void readShapes() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ignored data
|
||||||
|
*/
|
||||||
|
private void readSettings() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isYachtNode(Node boatNode) {
|
||||||
|
return boatNode.getAttributes().getNamedItem("Type").getTextContent().toLowerCase().equals("yacht");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the information about one boat
|
||||||
|
* Ignored values: ShapeID, StoweName, HullNum, Skipper, Type
|
||||||
|
*/
|
||||||
|
private void readSingleBoat(Node boatNode) {
|
||||||
|
Boat boat;
|
||||||
|
String country = null;
|
||||||
|
int sourceID = Integer.parseInt(boatNode.getAttributes().getNamedItem("SourceID").getTextContent());
|
||||||
|
String name = boatNode.getAttributes().getNamedItem("BoatName").getTextContent();
|
||||||
|
String shortName = boatNode.getAttributes().getNamedItem("ShortName").getTextContent();
|
||||||
|
if (exists(boatNode, "Country")) country = boatNode.getAttributes().getNamedItem("Country").getTextContent();
|
||||||
|
|
||||||
|
if (isYachtNode(boatNode)) {
|
||||||
|
if (country != null) {
|
||||||
|
boat = new Boat(sourceID, name, country);
|
||||||
|
} else {
|
||||||
|
boat = new Boat(sourceID, name, shortName);
|
||||||
|
}
|
||||||
|
boatMap.put(sourceID, boat);
|
||||||
|
} else {
|
||||||
|
Mark mark = new Mark(name);
|
||||||
|
markerMap.put(sourceID, mark);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Boat> getBoats() {
|
||||||
|
return new ArrayList<>(boatMap.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Mark> getMarkerBoats() {
|
||||||
|
return new ArrayList<>(markerMap.values());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package seng302.Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by cbt24 on 10/05/17.
|
||||||
|
*/
|
||||||
|
public class Mark {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Mark(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue