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