Coverage Report - org.owasp.dependencycheck.data.nuget.XPathNuspecParser
 
Classes in this File Line Coverage Branch Coverage Complexity
XPathNuspecParser
94%
17/18
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  
 
 22  
 import javax.xml.parsers.DocumentBuilderFactory;
 23  
 import javax.xml.xpath.XPath;
 24  
 import javax.xml.xpath.XPathConstants;
 25  
 import javax.xml.xpath.XPathFactory;
 26  
 
 27  
 import org.w3c.dom.Document;
 28  
 import org.w3c.dom.Node;
 29  
 
 30  
 /**
 31  
  * Parse a Nuspec file using XPath.
 32  
  *
 33  
  * @author willstranathan
 34  
  */
 35  3
 public class XPathNuspecParser implements NuspecParser {
 36  
     /**
 37  
      * Gets the string value of a node or null if it's not present
 38  
      *
 39  
      * @param n the node to test
 40  
      * @return the string content of the node, or null if the node itself is 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 Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stream);
 61  2
             final XPath xpath = XPathFactory.newInstance().newXPath();
 62  2
             final NugetPackage nuspec = new NugetPackage();
 63  
 
 64  2
             if (xpath.evaluate("/package/metadata/id", d, XPathConstants.NODE) == null
 65  
                     || xpath.evaluate("/package/metadata/version", d, XPathConstants.NODE) == null
 66  
                     || xpath.evaluate("/package/metadata/authors", d, XPathConstants.NODE) == null
 67  
                     || xpath.evaluate("/package/metadata/description", d, XPathConstants.NODE) == null) {
 68  1
                 throw new NuspecParseException("Invalid Nuspec format");
 69  
             }
 70  
 
 71  1
             nuspec.setId(xpath.evaluate("/package/metadata/id", d));
 72  1
             nuspec.setVersion(xpath.evaluate("/package/metadata/version", d));
 73  1
             nuspec.setAuthors(xpath.evaluate("/package/metadata/authors", d));
 74  1
             nuspec.setOwners(getOrNull((Node) xpath.evaluate("/package/metadata/owners",  d, XPathConstants.NODE)));
 75  1
             nuspec.setLicenseUrl(getOrNull((Node) xpath.evaluate("/package/metadata/licenseUrl",  d, XPathConstants.NODE)));
 76  1
             nuspec.setTitle(getOrNull((Node) xpath.evaluate("/package/metadata/title",  d, XPathConstants.NODE)));
 77  1
             return nuspec;
 78  2
         } catch (Exception e) {
 79  2
             throw new NuspecParseException("Unable to parse nuspec", e);
 80  
         }
 81  
     }
 82  
 }