فهرست منبع

Allow languages to provide a view on the input files in order to depict the view in the report instead of the input files themselves.

Timur Sağlam 4 سال پیش
والد
کامیت
d8a458f9c5

+ 21 - 3
jplag.frontend-utils/src/main/java/de/jplag/Language.java

@@ -50,13 +50,31 @@ public interface Language {
      */
     boolean isPreformatted();
 
+    /**
+     * Number of defined parse tree tokens in the language.
+     */
+    int numberOfTokens();
+
     /**
      * Determines whether tokens from the scanner are indexed.
      */
-    boolean usesIndex();
+    default boolean usesIndex() {
+        return false;
+    }
 
     /**
-     * Number of defined tokens in the scanner of the language.
+     * Indicates whether the input files (code) should be used as representation in the report, or different files that form
+     * a view on the input files.
      */
-    int numberOfTokens();
+    default boolean useViewFiles() {
+        return false;
+    }
+
+    /**
+     * If the language uses representation files, this method returns the suffix used for the representation files.
+     */
+    default String viewFileSuffix() {
+        return "";
+    }
+
 }

+ 3 - 0
jplag.frontend-utils/src/main/java/de/jplag/TokenPrinter.java

@@ -80,6 +80,9 @@ public final class TokenPrinter {
                 columnIndex = 1;
 
                 String fileName = token.getFile().isEmpty() ? root.getName() : token.getFile();
+                if (!filesToLines.containsKey(fileName)) {
+                    throw new IllegalStateException("Cannot find file " + fileName + " in " + filesToLines.keySet());
+                }
                 String currentLine = filesToLines.get(fileName).get(lineIndex - 1);
                 if (REPLACE_TABS) {
                     currentLine = currentLine.replace(TAB, TAB_REPLACEMENT);

+ 8 - 4
jplag/src/main/java/de/jplag/reporting/reportobject/ReportObjectFactory.java

@@ -69,12 +69,14 @@ public class ReportObjectFactory {
      */
     private static List<ComparisonReport> generateComparisonReports(JPlagResult result) {
         List<ComparisonReport> comparisons = new ArrayList<>();
+        Language language = result.getOptions().getLanguage();
+        Optional<String> viewFileSuffix = language.useViewFiles() ? Optional.of(language.viewFileSuffix()) : Optional.empty();
         result.getComparisons().forEach(comparison -> comparisons.add( //
                 new ComparisonReport(comparison.getFirstSubmission().getName(), //
                         comparison.getSecondSubmission().getName(), //
                         comparison.similarity(), //
-                        getFilesForSubmission(comparison.getFirstSubmission()), //
-                        getFilesForSubmission(comparison.getSecondSubmission()), //
+                        getFilesForSubmission(comparison.getFirstSubmission(), viewFileSuffix), //
+                        getFilesForSubmission(comparison.getSecondSubmission(), viewFileSuffix), //
                         convertMatchesToReportMatches(result, comparison, comparison.getMatches()) //
                 )));
         return comparisons;
@@ -136,10 +138,12 @@ public class ReportObjectFactory {
 
     /**
      * Converts files of a submission to FilesOFSubmission DTO.
+     * @param viewFileSuffix optional suffix denoting the view files when they are used by the language.
      * @return A list containing FilesOfSubmission DTOs.
      */
-    private static List<FilesOfSubmission> getFilesForSubmission(Submission submission) {
-        return submission.getFiles().stream().map(file -> new FilesOfSubmission(file.getName(), readFileLines(file))).collect(Collectors.toList());
+    private static List<FilesOfSubmission> getFilesForSubmission(Submission submission, Optional<String> viewFileSuffix) {
+        var submissionFiles = submission.getFiles().stream().map(file -> new File(file.getAbsolutePath() + viewFileSuffix.orElse("")));
+        return submissionFiles.map(file -> new FilesOfSubmission(file.getName(), readFileLines(file))).collect(Collectors.toList());
     }
 
     /**