Coverage Report - org.owasp.dependencycheck.suppression.SuppressionParser
 
Classes in this File Line Coverage Branch Coverage Complexity
SuppressionParser
58%
17/29
N/A
10
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Dependency-check-core is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-core is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-core. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 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  
  * A simple validating parser for XML Suppression Rules.
 40  
  *
 41  
  * @author Jeremy Long <jeremy.long@owasp.org>
 42  
  */
 43  1
 public class SuppressionParser {
 44  
 
 45  
     /**
 46  
      * JAXP Schema Language. Source: http://docs.oracle.com/javase/tutorial/jaxp/sax/validation.html
 47  
      */
 48  
     public static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
 49  
     /**
 50  
      * W3C XML Schema. Source: http://docs.oracle.com/javase/tutorial/jaxp/sax/validation.html
 51  
      */
 52  
     public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
 53  
     /**
 54  
      * JAXP Schema Source. Source: http://docs.oracle.com/javase/tutorial/jaxp/sax/validation.html
 55  
      */
 56  
     public static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
 57  
 
 58  
     /**
 59  
      * Parses the given xml file and returns a list of the suppression rules contained.
 60  
      *
 61  
      * @param file an xml file containing suppression rules
 62  
      * @return a list of suppression rules
 63  
      * @throws SuppressionParseException thrown if the xml file cannot be parsed
 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  
             //in.setEncoding("UTF-8");
 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  
 }