1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
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;
/**
* This ElementNode class represents an HTML tag and nested tags.
*/
public class ElementNode implements Node, JsonAble {
private String tag;
private ArrayList<Pair<String,String>> attributes;
private ArrayList<Node> children;
/**
* EFFECTS: Constructs a new ElementNode from the arguments provided.
* MODIFIES: this
*/
public ElementNode(String tag, ArrayList<Pair<String, String>> attributes, ArrayList<Node> children) {
this.tag = tag;
this.attributes = attributes;
this.children = children;
}
/**
* Overloads the constructor for ease of use. We often don't provide children, at first.
* EFFECTS: Constructs a new ElementNode from the arguments provided.
* MODIFIES: this
*/
public ElementNode(String tag, ArrayList<Pair<String, String>> attributes) {
this(tag, attributes, new ArrayList<>());
}
/**
* Overloads the constructor for ease of use. Should probably only be used for tests.
* EFFECTS: Constructs a new ElementNode from the arguments provided.
* MODIFIES: this
*/
public ElementNode(String tag) {
this(tag, new ArrayList<>(), new ArrayList<>());
}
/**
* EFFECTS: Adds a child to the children ArrayList.
* MODIFIES: this
*/
public void addChild(Node child) {
this.children.add(child);
}
public String getTag() {
return this.tag;
}
public ArrayList<Pair<String, String>> getAttributes() {
return this.attributes;
}
public ArrayList<Node> getChildren() {
return this.children;
}
// We implement this method for easy debugging.
public String getData() {
return getTag() + " " + getAttributes().toString();
}
@Override
public JSONObject serialize() {
return new JSONObject(this);
}
}
|