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

javadev / underscore-java / #3210

pending completion
#3210

push

web-flow
Bump maven-project-info-reports-plugin from 3.4.4 to 3.4.5

4359 of 4359 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
/*
2
 * The MIT License (MIT)
3
 *
4
 * Copyright 2023 Valentyn Kolesnikov
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
package com.github.underscore;
25

26
import java.util.ArrayList;
27
import java.util.LinkedHashMap;
28
import java.util.List;
29
import java.util.Map;
30

31
public class XmlBuilder {
32
    private static final String SELF_CLOSING = "-self-closing";
33
    private static final String TRUE = "true";
34
    private final Map<String, Object> data;
35
    private String path;
36
    private String savedPath;
37

38
    XmlBuilder(String rootName) {
1✔
39
        data = new LinkedHashMap<>();
1✔
40
        Map<String, Object> value = new LinkedHashMap<>();
1✔
41
        value.put(SELF_CLOSING, TRUE);
1✔
42
        data.put(rootName, value);
1✔
43
        path = rootName;
1✔
44
    }
1✔
45

46
    public static XmlBuilder create(String rootName) {
47
        return new XmlBuilder(rootName);
1✔
48
    }
49

50
    public static XmlBuilder parse(String xml) {
51
        Map<String, Object> xmlData = U.fromXmlMap(xml);
1✔
52
        XmlBuilder xmlBuilder = new XmlBuilder(Xml.XmlValue.getMapKey(xmlData));
1✔
53
        xmlBuilder.setData(xmlData);
1✔
54
        return xmlBuilder;
1✔
55
    }
56

57
    @SuppressWarnings("unchecked")
58
    public XmlBuilder e(String elementName) {
59
        U.remove(data, path + "." + SELF_CLOSING);
1✔
60
        Map<String, Object> value = new LinkedHashMap<>();
1✔
61
        value.put(SELF_CLOSING, TRUE);
1✔
62
        Object object = U.get(data, path + "." + elementName);
1✔
63
        if (object instanceof Map) {
1✔
64
            List<Object> list = new ArrayList<>();
1✔
65
            list.add(object);
1✔
66
            list.add(value);
1✔
67
            U.set(data, path + "." + elementName, list);
1✔
68
            path += "." + elementName + ".1";
1✔
69
            savedPath = path;
1✔
70
        } else if (object instanceof List) {
1✔
71
            path += "." + elementName + "." + ((List<Object>) object).size();
1✔
72
            savedPath = path;
1✔
73
            ((List<Object>) object).add(value);
1✔
74
        } else {
75
            U.set(data, path + "." + elementName, value);
1✔
76
            path += "." + elementName;
1✔
77
        }
78
        return this;
1✔
79
    }
80

81
    public XmlBuilder a(String attributeName, String value) {
82
        U.remove(data, path + "." + SELF_CLOSING);
1✔
83
        U.set(data, path + ".-" + attributeName, value);
1✔
84
        return this;
1✔
85
    }
86

87
    public XmlBuilder c(String comment) {
88
        U.remove(data, path + "." + SELF_CLOSING);
1✔
89
        U.update(data, path + ".#comment", comment);
1✔
90
        return this;
1✔
91
    }
92

93
    public XmlBuilder i(String target, String value) {
94
        U.remove(data, path + "." + SELF_CLOSING);
1✔
95
        U.set(data, "?" + target, value);
1✔
96
        return this;
1✔
97
    }
98

99
    public XmlBuilder d(String cdata) {
100
        U.remove(data, path + "." + SELF_CLOSING);
1✔
101
        U.update(data, path + ".#cdata-section", cdata);
1✔
102
        return this;
1✔
103
    }
104

105
    public XmlBuilder t(String text) {
106
        U.remove(data, path + "." + SELF_CLOSING);
1✔
107
        U.update(data, path + ".#text", text);
1✔
108
        return this;
1✔
109
    }
110

111
    public XmlBuilder importXmlBuilder(XmlBuilder xmlBuilder) {
112
        data.putAll(xmlBuilder.data);
1✔
113
        return this;
1✔
114
    }
115

116
    public XmlBuilder up() {
117
        if (path.equals(savedPath)) {
1✔
118
            path = path.substring(0, path.lastIndexOf("."));
1✔
119
        }
120
        path = path.substring(0, path.lastIndexOf("."));
1✔
121
        return this;
1✔
122
    }
123

124
    public XmlBuilder root() {
125
        int index = path.indexOf(".");
1✔
126
        XmlBuilder xmlBuilder = new XmlBuilder(index == -1 ? path : path.substring(0, index));
1✔
127
        xmlBuilder.setData(data);
1✔
128
        return xmlBuilder;
1✔
129
    }
130

131
    public org.w3c.dom.Document getDocument() {
132
        try {
133
            return Xml.Document.createDocument(asString());
1✔
134
        } catch (Exception ex) {
1✔
135
            throw new IllegalArgumentException(ex);
1✔
136
        }
137
    }
138

139
    public XmlBuilder set(final String path, final Object value) {
140
        U.set(data, path, value);
1✔
141
        return this;
1✔
142
    }
143

144
    public XmlBuilder remove(final String key) {
145
        U.remove(data, key);
1✔
146
        return this;
1✔
147
    }
148

149
    public Map<String, Object> build() {
150
        return U.deepCopyMap(data);
1✔
151
    }
152

153
    public XmlBuilder clear() {
154
        data.clear();
1✔
155
        return this;
1✔
156
    }
157

158
    public boolean isEmpty() {
159
        return data.isEmpty();
1✔
160
    }
161

162
    public int size() {
163
        return data.size();
1✔
164
    }
165

166
    public String asString() {
167
        return U.toXml(data);
1✔
168
    }
169

170
    public String toXml(Xml.XmlStringBuilder.Step identStep) {
171
        return Xml.toXml(data, identStep);
1✔
172
    }
173

174
    public String toXml() {
175
        return U.toXml(data);
1✔
176
    }
177

178
    public String toJson(Json.JsonStringBuilder.Step identStep) {
179
        return Json.toJson(data, identStep);
1✔
180
    }
181

182
    public String toJson() {
183
        return U.toJson(data);
1✔
184
    }
185

186
    private void setData(Map<String, Object> newData) {
187
        data.clear();
1✔
188
        data.putAll(newData);
1✔
189
    }
1✔
190
}
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