1. import java.io.File;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.nio.file.Paths;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.stream.Collectors;
  11. public class HtmlDiff {
  12. public static void main(String[] args) {
  13. String file1Path = null;
  14. String file2Path = null;
  15. String outputFilePath = null;
  16. boolean ignoreWhitespace = false;
  17. boolean ignoreAttributes = false;
  18. for (int i = 0; i < args.length; i++) {
  19. String arg = args[i];
  20. if (arg.equals("-f")) {
  21. if (i + 1 < args.length) {
  22. file1Path = args[i + 1];
  23. i++;
  24. } else {
  25. System.err.println("Error: -f requires a file path.");
  26. return;
  27. }
  28. } else if (arg.equals("-s")) {
  29. if (i + 1 < args.length) {
  30. file2Path = args[i + 1];
  31. i++;
  32. } else {
  33. System.err.println("Error: -s requires a file path.");
  34. return;
  35. }
  36. } else if (arg.equals("-o")) {
  37. if (i + 1 < args.length) {
  38. outputFilePath = args[i + 1];
  39. i++;
  40. } else {
  41. System.err.println("Error: -o requires a file path.");
  42. return;
  43. }
  44. } else if (arg.equals("-w")) {
  45. ignoreWhitespace = true;
  46. } else if (arg.equals("-a")) {
  47. ignoreAttributes = true;
  48. } else {
  49. System.err.println("Error: Unknown argument: " + arg);
  50. return;
  51. }
  52. }
  53. if (file1Path == null || file2Path == null) {
  54. System.err.println("Error: Requires -f and -s arguments.");
  55. return;
  56. }
  57. try {
  58. Map<String, Map<String, List<String>>> diff = diffHtmlFiles(file1Path, file2Path, ignoreWhitespace, ignoreAttributes);
  59. if (outputFilePath != null) {
  60. writeDiffToFile(diff, outputFilePath);
  61. } else {
  62. printDiff(diff);
  63. }
  64. } catch (IOException e) {
  65. System.err.println("Error: " + e.getMessage());
  66. }
  67. }
  68. private static Map<String, Map<String, List<String>>> diffHtmlFiles(String file1Path, String file2Path, boolean ignoreWhitespace, boolean ignoreAttributes) throws IOException {
  69. List<String> file1Lines = Files.readAllLines(Paths.get(file1Path));
  70. List<String> file2Lines = Files.readAllLines(Paths.get(file2Path));
  71. Map<String, Map<String, List<String>>> diff = new HashMap<>();
  72. // Extract elements and attributes from file1
  73. Map<String, List<String>> file1Elements = extractElements(file1Lines, ignoreWhitespace, ignoreAttributes);
  74. diff.put("file1", file1Elements);
  75. // Extract elements and attributes from file2
  76. Map<String, List<String>> file2Elements = extractElements(file2Lines, ignoreWhitespace, ignoreAttributes);
  77. diff.put("file2", file2Elements);
  78. // Compare the elements
  79. compareElements(file1Elements, file2Elements, diff);
  80. return diff;
  81. }
  82. private static Map<String, List<String>> extractElements(List<String> lines, boolean ignoreWhitespace, boolean ignoreAttributes) {
  83. Map<String, List<String>> elements = new HashMap<>();
  84. String currentElement = null;
  85. for (String line : lines) {
  86. if (line.startsWith("<") && line.endsWith(">")) {
  87. currentElement = line;
  88. elements.computeIfAbsent(currentElement, k -> new ArrayList<>()).add(line);
  89. } else if (currentElement != null) {
  90. if (ignoreWhitespace) {
  91. String trimmedLine = line.trim();
  92. if (!trimmedLine.startsWith("<") || !

Add your comment