Coverage Report - org.owasp.dependencycheck.utils.FileUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FileUtils
36%
8/22
33%
4/12
2.6
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Dependency-check-core is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-core is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-core. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.utils;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.FileNotFoundException;
 23  
 import java.io.IOException;
 24  
 import java.io.UnsupportedEncodingException;
 25  
 import java.net.URLDecoder;
 26  
 
 27  
 /**
 28  
  * A collection of utilities for processing information about files.
 29  
  *
 30  
  * @author Jeremy Long (jeremy.long@owasp.org)
 31  
  */
 32  
 public final class FileUtils {
 33  
 
 34  
     /**
 35  
      * Private constructor for a utility class.
 36  
      */
 37  0
     private FileUtils() {
 38  0
     }
 39  
 
 40  
     /**
 41  
      * Returns the (lowercase) file extension for a specified file.
 42  
      *
 43  
      * @param fileName the file name to retrieve the file extension from.
 44  
      * @return the file extension.
 45  
      */
 46  
     public static String getFileExtension(String fileName) {
 47  18102
         String ret = null;
 48  18102
         final int pos = fileName.lastIndexOf(".");
 49  18102
         if (pos >= 0) {
 50  17871
             ret = fileName.substring(pos + 1, fileName.length()).toLowerCase();
 51  
         }
 52  18102
         return ret;
 53  
     }
 54  
 
 55  
     /**
 56  
      * Deletes a file. If the File is a directory it will recursively delete the
 57  
      * contents.
 58  
      *
 59  
      * @param file the File to delete
 60  
      * @throws IOException is thrown if the file could not be deleted
 61  
      */
 62  
     public static void delete(File file) throws IOException {
 63  3
         if (file.isDirectory()) {
 64  0
             for (File c : file.listFiles()) {
 65  0
                 delete(c);
 66  
             }
 67  
         }
 68  3
         if (!file.delete()) {
 69  0
             throw new FileNotFoundException("Failed to delete file: " + file);
 70  
         }
 71  3
     }
 72  
 
 73  
     /**
 74  
      * Returns the data directory. If a path was specified in
 75  
      * dependencycheck.properties or was specified using the Settings object,
 76  
      * and the path exists, that path will be returned as a File object. If it
 77  
      * does not exist, then a File object will be created based on the file
 78  
      * location of the JAR containing the specified class.
 79  
      *
 80  
      * @param configuredFilePath the configured relative or absolute path
 81  
      * @param clazz the class whos path will be resolved
 82  
      * @return a File object
 83  
      * @throws IOException is thrown if the path could not be decoded
 84  
      * @deprecated This method should no longer be used. See the implementation
 85  
      * in dependency-check-cli/App.java to see how the data directory should be
 86  
      * set.
 87  
      */
 88  
     @java.lang.Deprecated
 89  
     public static File getDataDirectory(String configuredFilePath, Class clazz) throws IOException {
 90  0
         final File file = new File(configuredFilePath);
 91  0
         if (file.isDirectory() && file.canWrite()) {
 92  0
             return new File(file.getCanonicalPath());
 93  
         } else {
 94  0
             final File exePath = getPathToJar(clazz);
 95  0
             return new File(exePath, configuredFilePath);
 96  
         }
 97  
     }
 98  
 
 99  
     /**
 100  
      * Retrieves the physical path to the parent directory containing the
 101  
      * provided class. For example, if a JAR file contained a class
 102  
      * org.something.clazz this method would return the parent directory of the
 103  
      * JAR file.
 104  
      *
 105  
      * @param clazz the class to determine the parent directory of
 106  
      * @return the parent directory of the file containing the specified class.
 107  
      * @throws UnsupportedEncodingException thrown if UTF-8 is not supported.
 108  
      * @deprecated this should no longer be used.
 109  
      */
 110  
     @java.lang.Deprecated
 111  
     public static File getPathToJar(Class clazz) throws UnsupportedEncodingException {
 112  0
         final String filePath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
 113  0
         final String decodedPath = URLDecoder.decode(filePath, "UTF-8");
 114  0
         final File jarPath = new File(decodedPath);
 115  0
         return jarPath.getParentFile();
 116  
     }
 117  
 }