1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.xml.suppression;
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 java.util.List;
28 import javax.xml.parsers.ParserConfigurationException;
29 import javax.xml.parsers.SAXParser;
30 import org.owasp.dependencycheck.utils.XmlUtils;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.xml.sax.InputSource;
35 import org.xml.sax.SAXException;
36 import org.xml.sax.XMLReader;
37
38
39
40
41
42
43 public class SuppressionParser {
44
45
46
47
48 private static final Logger LOGGER = LoggerFactory.getLogger(SuppressionParser.class);
49
50
51
52 public static final String SUPPRESSION_SCHEMA = "schema/dependency-suppression.1.1.xsd";
53
54
55
56 private static final String OLD_SUPPRESSION_SCHEMA = "schema/suppression.xsd";
57
58
59
60
61
62
63
64
65
66 public List<SuppressionRule> parseSuppressionRules(File file) throws SuppressionParseException {
67 FileInputStream fis = null;
68 try {
69 fis = new FileInputStream(file);
70 return parseSuppressionRules(fis);
71 } catch (IOException ex) {
72 LOGGER.debug("", ex);
73 throw new SuppressionParseException(ex);
74 } catch (SAXException ex) {
75 try {
76 if (fis != null) {
77 try {
78 fis.close();
79 } catch (IOException ex1) {
80 LOGGER.debug("Unable to close stream", ex1);
81 }
82 }
83 fis = new FileInputStream(file);
84 } catch (FileNotFoundException ex1) {
85 throw new SuppressionParseException(ex);
86 }
87 try {
88 return parseSuppressionRules(fis, OLD_SUPPRESSION_SCHEMA);
89 } catch (SAXException ex1) {
90 throw new SuppressionParseException(ex);
91 }
92 } finally {
93 if (fis != null) {
94 try {
95 fis.close();
96 } catch (IOException ex) {
97 LOGGER.debug("Unable to close stream", ex);
98 }
99 }
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112 public List<SuppressionRule> parseSuppressionRules(InputStream inputStream) throws SuppressionParseException, SAXException {
113 return parseSuppressionRules(inputStream, SUPPRESSION_SCHEMA);
114 }
115
116
117
118
119
120
121
122
123
124
125
126 private List<SuppressionRule> parseSuppressionRules(InputStream inputStream, String schema) throws SuppressionParseException, SAXException {
127 InputStream schemaStream = null;
128 try {
129 schemaStream = this.getClass().getClassLoader().getResourceAsStream(schema);
130 final SuppressionHandler handler = new SuppressionHandler();
131 final SAXParser saxParser = XmlUtils.buildSecureSaxParser(schemaStream);
132 final XMLReader xmlReader = saxParser.getXMLReader();
133 xmlReader.setErrorHandler(new SuppressionErrorHandler());
134 xmlReader.setContentHandler(handler);
135 final Reader reader = new InputStreamReader(inputStream, "UTF-8");
136 final InputSource in = new InputSource(reader);
137 xmlReader.parse(in);
138 return handler.getSuppressionRules();
139 } catch (ParserConfigurationException ex) {
140 LOGGER.debug("", ex);
141 throw new SuppressionParseException(ex);
142 } catch (SAXException ex) {
143 if (ex.getMessage().contains("Cannot find the declaration of element 'suppressions'.")) {
144 throw ex;
145 } else {
146 LOGGER.debug("", ex);
147 throw new SuppressionParseException(ex);
148 }
149 } catch (FileNotFoundException ex) {
150 LOGGER.debug("", ex);
151 throw new SuppressionParseException(ex);
152 } catch (IOException ex) {
153 LOGGER.debug("", ex);
154 throw new SuppressionParseException(ex);
155 } finally {
156 if (schemaStream != null) {
157 try {
158 schemaStream.close();
159 } catch (IOException ex) {
160 LOGGER.debug("Error closing suppression file stream", ex);
161 }
162 }
163 }
164 }
165 }