Coverage Report - org.owasp.dependencycheck.data.nuget.XPathNuspecParser
 
Classes in this File Line Coverage Branch Coverage Complexity
XPathNuspecParser
95%
22/23
60%
6/10
6
 
 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  
 
 20  
 import java.io.InputStream;
 21  
 import javax.xml.parsers.DocumentBuilderFactory;
 22  
 import javax.xml.xpath.XPath;
 23  
 import javax.xml.xpath.XPathConstants;
 24  
 import javax.xml.xpath.XPathFactory;
 25  
 import org.w3c.dom.Document;
 26  
 import org.w3c.dom.Node;
 27  
 
 28  
 /**
 29  
  * Parse a Nuspec file using XPath.
 30  
  *
 31  
  * @author colezlaw
 32  
  */
 33  3
 public class XPathNuspecParser implements NuspecParser {
 34  
 
 35  
     /**
 36  
      * Gets the string value of a node or null if it's not present
 37  
      *
 38  
      * @param n the node to test
 39  
      * @return the string content of the node, or null if the node itself is
 40  
      * null
 41  
      */
 42  
     private String getOrNull(Node n) {
 43  3
         if (n != null) {
 44  3
             return n.getTextContent();
 45  
         } else {
 46  0
             return null;
 47  
         }
 48  
     }
 49  
 
 50  
     /**
 51  
      * Parse an input stream and return the resulting {@link NugetPackage}.
 52  
      *
 53  
      * @param stream the input stream to parse
 54  
      * @return the populated bean
 55  
      * @throws NuspecParseException when an exception occurs
 56  
      */
 57  
     @Override
 58  
     public NugetPackage parse(InputStream stream) throws NuspecParseException {
 59  
         try {
 60  3
             final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 61  3
             factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
 62  3
             final Document d = factory.newDocumentBuilder().parse(stream);
 63  
 
 64  2
             final XPath xpath = XPathFactory.newInstance().newXPath();
 65  2
             final NugetPackage nuspec = new NugetPackage();
 66  
 
 67  2
             if (xpath.evaluate("/package/metadata/id", d, XPathConstants.NODE) == null
 68  1
                     || xpath.evaluate("/package/metadata/version", d, XPathConstants.NODE) == null
 69  1
                     || xpath.evaluate("/package/metadata/authors", d, XPathConstants.NODE) == null
 70  1
                     || xpath.evaluate("/package/metadata/description", d, XPathConstants.NODE) == null) {
 71  1
                 throw new NuspecParseException("Invalid Nuspec format");
 72  
             }
 73  
 
 74  1
             nuspec.setId(xpath.evaluate("/package/metadata/id", d));
 75  1
             nuspec.setVersion(xpath.evaluate("/package/metadata/version", d));
 76  1
             nuspec.setAuthors(xpath.evaluate("/package/metadata/authors", d));
 77  1
             nuspec.setOwners(getOrNull((Node) xpath.evaluate("/package/metadata/owners", d, XPathConstants.NODE)));
 78  1
             nuspec.setLicenseUrl(getOrNull((Node) xpath.evaluate("/package/metadata/licenseUrl", d, XPathConstants.NODE)));
 79  1
             nuspec.setTitle(getOrNull((Node) xpath.evaluate("/package/metadata/title", d, XPathConstants.NODE)));
 80  1
             return nuspec;
 81  2
         } catch (Throwable e) {
 82  2
             throw new NuspecParseException("Unable to parse nuspec", e);
 83  
         }
 84  
     }
 85  
 }