• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

javadev / underscore-java / #3107

pending completion
#3107

push

web-flow
Added com.github.underscore.XmlBuilder

4410 of 4410 relevant lines covered (100.0%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/main/java/com/github/underscore/XmlBuilder.java
1
package com.github.underscore;
2

3
import java.util.LinkedHashMap;
4
import java.util.Map;
5
import java.util.Objects;
6

7
public class XmlBuilder {
8
    private static final String SELF_CLOSING = "-self-closing";
9
    private static final String TRUE = "true";
10
    private final Map<String, Object> data;
11
    private String path;
12

13
    XmlBuilder(String rootName) {
1✔
14
        data = new LinkedHashMap<>();
1✔
15
        Map<String, Object> value = new LinkedHashMap<>();
1✔
16
        value.put(SELF_CLOSING, TRUE);
1✔
17
        data.put(rootName, value);
1✔
18
        path = rootName;
1✔
19
    }
1✔
20

21
    public static XmlBuilder create(String rootName) {
22
        return new XmlBuilder(rootName);
1✔
23
    }
24

25
    public static XmlBuilder parse(String xml) {
26
        Map<String, Object> xmlData = U.fromXmlMap(xml);
1✔
27
        XmlBuilder xmlBuilder = new XmlBuilder(Xml.XmlValue.getMapKey(xmlData));
1✔
28
        xmlBuilder.setData(xmlData);
1✔
29
        return xmlBuilder;
1✔
30
    }
31

32
    public XmlBuilder e(String elementName) {
33
        U.remove(data, path + "." + SELF_CLOSING);
1✔
34
        Map<String, Object> value = new LinkedHashMap<>();
1✔
35
        value.put(SELF_CLOSING, TRUE);
1✔
36
        U.set(data, path + "." + elementName, value);
1✔
37
        path += "." + elementName;
1✔
38
        return this;
1✔
39
    }
40

41
    public XmlBuilder a(String attributeName, String value) {
42
        U.remove(data, path + "." + SELF_CLOSING);
1✔
43
        U.set(data, path + ".-" + attributeName, value);
1✔
44
        return this;
1✔
45
    }
46

47
    public XmlBuilder t(String text) {
48
        U.remove(data, path + "." + SELF_CLOSING);
1✔
49
        if (Objects.nonNull(U.get(data, path + ".#text"))) {
1✔
50
            U.set(data, path + ".#text1", text);
1✔
51
        } else {
52
            U.set(data, path + ".#text", text);
1✔
53
        }
54
        return this;
1✔
55
    }
56

57
    public XmlBuilder up() {
58
        path = path.substring(0, path.lastIndexOf("."));
1✔
59
        return this;
1✔
60
    }
61

62
    public org.w3c.dom.Document root() {
63
        try {
64
            return Xml.Document.createDocument(asString());
1✔
65
        } catch (Exception ex) {
1✔
66
            throw new IllegalArgumentException(ex);
1✔
67
        }
68
    }
69

70
    public org.w3c.dom.Document getDocument() {
71
        return root();
1✔
72
    }
73

74
    public String asString() {
75
        return U.toXml(data);
1✔
76
    }
77

78
    public String xml() {
79
        return U.toXml(data);
1✔
80
    }
81

82
    public String json() {
83
        return U.toJson(data);
1✔
84
    }
85

86
    private void setData(Map<String, Object> newData) {
87
        data.clear();
1✔
88
        data.putAll(newData);
1✔
89
    }
1✔
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc