mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Created course parser as a subclass of file parser
- refactored file parser as an abstract class - created course parser to parse course xml file #implement #fix #refactor #story[9] #story[10]
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
package seng302.models.parsers;
|
||||
|
||||
import org.w3c.dom.*;
|
||||
import seng302.models.GateMark;
|
||||
import seng302.models.Mark;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* parse a course xml file
|
||||
* Created by Haoming Yin (hyi25) on 16/3/2017
|
||||
*/
|
||||
public class CourseParser extends FileParser {
|
||||
|
||||
private Document doc;
|
||||
|
||||
public CourseParser(String path) {
|
||||
super(path);
|
||||
this.doc = this.parseFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* create a mark by given node
|
||||
*
|
||||
* @param node
|
||||
* @return a mark, or null if fails to create a mark
|
||||
*/
|
||||
private Mark generateMark(Node node) {
|
||||
try {
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
String name = element.getElementsByTagName("name").item(0).getTextContent();
|
||||
double lat = Double.valueOf(element.getElementsByTagName("latitude").item(0).getTextContent());
|
||||
double lon = Double.valueOf(element.getElementsByTagName("longitude").item(0).getTextContent());
|
||||
Mark mark = new Mark(name, lat, lon);
|
||||
return mark;
|
||||
} else {
|
||||
throw new NoSuchElementException("Cannot generate a mark by given node.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* generate an arrayList of gates
|
||||
*
|
||||
* @return an arrayList of gates, or null if no gate has been found.
|
||||
*/
|
||||
public ArrayList<GateMark> getGates() {
|
||||
ArrayList<GateMark> gateMarks = new ArrayList<>();
|
||||
|
||||
try {
|
||||
NodeList nodes = doc.getElementsByTagName("gate");
|
||||
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
Node node = nodes.item(i);
|
||||
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
String name = element.getElementsByTagName("name").item(0).getTextContent();
|
||||
Mark mark1 = generateMark(element.getElementsByTagName("mark").item(0));
|
||||
Mark mark2 = generateMark(element.getElementsByTagName("mark").item(1));
|
||||
GateMark gateMark = new GateMark(name, mark1, mark2);
|
||||
gateMarks.add(gateMark);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return gateMarks;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate an arrayList of marks
|
||||
*
|
||||
* @return an arrayList of marks, or null if no gate has been found.
|
||||
*/
|
||||
public ArrayList<Mark> getMarks() {
|
||||
ArrayList<Mark> marks = new ArrayList<>();
|
||||
|
||||
try {
|
||||
// find the "marks" tag
|
||||
Node node = doc.getElementsByTagName("marks").item(0);
|
||||
// iterate all "marks"'s children
|
||||
for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
|
||||
// if node's tag name is "mark"
|
||||
if (n.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) n;
|
||||
if (element.getNodeName() == "mark") {
|
||||
marks.add(generateMark(n));
|
||||
}
|
||||
}
|
||||
}
|
||||
return marks;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* return the order of all the marks along a course
|
||||
*
|
||||
* @return an arrayList of the names of ordered course marks
|
||||
*/
|
||||
public ArrayList<String> getOrder() {
|
||||
ArrayList<String> markOrder = new ArrayList<>();
|
||||
|
||||
try {
|
||||
Node orderNode = doc.getElementsByTagName("order").item(0);
|
||||
for (Node node = orderNode.getFirstChild(); node != null; node = node.getNextSibling()) {
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
String name = element.getTextContent();
|
||||
markOrder.add(name);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return markOrder;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package seng302.models.parsers;
|
||||
|
||||
import org.w3c.dom.*;
|
||||
import javax.xml.parsers.*;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* Created by Haoming Yin (hyi25) on 16/3/2017
|
||||
*/
|
||||
public abstract class FileParser {
|
||||
|
||||
private String filePath;
|
||||
|
||||
public FileParser(String path) {
|
||||
this.filePath = path;
|
||||
}
|
||||
|
||||
protected Document parseFile () {
|
||||
try {
|
||||
File file = new File(this.filePath);
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document doc = builder.parse(file);
|
||||
// optional, in order to recover info from broken line.
|
||||
doc.getDocumentElement().normalize();
|
||||
return doc;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user