Skip to content

Commit 80b1bcc

Browse files
authored
Improve toXml() for map with single element.
1 parent f7c1458 commit 80b1bcc

File tree

4 files changed

+102
-54
lines changed
  • lodash-plugin/src
  • string-plugin/src

4 files changed

+102
-54
lines changed

lodash-plugin/src/main/java/com/github/underscore/lodash/$.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,14 +1711,19 @@ public static String toJson(Map map) {
17111711
}
17121712

17131713
public static class XmlStringBuilder {
1714-
private final StringBuilder builder;
1714+
protected final StringBuilder builder;
17151715
private int ident;
17161716

17171717
public XmlStringBuilder() {
17181718
builder = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n");
17191719
ident = 2;
17201720
}
17211721

1722+
public XmlStringBuilder(StringBuilder builder, int ident) {
1723+
this.builder = builder;
1724+
this.ident = ident;
1725+
}
1726+
17221727
public XmlStringBuilder append(final String string) {
17231728
builder.append(string);
17241729
return this;
@@ -1751,6 +1756,16 @@ public String toString() {
17511756
}
17521757
}
17531758

1759+
public static class XmlStringBuilderWithoutRoot extends XmlStringBuilder {
1760+
public XmlStringBuilderWithoutRoot() {
1761+
super(new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"), 0);
1762+
}
1763+
1764+
public String toString() {
1765+
return builder.toString();
1766+
}
1767+
}
1768+
17541769
public static class XmlArray {
17551770
public static void writeXml(Collection collection, XmlStringBuilder builder) {
17561771
if (collection == null) {
@@ -2101,7 +2116,12 @@ public String toXml() {
21012116
}
21022117

21032118
public static String toXml(Map map) {
2104-
final XmlStringBuilder builder = new XmlStringBuilder();
2119+
final XmlStringBuilder builder;
2120+
if (map != null && map.size() == 1) {
2121+
builder = new XmlStringBuilderWithoutRoot();
2122+
} else {
2123+
builder = new XmlStringBuilder();
2124+
}
21052125

21062126
XmlObject.writeXml(map, builder);
21072127
return builder.toString();

lodash-plugin/src/test/java/com/github/underscore/lodash/StringTest.java

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,18 @@ public void toXmlFromMap() {
12881288
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\nnull\n</root>", $.toXml((Map) null));
12891289
}
12901290

1291+
@SuppressWarnings("unchecked")
1292+
@Test
1293+
public void toXmlAndFromXmlFromMap() {
1294+
final Map<String, String> testMap = new LinkedHashMap<String, String>();
1295+
testMap.put("FirstItem", "1");
1296+
testMap.put("SecondItem", "2");
1297+
1298+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n"
1299+
+ " <FirstItem>1</FirstItem>\n <SecondItem>2</SecondItem>\n</root>",
1300+
$.toXml((Map<String, Object>) $.fromXml($.toXml(testMap))));
1301+
}
1302+
12911303
@SuppressWarnings("unchecked")
12921304
@Test
12931305
public void toXml() {
@@ -1300,31 +1312,29 @@ public void toXml() {
13001312
+ " \"GlossSeeAlso\": [\n \"GML\",\n \"XML\"\n ]\n"
13011313
+ " },\n \"GlossSee\": \"markup\"\n }\n }\n }\n }\n}";
13021314
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1303-
+ "\n<root>"
1304-
+ "\n <glossary>"
1305-
+ "\n <title>example glossary</title>"
1306-
+ "\n <GlossDiv>"
1307-
+ "\n <title>S</title>"
1308-
+ "\n <GlossList>"
1309-
+ "\n <GlossEntry>"
1310-
+ "\n <ID>SGML</ID>"
1311-
+ "\n <SortAs>SGML</SortAs>"
1312-
+ "\n <GlossTerm>Standard Generalized Markup Language</GlossTerm>"
1313-
+ "\n <Acronym>SGML</Acronym>"
1314-
+ "\n <Abbrev>ISO 8879:1986</Abbrev>"
1315-
+ "\n <GlossDef>"
1316-
+ "\n <para>A meta-markup language, used to create markup languages such as DocBook.</para>"
1317-
+ "\n <GlossSeeAlso>"
1318-
+ "\n <element>GML</element>"
1319-
+ "\n <element>XML</element>"
1320-
+ "\n </GlossSeeAlso>"
1321-
+ "\n </GlossDef>"
1322-
+ "\n <GlossSee>markup</GlossSee>"
1323-
+ "\n </GlossEntry>"
1324-
+ "\n </GlossList>"
1325-
+ "\n </GlossDiv>"
1326-
+ "\n </glossary>"
1327-
+ "\n</root>", $.toXml((Map<String, Object>) $.fromJson(string)));
1315+
+ "\n<glossary>"
1316+
+ "\n <title>example glossary</title>"
1317+
+ "\n <GlossDiv>"
1318+
+ "\n <title>S</title>"
1319+
+ "\n <GlossList>"
1320+
+ "\n <GlossEntry>"
1321+
+ "\n <ID>SGML</ID>"
1322+
+ "\n <SortAs>SGML</SortAs>"
1323+
+ "\n <GlossTerm>Standard Generalized Markup Language</GlossTerm>"
1324+
+ "\n <Acronym>SGML</Acronym>"
1325+
+ "\n <Abbrev>ISO 8879:1986</Abbrev>"
1326+
+ "\n <GlossDef>"
1327+
+ "\n <para>A meta-markup language, used to create markup languages such as DocBook.</para>"
1328+
+ "\n <GlossSeeAlso>"
1329+
+ "\n <element>GML</element>"
1330+
+ "\n <element>XML</element>"
1331+
+ "\n </GlossSeeAlso>"
1332+
+ "\n </GlossDef>"
1333+
+ "\n <GlossSee>markup</GlossSee>"
1334+
+ "\n </GlossEntry>"
1335+
+ "\n </GlossList>"
1336+
+ "\n </GlossDiv>"
1337+
+ "\n</glossary>", $.toXml((Map<String, Object>) $.fromJson(string)));
13281338
}
13291339

13301340
@SuppressWarnings("unchecked")

string-plugin/src/main/java/com/github/underscore/string/$.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,14 +1231,19 @@ public static String toJson(Map map) {
12311231
}
12321232

12331233
public static class XmlStringBuilder {
1234-
private final StringBuilder builder;
1234+
protected final StringBuilder builder;
12351235
private int ident;
12361236

12371237
public XmlStringBuilder() {
12381238
builder = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n");
12391239
ident = 2;
12401240
}
12411241

1242+
public XmlStringBuilder(StringBuilder builder, int ident) {
1243+
this.builder = builder;
1244+
this.ident = ident;
1245+
}
1246+
12421247
public XmlStringBuilder append(final String string) {
12431248
builder.append(string);
12441249
return this;
@@ -1271,6 +1276,16 @@ public String toString() {
12711276
}
12721277
}
12731278

1279+
public static class XmlStringBuilderWithoutRoot extends XmlStringBuilder {
1280+
public XmlStringBuilderWithoutRoot() {
1281+
super(new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"), 0);
1282+
}
1283+
1284+
public String toString() {
1285+
return builder.toString();
1286+
}
1287+
}
1288+
12741289
public static class XmlArray {
12751290
public static void writeXml(Collection collection, XmlStringBuilder builder) {
12761291
if (collection == null) {
@@ -1621,7 +1636,12 @@ public String toXml() {
16211636
}
16221637

16231638
public static String toXml(Map map) {
1624-
final XmlStringBuilder builder = new XmlStringBuilder();
1639+
final XmlStringBuilder builder;
1640+
if (map != null && map.size() == 1) {
1641+
builder = new XmlStringBuilderWithoutRoot();
1642+
} else {
1643+
builder = new XmlStringBuilder();
1644+
}
16251645

16261646
XmlObject.writeXml(map, builder);
16271647
return builder.toString();

string-plugin/src/test/java/com/github/underscore/string/StringTest.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,31 +1302,29 @@ public void toXml() {
13021302
+ " \"GlossSeeAlso\": [\n \"GML\",\n \"XML\"\n ]\n"
13031303
+ " },\n \"GlossSee\": \"markup\"\n }\n }\n }\n }\n}";
13041304
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1305-
+ "\n<root>"
1306-
+ "\n <glossary>"
1307-
+ "\n <title>example glossary</title>"
1308-
+ "\n <GlossDiv>"
1309-
+ "\n <title>S</title>"
1310-
+ "\n <GlossList>"
1311-
+ "\n <GlossEntry>"
1312-
+ "\n <ID>SGML</ID>"
1313-
+ "\n <SortAs>SGML</SortAs>"
1314-
+ "\n <GlossTerm>Standard Generalized Markup Language</GlossTerm>"
1315-
+ "\n <Acronym>SGML</Acronym>"
1316-
+ "\n <Abbrev>ISO 8879:1986</Abbrev>"
1317-
+ "\n <GlossDef>"
1318-
+ "\n <para>A meta-markup language, used to create markup languages such as DocBook.</para>"
1319-
+ "\n <GlossSeeAlso>"
1320-
+ "\n <element>GML</element>"
1321-
+ "\n <element>XML</element>"
1322-
+ "\n </GlossSeeAlso>"
1323-
+ "\n </GlossDef>"
1324-
+ "\n <GlossSee>markup</GlossSee>"
1325-
+ "\n </GlossEntry>"
1326-
+ "\n </GlossList>"
1327-
+ "\n </GlossDiv>"
1328-
+ "\n </glossary>"
1329-
+ "\n</root>", $.toXml((Map<String, Object>) $.fromJson(string)));
1305+
+ "\n<glossary>"
1306+
+ "\n <title>example glossary</title>"
1307+
+ "\n <GlossDiv>"
1308+
+ "\n <title>S</title>"
1309+
+ "\n <GlossList>"
1310+
+ "\n <GlossEntry>"
1311+
+ "\n <ID>SGML</ID>"
1312+
+ "\n <SortAs>SGML</SortAs>"
1313+
+ "\n <GlossTerm>Standard Generalized Markup Language</GlossTerm>"
1314+
+ "\n <Acronym>SGML</Acronym>"
1315+
+ "\n <Abbrev>ISO 8879:1986</Abbrev>"
1316+
+ "\n <GlossDef>"
1317+
+ "\n <para>A meta-markup language, used to create markup languages such as DocBook.</para>"
1318+
+ "\n <GlossSeeAlso>"
1319+
+ "\n <element>GML</element>"
1320+
+ "\n <element>XML</element>"
1321+
+ "\n </GlossSeeAlso>"
1322+
+ "\n </GlossDef>"
1323+
+ "\n <GlossSee>markup</GlossSee>"
1324+
+ "\n </GlossEntry>"
1325+
+ "\n </GlossList>"
1326+
+ "\n </GlossDiv>"
1327+
+ "\n</glossary>", $.toXml((Map<String, Object>) $.fromJson(string)));
13301328
}
13311329

13321330
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)