1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.xml.pom;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.Reader;
27 import javax.xml.parsers.ParserConfigurationException;
28 import javax.xml.parsers.SAXParser;
29 import org.owasp.dependencycheck.utils.XmlUtils;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.xml.sax.InputSource;
34 import org.xml.sax.SAXException;
35 import org.xml.sax.XMLReader;
36
37
38
39
40
41
42 public class PomParser {
43
44
45
46
47 private static final Logger LOGGER = LoggerFactory.getLogger(PomParser.class);
48
49
50
51
52
53
54
55
56
57
58 public Model parse(File file) throws PomParseException {
59 FileInputStream fis = null;
60 try {
61 fis = new FileInputStream(file);
62 return parse(fis);
63 } catch (IOException ex) {
64 LOGGER.debug("", ex);
65 throw new PomParseException(ex);
66 } finally {
67 if (fis != null) {
68 try {
69 fis.close();
70 } catch (IOException ex) {
71 LOGGER.debug("Unable to close stream", ex);
72 }
73 }
74 }
75 }
76
77
78
79
80
81
82
83
84
85 public Model parse(InputStream inputStream) throws PomParseException {
86 try {
87 final PomHandler handler = new PomHandler();
88 final SAXParser saxParser = XmlUtils.buildSecureSaxParser();
89 final XMLReader xmlReader = saxParser.getXMLReader();
90 xmlReader.setContentHandler(handler);
91 final Reader reader = new InputStreamReader(inputStream, "UTF-8");
92 final InputSource in = new InputSource(reader);
93 xmlReader.parse(in);
94 return handler.getModel();
95 } catch (ParserConfigurationException ex) {
96 LOGGER.debug("", ex);
97 throw new PomParseException(ex);
98 } catch (SAXException ex) {
99 LOGGER.debug("", ex);
100 throw new PomParseException(ex);
101 } catch (FileNotFoundException ex) {
102 LOGGER.debug("", ex);
103 throw new PomParseException(ex);
104 } catch (IOException ex) {
105 LOGGER.debug("", ex);
106 throw new PomParseException(ex);
107 }
108 }
109 }