From afd767bf26c6853c36178e2fc0d091ba1b598fea Mon Sep 17 00:00:00 2001 From: Michael Foiani Date: Mon, 5 Apr 2021 14:35:49 -0400 Subject: Added a basic xml parser that deals with local files. TODO: add a url one. Also, added some tests to ensure it's ok. Needs some edge case testing still. --- .../cs/student/term/parsing/LocalXmlParser.java | 38 ++++++++++++++++++++++ .../cs/student/term/parsing/UrlXmlParser.java | 2 ++ .../brown/cs/student/term/parsing/XmlParser.java | 37 +++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/main/java/edu/brown/cs/student/term/parsing/LocalXmlParser.java create mode 100644 src/main/java/edu/brown/cs/student/term/parsing/UrlXmlParser.java create mode 100644 src/main/java/edu/brown/cs/student/term/parsing/XmlParser.java (limited to 'src/main') diff --git a/src/main/java/edu/brown/cs/student/term/parsing/LocalXmlParser.java b/src/main/java/edu/brown/cs/student/term/parsing/LocalXmlParser.java new file mode 100644 index 0000000..27c3988 --- /dev/null +++ b/src/main/java/edu/brown/cs/student/term/parsing/LocalXmlParser.java @@ -0,0 +1,38 @@ +package edu.brown.cs.student.term.parsing; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import java.io.File; +import java.io.IOException; + +public class LocalXmlParser extends XmlParser { + public LocalXmlParser() { + super(); + } + + /** + * Method used to parse the xml file. + * + * @param pathToXml The path to the xml text file. + * @return The tree structure parsed as an xml doc. + */ + @Override + public Document parse(String pathToXml) { + // TODO: change to online hosted file option + // Creating the file reference. + System.err.println("LOG: To make file reference for " + pathToXml + " in " + getClass()); + File file = new File(pathToXml); + + // Parsing the file. + try { + System.err.println("LOG: Calling builder.parse() in " + getClass()); + return builder.parse(file); + } catch (SAXException e) { + System.err.println("INTERNAL: SAX " + getClass() + " : " + e.getClass()); + } catch (IOException e) { + System.err.println("INTERNAL: IO " + getClass() + " : " + e.getClass()); + } + return null; + } +} diff --git a/src/main/java/edu/brown/cs/student/term/parsing/UrlXmlParser.java b/src/main/java/edu/brown/cs/student/term/parsing/UrlXmlParser.java new file mode 100644 index 0000000..adad835 --- /dev/null +++ b/src/main/java/edu/brown/cs/student/term/parsing/UrlXmlParser.java @@ -0,0 +1,2 @@ +package edu.brown.cs.student.term.parsing;public class UrlXmlParser { +} diff --git a/src/main/java/edu/brown/cs/student/term/parsing/XmlParser.java b/src/main/java/edu/brown/cs/student/term/parsing/XmlParser.java new file mode 100644 index 0000000..d8182d6 --- /dev/null +++ b/src/main/java/edu/brown/cs/student/term/parsing/XmlParser.java @@ -0,0 +1,37 @@ +package edu.brown.cs.student.term.parsing; + +import org.w3c.dom.Document; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +public abstract class XmlParser { + protected DocumentBuilder builder = null; + + /** + * This constructor crates and saves the builder that turns the xml text into a tree stricture. + */ + protected XmlParser() { + // Builds the immutable factory + System.err.println("LOG: Constructor of " + getClass() + ". To make XML parser factory."); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setValidating(true); + factory.setIgnoringElementContentWhitespace(true); + + // Creates the builder from the factory + try { + System.err.println("LOG: To make documentBuilder in " + getClass()); + builder = factory.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + System.err.println("INTERNAL: " + getClass() + " : " + e.getClass()); + } + } + + /** + * Method used to parse the xml file. + * @param pathToXml The path to the xml text file. + * @return The tree structure parsed as an xml doc. + */ + public abstract Document parse(String pathToXml); +} -- cgit v1.2.3-70-g09d2