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.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.Reader;
26 import java.nio.charset.StandardCharsets;
27 import javax.annotation.concurrent.ThreadSafe;
28 import javax.xml.parsers.ParserConfigurationException;
29 import javax.xml.parsers.SAXParser;
30 import org.apache.commons.io.ByteOrderMark;
31 import org.apache.commons.io.input.BOMInputStream;
32 import org.owasp.dependencycheck.utils.XmlUtils;
33 import org.owasp.dependencycheck.xml.XmlInputStream;
34
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.xml.sax.InputSource;
38 import org.xml.sax.SAXException;
39 import org.xml.sax.XMLReader;
40
41
42
43
44
45
46 @ThreadSafe
47 public class PomParser {
48
49
50
51
52 private static final Logger LOGGER = LoggerFactory.getLogger(PomParser.class);
53
54
55
56
57
58
59
60
61
62
63
64 public Model parse(File file) throws PomParseException {
65 try (FileInputStream fis = new FileInputStream(file)) {
66 return parse(fis);
67 } catch (IOException ex) {
68 if (ex instanceof PomParseException) {
69 throw (PomParseException) ex;
70 }
71 LOGGER.debug("", ex);
72 throw new PomParseException(String.format("Unable to parse pom '%s'", file), ex);
73 }
74 }
75
76
77
78
79
80
81
82
83
84
85
86 public Model parseWithoutDocTypeCleanup(File file) throws PomParseException {
87 try (FileInputStream fis = new FileInputStream(file)) {
88 return parseWithoutDocTypeCleanup(fis);
89 } catch (IOException ex) {
90 if (ex instanceof PomParseException) {
91 throw (PomParseException) ex;
92 }
93 LOGGER.debug("", ex);
94 throw new PomParseException(String.format("Unable to parse pom '%s'", file), ex);
95 }
96 }
97
98
99
100
101
102
103
104
105
106
107 public Model parse(InputStream inputStream) throws PomParseException {
108 try {
109 final PomHandler handler = new PomHandler();
110 final XMLReader xmlReader = XmlUtils.buildSecureXmlReader();
111 xmlReader.setContentHandler(handler);
112
113 final BOMInputStream bomStream = BOMInputStream.builder()
114 .setInputStream(new XmlInputStream(new PomProjectInputStream(inputStream))).get();
115 final ByteOrderMark bom = bomStream.getBOM();
116 final String defaultEncoding = StandardCharsets.UTF_8.name();
117 final String charsetName = bom == null ? defaultEncoding : bom.getCharsetName();
118 final Reader reader = new InputStreamReader(bomStream, charsetName);
119 final InputSource in = new InputSource(reader);
120 xmlReader.parse(in);
121 return handler.getModel();
122 } catch (ParserConfigurationException | SAXException | IOException ex) {
123 LOGGER.debug("", ex);
124 throw new PomParseException(ex);
125 }
126 }
127
128
129
130
131
132
133
134
135
136
137 public Model parseWithoutDocTypeCleanup(InputStream inputStream) throws PomParseException {
138 try {
139 final PomHandler handler = new PomHandler();
140 final XMLReader xmlReader = XmlUtils.buildSecureXmlReader();
141 xmlReader.setContentHandler(handler);
142
143 final BOMInputStream bomStream = BOMInputStream.builder().setInputStream(new XmlInputStream(inputStream)).get();
144 final ByteOrderMark bom = bomStream.getBOM();
145 final String defaultEncoding = StandardCharsets.UTF_8.name();
146 final String charsetName = bom == null ? defaultEncoding : bom.getCharsetName();
147 final Reader reader = new InputStreamReader(bomStream, charsetName);
148 final InputSource in = new InputSource(reader);
149 xmlReader.parse(in);
150 return handler.getModel();
151 } catch (ParserConfigurationException | SAXException | IOException ex) {
152 LOGGER.debug("", ex);
153 throw new PomParseException(ex);
154 }
155 }
156 }