From 622c04d788615dc79665203ea9a61e57e1935c49 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 14 Feb 2016 11:42:44 +0000 Subject: Rename HtmlElementNode to HtmlElement --- .../java/org/zwobble/mammoth/html/HtmlElement.java | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/org/zwobble/mammoth/html/HtmlElement.java (limited to 'src/main/java/org/zwobble/mammoth/html/HtmlElement.java') diff --git a/src/main/java/org/zwobble/mammoth/html/HtmlElement.java b/src/main/java/org/zwobble/mammoth/html/HtmlElement.java new file mode 100644 index 0000000..312cebe --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/html/HtmlElement.java @@ -0,0 +1,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 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(""); + } + + private static > Ordering orderBy(Function getKey) { + return Ordering.from((first, second) -> getKey.apply(first).compareTo(getKey.apply(second))); + } +} -- cgit v1.2.3