| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | 2 | public class PomParser { |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 1 | private static final Logger LOGGER = LoggerFactory.getLogger(PomParser.class); |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 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 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 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 | |
|
| 87 | |
|
| 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 | |
|
| 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 | |
} |