in reports/src/main/java/nl/obren/sokrates/reports/generators/statichtml/DuplicationReportGenerator.java [249:302]
private String saveDuplicates(ComponentDependency componentDependency, String logicalDecompositionName, List<DuplicationInstance> allInstances) {
File file = new File(this.report.getReportsFolder(), "data/text/intercomponent_duplicates_" + componentDuplicatesCount++ + ".txt");
List<DuplicationInstance> duplicates = this.codeAnalysisResults.getDuplicationAnalysisResults().getAllDuplicates();
String from = componentDependency.getFromComponent();
String to = componentDependency.getToComponent();
List<DuplicationInstance> instances = new ArrayList<>();
allInstances.forEach(duplicate -> {
boolean fromPresent[] = {false};
boolean toPresent[] = {false};
duplicate.getDuplicatedFileBlocks().forEach(duplicatedFileBlock -> {
SourceFile sourceFile = duplicatedFileBlock.getSourceFile();
List<NamedSourceCodeAspect> components = sourceFile.getLogicalComponents(logicalDecompositionName);
if (components.stream().filter(c -> c.getName().equalsIgnoreCase(from)).findAny().isPresent()) {
fromPresent[0] = true;
}
if (components.stream().filter(c -> c.getName().equalsIgnoreCase(to)).findAny().isPresent()) {
toPresent[0] = true;
}
});
if (toPresent[0] && fromPresent[0]) {
instances.add(duplicate);
}
});
StringBuilder stringBuilder = new StringBuilder();
Collections.sort(instances, (o1, o2) -> o2.getBlockSize() - o1.getBlockSize());
instances.forEach(instance -> {
stringBuilder.append(instance.getBlockSize() + " duplicated lines in:\n");
instance.getDuplicatedFileBlocks().forEach(block -> {
stringBuilder.append(" - ");
stringBuilder.append(block.getSourceFile().getRelativePath());
stringBuilder.append(" (");
stringBuilder.append(block.getStartLine());
stringBuilder.append(":");
stringBuilder.append(block.getEndLine());
stringBuilder.append(")\n");
});
stringBuilder.append("\n");
});
try {
FileUtils.write(file, stringBuilder.toString(), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
return "../data/text/" + file.getName();
}