1. import java.io.FileWriter;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. public class HtmlLogger {
  6. private static List<String> log = new ArrayList<>(); // List to store log entries
  7. private static String logFile = "html_log.txt"; // Name of the log file
  8. public static void logOperation(String operation) {
  9. // Add the current operation to the log
  10. log.add(operation);
  11. }
  12. public static void logStartDocument() {
  13. logOperation("Document started.");
  14. }
  15. public static void logEndDocument() {
  16. logOperation("Document ended.");
  17. }
  18. public static void logTagOpen(String tag) {
  19. logOperation("Tag opened: " + tag);
  20. }
  21. public static void logTagClose(String tag) {
  22. logOperation("Tag closed: " + tag);
  23. }
  24. public static void logAttribute(String tag, String attribute, String value) {
  25. logOperation("Attribute: " + tag + "[" + attribute + "]" + "=" + value);
  26. }
  27. public static void logText(String text) {
  28. logOperation("Text: " + text);
  29. }
  30. public static void saveLog() throws IOException {
  31. // Save the log entries to the file
  32. try (FileWriter writer = new FileWriter(logFile, true)) { // Append to the file
  33. for (String entry : log) {
  34. writer.write(entry + "\n");
  35. }
  36. }
  37. }
  38. public static void main(String[] args) throws IOException {
  39. //Example usage
  40. logStartDocument();
  41. logTagOpen("<html>");
  42. logAttribute("html", "lang", "en");
  43. logTagOpen("head");
  44. logTagOpen("title");
  45. logText("My Document");
  46. logTagClose("title");
  47. logTagClose("head");
  48. logTagClose("<html>");
  49. logEndDocument();
  50. saveLog();
  51. }
  52. }

Add your comment