Skip to content

Commit

Permalink
Removed comments + added line highlight at position of keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcojocaru2002 committed Sep 19, 2023
1 parent 9a35d34 commit a7a0c58
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand All @@ -30,14 +32,14 @@ public void execute(Context ctx, ResultBuilder result) {
}

String code;

try {
code = Files.readString(Path.of(solutionFile.get(0).getPath()));
} catch (IOException e) {
result.genericFailure(this, e);
return;
}

code = removeComments(code);
if (!checkPackageName(code, result)) return;

if (!checkForKeywords(code, result)) return;
Expand All @@ -46,7 +48,7 @@ public void execute(Context ctx, ResultBuilder result) {
private boolean checkPackageName(String code, ResultBuilder result) {
Pattern pattern = Pattern.compile("^\\s*package\\s+delft\\s*;.*", Pattern.DOTALL);
if (!pattern.matcher(code).find()) {
result.compilationSecurityFail("The package name of your solution must be \"delft\"");
result.compilationSecurityFail("The package name of your solution must be \"delft\"", null);
return false;
}

Expand Down Expand Up @@ -90,14 +92,32 @@ private boolean checkForKeywords(String code, ResultBuilder result) {
);
for (String keyword : keywords.keySet()) {
if (code.contains(keyword)) {
result.compilationSecurityFail(keywords.get(keyword));
return false;
String[] lines = code.split("\\n");
// Split lines and search every line for keyword
for (int lineNumber = 0; lineNumber < lines.length; lineNumber++)
if (lines[lineNumber].contains(keyword)) {
// Adding 1 to lineNumber to convert from zero-based index to one-based index
result.compilationSecurityFail(keywords.get(keyword), Optional.of(lineNumber+1));
return false;
}
}
}

return true;
}

public String removeComments(String code) {
// Remove single-line comments (//)
code = code.replaceAll("//.*", "");

// Remove multi-line comments (/* */)
Pattern pattern = Pattern.compile("/\\*.*?\\*/", Pattern.DOTALL);
Matcher matcher = pattern.matcher(code);
code = matcher.replaceAll("");

return code;
}

@Override
public boolean equals(Object other) {
return other instanceof SourceCodeSecurityCheckStep;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public void compilationFail(List<Diagnostic<? extends JavaFileObject>> errors) {
this.compilation = CompilationResult.compilationFail(compilationErrors);
}

public void compilationSecurityFail(String message) {
public void compilationSecurityFail(String message, Optional<Integer> lineNumber) {
this.compilation = CompilationResult.compilationFail(List.of(
new CompilationErrorInfo("Solution.java", 1, message)
new CompilationErrorInfo("Solution.java", lineNumber.orElse(1), message) // Highlight first line if the line number does not exist
));
}

Expand Down

0 comments on commit a7a0c58

Please sign in to comment.