Skip to content

Commit

Permalink
better GAV extraction with base path argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Cornul11 committed Mar 1, 2024
1 parent 42c5e0c commit cab741b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@ public class JarAndPomInfoExtractor {
String artifactId;
String version;

public JarAndPomInfoExtractor(String jarFilePath) {
parseJarOrPomFilePath(jarFilePath);
public JarAndPomInfoExtractor(String jarFilePath, String basePath) {
parseJarOrPomFilePath(jarFilePath, basePath);
}

// TODO: this has to be thoroughly tested, maybe also crosscheck with the pom file for certainty
private void parseJarOrPomFilePath(String jarFilePath) {
private void parseJarOrPomFilePath(String jarFilePath, String basePath) {
jarFilePath = jarFilePath.replace("\\", "/");
if (basePath != null) {
basePath = basePath.replace("\\", "/").endsWith("/") ? basePath : basePath + "/";
if (!basePath.isEmpty() && jarFilePath.startsWith(basePath)) {
jarFilePath = jarFilePath.substring(basePath.length());
}
} else {
String defaultBase = ".m2/repository/";
int index = jarFilePath.indexOf(defaultBase);
if (index != -1) {
jarFilePath = jarFilePath.substring(index + defaultBase.length());
}
}

String[] splitPath = jarFilePath.split("/");

String version = splitPath[splitPath.length - 2];
String artifactID = splitPath[splitPath.length - 3];

// Find the position of '.m2/repository' by comparing adjacent elements in the 'splitPath' array
int repoIndex = -1;
for (int i = 0; i < splitPath.length - 1; i++) {
if (splitPath[i].equals(".m2") && splitPath[i + 1].equals("repository")) {
repoIndex = i;
break;
}
}

// Start building the 'groupId' from the position just after '.m2/repository'
int startIdx = repoIndex + 2;

StringBuilder groupID = new StringBuilder();
for (int i = startIdx; i < splitPath.length - 3; i++) {
for (int i = 0; i < splitPath.length - 3; i++) {
groupID.append(splitPath[i]);
if (i < splitPath.length - 4) {
groupID.append('.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ public class FileAnalyzer {
private final AtomicInteger processedJars = new AtomicInteger(0);
private final int totalJars;
private final long startTime = System.currentTimeMillis();
private final String basePath;

public FileAnalyzer(SignatureDAO signatureDao, ConfigurationLoader config) {
this.config = config;
this.signatureDao = signatureDao;
this.totalJars = config.getTotalJars();
this.basePath = config.getBasePath();
}

public void printIgnoredUberJars() {
Expand All @@ -57,7 +59,7 @@ public int processJarFile(Path jarFilePath) {
long jarCrc = jarHandler.getJarCrc();
long jarCreationDate = jarHandler.getJarCreationDate();

JarAndPomInfoExtractor jarAndPomInfoExtractor = new JarAndPomInfoExtractor(jarFilePath.toString());
JarAndPomInfoExtractor jarAndPomInfoExtractor = new JarAndPomInfoExtractor(jarFilePath.toString(), basePath);
if (signatures.isEmpty()) { // it's probably an uber-JAR, let's still add it to the db
insertedUberJars.incrementAndGet();
return commitLibrary(jarAndPomInfoExtractor, jarHash, jarCrc, jarHandler.isBrokenJar(), jarCreationDate);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package nl.tudelft.cornul11.thesis.corpus.util;

import nl.tudelft.cornul11.thesis.corpus.database.DatabaseConfig;

import java.io.FileInputStream;
Expand Down Expand Up @@ -62,6 +63,10 @@ public int getTotalJars() {
return config.getProperty("totalJars") == null ? -1 : Integer.parseInt(config.getProperty("totalJars"));
}

public String getBasePath() {
return config.getProperty("basePath") == null ? null : config.getProperty("basePath");
}

public String getMongoDbDatabase() {
return config.getProperty("mongoDbDatabase");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private List<String> inferLibraries(Path jarPath) throws Exception {
}

private String getGAVFromPath(Path jarPath) {
JarAndPomInfoExtractor jarAndPomInfoExtractor = new JarAndPomInfoExtractor(jarPath.toString());
JarAndPomInfoExtractor jarAndPomInfoExtractor = new JarAndPomInfoExtractor(jarPath.toString(), null);
String groupId = jarAndPomInfoExtractor.getGroupId();
String artifactId = jarAndPomInfoExtractor.getArtifactId();
String version = jarAndPomInfoExtractor.getVersion();
Expand Down

0 comments on commit cab741b

Please sign in to comment.