Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the java file I just updated https://drive.google.com/open?id=0B1hCGiJn2

ID: 3838545 • Letter: H

Question

Here is the java file I just updated

https://drive.google.com/open?id=0B1hCGiJn2wUfazg2bzZ4djZxS0k

https://drive.google.com/file/d/0B1hCGiJn2wUfcW8zaWUtMEcwSUU/view

Overview. We will implement a parser and renderer by supporting documents consisting of lists of elements We will not worry about attributes, children, or nested elements inthis implementation. Our subset of FML only contains 2 elements: document paragraph Your program will read FML documents from a file containing multiple elements and produce output in a Drawing Panel. For example: 1.

Explanation / Answer


FmlExtendedParser.java
--------------------------------
package chegg.xmlparser;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* This class manages a collection of customer accounts for an accounts
* receivable system.
*
* @author
*/
public class FmlExtendedParser implements FmlParser {
   private DocumentBuilder builder;
   private XPath path;
   private String filePath;
   public FmlExtendedParser(String filePath) {
       this.filePath = filePath;
   }
   /**
   * Initialize the list of customerAccounts.
   *
   * @throws ParserConfigurationException
   */
   public FmlExtendedParser() throws ParserConfigurationException {
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       // factory.setValidating(true);
       factory.setIgnoringElementContentWhitespace(true);
       builder = factory.newDocumentBuilder();
       XPathFactory xpfactory = XPathFactory.newInstance();
       path = xpfactory.newXPath();
   }
   @Override
   public List<FmlElement> roots() {
       List<FmlElement> fmlElementList = null;
       try {
           File f = new File(this.filePath);
           Document doc = builder.parse(f);
           if (doc == null)
               return null;
           NodeList nList = doc.getElementsByTagName("document");
           fmlElementList = new ArrayList<FmlElement>();
           for (int i = 0; i < nList.getLength(); i++) {
               if (doc.getElementsByTagName("document").item(i) != null
                       || doc.getElementsByTagName("document").item(i)
                               .getTextContent() != null) {
                   DocumentElement docEle = new DocumentElement("document",
                           doc.getElementsByTagName("document").item(i)
                                   .getTextContent());
                   fmlElementList.add(docEle);
               }
               if (doc.getElementsByTagName("paragrph").item(i) != null
                       || doc.getElementsByTagName("paragrph").item(i)
                               .getTextContent() != null) {
                   ParagraphElement pragraphEle = new ParagraphElement(
                           "paragrph", doc.getElementsByTagName("paragrph")
                                   .item(i).getTextContent());
                   fmlElementList.add(pragraphEle);
               }
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
       return fmlElementList;
   }
}
DocumentElement.java
-------------------------------
package chegg.xmlparser;
public class DocumentElement extends FmlElement {
   public DocumentElement(String name, String text) {
       super(name, text);
   }
}
ParagraphElement.java
-------------------------------
package chegg.xmlparser;
public class ParagraphElement extends FmlElement {
   public ParagraphElement(String name, String text) {
       super(name, text);
   }
}