| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.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 javax.xml.parsers.SAXParserFactory; |
| 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 | 6 | public class SuppressionParser { |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | 1 | private static final Logger LOGGER = LoggerFactory.getLogger(SuppressionParser.class); |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public List<SuppressionRule> parseSuppressionRules(File file) throws SuppressionParseException { |
| 70 | 4 | FileInputStream fis = null; |
| 71 | |
try { |
| 72 | 4 | fis = new FileInputStream(file); |
| 73 | 3 | return parseSuppressionRules(fis); |
| 74 | 1 | } catch (IOException ex) { |
| 75 | 1 | LOGGER.debug("", ex); |
| 76 | 1 | throw new SuppressionParseException(ex); |
| 77 | |
} finally { |
| 78 | 4 | if (fis != null) { |
| 79 | |
try { |
| 80 | 3 | fis.close(); |
| 81 | 0 | } catch (IOException ex) { |
| 82 | 0 | LOGGER.debug("Unable to close stream", ex); |
| 83 | 7 | } |
| 84 | |
} |
| 85 | |
} |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public List<SuppressionRule> parseSuppressionRules(InputStream inputStream) throws SuppressionParseException { |
| 96 | |
try { |
| 97 | 8 | final InputStream schemaStream = this.getClass().getClassLoader().getResourceAsStream("schema/suppression.xsd"); |
| 98 | 8 | final SuppressionHandler handler = new SuppressionHandler(); |
| 99 | 8 | final SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 100 | 8 | factory.setNamespaceAware(true); |
| 101 | 8 | factory.setValidating(true); |
| 102 | 8 | final SAXParser saxParser = factory.newSAXParser(); |
| 103 | 8 | saxParser.setProperty(SuppressionParser.JAXP_SCHEMA_LANGUAGE, SuppressionParser.W3C_XML_SCHEMA); |
| 104 | 8 | saxParser.setProperty(SuppressionParser.JAXP_SCHEMA_SOURCE, new InputSource(schemaStream)); |
| 105 | 8 | final XMLReader xmlReader = saxParser.getXMLReader(); |
| 106 | 8 | xmlReader.setErrorHandler(new SuppressionErrorHandler()); |
| 107 | 8 | xmlReader.setContentHandler(handler); |
| 108 | |
|
| 109 | 8 | final Reader reader = new InputStreamReader(inputStream, "UTF-8"); |
| 110 | 8 | final InputSource in = new InputSource(reader); |
| 111 | |
|
| 112 | |
|
| 113 | 8 | xmlReader.parse(in); |
| 114 | |
|
| 115 | 8 | return handler.getSuppressionRules(); |
| 116 | 0 | } catch (ParserConfigurationException ex) { |
| 117 | 0 | LOGGER.debug("", ex); |
| 118 | 0 | throw new SuppressionParseException(ex); |
| 119 | 0 | } catch (SAXException ex) { |
| 120 | 0 | LOGGER.debug("", ex); |
| 121 | 0 | throw new SuppressionParseException(ex); |
| 122 | 0 | } catch (FileNotFoundException ex) { |
| 123 | 0 | LOGGER.debug("", ex); |
| 124 | 0 | throw new SuppressionParseException(ex); |
| 125 | 0 | } catch (IOException ex) { |
| 126 | 0 | LOGGER.debug("", ex); |
| 127 | 0 | throw new SuppressionParseException(ex); |
| 128 | |
} |
| 129 | |
} |
| 130 | |
} |