Browse Source

Enable consistent file encoding by always using UTF-8 (system independent).

Timur Sağlam 5 năm trước cách đây
mục cha
commit
2e593f7301

+ 2 - 2
jplag.frontend.chars/src/main/java/jplag/chars/Parser.java

@@ -2,6 +2,7 @@ package jplag.chars;
 
 
 import java.io.File;
 import java.io.File;
 import java.io.FileReader;
 import java.io.FileReader;
+import java.nio.charset.StandardCharsets;
 
 
 import jplag.TokenList;
 import jplag.TokenList;
 
 
@@ -18,7 +19,6 @@ public class Parser extends jplag.Parser implements jplag.TokenConstants {
 				errors++;
 				errors++;
 			struct.addToken(new CharToken(FILE_END, files[i], this));
 			struct.addToken(new CharToken(FILE_END, files[i], this));
 		}
 		}
-		//System.err.println(struct.toString());
 		if (errors == 0)
 		if (errors == 0)
 			program.print(null, "OK");
 			program.print(null, "OK");
 		else
 		else
@@ -35,7 +35,7 @@ public class Parser extends jplag.Parser implements jplag.TokenConstants {
 		int offset = 0;
 		int offset = 0;
 
 
 		try {
 		try {
-			FileReader fis = new FileReader(new File(dir, file));
+			FileReader fis = new FileReader(new File(dir, file), StandardCharsets.UTF_8);
 
 
 			do {
 			do {
 				length = fis.read(buffer);
 				length = fis.read(buffer);

+ 0 - 5
jplag.frontend.java-1.1exp/src/main/java/jplag/javax/Parser.java

@@ -19,7 +19,6 @@ public class Parser extends jplag.Parser implements JavaTokenConstants {
 				errors++;
 				errors++;
 			struct.addToken(new JavaToken(FILE_END, actFile, 0));
 			struct.addToken(new JavaToken(FILE_END, actFile, 0));
 		}
 		}
-		// System.err.println(struct.toString());
 		if (errors == 0)
 		if (errors == 0)
 			program.print(null, "OK\n");
 			program.print(null, "OK\n");
 		else
 		else
@@ -30,10 +29,6 @@ public class Parser extends jplag.Parser implements JavaTokenConstants {
 
 
 	public void add(int type, Token token) {
 	public void add(int type, Token token) {
 		struct.addToken(new JavaToken(type, actFile, token.beginLine));
 		struct.addToken(new JavaToken(type, actFile, token.beginLine));
-		/*
-		 * System.out.println(token.beginLine+"\t"+ (new
-		 * JavaToken(0,null,0)).type2string(type)+"\t"+ token.image);
-		 */
 	}
 	}
 
 
 }
 }

+ 2 - 1
jplag/src/main/java/jplag/JPlag.java

@@ -10,6 +10,7 @@ import java.io.FileReader;
 import java.io.IOException;
 import java.io.IOException;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.InvocationTargetException;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Iterator;
@@ -444,7 +445,7 @@ public class JPlag implements ProgramI {
         excludedFileNames = new HashSet<>();
         excludedFileNames = new HashSet<>();
 
 
         try {
         try {
-            BufferedReader reader = new BufferedReader(new FileReader(options.getExclusionFileName()));
+            BufferedReader reader = new BufferedReader(new FileReader(options.getExclusionFileName(), StandardCharsets.UTF_8));
             String line;
             String line;
 
 
             while ((line = reader.readLine()) != null) {
             while ((line = reader.readLine()) != null) {

+ 1 - 3
jplag/src/main/java/jplag/Submission.java

@@ -17,8 +17,6 @@ import java.util.Collections;
 import java.util.List;
 import java.util.List;
 import java.util.Vector;
 import java.util.Vector;
 
 
-import jplag.options.Verbosity;
-
 /**
 /**
  * Represents a single submission. A submission can contain multiple files.
  * Represents a single submission. A submission can contain multiple files.
  */
  */
@@ -209,7 +207,7 @@ public class Submission implements Comparable<Submission> {
                 int size = (int) file.length();
                 int size = (int) file.length();
                 char[] buffer = new char[size];
                 char[] buffer = new char[size];
 
 
-                FileReader reader = new FileReader(file);
+                FileReader reader = new FileReader(file, StandardCharsets.UTF_8);
 
 
                 if (size != reader.read(buffer)) {
                 if (size != reader.read(buffer)) {
                     System.out.println("Not right size read from the file, " + "but I will still continue...");
                     System.out.println("Not right size read from the file, " + "but I will still continue...");

+ 2 - 1
jplag/src/main/java/jplag/reporting/Report.java

@@ -7,6 +7,7 @@ import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.IOException;
 import java.net.URL;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.text.SimpleDateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Comparator;
 import java.util.Comparator;
 import java.util.Date;
 import java.util.Date;
@@ -651,7 +652,7 @@ public class Report { // Mostly legacy code with some minor improvements.
 
 
         try {
         try {
             csvFile.createNewFile();
             csvFile.createNewFile();
-            writer = new FileWriter(csvFile);
+            writer = new FileWriter(csvFile, StandardCharsets.UTF_8);
 
 
             for (JPlagComparison comparison : comparisons) {
             for (JPlagComparison comparison : comparisons) {
                 String submissionNameA = comparison.getFirstSubmission().name;
                 String submissionNameA = comparison.getFirstSubmission().name;

+ 2 - 1
jplag/src/test/java/jplag/VolumeTest.java

@@ -5,6 +5,7 @@ import static org.junit.Assert.assertTrue;
 
 
 import java.io.File;
 import java.io.File;
 import java.io.IOException;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Path;
 import java.util.HashMap;
 import java.util.HashMap;
@@ -59,7 +60,7 @@ public class VolumeTest extends TestBase {
     }
     }
 
 
     private Map<String, Float> readCSVResults(String filePathAndName) throws IOException {
     private Map<String, Float> readCSVResults(String filePathAndName) throws IOException {
-        List<String> lines = Files.readAllLines(Path.of(filePathAndName));
+        List<String> lines = Files.readAllLines(Path.of(filePathAndName), StandardCharsets.UTF_8);
         var results = new HashMap<String, Float>();
         var results = new HashMap<String, Float>();
 
 
         lines.forEach(line -> {
         lines.forEach(line -> {