Coverage Report - org.owasp.dependencycheck.analyzer.JavaScriptAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
JavaScriptAnalyzer
28%
7/25
0%
0/4
2.2
 
 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.analyzer;
 19  
 
 20  
 import java.io.BufferedReader;
 21  
 import java.io.File;
 22  
 import java.io.FileNotFoundException;
 23  
 import java.io.FileReader;
 24  
 import java.io.IOException;
 25  
 import java.util.Set;
 26  
 import java.util.logging.Level;
 27  
 import java.util.logging.Logger;
 28  
 import java.util.regex.Pattern;
 29  
 import org.owasp.dependencycheck.Engine;
 30  
 import org.owasp.dependencycheck.dependency.Dependency;
 31  
 
 32  
 /**
 33  
  *
 34  
  * Used to analyze a JavaScript file to gather information to aid in identification of a CPE identifier.
 35  
  *
 36  
  * @author Jeremy Long (jeremy.long@owasp.org)
 37  
  */
 38  5
 public class JavaScriptAnalyzer extends AbstractAnalyzer implements Analyzer {
 39  
 
 40  
     //<editor-fold defaultstate="collapsed" desc="All standard implmentation details of Analyzer">
 41  
     /**
 42  
      * The name of the analyzer.
 43  
      */
 44  
     private static final String ANALYZER_NAME = "JavaScript Analyzer";
 45  
     /**
 46  
      * The phase that this analyzer is intended to run in.
 47  
      */
 48  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION;
 49  
     /**
 50  
      * The set of file extensions supported by this analyzer.
 51  
      */
 52  1
     private static final Set<String> EXTENSIONS = newHashSet("js");
 53  
 
 54  
     /**
 55  
      * Returns a list of file EXTENSIONS supported by this analyzer.
 56  
      *
 57  
      * @return a list of file EXTENSIONS supported by this analyzer.
 58  
      */
 59  
     @Override
 60  
     public Set<String> getSupportedExtensions() {
 61  1
         return EXTENSIONS;
 62  
     }
 63  
 
 64  
     /**
 65  
      * Returns the name of the analyzer.
 66  
      *
 67  
      * @return the name of the analyzer.
 68  
      */
 69  
     @Override
 70  
     public String getName() {
 71  1
         return ANALYZER_NAME;
 72  
     }
 73  
 
 74  
     /**
 75  
      * Returns whether or not this analyzer can process the given extension.
 76  
      *
 77  
      * @param extension the file extension to test for support.
 78  
      * @return whether or not the specified file extension is supported by this analyzer.
 79  
      */
 80  
     @Override
 81  
     public boolean supportsExtension(String extension) {
 82  1
         return EXTENSIONS.contains(extension);
 83  
     }
 84  
 
 85  
     /**
 86  
      * Returns the phase that the analyzer is intended to run in.
 87  
      *
 88  
      * @return the phase that the analyzer is intended to run in.
 89  
      */
 90  
     @Override
 91  
     public AnalysisPhase getAnalysisPhase() {
 92  1
         return ANALYSIS_PHASE;
 93  
     }
 94  
     //</editor-fold>
 95  
 
 96  
     /**
 97  
      * Loads a specified JavaScript file and collects information from the copyright information contained within.
 98  
      *
 99  
      * @param dependency the dependency to analyze.
 100  
      * @param engine the engine that is scanning the dependencies
 101  
      * @throws AnalysisException is thrown if there is an error reading the JavaScript file.
 102  
      */
 103  
     @Override
 104  
     public void analyze(Dependency dependency, Engine engine) throws AnalysisException {
 105  0
         BufferedReader fin = null;;
 106  
         try {
 107  
             //  /\*([^\*][^/]|[\r\n\f])+?\*/
 108  0
             final Pattern extractComments = Pattern.compile("(/\\*([^*]|[\\r\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+/)|(//.*)", Pattern.MULTILINE);
 109  0
             File file = dependency.getActualFile();
 110  0
             fin = new BufferedReader(new FileReader(file));
 111  0
             StringBuilder sb = new StringBuilder(2000);
 112  
             String text;
 113  0
             while ((text = fin.readLine()) != null) {
 114  0
                 sb.append(text);
 115  
             }
 116  0
         } catch (FileNotFoundException ex) {
 117  0
             final String msg = String.format("Dependency file not found: '%s'", dependency.getActualFilePath());
 118  0
             throw new AnalysisException(msg, ex);
 119  0
         } catch (IOException ex) {
 120  0
             Logger.getLogger(JavaScriptAnalyzer.class.getName()).log(Level.SEVERE, null, ex);
 121  
         } finally {
 122  0
             if (fin != null) {
 123  
                 try {
 124  0
                     fin.close();
 125  0
                 } catch (IOException ex) {
 126  0
                     Logger.getLogger(JavaScriptAnalyzer.class.getName()).log(Level.FINEST, null, ex);
 127  0
                 }
 128  
             }
 129  
         }
 130  0
     }
 131  
 }