diff options
author | j-james | 2022-10-29 04:22:06 +0000 |
---|---|---|
committer | j-james | 2022-10-29 04:22:06 +0000 |
commit | 106a0fe85effd27f8da28d17cf1053d5c50cd5fc (patch) | |
tree | a5f706008452fb30091e59752576a09aaf6ca390 /src/main/model | |
parent | 9a512732accf6869764fedc7c5d1abad57c6cb28 (diff) |
Implement functionality for P2
Diffstat (limited to 'src/main/model')
-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/TextNode.java | 9 | ||||
-rw-r--r-- | src/main/model/util/Node.java | 4 |
4 files changed, 27 insertions, 3 deletions
diff --git a/src/main/model/html/ElementNode.java b/src/main/model/html/ElementNode.java index 16438f9..5ff4a0f 100644 --- a/src/main/model/html/ElementNode.java +++ b/src/main/model/html/ElementNode.java @@ -2,6 +2,8 @@ package model.html; import model.util.Node; import org.javatuples.Pair; +import org.json.JSONObject; +import persistance.JsonAble; import java.util.ArrayList; import java.util.Optional; @@ -9,7 +11,7 @@ import java.util.Optional; /** * This ElementNode class represents an HTML tag and nested tags. */ -public class ElementNode implements Node { +public class ElementNode implements Node, JsonAble { private String tag; private ArrayList<Pair<String,String>> attributes; @@ -67,4 +69,9 @@ public class ElementNode implements Node { 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 d07a2ff..f0829f4 100644 --- a/src/main/model/html/HtmlParser.java +++ b/src/main/model/html/HtmlParser.java @@ -4,6 +4,8 @@ import java.util.*; import model.util.Node; import org.javatuples.*; +import org.json.JSONObject; +import persistance.JsonAble; /** * This class represents the state of and implements an LL(1) HTML parser. @@ -18,7 +20,7 @@ import org.javatuples.*; * SELF_CLOSING_TAG ::= 'img' | ... * (note that \forall T \in SELF_CLOSING_TAG, T \notin TAG) */ -public class HtmlParser { +public class HtmlParser implements JsonAble { /** * HTML is not nice to parse. We manage to get away with a relatively small number of parser states regardless. @@ -343,6 +345,10 @@ public class HtmlParser { return false; } } + + public JSONObject serialize() { + return new JSONObject(this); + } } /* diff --git a/src/main/model/html/TextNode.java b/src/main/model/html/TextNode.java index f6d3ce1..2e89326 100644 --- a/src/main/model/html/TextNode.java +++ b/src/main/model/html/TextNode.java @@ -1,11 +1,13 @@ package model.html; import model.util.Node; +import org.json.JSONObject; +import persistance.JsonAble; /** * This TextNode class represents raw text, with no nested tags. */ -public class TextNode implements Node { +public class TextNode implements Node, JsonAble { private String text = ""; /** @@ -24,4 +26,9 @@ public class TextNode implements Node { public String getData() { return getText(); } + + @Override + public JSONObject serialize() { + return new JSONObject(this); + } } diff --git a/src/main/model/util/Node.java b/src/main/model/util/Node.java index 4057fab..a6fedaf 100644 --- a/src/main/model/util/Node.java +++ b/src/main/model/util/Node.java @@ -1,5 +1,7 @@ package model.util; +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. @@ -7,4 +9,6 @@ package model.util; public interface Node { // Return a representation of the Node. Useful for debugging. public String getData(); + + public JSONObject serialize(); } |