import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class HtmlDiff {
public static void main(String[] args) {
String file1Path = null;
String file2Path = null;
String outputFilePath = null;
boolean ignoreWhitespace = false;
boolean ignoreAttributes = false;
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (arg.equals("-f")) {
if (i + 1 < args.length) {
file1Path = args[i + 1];
i++;
} else {
System.err.println("Error: -f requires a file path.");
return;
}
} else if (arg.equals("-s")) {
if (i + 1 < args.length) {
file2Path = args[i + 1];
i++;
} else {
System.err.println("Error: -s requires a file path.");
return;
}
} else if (arg.equals("-o")) {
if (i + 1 < args.length) {
outputFilePath = args[i + 1];
i++;
} else {
System.err.println("Error: -o requires a file path.");
return;
}
} else if (arg.equals("-w")) {
ignoreWhitespace = true;
} else if (arg.equals("-a")) {
ignoreAttributes = true;
} else {
System.err.println("Error: Unknown argument: " + arg);
return;
}
}
if (file1Path == null || file2Path == null) {
System.err.println("Error: Requires -f and -s arguments.");
return;
}
try {
Map<String, Map<String, List<String>>> diff = diffHtmlFiles(file1Path, file2Path, ignoreWhitespace, ignoreAttributes);
if (outputFilePath != null) {
writeDiffToFile(diff, outputFilePath);
} else {
printDiff(diff);
}
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
private static Map<String, Map<String, List<String>>> diffHtmlFiles(String file1Path, String file2Path, boolean ignoreWhitespace, boolean ignoreAttributes) throws IOException {
List<String> file1Lines = Files.readAllLines(Paths.get(file1Path));
List<String> file2Lines = Files.readAllLines(Paths.get(file2Path));
Map<String, Map<String, List<String>>> diff = new HashMap<>();
// Extract elements and attributes from file1
Map<String, List<String>> file1Elements = extractElements(file1Lines, ignoreWhitespace, ignoreAttributes);
diff.put("file1", file1Elements);
// Extract elements and attributes from file2
Map<String, List<String>> file2Elements = extractElements(file2Lines, ignoreWhitespace, ignoreAttributes);
diff.put("file2", file2Elements);
// Compare the elements
compareElements(file1Elements, file2Elements, diff);
return diff;
}
private static Map<String, List<String>> extractElements(List<String> lines, boolean ignoreWhitespace, boolean ignoreAttributes) {
Map<String, List<String>> elements = new HashMap<>();
String currentElement = null;
for (String line : lines) {
if (line.startsWith("<") && line.endsWith(">")) {
currentElement = line;
elements.computeIfAbsent(currentElement, k -> new ArrayList<>()).add(line);
} else if (currentElement != null) {
if (ignoreWhitespace) {
String trimmedLine = line.trim();
if (!trimmedLine.startsWith("<") || !
Add your comment