View Javadoc
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 org.owasp.dependencycheck.utils.XmlUtils;
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  public class PomParser {
43  
44      /**
45       * The logger.
46       */
47      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
51       * fields dependency-check requires.
52       *
53       * @param file a pom.xml
54       * @return a Model object containing only the fields dependency-check
55       * requires
56       * @throws PomParseException thrown if the xml file cannot be parsed
57       */
58      public Model parse(File file) throws PomParseException {
59          FileInputStream fis = null;
60          try {
61              fis = new FileInputStream(file);
62              return parse(fis);
63          } catch (IOException ex) {
64              LOGGER.debug("", ex);
65              throw new PomParseException(ex);
66          } finally {
67              if (fis != null) {
68                  try {
69                      fis.close();
70                  } catch (IOException ex) {
71                      LOGGER.debug("Unable to close stream", ex);
72                  }
73              }
74          }
75      }
76  
77      /**
78       * Parses the given XML file and returns a Model object containing only the
79       * fields dependency-check requires.
80       *
81       * @param inputStream an InputStream containing suppression rues
82       * @return a list of suppression rules
83       * @throws PomParseException if the XML cannot be parsed
84       */
85      public Model parse(InputStream inputStream) throws PomParseException {
86          try {
87              final PomHandler handler = new PomHandler();
88              final SAXParser saxParser = XmlUtils.buildSecureSaxParser();
89              final XMLReader xmlReader = saxParser.getXMLReader();
90              xmlReader.setContentHandler(handler);
91              final Reader reader = new InputStreamReader(inputStream, "UTF-8");
92              final InputSource in = new InputSource(reader);
93              xmlReader.parse(in);
94              return handler.getModel();
95          } catch (ParserConfigurationException ex) {
96              LOGGER.debug("", ex);
97              throw new PomParseException(ex);
98          } catch (SAXException ex) {
99              LOGGER.debug("", ex);
100             throw new PomParseException(ex);
101         } catch (FileNotFoundException ex) {
102             LOGGER.debug("", ex);
103             throw new PomParseException(ex);
104         } catch (IOException ex) {
105             LOGGER.debug("", ex);
106             throw new PomParseException(ex);
107         }
108     }
109 }