From c14e53775591cb4d75b486d21f4849552d5c7c8c Mon Sep 17 00:00:00 2001 From: JJ Date: Mon, 26 Dec 2022 21:38:32 -0800 Subject: Remove garbage serialization/logging code --- .idea/libraries/json_20210307.xml | 9 --- apus.iml | 1 - data/apus.cache | 1 - src/main/model/BrowserState.java | 5 -- src/main/model/html/ElementNode.java | 9 +-- src/main/model/html/HtmlParser.java | 8 +-- src/main/model/html/Node.java | 5 -- src/main/model/html/TextNode.java | 10 +-- src/main/model/util/Event.java | 66 ------------------- src/main/model/util/EventLog.java | 60 ----------------- src/main/persistance/JsonAble.java | 7 -- src/main/persistance/JsonUtils.java | 46 ------------- src/main/ui/BrowserApp.java | 110 -------------------------------- src/main/ui/BrowserWindow.java | 67 ------------------- src/test/persistance/JsonUtilsTest.java | 36 ----------- 15 files changed, 3 insertions(+), 437 deletions(-) delete mode 100644 .idea/libraries/json_20210307.xml delete mode 100644 data/apus.cache delete mode 100644 src/main/model/util/Event.java delete mode 100644 src/main/model/util/EventLog.java delete mode 100644 src/main/persistance/JsonAble.java delete mode 100644 src/main/persistance/JsonUtils.java delete mode 100644 src/test/persistance/JsonUtilsTest.java diff --git a/.idea/libraries/json_20210307.xml b/.idea/libraries/json_20210307.xml deleted file mode 100644 index f64aae4..0000000 --- a/.idea/libraries/json_20210307.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/apus.iml b/apus.iml index 5001d20..19129c3 100644 --- a/apus.iml +++ b/apus.iml @@ -28,6 +28,5 @@ - \ No newline at end of file diff --git a/data/apus.cache b/data/apus.cache deleted file mode 100644 index 0637a08..0000000 --- a/data/apus.cache +++ /dev/null @@ -1 +0,0 @@ -[] \ No newline at end of file diff --git a/src/main/model/BrowserState.java b/src/main/model/BrowserState.java index bb15abf..da21c41 100644 --- a/src/main/model/BrowserState.java +++ b/src/main/model/BrowserState.java @@ -1,8 +1,5 @@ package model; -import model.util.Event; -import model.util.EventLog; - import java.util.ArrayDeque; // This BrowserState function collects the stateful portions of the browser into one modelable class. @@ -37,13 +34,11 @@ public class BrowserState { if (!this.tabs.contains(added)) { this.tabs.add(added); } - EventLog.getInstance().logEvent(new Event("Added tab " + added + " to tablist")); } // MODIFIES: this // EFFECTS: removes a tab from the tablist public void removeTab(String removed) { this.tabs.remove(removed); - EventLog.getInstance().logEvent(new Event("Removed tab " + removed + " from tablist")); } } diff --git a/src/main/model/html/ElementNode.java b/src/main/model/html/ElementNode.java index ef241e4..6f0a556 100644 --- a/src/main/model/html/ElementNode.java +++ b/src/main/model/html/ElementNode.java @@ -1,15 +1,13 @@ package model.html; import org.javatuples.Pair; -import org.json.JSONObject; -import persistance.JsonAble; import java.util.ArrayList; /** * This ElementNode class represents an HTML tag and nested tags. */ -public class ElementNode implements Node, JsonAble { +public class ElementNode implements Node { private String tag; private ArrayList> attributes; @@ -67,9 +65,4 @@ public class ElementNode implements Node, JsonAble { public String getData() { return getTag() + " " + getAttributes().toString(); } - - @Override - public JSONObject serialize() { - return new JSONObject(this); - } } diff --git a/src/main/model/html/HtmlParser.java b/src/main/model/html/HtmlParser.java index 5170205..e50f713 100644 --- a/src/main/model/html/HtmlParser.java +++ b/src/main/model/html/HtmlParser.java @@ -3,8 +3,6 @@ package model.html; import java.util.*; import org.javatuples.*; -import org.json.JSONObject; -import persistance.JsonAble; /** * This class represents the state of and implements an LL(1) HTML parser. @@ -19,7 +17,7 @@ import persistance.JsonAble; * SELF_CLOSING_TAG ::= 'img' | ... * (note that \forall T \in SELF_CLOSING_TAG, T \notin TAG) */ -public class HtmlParser implements JsonAble { +public class HtmlParser { /** * HTML is not nice to parse. We manage to get away with a relatively small number of parser states regardless. @@ -346,10 +344,6 @@ public class HtmlParser implements JsonAble { return false; } } - - public JSONObject serialize() { - return new JSONObject(this); - } } /* diff --git a/src/main/model/html/Node.java b/src/main/model/html/Node.java index 5c3ea41..9a846ee 100644 --- a/src/main/model/html/Node.java +++ b/src/main/model/html/Node.java @@ -1,7 +1,5 @@ package model.html; -import org.json.JSONObject; - /** * This Node represents an abstract relationship between ElementNode and TextNode. * It's extremely helpful / necessary for Lists of arbitrary ElementNodes/TextNodes. @@ -9,7 +7,4 @@ import org.json.JSONObject; public interface Node { // Return a representation of the Node. Useful for debugging. public String getData(); - - // EFFECTS: returns a serialized form of the data - public JSONObject serialize(); } diff --git a/src/main/model/html/TextNode.java b/src/main/model/html/TextNode.java index 82ae245..464180f 100644 --- a/src/main/model/html/TextNode.java +++ b/src/main/model/html/TextNode.java @@ -1,12 +1,9 @@ package model.html; -import org.json.JSONObject; -import persistance.JsonAble; - /** * This TextNode class represents raw text, with no nested tags. */ -public class TextNode implements Node, JsonAble { +public class TextNode implements Node { private String text = ""; /** @@ -25,9 +22,4 @@ public class TextNode implements Node, JsonAble { public String getData() { return getText(); } - - @Override - public JSONObject serialize() { - return new JSONObject(this); - } } diff --git a/src/main/model/util/Event.java b/src/main/model/util/Event.java deleted file mode 100644 index 8a42ef8..0000000 --- a/src/main/model/util/Event.java +++ /dev/null @@ -1,66 +0,0 @@ -package model.util; - -import java.util.Calendar; -import java.util.Date; - - -/** - * Represents an alarm system event. - */ -public class Event { - private static final int HASH_CONSTANT = 13; - private Date dateLogged; - private String description; - - /** - * Creates an event with the given description - * and the current date/time stamp. - * @param description a description of the event - */ - public Event(String description) { - dateLogged = Calendar.getInstance().getTime(); - this.description = description; - } - - /** - * Gets the date of this event (includes time). - * @return the date of the event - */ - public Date getDate() { - return dateLogged; - } - - /** - * Gets the description of this event. - * @return the description of the event - */ - public String getDescription() { - return description; - } - - @Override - public boolean equals(Object other) { - if (other == null) { - return false; - } - - if (other.getClass() != this.getClass()) { - return false; - } - - Event otherEvent = (Event) other; - - return (this.dateLogged.equals(otherEvent.dateLogged) - && this.description.equals(otherEvent.description)); - } - - @Override - public int hashCode() { - return (HASH_CONSTANT * dateLogged.hashCode() + description.hashCode()); - } - - @Override - public String toString() { - return dateLogged.toString() + "\n" + description; - } -} diff --git a/src/main/model/util/EventLog.java b/src/main/model/util/EventLog.java deleted file mode 100644 index 507c9b2..0000000 --- a/src/main/model/util/EventLog.java +++ /dev/null @@ -1,60 +0,0 @@ -package model.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; - -/** - * Represents a log of alarm system events. - * We use the Singleton Design Pattern to ensure that there is only - * one EventLog in the system and that the system has global access - * to the single instance of the EventLog. - */ -public class EventLog implements Iterable { - /** the only EventLog in the system (Singleton Design Pattern) */ - private static EventLog theLog; - private Collection events; - - /** - * Prevent external construction. - * (Singleton Design Pattern). - */ - private EventLog() { - events = new ArrayList(); - } - - /** - * Gets instance of EventLog - creates it - * if it doesn't already exist. - * (Singleton Design Pattern) - * @return instance of EventLog - */ - public static EventLog getInstance() { - if (theLog == null) { - theLog = new EventLog(); - } - - return theLog; - } - - /** - * Adds an event to the event log. - * @param e the event to be added - */ - public void logEvent(Event e) { - events.add(e); - } - - /** - * Clears the event log and logs the event. - */ - public void clear() { - events.clear(); - logEvent(new Event("Event log cleared.")); - } - - @Override - public Iterator iterator() { - return events.iterator(); - } -} diff --git a/src/main/persistance/JsonAble.java b/src/main/persistance/JsonAble.java deleted file mode 100644 index 408cb06..0000000 --- a/src/main/persistance/JsonAble.java +++ /dev/null @@ -1,7 +0,0 @@ -package persistance; - -import org.json.JSONObject; - -public interface JsonAble { - public JSONObject serialize(); -} diff --git a/src/main/persistance/JsonUtils.java b/src/main/persistance/JsonUtils.java deleted file mode 100644 index c97e5fd..0000000 --- a/src/main/persistance/JsonUtils.java +++ /dev/null @@ -1,46 +0,0 @@ -package persistance; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.io.PrintWriter; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -public class JsonUtils { - - /** - * REQUIRES: A valid filepath path, a writeable JSONObject json - * EFFECTS: writes a String to a file - */ - public static void writeToFile(JSONArray json, String path) { - PrintWriter writer; - try { - writer = new PrintWriter(path); - writer.print(json); - writer.close(); - } catch (Exception e) { - System.out.printf("Write to file failed with %s", e.toString()); - } - } - - /** - * REQUIRES: a path to a valid file containing JSONObject-serialized data - * EFFECTS: reads a serialized String into a JSONObject - */ - public static JSONArray readFromFile(String pathString) { - String content; - JSONArray deread; - Path path; - try { - path = Paths.get(pathString); - content = new String(Files.readAllBytes(path)); - deread = new JSONArray(content); - } catch (Exception e) { - System.out.println("Read from file failed with %s"); - deread = new JSONArray(); - } - return deread; - } -} diff --git a/src/main/ui/BrowserApp.java b/src/main/ui/BrowserApp.java index c4bbf99..520cac8 100644 --- a/src/main/ui/BrowserApp.java +++ b/src/main/ui/BrowserApp.java @@ -4,11 +4,7 @@ import model.html.ElementNode; import model.html.HtmlParser; import model.html.TextNode; import model.html.Node; -import org.json.JSONArray; -import org.json.JSONObject; -import persistance.JsonUtils; -import java.io.File; import java.nio.file.*; import java.util.*; @@ -18,7 +14,6 @@ import java.util.*; public class BrowserApp { private Scanner input; private static final String border = "==============================================="; - private static final String storagePath = "data/apus.cache"; private String pathString; private ArrayList parsed; private ArrayDeque tabs; @@ -30,38 +25,9 @@ public class BrowserApp { println("apus: currently a barebones html/css renderer"); this.input = new Scanner(System.in); this.tabs = new ArrayDeque<>(); - - askToRestoreTabs(); mainLoop(); } - /** - * EFFECTS: Asks the user if they'd like to restore previously closed tabs. - */ - private void askToRestoreTabs() { - if (new File(storagePath).length() > 2) { - println("Would you like to restore your previously closed tabs? (Y/N)"); - String answer; - while (true) { - answer = this.input.next(); - if (answer.equalsIgnoreCase("y")) { - restoreClosedTabs(); - break; - } else if (answer.equalsIgnoreCase("n")) { - JsonUtils.writeToFile(new JSONArray(), storagePath); - println("please provide a path to a file (examples located in data/*):"); - pathString = this.input.next(); - break; - } else { - println("Sorry, I didn't quite get that. Please try again."); - } - } - } else { - println("please provide a path to a file (examples located in data/*):"); - pathString = this.input.next(); - } - } - /** * EFFECTS: Runs the main loop */ @@ -86,23 +52,6 @@ public class BrowserApp { } } - /** - * EFFECTS: restores previous closed tabs from a cache file. - */ - private void restoreClosedTabs() { - try { - JSONArray state = JsonUtils.readFromFile(storagePath); - for (int i = 0; i < state.length(); i++) { - println(state.get(i).getClass().getName()); - tabs.add((String) state.get(i)); - } - pathString = tabs.removeLast(); - } catch (Exception e) { - println("Restoring state from disk failed with " + e.toString()); - System.exit(0); - } - } - /** * EFFECTS: Barebones HTML rendering. Iterates through a list of Nodes and their children and prints any text. */ @@ -135,7 +84,6 @@ public class BrowserApp { pathString = this.tabs.removeFirst(); break; case "quit": - handleQuit(); System.exit(0); break; default: @@ -144,64 +92,6 @@ public class BrowserApp { } } - /** - * Helper function for the quit() case. - * EFFECTS: Asks a user whether they'd like to save their tabs, and exists the program. - */ - private void handleQuit() { - println("Would you like to save your currently opened tabs to disk? (Y/N)"); - String answer; - while (true) { - answer = this.input.next(); - if (answer.equalsIgnoreCase("y")) { - this.tabs.add(pathString); - JsonUtils.writeToFile(new JSONArray(tabs), storagePath); - break; - } else if (answer.equalsIgnoreCase("n")) { - JsonUtils.writeToFile(new JSONArray(), storagePath); - break; - } else { - println("Sorry, I didn't quite get that. Please try again."); - } - } - } - - /** - * EFFECTS: writes the current program configuration to the disk - */ - private void writeToDisk() { - ArrayList> jsonArray = new ArrayList<>(); - for (String p : tabs) { - ArrayList jsonArrayII = new ArrayList<>(); - try { - Path path = Paths.get(pathString); - String file = new String(Files.readAllBytes(path)); - HtmlParser parser = new HtmlParser(); - for (Node n : parser.parseHtml(file)) { - jsonArrayII.add(n.serialize()); - } - } catch (Exception e) { - System.out.printf("Failed to write to disk with %s", e); - } - jsonArray.add(jsonArrayII); - } - JsonUtils.writeToFile(new JSONArray(jsonArray), storagePath); - } - - /** - * EFFECTS: restores program state from a last written to state - */ - private void restoreFromDisk(JSONArray state) { - for (int i = 0; i < state.length(); i++) { - Object tab = state.get(i); - if (tab instanceof JSONArray) { - for (int j = 0; j < ((JSONArray) tab).length(); j++) { - tabs.add(((JSONArray) tab).toString()); - } - } - } - } - private void print(String toPrint) { System.out.print(toPrint); } diff --git a/src/main/ui/BrowserWindow.java b/src/main/ui/BrowserWindow.java index 2375fc1..bd1cf7f 100644 --- a/src/main/ui/BrowserWindow.java +++ b/src/main/ui/BrowserWindow.java @@ -2,16 +2,9 @@ package ui; import model.BrowserState; import model.html.HtmlParser; -import model.util.Event; -import model.util.EventLog; -import org.json.JSONArray; -import persistance.JsonUtils; import javax.swing.*; import java.awt.*; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -22,7 +15,6 @@ import java.util.ArrayList; public class BrowserWindow extends JFrame { public static final int WIDTH = 1200; public static final int HEIGHT = 800; - private static final String storagePath = "data/apus.cache"; private BrowserCanvas canvas; private BrowserBar browserBar; @@ -46,9 +38,6 @@ public class BrowserWindow extends JFrame { // render("data/example.hctml"); // browserBar.addTab("/home/apropos/Projects/website/j-james/index.html"); setVisible(true); - setClosingBehavior(); - - initializeBrowser(); } // MODIFIES: this @@ -72,62 +61,6 @@ public class BrowserWindow extends JFrame { setVisible(true); } - // EFFECTS: Prompts the user to save their tabs before quitting - private void setClosingBehavior() { - this.addWindowListener(new WindowAdapter() { - @Override - public void windowClosing(WindowEvent e) { - if (state.getTabs().size() > 0) { - saveCurrentTabs(); - } - for (Event event : EventLog.getInstance()) { - System.out.println(event); - } - super.windowClosing(e); - } - }); - } - - // EFFECTS: sets up the browser upon launching - private void initializeBrowser() { - if (new File(storagePath).length() > 2) { - restorePreviousTabs(); - } - } - - // MODIFIES: this - // EFFECTS: prompts the user to restore their previous tabs - private void restorePreviousTabs() { - int answer = JOptionPane.showOptionDialog( - this, "Would you like to restore your previous tabs?", "apus", - JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, - null, new String[]{"Yes", "No"}, "Yes"); - if (answer == 0) { - try { - JSONArray state = JsonUtils.readFromFile(storagePath); - for (int i = 0; i < state.length(); i++) { - this.browserBar.addTab((String) state.get(i)); - this.state.addTab((String) state.get(i)); - } - } catch (Exception e) { - System.out.println("Restoring state from disk failed with " + e.toString()); - } - } - } - - // EFFECTS: prompts the user to save their current tabs before closing - private void saveCurrentTabs() { - int answer = JOptionPane.showOptionDialog( - this, "Would you like to save your current tabs?", "apus", - JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, - null, new String[]{"Yes", "No"}, "Yes"); - if (answer == 0) { - JsonUtils.writeToFile(new JSONArray(state.getTabs()), storagePath); - } else { - JsonUtils.writeToFile(new JSONArray(), storagePath); - } - } - public BrowserState getBrowserState() { return this.state; } diff --git a/src/test/persistance/JsonUtilsTest.java b/src/test/persistance/JsonUtilsTest.java deleted file mode 100644 index 9ab9b1c..0000000 --- a/src/test/persistance/JsonUtilsTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package persistance; - -import org.json.JSONArray; -import org.json.JSONObject; -import org.junit.jupiter.api.Test; - -import java.util.ArrayDeque; - -import static org.junit.jupiter.api.Assertions.*; - -public class JsonUtilsTest { - private static final String testPath = "data/test.cache"; - - @Test - void testWritingEmptyFile() { - ArrayDeque test = new ArrayDeque<>(); - JsonUtils.writeToFile(new JSONArray(test), testPath); - - JSONArray dugUp = JsonUtils.readFromFile(testPath); - assertTrue(dugUp.isEmpty()); - } - - @Test - void testWritingLists() { - ArrayDeque test = new ArrayDeque<>(); - test.add("/home/apropos/foo/bar"); // absolute path - test.add("data"); // relative path - test.add("/home/apropos/baz/bar"); // another path - JsonUtils.writeToFile(new JSONArray(test), testPath); - - JSONArray dugUp = JsonUtils.readFromFile(testPath); - for (int i = 0; i < dugUp.length(); i++) { - assertTrue(test.removeFirst().equals(dugUp.get(i))); - } - } -} -- cgit v1.2.3-70-g09d2