diff --git a/doc/examples/config.xml b/doc/examples/config.xml index 34e008cc..4a1b0770 100644 --- a/doc/examples/config.xml +++ b/doc/examples/config.xml @@ -4,5 +4,6 @@ AC35 6 1.0 + 135 diff --git a/src/main/java/seng302/models/parsers/ConfigParser.java b/src/main/java/seng302/models/parsers/ConfigParser.java new file mode 100644 index 00000000..74d0986a --- /dev/null +++ b/src/main/java/seng302/models/parsers/ConfigParser.java @@ -0,0 +1,80 @@ +package seng302.models.parsers; + + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import java.util.DoubleSummaryStatistics; + +public class ConfigParser extends FileParser { + + private Document doc; + + public ConfigParser(String path) { + super(path); + this.doc = this.parseFile(); + } + + /** + * Gets wind direction from config file. + * + * @return a double type degree, or 0 if no value or invalid value is found + */ + public double getWindDirection() { + return getDoubleByTagName("wind-direction", 0.0); + } + + /** + * Gets a non negative time scale for the race + * + * @return a double type scale, or 0 if no scale or invalid scale is found + */ + public double getTimeScale() { + return getDoubleByTagName("time-scale", 1.0); + } + + /** + * Gets a double type number by given tag name found in xml file + * + * @param tagName a string of tag name + * @param defaultVal value returned if no value or invalid value is found + * @return value found + */ + public double getDoubleByTagName(String tagName, double defaultVal) { + double val = defaultVal; + try { + Node node = this.doc.getElementsByTagName(tagName).item(0); + if (node.getNodeType() == Node.ELEMENT_NODE) { + Element element = (Element) node; + val = Double.valueOf(element.getTextContent()); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + return val; + } + } + + /** + * Gets a string by given tag name found in xml file + * + * @param tagName a string of tag name + * @param defaultVal a string returned if no value or invalid value is found + * @return string found + */ + public String getStringByTagName(String tagName, String defaultVal) { + String string = defaultVal; + try { + Node node = this.doc.getElementsByTagName(tagName).item(0); + if (node.getNodeType() == Node.ELEMENT_NODE) { + Element element = (Element) node; + string = element.getTextContent(); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + return string; + } + } +} diff --git a/src/test/java/seng302/models/parsers/ConfigParserTest.java b/src/test/java/seng302/models/parsers/ConfigParserTest.java new file mode 100644 index 00000000..26387220 --- /dev/null +++ b/src/test/java/seng302/models/parsers/ConfigParserTest.java @@ -0,0 +1,42 @@ +package seng302.models.parsers; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by Haoming on 23/03/17. + */ +public class ConfigParserTest { + + private ConfigParser cp; + + @Before + public void initializeParser() throws Exception { + cp = new ConfigParser("doc/examples/config.xml"); + } + + @Test + public void getWindDirection() throws Exception { + assertEquals(135, cp.getWindDirection(), 1e-10); + } + + @Test + public void getTimeScale() throws Exception { + assertEquals(1.0, cp.getTimeScale(), 1e-10); + } + + @Test + public void getDoubleByTagName() throws Exception { + assertEquals(6, cp.getDoubleByTagName("race-size", 0), 1e-10); + assertEquals(100, cp.getDoubleByTagName("noTag", 100), 1e-10); + } + + @Test + public void getStringByTagName() throws Exception { + assertEquals("AC35", cp.getStringByTagName("race-name", "11")); + assertEquals("oops", cp.getStringByTagName("noTag", "oops")); + } + +} \ No newline at end of file