package com.cadrlife.devsearch.agent.service.analysis;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import java.nio.file.Path;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.apache.commons.lang.StringUtils.remove;
public class FileProcessor {
JavaReferenceFinder javaReferenceFinder;
@Inject
public FileProcessor(JavaReferenceFinder javaReferenceFinder) {
this.javaReferenceFinder = javaReferenceFinder;
}
private static final Logger LOG = LoggerFactory.getLogger(FileProcessor.class);
public void addSourceAnalysisFields(ImmutableMap.Builder<String,Object> mapBuilder, Path filePath, String content) {
String fileName = filePath.getFileName().toString();
if (fileName.endsWith(".java") || fileName.endsWith(".groovy")) {
LOG.debug("Analyzing file {}", filePath);
mapBuilder.putAll(computeJavaStuff(content, filePath).asMap());
}
}
JavaAnalysis computeJavaStuff(String content, Path filePath) {
JavaAnalysis analysis = new JavaAnalysis();
Set<String> imports = new TreeSet<String>();
Scanner scanner = new Scanner(content);
int lineNumber = 0;
while( scanner.hasNextLine() ) {
lineNumber++;
String trimmedLine = scanner.nextLine().trim();
if (trimmedLine.startsWith("package ")) {
String filePackage = remove(remove(trimmedLine, "package "), ";").trim();
String baseName = com.google.common.io.Files.getNameWithoutExtension(filePath.getFileName().toString());
LOG.debug("Detected package {}", filePackage);
LOG.debug("Detected javaName {}", baseName);
analysis.setJavaPackage(filePackage);
analysis.setName(baseName);
}
if (trimmedLine.startsWith("import ")) {
String foundImport = remove(remove(remove(remove(trimmedLine, "import "), ";"), "static "), ".*").trim();
LOG.debug("Detected import {}", foundImport);
analysis.addImport(lineNumber, foundImport);
} else {
Matcher m = Pattern.compile("\\b(([a-z0-9]+[\\.])+[A-Z]+\\w+)").matcher(trimmedLine);
if( m.find()) {
String foundImport = m.group(0);
LOG.debug("Detected import with weird regex {}", foundImport);
imports.add(foundImport);
}
}
}
scanner.close();
javaReferenceFinder.populateReferences(analysis, content);
return analysis;
}
}