Procházet zdrojové kódy

Clean up the submission class and adapt the builder.

Timur Sağlam před 4 roky
rodič
revize
e0340448a9

+ 16 - 22
jplag/src/main/java/de/jplag/Submission.java

@@ -13,9 +13,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
-import de.jplag.exceptions.ExitException;
 import de.jplag.exceptions.ReportGenerationException;
-import de.jplag.exceptions.SubmissionException;
 import de.jplag.options.JPlagOptions;
 
 /**
@@ -35,7 +33,7 @@ public class Submission implements Comparable<Submission> {
     /**
      * Root of the submission files (including the subdir if used).
      */
-    private final File submissionRoot;
+    private final File submissionRootFile;
 
     /**
      * Files of the submission.
@@ -63,14 +61,14 @@ public class Submission implements Comparable<Submission> {
     /**
      * Creates a submission.
      * @param name Identification of the submission (directory or filename).
-     * @param submissionRoot Root of the submission (either a file or a directory).
+     * @param submissionRootFile is the submission file, or the root of the submission itself.
      * @param files are the files of the submissions, if the root is a single file it should just contain one file.
      * @param language is the language of the submission.
      * @param errorCollector is the interface for error reporting.
      */
-    public Submission(String name, File submissionRoot, Collection<File> files, Language language, ErrorCollector errorCollector) {
+    public Submission(String name, File submissionRootFile, Collection<File> files, Language language, ErrorCollector errorCollector) {
         this.name = name;
-        this.submissionRoot = submissionRoot;
+        this.submissionRootFile = submissionRootFile;
         this.files = files;
         this.language = language;
         this.errorCollector = errorCollector;
@@ -84,22 +82,18 @@ public class Submission implements Comparable<Submission> {
     }
 
     /**
-     * @return Identification of the submission (often a directory or file name).
+     * @return name of the submission (directory or file name).
      */
     public String getName() {
         return name;
     }
 
+    /**
+     * @return the unique file of the submission, which is either in a root folder or a subfolder of root folder when the
+     * subdirectory option is used.
+     */
     public File getRoot() {
-        return submissionRoot;
-    }
-
-    public File getCanonicalRoot() throws ExitException {
-        try {
-            return submissionRoot.getCanonicalFile();
-        } catch (IOException exception) {
-            throw new SubmissionException(String.format("Cannot compute canonical file path of \"%s\".", submissionRoot.toString()), exception);
-        }
+        return submissionRootFile;
     }
 
     /**
@@ -172,9 +166,9 @@ public class Submission implements Comparable<Submission> {
             return false;
         }
 
-        String[] relativeFilePaths = getRelativeFilePaths(submissionRoot, files);
+        String[] relativeFilePaths = getRelativeFilePaths(submissionRootFile, files);
 
-        tokenList = language.parse(submissionRoot, relativeFilePaths);
+        tokenList = language.parse(submissionRootFile, relativeFilePaths);
         if (!language.hasErrors()) {
             if (tokenList.size() < 3) {
                 errorCollector.print("Submission \"" + name + "\" is too short!", null);
@@ -205,7 +199,7 @@ public class Submission implements Comparable<Submission> {
             text.clear();
 
             try {
-                FileInputStream fileInputStream = new FileInputStream(new File(submissionRoot, files[i]));
+                FileInputStream fileInputStream = new FileInputStream(new File(submissionRootFile, files[i]));
                 InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, JPlagOptions.CHARSET);
                 BufferedReader in = new BufferedReader(inputStreamReader);
 
@@ -221,7 +215,7 @@ public class Submission implements Comparable<Submission> {
                 inputStreamReader.close();
                 fileInputStream.close();
             } catch (FileNotFoundException e) {
-                System.out.println("File not found: " + ((new File(submissionRoot, files[i])).toString()));
+                System.out.println("File not found: " + ((new File(submissionRootFile, files[i])).toString()));
             } catch (IOException e) {
                 throw new ReportGenerationException("I/O exception!", e);
             }
@@ -243,7 +237,7 @@ public class Submission implements Comparable<Submission> {
             // If the token path is absolute, ignore the provided directory
             File file = new File(files[i]);
             if (!file.isAbsolute()) {
-                file = new File(submissionRoot, files[i]);
+                file = new File(submissionRootFile, files[i]);
             }
 
             try {
@@ -303,7 +297,7 @@ public class Submission implements Comparable<Submission> {
      * This method is used to copy files that can not be parsed to a special folder.
      */
     private void copySubmission() {
-        File rootDirectory = submissionRoot.getParentFile();
+        File rootDirectory = submissionRootFile.getParentFile();
         assert rootDirectory != null;
         File submissionDirectory = createSubdirectory(rootDirectory, ERROR_FOLDER, language.getShortName(), name);
         for (File file : files) {

+ 39 - 41
jplag/src/main/java/de/jplag/SubmissionSetBuilder.java

@@ -108,25 +108,15 @@ public class SubmissionSetBuilder {
                 if (rootDirectories.size() > 1) {
                     throw new BasecodeException("The base code submissiom needs to be specified by path instead of by name!");
                 }
-
+                File rootDirectory = rootDirectories.iterator().next();
                 // Single root-directory, try the legacy way of specifying basecode.
-                baseCode = loadBaseCodeAsRootSubDirectory(baseCodeName, foundSubmissions);
-
-                if (baseCode == null) {
-                    // No base code found at all, report an error.
-                    throw new BasecodeException(
-                            String.format("Basecode path \"%s\" relative to the working directory could not be found.", baseCodeName));
-                } else {
-                    // Found a base code as a submission, report about obsolete usage.
-                    System.out.printf("Deprecated use of the -bc option found, please specify the basecode as \"%s%s%s\" instead.\n",
-                            rootDirectories.iterator().next(), File.separator, baseCodeName);
-                }
+                baseCode = loadBaseCodeAsRootSubDirectory(baseCodeName, rootDirectory, foundSubmissions);
             }
             baseCodeSubmission = Optional.of(baseCode);
             System.out.println(String.format("Basecode directory \"%s\" will be used.", baseCode.getRoot().toString()));
 
             // Basecode may also be registered as a user submission. If so, remove the latter.
-            Submission removed = foundSubmissions.remove(baseCode.getCanonicalRoot());
+            Submission removed = foundSubmissions.remove(baseCode.getRoot());
             if (removed != null) {
                 System.out.println(String.format("Skipping \"%s\" as user submission.", removed.getRoot().toString()));
             }
@@ -152,9 +142,8 @@ public class SubmissionSetBuilder {
 
         try {
             // Use an unlikely short name for the base code. If all is well, this name should not appear
-            // in the output since basecode matches are removed from it.
-            return processDirectoryEntry(basecodeSubmission, "**basecode**");
-
+            // in the output since basecode matches are removed from it
+            return processSubmission(baseCodeName, basecodeSubmission);
         } catch (SubmissionException exception) {
             throw new BasecodeException(exception.getMessage(), exception); // Change thrown exception to basecode exception.
 
@@ -168,7 +157,8 @@ public class SubmissionSetBuilder {
      * @return the base code submission if a fitting subdirectory was found, else {@code null}.
      * @throws ExitException when the option value is a sub-directory with errors.
      */
-    private Submission loadBaseCodeAsRootSubDirectory(String baseCodeName, Map<File, Submission> foundSubmissions) throws ExitException {
+    private Submission loadBaseCodeAsRootSubDirectory(String baseCodeName, File rootDirectory, Map<File, Submission> foundSubmissions)
+            throws ExitException {
         // Is the option value a single name after trimming spurious separators?
         String name = baseCodeName;
         while (name.startsWith(File.separator)) {
@@ -188,11 +178,20 @@ public class SubmissionSetBuilder {
         }
 
         // Grab the basecode submission from the regular submissions.
-        File rootDirectory = new File(options.getRootDirectoryNames().get(0));
         File basecodePath = new File(rootDirectory, baseCodeName);
 
         try {
-            return foundSubmissions.get(basecodePath.getCanonicalFile());
+            Submission baseCode = foundSubmissions.get(basecodePath.getCanonicalFile());
+            if (baseCode == null) {
+                // No base code found at all, report an error.
+                throw new BasecodeException(
+                        String.format("Basecode path \"%s\" relative to the working directory could not be found.", baseCodeName));
+            } else {
+                // Found a base code as a submission, report about obsolete usage.
+                System.out.printf("Deprecated use of the -bc option found, please specify the basecode as \"%s%s%s\" instead.\n", rootDirectory,
+                        File.separator, baseCodeName);
+            }
+            return baseCode;
         } catch (IOException exception) {
             throw new BasecodeException(String.format("Cannot compute canonical file path of \"%s\".", basecodePath.toString()), exception);
         }
@@ -239,34 +238,33 @@ public class SubmissionSetBuilder {
     }
 
     /**
-     * Process the given directory entry as a submission root, the path MUST not be excluded.
-     * @param submissionEntry Entry to process.
-     * @param rootDirectoryPrefix Prefix for connecting submissions to the root directory, may be empty.
+     * Process the given directory entry as a submission, the path MUST not be excluded.
+     * @param submissionFile the file for the submission.
+     * @param rootDirectoryPrefix Prefix for submission submissions to the root directory, may be empty.
      * @return The entry converted to a submission.
      * @throws ExitException when an error has been found with the entry.
      */
-    private Submission processDirectoryEntry(File submissionEntry, String rootDirectoryPrefix) throws ExitException {
-        if (isExcludedEntry(submissionEntry) != null) {
-            throw new AssertionError("Pre-condition of non-exclusion is violated.");
-        }
+    private Submission processSubmission(String submissionName, File submissionFile) throws ExitException {
 
-        String fileName = submissionEntry.getName();
-        if (submissionEntry.isDirectory() && options.getSubdirectoryName() != null) {
+        if (submissionFile.isDirectory() && options.getSubdirectoryName() != null) {
             // Use subdirectory instead
-            submissionEntry = new File(submissionEntry, options.getSubdirectoryName());
+            submissionFile = new File(submissionFile, options.getSubdirectoryName());
 
-            if (!submissionEntry.exists()) {
+            if (!submissionFile.exists()) {
                 throw new SubmissionException(
-                        String.format("Submission %s does not contain the given subdirectory '%s'", fileName, options.getSubdirectoryName()));
+                        String.format("Submission %s does not contain the given subdirectory '%s'", submissionName, options.getSubdirectoryName()));
             }
 
-            if (!submissionEntry.isDirectory()) {
+            if (!submissionFile.isDirectory()) {
                 throw new SubmissionException(String.format("The given subdirectory '%s' is not a directory!", options.getSubdirectoryName()));
             }
         }
-
-        String submissionName = rootDirectoryPrefix + fileName;
-        return new Submission(submissionName, submissionEntry, parseFilesRecursively(submissionEntry), language, errorCollector);
+        try {
+            submissionFile = submissionFile.getCanonicalFile();
+        } catch (IOException exception) {
+            throw new SubmissionException("Cannot create submission " + submissionName, exception);
+        }
+        return new Submission(submissionName, submissionFile, parseFilesRecursively(submissionFile), language, errorCollector);
     }
 
     /**
@@ -280,14 +278,14 @@ public class SubmissionSetBuilder {
             File submissionFile = new File(rootDirectory, fileName);
 
             String errorMessage = isExcludedEntry(submissionFile);
-            if (errorMessage != null) {
+            if (errorMessage == null) {
+                String rootDirectoryPrefix = multipleRoots ? (rootDirectory.getName() + "::") : "";
+                String submissionName = rootDirectoryPrefix + fileName;
+                Submission submission = processSubmission(submissionName, submissionFile);
+                foundSubmissions.put(submission.getRoot(), submission);
+            } else {
                 System.out.println(errorMessage);
-                continue;
             }
-
-            String rootDirectoryPrefix = multipleRoots ? (rootDirectory.getName() + "::") : "";
-            Submission submission = processDirectoryEntry(submissionFile, rootDirectoryPrefix);
-            foundSubmissions.put(submission.getCanonicalRoot(), submission);
         }
     }