import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HtmlLogger {
private static List<String> log = new ArrayList<>(); // List to store log entries
private static String logFile = "html_log.txt"; // Name of the log file
public static void logOperation(String operation) {
// Add the current operation to the log
log.add(operation);
}
public static void logStartDocument() {
logOperation("Document started.");
}
public static void logEndDocument() {
logOperation("Document ended.");
}
public static void logTagOpen(String tag) {
logOperation("Tag opened: " + tag);
}
public static void logTagClose(String tag) {
logOperation("Tag closed: " + tag);
}
public static void logAttribute(String tag, String attribute, String value) {
logOperation("Attribute: " + tag + "[" + attribute + "]" + "=" + value);
}
public static void logText(String text) {
logOperation("Text: " + text);
}
public static void saveLog() throws IOException {
// Save the log entries to the file
try (FileWriter writer = new FileWriter(logFile, true)) { // Append to the file
for (String entry : log) {
writer.write(entry + "\n");
}
}
}
public static void main(String[] args) throws IOException {
//Example usage
logStartDocument();
logTagOpen("<html>");
logAttribute("html", "lang", "en");
logTagOpen("head");
logTagOpen("title");
logText("My Document");
logTagClose("title");
logTagClose("head");
logTagClose("<html>");
logEndDocument();
saveLog();
}
}
Add your comment