1 package net.sf.mavenhist.extractor.util;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStream;
7
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import javax.xml.parsers.ParserConfigurationException;
11
12 import org.w3c.dom.Document;
13 import org.xml.sax.InputSource;
14 import org.xml.sax.SAXException;
15
16 /**
17 * Utility methods to read and parse an xml file.
18 */
19 public final class DocumentReader {
20
21 /**
22 * Private contructor for utility class.
23 */
24 private DocumentReader() { }
25
26 /**
27 * Gets the parsed document from a file path.
28 * @param filePath e.g. <code>src/test/resources/org/xxx/checkstyle-raw-report.xml</code>
29 * @return loaded document.
30 * @throws ParserConfigurationException
31 * @throws IOException IO Error occured when reading file
32 * @throws SAXException File is unparsable
33 * @throws Exception it's a test so this may happen.
34 */
35 public static Document getParsedDoc(InputStream inputStream) throws ParserConfigurationException, SAXException, FileNotFoundException, IOException {
36 InputSource in = new InputSource(inputStream);
37 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
38 DocumentBuilder builder = dfactory.newDocumentBuilder();
39 builder.setEntityResolver(new RemoveDTDLookupResolver());
40 return builder.parse(in);
41 }
42
43 public static InputStream getInputStream(String filePath) throws FileNotFoundException {
44 return new FileInputStream(filePath);
45 }
46 }