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  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 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 java.util.logging.Level;
 29  
 import java.util.logging.Logger;
 30  
 import javax.xml.parsers.ParserConfigurationException;
 31  
 import javax.xml.parsers.SAXParser;
 32  
 import javax.xml.parsers.SAXParserFactory;
 33  
 import org.xml.sax.InputSource;
 34  
 import org.xml.sax.SAXException;
 35  
 import org.xml.sax.XMLReader;
 36  
 
 37  
 /**
 38  
  * A simple validating parser for XML Suppression Rules.
 39  
  *
 40  
  * @author Jeremy Long <jeremy.long@owasp.org>
 41  
  */
 42  1
 public class SuppressionParser {
 43  
 
 44  
     /**
 45  
      * JAXP Schema Language. Source: http://docs.oracle.com/javase/tutorial/jaxp/sax/validation.html
 46  
      */
 47  
     public static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
 48  
     /**
 49  
      * W3C XML Schema. Source: http://docs.oracle.com/javase/tutorial/jaxp/sax/validation.html
 50  
      */
 51  
     public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
 52  
     /**
 53  
      * JAXP Schema Source. Source: http://docs.oracle.com/javase/tutorial/jaxp/sax/validation.html
 54  
      */
 55  
     public static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
 56  
 
 57  
     /**
 58  
      * Parses the given xml file and returns a list of the suppression rules contained.
 59  
      *
 60  
      * @param file an xml file containing suppression rules
 61  
      * @return a list of suppression rules
 62  
      * @throws SuppressionParseException thrown if the xml file cannot be parsed
 63  
      */
 64  
     public List<SuppressionRule> parseSuppressionRules(File file) throws SuppressionParseException {
 65  
         try {
 66  1
             final InputStream schemaStream = this.getClass().getClassLoader().getResourceAsStream("schema/suppression.xsd");
 67  1
             final SuppressionHandler handler = new SuppressionHandler();
 68  
 
 69  1
             final SAXParserFactory factory = SAXParserFactory.newInstance();
 70  1
             factory.setNamespaceAware(true);
 71  1
             factory.setValidating(true);
 72  1
             final SAXParser saxParser = factory.newSAXParser();
 73  1
             saxParser.setProperty(SuppressionParser.JAXP_SCHEMA_LANGUAGE, SuppressionParser.W3C_XML_SCHEMA);
 74  1
             saxParser.setProperty(SuppressionParser.JAXP_SCHEMA_SOURCE, new InputSource(schemaStream));
 75  1
             final XMLReader xmlReader = saxParser.getXMLReader();
 76  1
             xmlReader.setErrorHandler(new SuppressionErrorHandler());
 77  1
             xmlReader.setContentHandler(handler);
 78  
 
 79  1
             final InputStream inputStream = new FileInputStream(file);
 80  1
             final Reader reader = new InputStreamReader(inputStream, "UTF-8");
 81  1
             final InputSource in = new InputSource(reader);
 82  
             //in.setEncoding("UTF-8");
 83  
 
 84  1
             xmlReader.parse(in);
 85  
 
 86  1
             return handler.getSuppressionRules();
 87  0
         } catch (ParserConfigurationException ex) {
 88  0
             Logger.getLogger(SuppressionParser.class.getName()).log(Level.FINE, null, ex);
 89  0
             throw new SuppressionParseException(ex);
 90  0
         } catch (SAXException ex) {
 91  0
             Logger.getLogger(SuppressionParser.class.getName()).log(Level.FINE, null, ex);
 92  0
             throw new SuppressionParseException(ex);
 93  0
         } catch (FileNotFoundException ex) {
 94  0
             Logger.getLogger(SuppressionParser.class.getName()).log(Level.FINE, null, ex);
 95  0
             throw new SuppressionParseException(ex);
 96  0
         } catch (IOException ex) {
 97  0
             Logger.getLogger(SuppressionParser.class.getName()).log(Level.FINE, null, ex);
 98  0
             throw new SuppressionParseException(ex);
 99  
         }
 100  
     }
 101  
 }