You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.8 KiB

package seng302.Mock;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import seng302.XMLReader;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by jjg64 on 19/04/17.
*/
public class RegattaXMLReader extends XMLReader {
private Regatta regatta;
/**
* Constructor for Regatta XML
*
* @param filePath path of the file
* @throws IOException error
* @throws SAXException error
* @throws ParserConfigurationException error
*/
public RegattaXMLReader(String filePath) throws IOException, SAXException, ParserConfigurationException {
this(filePath, true);
}
/**
* Constructor for Regatta 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 RegattaXMLReader(String filePath, boolean read) throws IOException, SAXException, ParserConfigurationException {
super(filePath);
if (read) {
read();
}
}
public RegattaXMLReader(InputStream xmlString) throws IOException, SAXException, ParserConfigurationException {
super(xmlString);
read();
}
/**
* Read the XML
*/
private void read() {
NodeList attributeConfig = doc.getElementsByTagName("RegattaConfig");
Element attributes = (Element) attributeConfig.item(0);
makeRegatta(attributes);
}
private void makeRegatta(Element attributes) {
int regattaID = Integer.parseInt(getTextValueOfNode(attributes, "RegattaID"));
String regattaName = getTextValueOfNode(attributes, "RegattaName");
String courseName = getTextValueOfNode(attributes, "CourseName");
double centralLatitude = Double.parseDouble(getTextValueOfNode(attributes, "CentralLatitude"));
double centralLongitude = Double.parseDouble(getTextValueOfNode(attributes, "CentralLongitude"));
System.out.println(String.format("central lat %s long %s", centralLatitude, centralLongitude));
double centralAltitude = Double.parseDouble(getTextValueOfNode(attributes, "CentralAltitude"));
float utcOffset = Float.parseFloat(getTextValueOfNode(attributes, "UtcOffset"));
float magneticVariation = Float.parseFloat(getTextValueOfNode(attributes, "MagneticVariation"));
regatta = new Regatta(regattaID, regattaName, courseName, centralLatitude, centralLongitude, centralAltitude, utcOffset, magneticVariation);
}
public Regatta getRegatta() {
return regatta;
}
}