blob: d237f6be8e2528df763f664af73ad7e8a4875cbf (
plain)
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
|
package org.zwobble.mammoth.html;
import com.google.common.base.Function;
import com.google.common.collect.Ordering;
import java.util.List;
import java.util.Map;
public class HtmlElementNode implements HtmlNode {
private final String tagName;
private final Map<String, String> attributes;
private final List<HtmlNode> children;
public HtmlElementNode(String tagName, Map<String, String> attributes, List<HtmlNode> children) {
this.tagName = tagName;
this.attributes = attributes;
this.children = children;
}
@Override
public void write(StringBuilder builder) {
builder.append("<").append(tagName);
List<Map.Entry<String, String>> sortedAttributes = orderBy(Map.Entry<String, String>::getKey)
.sortedCopy(attributes.entrySet());
for (Map.Entry<String, String> attribute : sortedAttributes) {
builder
.append(" ")
.append(attribute.getKey())
.append("=\"")
.append(attribute.getValue())
.append("\"");
}
builder.append(">");
children.forEach(node -> node.write(builder));
builder
.append("</")
.append(tagName)
.append(">");
}
private static <T, R extends Comparable<R>> Ordering<T> orderBy(Function<T, R> getKey) {
return Ordering.from((first, second) -> getKey.apply(first).compareTo(getKey.apply(second)));
}
}
|