| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.owasp.dependencycheck.suppression; |
| 20 | |
|
| 21 | |
import java.io.File; |
| 22 | |
import java.io.FileInputStream; |
| 23 | |
import java.io.FileNotFoundException; |
| 24 | |
import java.io.IOException; |
| 25 | |
import java.io.InputStream; |
| 26 | |
import java.io.InputStreamReader; |
| 27 | |
import java.io.Reader; |
| 28 | |
import java.util.List; |
| 29 | |
import java.util.logging.Level; |
| 30 | |
import java.util.logging.Logger; |
| 31 | |
import javax.xml.parsers.ParserConfigurationException; |
| 32 | |
import javax.xml.parsers.SAXParser; |
| 33 | |
import javax.xml.parsers.SAXParserFactory; |
| 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 | 1 | public class SuppressionParser { |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public List<SuppressionRule> parseSuppressionRules(File file) throws SuppressionParseException { |
| 66 | |
try { |
| 67 | 1 | final InputStream schemaStream = this.getClass().getClassLoader().getResourceAsStream("schema/suppression.xsd"); |
| 68 | 1 | final SuppressionHandler handler = new SuppressionHandler(); |
| 69 | |
|
| 70 | 1 | final SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 71 | 1 | factory.setNamespaceAware(true); |
| 72 | 1 | factory.setValidating(true); |
| 73 | 1 | final SAXParser saxParser = factory.newSAXParser(); |
| 74 | 1 | saxParser.setProperty(SuppressionParser.JAXP_SCHEMA_LANGUAGE, SuppressionParser.W3C_XML_SCHEMA); |
| 75 | 1 | saxParser.setProperty(SuppressionParser.JAXP_SCHEMA_SOURCE, new InputSource(schemaStream)); |
| 76 | 1 | final XMLReader xmlReader = saxParser.getXMLReader(); |
| 77 | 1 | xmlReader.setErrorHandler(new SuppressionErrorHandler()); |
| 78 | 1 | xmlReader.setContentHandler(handler); |
| 79 | |
|
| 80 | 1 | final InputStream inputStream = new FileInputStream(file); |
| 81 | 1 | final Reader reader = new InputStreamReader(inputStream, "UTF-8"); |
| 82 | 1 | final InputSource in = new InputSource(reader); |
| 83 | |
|
| 84 | |
|
| 85 | 1 | xmlReader.parse(in); |
| 86 | |
|
| 87 | 1 | return handler.getSuppressionRules(); |
| 88 | 0 | } catch (ParserConfigurationException ex) { |
| 89 | 0 | Logger.getLogger(SuppressionParser.class.getName()).log(Level.FINE, null, ex); |
| 90 | 0 | throw new SuppressionParseException(ex); |
| 91 | 0 | } catch (SAXException ex) { |
| 92 | 0 | Logger.getLogger(SuppressionParser.class.getName()).log(Level.FINE, null, ex); |
| 93 | 0 | throw new SuppressionParseException(ex); |
| 94 | 0 | } catch (FileNotFoundException ex) { |
| 95 | 0 | Logger.getLogger(SuppressionParser.class.getName()).log(Level.FINE, null, ex); |
| 96 | 0 | throw new SuppressionParseException(ex); |
| 97 | 0 | } catch (IOException ex) { |
| 98 | 0 | Logger.getLogger(SuppressionParser.class.getName()).log(Level.FINE, null, ex); |
| 99 | 0 | throw new SuppressionParseException(ex); |
| 100 | |
} |
| 101 | |
} |
| 102 | |
} |