aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/html/HtmlWriter.java
blob: 50bf85bdb4dc08abbd13fa9dc153282e63810bc2 (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
package org.zwobble.mammoth.html;

import java.util.Map;

import static org.zwobble.mammoth.util.MammothLists.orderedBy;

public class HtmlWriter {
    public static String escapeText(String text) {
        return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
    }

    private static String escapeAttributeValue(String value) {
        return escapeText(value).replace("\"", "&quot;");
    }

    public static void generateAttributes(Map<String, String> attributes, StringBuilder builder) {
        for (Map.Entry<String, String> attribute : orderedBy(attributes.entrySet(), Map.Entry::getKey)) {
            builder
                .append(" ")
                .append(attribute.getKey())
                .append("=\"")
                .append(HtmlWriter.escapeAttributeValue(attribute.getValue()))
                .append("\"");
        }
    }
}