Skip to content

Commit affdfb6

Browse files
authored
Improved xmlBuilder.e(element), added methods set(), remove(), clear(), isEmpty(), size()
1 parent 86beea5 commit affdfb6

File tree

3 files changed

+138
-9
lines changed

3 files changed

+138
-9
lines changed

src/main/java/com/github/underscore/Xml.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ private static void checkLocalMap(
11931193
|| localMap2.size() != 1
11941194
|| XmlValue.getMapKey(localMap2).startsWith("-")
11951195
|| XmlValue.getMapValue(localMap2) instanceof List) {
1196-
if ("root".equals(XmlValue.getMapKey(localMap2))) {
1196+
if (ROOT.equals(XmlValue.getMapKey(localMap2))) {
11971197
writeArray((List) XmlValue.getMapValue(localMap2), builder, arrayTrue);
11981198
} else {
11991199
XmlObject.writeXml(

src/main/java/com/github/underscore/XmlBuilder.java

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.github.underscore;
22

3+
import java.util.ArrayList;
34
import java.util.LinkedHashMap;
5+
import java.util.List;
46
import java.util.Map;
57

68
public class XmlBuilder {
79
private static final String SELF_CLOSING = "-self-closing";
810
private static final String TRUE = "true";
911
private final Map<String, Object> data;
1012
private String path;
13+
private String savedPath;
1114

1215
XmlBuilder(String rootName) {
1316
data = new LinkedHashMap<>();
@@ -28,12 +31,27 @@ public static XmlBuilder parse(String xml) {
2831
return xmlBuilder;
2932
}
3033

34+
@SuppressWarnings("unchecked")
3135
public XmlBuilder e(String elementName) {
3236
U.remove(data, path + "." + SELF_CLOSING);
3337
Map<String, Object> value = new LinkedHashMap<>();
3438
value.put(SELF_CLOSING, TRUE);
35-
U.set(data, path + "." + elementName, value);
36-
path += "." + elementName;
39+
Object object = U.get(data, path + "." + elementName);
40+
if (object instanceof Map) {
41+
List<Object> list = new ArrayList<>();
42+
list.add(object);
43+
list.add(value);
44+
U.set(data, path + "." + elementName, list);
45+
path += "." + elementName + ".1";
46+
savedPath = path;
47+
} else if (object instanceof List) {
48+
path += "." + elementName + "." + ((List<Object>) object).size();
49+
savedPath = path;
50+
((List<Object>) object).add(value);
51+
} else {
52+
U.set(data, path + "." + elementName, value);
53+
path += "." + elementName;
54+
}
3755
return this;
3856
}
3957

@@ -62,6 +80,9 @@ public XmlBuilder t(String text) {
6280
}
6381

6482
public XmlBuilder up() {
83+
if (path.equals(savedPath)) {
84+
path = path.substring(0, path.lastIndexOf("."));
85+
}
6586
path = path.substring(0, path.lastIndexOf("."));
6687
return this;
6788
}
@@ -81,15 +102,38 @@ public org.w3c.dom.Document getDocument() {
81102
}
82103
}
83104

105+
public XmlBuilder set(final String path, final Object value) {
106+
U.set(data, path, value);
107+
return this;
108+
}
109+
110+
public XmlBuilder remove(final String key) {
111+
U.remove(data, key);
112+
return this;
113+
}
114+
115+
public XmlBuilder clear() {
116+
data.clear();
117+
return this;
118+
}
119+
120+
public boolean isEmpty() {
121+
return data.isEmpty();
122+
}
123+
124+
public int size() {
125+
return data.size();
126+
}
127+
84128
public String asString() {
85129
return U.toXml(data);
86130
}
87131

88-
public String xml() {
132+
public String toXml() {
89133
return U.toXml(data);
90134
}
91135

92-
public String json() {
136+
public String toJson() {
93137
return U.toJson(data);
94138
}
95139

src/test/java/com/github/underscore/XmlBuilderTest.java

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.underscore;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
45
import static org.junit.jupiter.api.Assertions.assertThrows;
56
import static org.junit.jupiter.api.Assertions.assertTrue;
67

@@ -88,6 +89,51 @@ public String asString() {
8889
assertThrows(IllegalArgumentException.class, xmlBuilder::getDocument);
8990
}
9091

92+
@Test
93+
void set() {
94+
XmlBuilder xmlBuilder = new XmlBuilder("json");
95+
xmlBuilder.set("xml", "123");
96+
assertEquals(
97+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
98+
+ "<root>\n"
99+
+ " <json/>\n"
100+
+ " <xml>123</xml>\n"
101+
+ "</root>",
102+
xmlBuilder.asString());
103+
}
104+
105+
@Test
106+
void remove() {
107+
XmlBuilder xmlBuilder = new XmlBuilder("root").e("123");
108+
xmlBuilder.remove("root.123");
109+
assertEquals(
110+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root></root>",
111+
xmlBuilder.asString());
112+
}
113+
114+
@Test
115+
void clear() {
116+
XmlBuilder xmlBuilder = new XmlBuilder("xml").e("123");
117+
xmlBuilder.clear();
118+
assertEquals(
119+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root></root>",
120+
xmlBuilder.asString());
121+
}
122+
123+
@Test
124+
void isEmpty() {
125+
XmlBuilder xmlBuilder = new XmlBuilder("xml").e("123");
126+
assertFalse(xmlBuilder.isEmpty());
127+
xmlBuilder.clear();
128+
assertTrue(xmlBuilder.isEmpty());
129+
}
130+
131+
@Test
132+
void size() {
133+
XmlBuilder xmlBuilder = new XmlBuilder("xml").e("123");
134+
assertEquals(1, xmlBuilder.size());
135+
}
136+
91137
@Test
92138
void root() {
93139
XmlBuilder xmlBuilder = new XmlBuilder("root");
@@ -104,15 +150,54 @@ void root() {
104150
}
105151

106152
@Test
107-
void xml() {
153+
void xmlBuilderArray() {
154+
String lines = "1,name1,department1\n2,name2,department2\n3,name3,department3";
155+
XmlBuilder xmlBuilder = XmlBuilder.create("Employees");
156+
for (String line : lines.split("\n")) {
157+
String[] columns = line.split(",");
158+
xmlBuilder
159+
.e("Employee")
160+
.e("Code")
161+
.t(columns[0])
162+
.up()
163+
.e("Name")
164+
.t(columns[1])
165+
.up()
166+
.e("Department")
167+
.t(columns[2])
168+
.up()
169+
.up();
170+
}
171+
assertEquals(
172+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
173+
+ "<Employees>\n"
174+
+ " <Employee>\n"
175+
+ " <Code>1</Code>\n"
176+
+ " <Name>name1</Name>\n"
177+
+ " <Department>department1</Department>\n"
178+
+ " </Employee> <Employee>\n"
179+
+ " <Code>2</Code>\n"
180+
+ " <Name>name2</Name>\n"
181+
+ " <Department>department2</Department>\n"
182+
+ " </Employee> <Employee>\n"
183+
+ " <Code>3</Code>\n"
184+
+ " <Name>name3</Name>\n"
185+
+ " <Department>department3</Department>\n"
186+
+ " </Employee>\n"
187+
+ "</Employees>",
188+
xmlBuilder.asString());
189+
}
190+
191+
@Test
192+
void toXml() {
108193
XmlBuilder xmlBuilder = XmlBuilder.parse(XML);
109-
assertEquals(XML, xmlBuilder.xml());
194+
assertEquals(XML, xmlBuilder.toXml());
110195
}
111196

112197
@Test
113-
void json() {
198+
void toJson() {
114199
XmlBuilder xmlBuilder = XmlBuilder.parse(XML);
115-
assertEquals(U.xmlToJson(XML), xmlBuilder.json());
200+
assertEquals(U.xmlToJson(XML), xmlBuilder.toJson());
116201
}
117202

118203
@Test

0 commit comments

Comments
 (0)