mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-03-20 16:24:11 +01:00
Merge pull request #385 from awhitford/FileUtils
Leverage FilenameUtils
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
package org.owasp.dependencycheck.analyzer;
|
package org.owasp.dependencycheck.analyzer;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.owasp.dependencycheck.Engine;
|
import org.owasp.dependencycheck.Engine;
|
||||||
import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
|
import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
|
||||||
import org.owasp.dependencycheck.dependency.Confidence;
|
import org.owasp.dependencycheck.dependency.Confidence;
|
||||||
@@ -76,13 +77,7 @@ public class FileNameAnalyzer extends AbstractAnalyzer implements Analyzer {
|
|||||||
|
|
||||||
//strip any path information that may get added by ArchiveAnalyzer, etc.
|
//strip any path information that may get added by ArchiveAnalyzer, etc.
|
||||||
final File f = dependency.getActualFile();
|
final File f = dependency.getActualFile();
|
||||||
String fileName = f.getName();
|
final String fileName = FilenameUtils.removeExtension(f.getName());
|
||||||
|
|
||||||
//remove file extension
|
|
||||||
final int pos = fileName.lastIndexOf(".");
|
|
||||||
if (pos > 0) {
|
|
||||||
fileName = fileName.substring(0, pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
//add version evidence
|
//add version evidence
|
||||||
final DependencyVersion version = DependencyVersionUtil.parseVersion(fileName);
|
final DependencyVersion version = DependencyVersionUtil.parseVersion(fileName);
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ import java.util.jar.Manifest;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import org.apache.commons.compress.utils.IOUtils;
|
import org.apache.commons.compress.utils.IOUtils;
|
||||||
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
import org.owasp.dependencycheck.Engine;
|
import org.owasp.dependencycheck.Engine;
|
||||||
import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
|
import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
|
||||||
@@ -269,8 +270,7 @@ public class JarAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
}
|
}
|
||||||
File externalPom = null;
|
File externalPom = null;
|
||||||
if (pomEntries.isEmpty()) {
|
if (pomEntries.isEmpty()) {
|
||||||
String pomPath = dependency.getActualFilePath();
|
final String pomPath = FilenameUtils.removeExtension(dependency.getActualFilePath()) + ".pom";
|
||||||
pomPath = pomPath.substring(0, pomPath.lastIndexOf('.')) + ".pom";
|
|
||||||
externalPom = new File(pomPath);
|
externalPom = new File(pomPath);
|
||||||
if (externalPom.isFile()) {
|
if (externalPom.isFile()) {
|
||||||
pomEntries.add(pomPath);
|
pomEntries.add(pomPath);
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.owasp.dependencycheck.utils;
|
package org.owasp.dependencycheck.utils;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -58,12 +59,8 @@ public final class FileUtils {
|
|||||||
* @return the file extension.
|
* @return the file extension.
|
||||||
*/
|
*/
|
||||||
public static String getFileExtension(String fileName) {
|
public static String getFileExtension(String fileName) {
|
||||||
String ret = null;
|
final String fileExt = FilenameUtils.getExtension(fileName);
|
||||||
final int pos = fileName.lastIndexOf(".");
|
return null == fileExt || fileExt.isEmpty() ? null : fileExt.toLowerCase();
|
||||||
if (pos >= 0) {
|
|
||||||
ret = fileName.substring(pos + 1).toLowerCase();
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -73,9 +70,8 @@ public final class FileUtils {
|
|||||||
* @return true if the file was deleted successfully, otherwise false
|
* @return true if the file was deleted successfully, otherwise false
|
||||||
*/
|
*/
|
||||||
public static boolean delete(File file) {
|
public static boolean delete(File file) {
|
||||||
boolean success = true;
|
final boolean success = org.apache.commons.io.FileUtils.deleteQuietly(file);
|
||||||
if (!org.apache.commons.io.FileUtils.deleteQuietly(file)) {
|
if (!success) {
|
||||||
success = false;
|
|
||||||
LOGGER.debug("Failed to delete file: {}; attempting to delete on exit.", file.getPath());
|
LOGGER.debug("Failed to delete file: {}; attempting to delete on exit.", file.getPath());
|
||||||
file.deleteOnExit();
|
file.deleteOnExit();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ public class FileUtilsTest extends BaseTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testGetFileExtension() {
|
public void testGetFileExtension() {
|
||||||
String[] fileName = {"something-0.9.5.jar", "lib2-1.1.js"};
|
String[] fileName = {"something-0.9.5.jar", "lib2-1.1.js", "dir.tmp/noext"};
|
||||||
String[] expResult = {"jar", "js"};
|
String[] expResult = {"jar", "js", null};
|
||||||
|
|
||||||
for (int i = 0; i < fileName.length; i++) {
|
for (int i = 0; i < fileName.length; i++) {
|
||||||
String result = FileUtils.getFileExtension(fileName[i]);
|
String result = FileUtils.getFileExtension(fileName[i]);
|
||||||
|
|||||||
Reference in New Issue
Block a user