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

javadev / underscore-java / #3583

22 Apr 2023 04:34AM CUT coverage: 100.0%. Remained the same
#3583

push

web-flow
Update README.md

4424 of 4424 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

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

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

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

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

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

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

46
    public XmlBuilder c(String comment) {
47
        U.remove(data, path + "." + SELF_CLOSING);
1✔
48
        U.update(data, path + ".#comment", comment);
1✔
49
        return this;
1✔
50
    }
51

52
    public XmlBuilder d(String cdata) {
53
        U.remove(data, path + "." + SELF_CLOSING);
1✔
54
        U.update(data, path + ".#cdata-section", cdata);
1✔
55
        return this;
1✔
56
    }
57

58
    public XmlBuilder t(String text) {
59
        U.remove(data, path + "." + SELF_CLOSING);
1✔
60
        U.update(data, path + ".#text", text);
1✔
61
        return this;
1✔
62
    }
63

64
    public XmlBuilder up() {
65
        path = path.substring(0, path.lastIndexOf("."));
1✔
66
        return this;
1✔
67
    }
68

69
    public XmlBuilder root() {
70
        int index = path.indexOf(".");
1✔
71
        XmlBuilder xmlBuilder = new XmlBuilder(index == -1 ? path : path.substring(0, index));
1✔
72
        xmlBuilder.setData(data);
1✔
73
        return xmlBuilder;
1✔
74
    }
75

76
    public org.w3c.dom.Document getDocument() {
77
        try {
78
            return Xml.Document.createDocument(asString());
1✔
79
        } catch (Exception ex) {
1✔
80
            throw new IllegalArgumentException(ex);
1✔
81
        }
82
    }
83

84
    public String asString() {
85
        return U.toXml(data);
1✔
86
    }
87

88
    public String xml() {
89
        return U.toXml(data);
1✔
90
    }
91

92
    public String json() {
93
        return U.toJson(data);
1✔
94
    }
95

96
    private void setData(Map<String, Object> newData) {
97
        data.clear();
1✔
98
        data.putAll(newData);
1✔
99
    }
1✔
100
}
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