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 HtmlWriter { public static String escapeText(String text) { return text.replace("&", "&").replace("<", "<").replace(">", ">"); } private static String escapeAttributeValue(String value) { return escapeText(value).replace("\"", """); } public static void generateAttributes(Map attributes, StringBuilder builder) { List> sortedAttributes = orderBy(Map.Entry::getKey) .sortedCopy(attributes.entrySet()); for (Map.Entry attribute : sortedAttributes) { builder .append(" ") .append(attribute.getKey()) .append("=\"") .append(HtmlWriter.escapeAttributeValue(attribute.getValue())) .append("\""); } } private static > Ordering orderBy(Function getKey) { return Ordering.from((first, second) -> getKey.apply(first).compareTo(getKey.apply(second))); } }