Coverage Report - org.owasp.dependencycheck.xml.pom.PomParser
 
Classes in this File Line Coverage Branch Coverage Complexity
PomParser
50%
17/34
50%
1/2
8
 
 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) 2015 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.xml.pom;
 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 javax.xml.parsers.ParserConfigurationException;
 28  
 import javax.xml.parsers.SAXParser;
 29  
 import javax.xml.parsers.SAXParserFactory;
 30  
 
 31  
 import org.slf4j.Logger;
 32  
 import org.slf4j.LoggerFactory;
 33  
 import org.xml.sax.InputSource;
 34  
 import org.xml.sax.SAXException;
 35  
 import org.xml.sax.XMLReader;
 36  
 
 37  
 /**
 38  
  * A parser for pom.xml files.
 39  
  *
 40  
  * @author Jeremy Long
 41  
  */
 42  2
 public class PomParser {
 43  
 
 44  
     /**
 45  
      * The logger.
 46  
      */
 47  1
     private static final Logger LOGGER = LoggerFactory.getLogger(PomParser.class);
 48  
 
 49  
     /**
 50  
      * Parses the given xml file and returns a Model object containing only the fields dependency-check requires.
 51  
      *
 52  
      * @param file a pom.xml
 53  
      * @return a Model object containing only the fields dependency-check requires
 54  
      * @throws PomParseException thrown if the xml file cannot be parsed
 55  
      */
 56  
     public Model parse(File file) throws PomParseException {
 57  1
         FileInputStream fis = null;
 58  
         try {
 59  1
             fis = new FileInputStream(file);
 60  1
             return parse(fis);
 61  0
         } catch (IOException ex) {
 62  0
             LOGGER.debug("", ex);
 63  0
             throw new PomParseException(ex);
 64  
         } finally {
 65  1
             if (fis != null) {
 66  
                 try {
 67  1
                     fis.close();
 68  0
                 } catch (IOException ex) {
 69  0
                     LOGGER.debug("Unable to close stream", ex);
 70  2
                 }
 71  
             }
 72  
         }
 73  
     }
 74  
 
 75  
     /**
 76  
      * Parses the given XML file and returns a Model object containing only the fields dependency-check requires.
 77  
      *
 78  
      * @param inputStream an InputStream containing suppression rues
 79  
      * @return a list of suppression rules
 80  
      * @throws PomParseException if the XML cannot be parsed
 81  
      */
 82  
     public Model parse(InputStream inputStream) throws PomParseException {
 83  
         try {
 84  2
             final PomHandler handler = new PomHandler();
 85  2
             final SAXParserFactory factory = SAXParserFactory.newInstance();
 86  
 //            factory.setNamespaceAware(true);
 87  
 //            factory.setValidating(true);
 88  2
             final SAXParser saxParser = factory.newSAXParser();
 89  2
             final XMLReader xmlReader = saxParser.getXMLReader();
 90  2
             xmlReader.setContentHandler(handler);
 91  
 
 92  2
             final Reader reader = new InputStreamReader(inputStream, "UTF-8");
 93  2
             final InputSource in = new InputSource(reader);
 94  
             //in.setEncoding("UTF-8");
 95  
 
 96  2
             xmlReader.parse(in);
 97  
 
 98  2
             return handler.getModel();
 99  0
         } catch (ParserConfigurationException ex) {
 100  0
             LOGGER.debug("", ex);
 101  0
             throw new PomParseException(ex);
 102  0
         } catch (SAXException ex) {
 103  0
             LOGGER.debug("", ex);
 104  0
             throw new PomParseException(ex);
 105  0
         } catch (FileNotFoundException ex) {
 106  0
             LOGGER.debug("", ex);
 107  0
             throw new PomParseException(ex);
 108  0
         } catch (IOException ex) {
 109  0
             LOGGER.debug("", ex);
 110  0
             throw new PomParseException(ex);
 111  
         }
 112  
     }
 113  
 }