diff options
author | JJ | 2022-12-27 05:38:32 +0000 |
---|---|---|
committer | JJ | 2022-12-27 15:30:20 +0000 |
commit | c14e53775591cb4d75b486d21f4849552d5c7c8c (patch) | |
tree | fc9d67b66107f4965f3173183ec846e4d8e090c4 /src/main | |
parent | 0e693fd4814572712a4192cb4f176e71f7a344f7 (diff) |
Remove garbage serialization/logging code
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/model/BrowserState.java | 5 | ||||
-rw-r--r-- | src/main/model/html/ElementNode.java | 9 | ||||
-rw-r--r-- | src/main/model/html/HtmlParser.java | 8 | ||||
-rw-r--r-- | src/main/model/html/Node.java | 5 | ||||
-rw-r--r-- | src/main/model/html/TextNode.java | 10 | ||||
-rw-r--r-- | src/main/model/util/Event.java | 66 | ||||
-rw-r--r-- | src/main/model/util/EventLog.java | 60 | ||||
-rw-r--r-- | src/main/persistance/JsonAble.java | 7 | ||||
-rw-r--r-- | src/main/persistance/JsonUtils.java | 46 | ||||
-rw-r--r-- | src/main/ui/BrowserApp.java | 110 | ||||
-rw-r--r-- | src/main/ui/BrowserWindow.java | 67 |
11 files changed, 3 insertions, 390 deletions
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<Pair<String,String>> 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<Event> { - /** the only EventLog in the system (Singleton Design Pattern) */ - private static EventLog theLog; - private Collection<Event> events; - - /** - * Prevent external construction. - * (Singleton Design Pattern). - */ - private EventLog() { - events = new ArrayList<Event>(); - } - - /** - * 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<Event> 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<Node> parsed; private ArrayDeque<String> tabs; @@ -30,39 +25,10 @@ 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 */ private void mainLoop() { @@ -87,23 +53,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. */ private void renderHtml(ArrayList<Node> html) { @@ -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<ArrayList<JSONObject>> jsonArray = new ArrayList<>(); - for (String p : tabs) { - ArrayList<JSONObject> 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; } |