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 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  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 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          FileInputStream fis = null;
58          try {
59              fis = new FileInputStream(file);
60              return parse(fis);
61          } catch (IOException ex) {
62              LOGGER.debug("", ex);
63              throw new PomParseException(ex);
64          } finally {
65              if (fis != null) {
66                  try {
67                      fis.close();
68                  } catch (IOException ex) {
69                      LOGGER.debug("Unable to close stream", ex);
70                  }
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              final PomHandler handler = new PomHandler();
85              final SAXParserFactory factory = SAXParserFactory.newInstance();
86  //            factory.setNamespaceAware(true);
87  //            factory.setValidating(true);
88              final SAXParser saxParser = factory.newSAXParser();
89              final XMLReader xmlReader = saxParser.getXMLReader();
90              xmlReader.setContentHandler(handler);
91  
92              final Reader reader = new InputStreamReader(inputStream, "UTF-8");
93              final InputSource in = new InputSource(reader);
94              //in.setEncoding("UTF-8");
95  
96              xmlReader.parse(in);
97  
98              return handler.getModel();
99          } catch (ParserConfigurationException ex) {
100             LOGGER.debug("", ex);
101             throw new PomParseException(ex);
102         } catch (SAXException ex) {
103             LOGGER.debug("", ex);
104             throw new PomParseException(ex);
105         } catch (FileNotFoundException ex) {
106             LOGGER.debug("", ex);
107             throw new PomParseException(ex);
108         } catch (IOException ex) {
109             LOGGER.debug("", ex);
110             throw new PomParseException(ex);
111         }
112     }
113 }