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); }