blob: 4262e031149d386be534183b50e058540dd45dae (
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
|
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<String, String> attributes, StringBuilder builder) {
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(HtmlWriter.escapeAttributeValue(attribute.getValue()))
.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)));
}
}
|