aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/edu/brown/cs/student/term/parsing/UrlXmlParser.java
blob: 21cd7c5f1b68f71ff24064f7f58e26026983aa61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package edu.brown.cs.student.term.parsing;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.time.Instant;

public class UrlXmlParser extends XmlParser{

  public UrlXmlParser() {
    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) {
    try {
      System.err.println("LOG: To make class for url: " + pathToXml + " in parse() of " + getClass());
      URL url = new URL(pathToXml);
      System.err.println("LOG: To establish urlConnection in parse() of " + getClass());
      URLConnection conn = url.openConnection();
      conn.addRequestProperty("User-Agent", "Chrome");
      System.err.println("LOG: Calling builder.parse() for url: " + pathToXml + " in " +  getClass());
      return builder.parse(conn.getInputStream());
    } catch (SAXException e) {
      System.err.println("INTERNAL: SAX " + getClass() + " : " + e.getClass());
    } catch (IOException e) {
      e.printStackTrace();
      System.err.println("INTERNAL: IO " + getClass() + " : " + e.getClass());
    }
    return null;
  }
}