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 HtmlElement implements HtmlNode {
private final String tagName;
private final Map attributes;
private final List children;
public HtmlElement(String tagName, Map attributes, List children) {
this.tagName = tagName;
this.attributes = attributes;
this.children = children;
}
@Override
public void write(StringBuilder builder) {
builder.append("<").append(tagName);
List> sortedAttributes = orderBy(Map.Entry::getKey)
.sortedCopy(attributes.entrySet());
for (Map.Entry 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 > Ordering orderBy(Function getKey) {
return Ordering.from((first, second) -> getKey.apply(first).compareTo(getKey.apply(second)));
}
}