-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeafNode.java
98 lines (80 loc) · 2.21 KB
/
LeafNode.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package org.jsoup.nodes;
import org.jsoup.helper.Validate;
import java.util.Collections;
import java.util.List;
abstract class LeafNode extends Node {
private static final List<Node> EmptyNodes = Collections.emptyList();
Object value; // either a string value, or an attribute map (in the rare case multiple attributes are set)
protected final boolean hasAttributes() {
return value instanceof Attributes;
}
@Override
public final Attributes attributes() {
ensureAttributes();
return (Attributes) value;
}
private void ensureAttributes() {
if (!hasAttributes()) {
Object coreValue = value;
Attributes attributes = new Attributes();
value = attributes;
if (coreValue != null)
attributes.put(nodeName(), (String) coreValue);
}
}
String coreValue() {
return attr(nodeName());
}
void coreValue(String value) {
attr(nodeName(), value);
}
@Override
public String attr(String key) {
Validate.notNull(key);
if (!hasAttributes()) {
return key.equals(nodeName()) ? (String) value : EmptyString;
}
return super.attr(key);
}
@Override
public Node attr(String key, String value) {
if (!hasAttributes() && key.equals(nodeName())) {
this.value = value;
} else {
ensureAttributes();
super.attr(key, value);
}
return this;
}
@Override
public boolean hasAttr(String key) {
ensureAttributes();
return super.hasAttr(key);
}
@Override
public Node removeAttr(String key) {
ensureAttributes();
return super.removeAttr(key);
}
@Override
public String absUrl(String key) {
ensureAttributes();
return super.absUrl(key);
}
@Override
public String baseUri() {
return hasParent() ? parent().baseUri() : "";
}
@Override
protected void doSetBaseUri(String baseUri) {
// noop
}
@Override
public int childNodeSize() {
return 0;
}
@Override
protected List<Node> ensureChildNodes() {
return EmptyNodes;
}
}