XPathNugetconfParser.java

  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) 2014 Jeremy Long. All Rights Reserved.
  17.  */
  18. package org.owasp.dependencycheck.data.nuget;

  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import javax.annotation.concurrent.ThreadSafe;
  24. import javax.xml.parsers.DocumentBuilder;
  25. import javax.xml.parsers.ParserConfigurationException;
  26. import javax.xml.xpath.XPath;
  27. import javax.xml.xpath.XPathConstants;
  28. import javax.xml.xpath.XPathExpressionException;
  29. import javax.xml.xpath.XPathFactory;
  30. import org.owasp.dependencycheck.utils.XmlUtils;
  31. import org.w3c.dom.Document;
  32. import org.w3c.dom.NamedNodeMap;
  33. import org.w3c.dom.Node;
  34. import org.w3c.dom.NodeList;
  35. import org.xml.sax.SAXException;

  36. /**
  37.  * Parse a packages.config file using XPath.
  38.  *
  39.  * @author doshyt
  40.  */
  41. @ThreadSafe
  42. public class XPathNugetconfParser {
  43.     /**
  44.      * Parse an input stream and return the resulting {@link NugetPackage}.
  45.      *
  46.      * @param stream the input stream to parse
  47.      * @return the populated bean
  48.      * @throws NugetconfParseException when an exception occurs
  49.      */
  50.     public List<NugetPackageReference> parse(InputStream stream) throws NugetconfParseException {
  51.         try {
  52.             final DocumentBuilder db = XmlUtils.buildSecureDocumentBuilder();
  53.             final Document d = db.parse(stream);

  54.             final XPath xpath = XPathFactory.newInstance().newXPath();
  55.             final List<NugetPackageReference> packages = new ArrayList<>();

  56.             final NodeList nodeList = (NodeList) xpath.evaluate("/packages/package", d, XPathConstants.NODESET);

  57.             if (nodeList == null) {
  58.                 throw new NugetconfParseException("Unable to parse packages.config file");
  59.             }

  60.             for (int i = 0; i < nodeList.getLength(); i++) {
  61.                 final Node node = nodeList.item(i);
  62.                 final NamedNodeMap attrs = node.getAttributes();
  63.                 final Node id = attrs.getNamedItem("id");
  64.                 final Node version = attrs.getNamedItem("version");

  65.                 if (id != null && version != null) {
  66.                     final NugetPackageReference npr = new NugetPackageReference();

  67.                     npr.setId(id.getNodeValue());
  68.                     npr.setVersion(version.getNodeValue());

  69.                     packages.add(npr);
  70.                 }
  71.             }

  72.             return packages;
  73.         } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException | NugetconfParseException e) {
  74.             throw new NugetconfParseException("Unable to parse packages.config file", e);
  75.         }
  76.     }
  77. }