blob: 5bb80991443e79c324eedd03a291b03ce79cd931 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package semanticanalysis;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
public class Share {
/**
* Removes the duplicate elements in a list of Semantic Errors. It's not
* generic because it's used a custom contains function.
*/
public static ArrayList<SemanticError> removeDuplicates(ArrayList<SemanticError> list) {
ArrayList<SemanticError> newList = new ArrayList<>();
for (SemanticError element : list) {
if (!customContains(newList, element)) {
newList.add(element);
}
}
return newList;
}
/**
* Normal contains did not work, so we made a custom contains function.
* Returns `true` if the String rappresentation of an object in the list is
* equal to the element given in input.
*/
private static boolean customContains(ArrayList<SemanticError> list, SemanticError e) {
String e1 = e.toString();
for (SemanticError element : list) {
String e2 = element.toString();
if (e2.equals(e1)) {
return true;
}
}
return false;
}
public static String getExtension(String fileName) {
int extensionIndex = fileName.lastIndexOf('.');
if (extensionIndex == -1) {
return fileName;
} else {
return fileName.substring(extensionIndex + 1);
}
}
public static String readFile(String filePath) throws IOException {
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
}
return content.toString();
}
public static void saveFile(String fileName, String content) {
try {
Path file = Paths.get(fileName);
if (!Files.exists(file)) {
Files.createFile(file);
}
Files.write(file, content.getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
} catch (Exception e) {
e.printStackTrace();
}
}
}
|